OSDN Git Service

X-T100で画像取得が可能なように改善。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / fujix / wrapper / connection / FujiXCameraConnectSequenceForPlayback.java
1 package net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.connection;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.graphics.Color;
6 import android.util.Log;
7
8 import androidx.annotation.NonNull;
9 import androidx.preference.PreferenceManager;
10
11 import net.osdn.gokigen.pkremote.R;
12 import net.osdn.gokigen.pkremote.camera.interfaces.control.ICameraConnection;
13 import net.osdn.gokigen.pkremote.camera.interfaces.status.ICameraStatusReceiver;
14 import net.osdn.gokigen.pkremote.camera.vendor.fujix.IFujiXInterfaceProvider;
15 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandCallback;
16 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXCommandPublisher;
17 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.IFujiXMessages;
18 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.QueryCameraCapabilities;
19 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.StatusRequestMessage;
20 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback1st;
21 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback2nd;
22 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback3rd;
23 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.changemode.ChangeToPlayback4th;
24 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.CameraRemoteMessage;
25 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.RegistrationMessage;
26 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage;
27 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage2ndRead;
28 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage3rd;
29 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage4th;
30 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.command.messages.start.StartMessage5th;
31 import net.osdn.gokigen.pkremote.camera.vendor.fujix.wrapper.status.IFujiXRunModeHolder;
32 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
33
34
35 public class FujiXCameraConnectSequenceForPlayback implements Runnable, IFujiXCommandCallback, IFujiXMessages
36 {
37     private final String TAG = this.toString();
38
39     private final Activity context;
40     private final ICameraConnection cameraConnection;
41     private final ICameraStatusReceiver cameraStatusReceiver;
42     private final IFujiXInterfaceProvider interfaceProvider;
43     private final IFujiXCommandPublisher commandIssuer;
44     private boolean isBothLiveView = false;
45
46     FujiXCameraConnectSequenceForPlayback(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IFujiXInterfaceProvider interfaceProvider)
47     {
48         Log.v(TAG, " FujiXCameraConnectSequenceForPlayback");
49         this.context = context;
50         this.cameraConnection = cameraConnection;
51         this.cameraStatusReceiver = statusReceiver;
52         this.interfaceProvider = interfaceProvider;
53         this.commandIssuer = interfaceProvider.getCommandPublisher();
54     }
55
56     @Override
57     public void run()
58     {
59         try
60         {
61             try
62             {
63                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
64                 isBothLiveView = preferences.getBoolean(IPreferencePropertyAccessor.FUJIX_DISPLAY_CAMERA_VIEW, false);
65             }
66             catch (Exception e)
67             {
68                 //isBothLiveView = false;
69                 e.printStackTrace();
70             }
71
72             // カメラとTCP接続
73             IFujiXCommandPublisher issuer = interfaceProvider.getCommandPublisher();
74             if (!issuer.isConnected())
75             {
76                 if (!interfaceProvider.getCommandCommunication().connect())
77                 {
78                     // 接続失敗...
79                     interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_fuji), false, true, Color.RED);
80                     onConnectError(context.getString(R.string.dialog_title_connect_failed_fuji));
81                     return;
82                 }
83             }
84             else
85             {
86                 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
87             }
88             // コマンドタスクの実行開始
89             issuer.start();
90
91             // 接続シーケンスの開始
92             sendRegistrationMessage();
93
94         }
95         catch (Exception e)
96         {
97             e.printStackTrace();
98             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_fuji), false, true, Color.RED);
99             onConnectError(e.getLocalizedMessage());
100         }
101     }
102
103     private void onConnectError(String reason)
104     {
105         cameraConnection.alertConnectingFailed(reason);
106     }
107
108     @Override
109     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
110     {
111         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
112     }
113
114     @Override
115     public boolean isReceiveMulti()
116     {
117         return (false);
118     }
119
120     @Override
121     public void receivedMessage(int id, byte[] rx_body)
122     {
123         //Log.v(TAG, "receivedMessage : " + id + "[" + rx_body.length + " bytes]");
124         //int bodyLength = 0;
125         IFujiXRunModeHolder runModeHolder;
126         switch (id)
127         {
128             case SEQ_REGISTRATION:
129                 if (checkRegistrationMessage(rx_body))
130                 {
131                     commandIssuer.enqueueCommand(new StartMessage(this));
132                 }
133                 else
134                 {
135                     onConnectError(context.getString(R.string.connect_error_message));
136                 }
137                 break;
138
139             case SEQ_START:
140                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting1), false, false, 0);
141                 commandIssuer.enqueueCommand(new StartMessage2ndRead(this));
142                 break;
143
144             case SEQ_START_2ND_READ:
145                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting2), false, false, 0);
146                 cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connecting));
147                 Log.v(TAG, " RECV: (SEQ_START_2ND_READ) : " + rx_body.length);
148                 if (rx_body.length == (int)rx_body[0])
149                 {
150                     // なぜかもうちょっとデータが飛んでくるので待つ
151                     //commandIssuer.enqueueCommand(new ReceiveOnly(this));
152
153                     commandIssuer.enqueueCommand(new StartMessage3rd(this));
154                 }
155                 else
156                 {
157                     IFujiXCommandCallback callback = interfaceProvider.getStatusHolder();
158                     if (callback != null)
159                     {
160                         callback.receivedMessage(id, rx_body);
161                     }
162                     commandIssuer.enqueueCommand(new StartMessage3rd(this));
163                 }
164                 break;
165
166             case SEQ_START_2ND_RECEIVE:
167                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting3), false, false, 0);
168                 commandIssuer.enqueueCommand(new StartMessage3rd(this));
169                 break;
170
171             case SEQ_START_3RD:
172                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting4), false, false, 0);
173                 commandIssuer.enqueueCommand(new StartMessage4th(this));
174                 break;
175
176             case SEQ_START_4TH:
177                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting5), false, false, 0);
178                 if (isBothLiveView)
179                 {
180                     // カメラのLCDと遠隔のライブビューを同時に表示する場合...
181                     commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
182                 }
183                 else
184                 {
185                     commandIssuer.enqueueCommand(new StartMessage5th(this));
186                 }
187                 break;
188
189             case SEQ_START_5TH:
190                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting6), false, false, 0);
191                 commandIssuer.enqueueCommand(new QueryCameraCapabilities(this));
192                 //commandIssuer.enqueueCommand(new StatusRequestMessage(this));
193                 break;
194
195             case SEQ_QUERY_CAMERA_CAPABILITIES:
196                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting7), false, false, 0);
197                 commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
198                 break;
199
200             case SEQ_CAMERA_REMOTE:
201                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting8), false, false, 0);
202                 commandIssuer.enqueueCommand(new ChangeToPlayback1st(this));
203                 runModeHolder = interfaceProvider.getRunModeHolder();
204                 if (runModeHolder != null)
205                 {
206                     runModeHolder.transitToPlaybackMode(false);
207                 }
208                 //connectFinished();
209                 break;
210
211             case SEQ_CHANGE_TO_PLAYBACK_1ST:
212                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting9), false, false, 0);
213                 //commandIssuer.enqueueCommand(new ChangeToPlayback2nd(this));
214                 commandIssuer.enqueueCommand(new StatusRequestMessage(this));
215                 break;
216
217             case SEQ_CHANGE_TO_PLAYBACK_2ND:
218                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting10), false, false, 0);
219                 commandIssuer.enqueueCommand(new ChangeToPlayback3rd(this));
220                 break;
221
222             case SEQ_CHANGE_TO_PLAYBACK_3RD:
223                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting11), false, false, 0);
224                 commandIssuer.enqueueCommand(new ChangeToPlayback4th(this));
225                 break;
226
227             case SEQ_CHANGE_TO_PLAYBACK_4TH:
228                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connecting12), false, false, 0);
229                 commandIssuer.enqueueCommand(new StatusRequestMessage(this));
230                 break;
231
232             case SEQ_STATUS_REQUEST:
233                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
234                 IFujiXCommandCallback callback = interfaceProvider.getStatusHolder();
235                 if (callback != null)
236                 {
237                     callback.receivedMessage(id, rx_body);
238                 }
239                 runModeHolder = interfaceProvider.getRunModeHolder();
240                 if (runModeHolder != null)
241                 {
242                     runModeHolder.transitToPlaybackMode(true);
243                 }
244                 connectFinished();
245                 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
246                 break;
247
248             default:
249                 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
250                 onConnectError(context.getString(R.string.connect_receive_unknown_message));
251                 break;
252         }
253     }
254
255     private void sendRegistrationMessage()
256     {
257         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
258         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
259         commandIssuer.enqueueCommand(new RegistrationMessage(this));
260     }
261
262     private boolean checkRegistrationMessage(byte[] receiveData)
263     {
264         // データがないときにはエラー
265         if ((receiveData == null)||(receiveData.length < 8))
266         {
267             return (false);
268         }
269
270         // 応答エラーかどうかをチェックする
271         if (receiveData.length == 8)
272         {
273             if ((receiveData[0] == 0x05) && (receiveData[1] == 0x00) && (receiveData[2] == 0x00) && (receiveData[3] == 0x00) &&
274                     (receiveData[4] == 0x19) && (receiveData[5] == 0x20) && (receiveData[6] == 0x00) && (receiveData[7] == 0x00)) {
275                 // 応答エラー...
276                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.error_reply_from_camera), false, true, Color.RED);
277                 return (false);
278             }
279             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.other_error_reply_from_camera), false, true, Color.RED);
280             return (false);
281         }
282         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.registration_reply_from_camera), false, false, 0);
283         return (true);
284     }
285
286
287     private void connectFinished()
288     {
289         try
290         {
291             // 接続成功のメッセージを出す
292             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
293
294             // ちょっと待つ
295             Thread.sleep(1000);
296
297             interfaceProvider.getAsyncEventCommunication().connect();
298             //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());  ステータスの定期確認は実施しない
299
300             // 接続成功!のメッセージを出す
301             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
302
303             onConnectNotify();
304         }
305         catch (Exception e)
306         {
307             e.printStackTrace();
308         }
309     }
310
311     private void onConnectNotify()
312     {
313         try
314         {
315             final Thread thread = new Thread(new Runnable()
316             {
317                 @Override
318                 public void run()
319                 {
320                     // カメラとの接続確立を通知する
321                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
322                     cameraStatusReceiver.onCameraConnected();
323                     Log.v(TAG, "onConnectNotify()");
324                 }
325             });
326             thread.start();
327         }
328         catch (Exception e)
329         {
330             e.printStackTrace();
331         }
332     }
333
334 }