OSDN Git Service

d79ef8904dde709bcf674da0b44dcd438743d078
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / ricohgr2 / wrapper / connection / RicohGr2CameraConnectSequence.java
1 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper.connection;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.preference.PreferenceManager;
6 import android.util.Log;
7
8 import net.osdn.gokigen.gr2control.R;
9 import net.osdn.gokigen.gr2control.camera.ICameraConnection;
10 import net.osdn.gokigen.gr2control.camera.ICameraStatusReceiver;
11 import net.osdn.gokigen.gr2control.camera.utils.SimpleHttpClient;
12 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
13
14 import androidx.annotation.NonNull;
15
16 class RicohGr2CameraConnectSequence implements Runnable
17 {
18     private final String TAG = this.toString();
19     private final Activity context;
20     private final ICameraConnection cameraConnection;
21     private final ICameraStatusReceiver cameraStatusReceiver;
22
23     RicohGr2CameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection)
24     {
25         Log.v(TAG, "RicohGr2CameraConnectSequence");
26         this.context = context;
27         this.cameraConnection = cameraConnection;
28         this.cameraStatusReceiver = statusReceiver;
29     }
30
31     @Override
32     public void run()
33     {
34         final String areYouThereUrl = "http://192.168.0.1/v1/ping";
35         final String grCommandUrl = "http://192.168.0.1/_gr";
36         final int TIMEOUT_MS = 5000;
37         try
38         {
39             String response = SimpleHttpClient.httpGet(areYouThereUrl, TIMEOUT_MS);
40             Log.v(TAG, areYouThereUrl + " " + response);
41             if (response.length() > 0)
42             {
43                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
44
45                 // 接続時、レンズロックOFF
46                 {
47                     final String postData = "cmd=acclock off";
48                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
49                     Log.v(TAG, grCommandUrl + " " + response0);
50                 }
51
52                 // 接続時、カメラの画面を消す
53                 if (preferences.getBoolean(IPreferencePropertyAccessor.GR2_LCD_SLEEP, false))
54                 {
55                     final String postData = "cmd=lcd sleep on";
56                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
57                     Log.v(TAG, grCommandUrl + " " + response0);
58                 }
59                 onConnectNotify();
60             }
61             else
62             {
63                 onConnectError(context.getString(R.string.camera_not_found));
64             }
65         }
66         catch (Exception e)
67         {
68             e.printStackTrace();
69             onConnectError(e.getLocalizedMessage());
70         }
71     }
72
73     private void onConnectNotify()
74     {
75         try
76         {
77             final Thread thread = new Thread(new Runnable()
78             {
79                 @Override
80                 public void run()
81                 {
82                     // カメラとの接続確立を通知する
83                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
84                     cameraStatusReceiver.onCameraConnected();
85                     Log.v(TAG, "onConnectNotify()");
86                 }
87             });
88             thread.start();
89         }
90         catch (Exception e)
91         {
92             e.printStackTrace();
93         }
94     }
95
96 /*
97     private void waitForAMoment(long mills)
98     {
99         if (mills > 0)
100         {
101             try {
102                 Log.v(TAG, " WAIT " + mills + "ms");
103                 Thread.sleep(mills);
104             } catch (Exception e) {
105                 e.printStackTrace();
106             }
107         }
108     }
109 */
110
111     private void onConnectError(String reason)
112     {
113         cameraConnection.alertConnectingFailed(reason);
114     }
115 }