OSDN Git Service

71f51075d8e41a0a0182a978e5ff6dd08ed7f95a
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / ricohgr2 / wrapper / connection / RicohGr2CameraConnectSequence.java
1 package net.osdn.gokigen.a01d.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.a01d.R;
9 import net.osdn.gokigen.a01d.camera.ICameraConnection;
10 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
11 import net.osdn.gokigen.a01d.camera.utils.SimpleHttpClient;
12 import net.osdn.gokigen.a01d.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
60                 // 表示するディスプレイモードを切り替える
61                 String dispMode = preferences.getString(IPreferencePropertyAccessor.GR2_DISPLAY_MODE,  IPreferencePropertyAccessor.GR2_DISPLAY_MODE_DEFAULT_VALUE);
62                 if (dispMode.contains("1"))
63                 {
64                     // Disp. ボタンを 1回 押す
65                     final String postData = "cmd=bdisp";
66                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
67                     Log.v(TAG, grCommandUrl + " " + response0);
68
69                 }
70                 else if (dispMode.contains("2"))
71                 {
72                     // Disp. ボタンを 2回 押す
73                     final String postData = "cmd=bdisp";
74                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
75                     String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
76                     Log.v(TAG, grCommandUrl + " " + response0 + " " + response1);
77                 }
78                 else if (dispMode.contains("3"))
79                 {
80                     // Disp. ボタンを 3回 押す
81                     final String postData = "cmd=bdisp";
82                     String response0 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
83                     String response1 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
84                     String response2 = SimpleHttpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS);
85                     Log.v(TAG, grCommandUrl + " " + response0 + " " + response1 + response2);
86                 }
87                 onConnectNotify();
88             }
89             else
90             {
91                 onConnectError(context.getString(R.string.camera_not_found));
92             }
93         }
94         catch (Exception e)
95         {
96             e.printStackTrace();
97             onConnectError(e.getLocalizedMessage());
98         }
99     }
100
101     private void onConnectNotify()
102     {
103         try
104         {
105             final Thread thread = new Thread(new Runnable()
106             {
107                 @Override
108                 public void run()
109                 {
110                     // カメラとの接続確立を通知する
111                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
112                     cameraStatusReceiver.onCameraConnected();
113                     Log.v(TAG, "onConnectNotify()");
114                 }
115             });
116             thread.start();
117         }
118         catch (Exception e)
119         {
120             e.printStackTrace();
121         }
122     }
123
124     private void waitForAMoment(long mills)
125     {
126         if (mills > 0)
127         {
128             try {
129                 Log.v(TAG, " WAIT " + mills + "ms");
130                 Thread.sleep(mills);
131             } catch (Exception e) {
132                 e.printStackTrace();
133             }
134         }
135     }
136
137     private void onConnectError(String reason)
138     {
139         cameraConnection.alertConnectingFailed(reason);
140     }
141 }