OSDN Git Service

FUJI X対応準備その4。デバッグ前。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / fujix / wrapper / connection / FujiXCameraConnectSequence.java
1 package net.osdn.gokigen.a01d.camera.fujix.wrapper.connection;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.util.Log;
6
7 import androidx.annotation.NonNull;
8 import androidx.preference.PreferenceManager;
9
10 import net.osdn.gokigen.a01d.R;
11 import net.osdn.gokigen.a01d.camera.ICameraConnection;
12 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
13 import net.osdn.gokigen.a01d.camera.fujix.IFujiXInterfaceProvider;
14 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommandCallback;
15 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.IFujiXCommandIssuer;
16 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.CameraRemoteMessage;
17 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.RegistrationMessage;
18 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage;
19 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage2nd;
20 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage3rd;
21 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartMessage4th;
22 import net.osdn.gokigen.a01d.camera.fujix.wrapper.command.messages.StartReceiveOnly;
23 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
24
25 public class FujiXCameraConnectSequence implements Runnable, IFujiXCommandCallback
26 {
27     private final String TAG = this.toString();
28
29     public static final int SEQ_DUMMY = 0;
30     public static final int SEQ_REGISTRATION = 1;
31     public static final int SEQ_START = 2;
32     public static final int SEQ_START_2ND = 3;
33     public static final int SEQ_START_2ND_RECEIVE = 4;
34     public static final int SEQ_START_3RD = 5;
35     public static final int SEQ_START_4TH = 6;
36     public static final int SEQ_CAMERA_REMOTE = 7;
37
38
39     private final Activity context;
40     private final ICameraConnection cameraConnection;
41     private final ICameraStatusReceiver cameraStatusReceiver;
42     private final IFujiXInterfaceProvider interfaceProvider;
43     private final IFujiXCommandIssuer commandIssuer;
44     private boolean isBothLiveView = false;
45
46     FujiXCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IFujiXInterfaceProvider interfaceProvider)
47     {
48         Log.v(TAG, "FujiXCameraConnectSequence");
49         this.context = context;
50         this.cameraConnection = cameraConnection;
51         this.cameraStatusReceiver = statusReceiver;
52         this.interfaceProvider = interfaceProvider;
53         this.commandIssuer = interfaceProvider.getCommandIssuer();
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             IFujiXCommandIssuer issuer = interfaceProvider.getCommandIssuer();
74             if (!issuer.isConnected())
75             {
76                 if (!interfaceProvider.getCommandCommunication().connect())
77                 {
78                     // 接続失敗...
79                     onConnectError(context.getString(R.string.dialog_title_connect_failed));
80                     return;
81                 }
82             }
83             // コマンドタスクの実行開始
84             issuer.start();
85
86             // 接続シーケンスの開始
87             sendRegistrationMessage();
88
89         }
90         catch (Exception e)
91         {
92             e.printStackTrace();
93             onConnectError(e.getLocalizedMessage());
94         }
95     }
96
97     private void onConnectError(String reason)
98     {
99         cameraConnection.alertConnectingFailed(reason);
100     }
101
102     @Override
103     public void receivedMessage(int id, byte[] rx_body)
104     {
105         Log.v(TAG, "receivedMessage : " + id + "[" + rx_body.length + " bytes]");
106         switch (id)
107         {
108             case SEQ_REGISTRATION:
109                 if (checkRegistrationMessage(rx_body))
110                 {
111                     commandIssuer.enqueueCommand(new StartMessage(this));
112                 }
113                 break;
114
115             case SEQ_START:
116                 commandIssuer.enqueueCommand(new StartMessage2nd(this));
117                 break;
118
119             case SEQ_START_2ND:
120                 if (rx_body.length == (int)rx_body[0])
121                 {
122                     // なぜかもうちょっとデータが飛んでくるので待つ
123                     //commandIssuer.enqueueCommand(new StartReceiveOnly(this));
124
125                     commandIssuer.enqueueCommand(new StartMessage3rd(this));
126                 }
127                 else
128                 {
129                     commandIssuer.enqueueCommand(new StartMessage3rd(this));
130                 }
131                 break;
132
133             case SEQ_START_2ND_RECEIVE:
134                 commandIssuer.enqueueCommand(new StartMessage3rd(this));
135                 break;
136
137             case SEQ_START_3RD:
138                 commandIssuer.enqueueCommand(new StartMessage4th(this));
139                 break;
140
141             case SEQ_START_4TH:
142                 commandIssuer.enqueueCommand(new CameraRemoteMessage(this));
143                 break;
144
145             case SEQ_CAMERA_REMOTE:
146                 connectFinished(rx_body);
147                 break;
148
149             default:
150                 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
151                 onConnectError(context.getString(R.string.connect_receive_unknown_message));
152                 break;
153         }
154     }
155
156     private void sendRegistrationMessage()
157     {
158         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
159         commandIssuer.enqueueCommand(new RegistrationMessage(this));
160     }
161
162     private boolean checkRegistrationMessage(byte[] receiveData)
163     {
164         // 応答エラーかどうかをチェックする
165         if (receiveData.length == 8)
166         {
167             if ((receiveData[0] == 0x05) && (receiveData[1] == 0x00) && (receiveData[2] == 0x00) && (receiveData[3] == 0x00) &&
168                     (receiveData[4] == 0x19) && (receiveData[5] == 0x20) && (receiveData[6] == 0x00) && (receiveData[7] == 0x00)) {
169                 // 応答エラー...
170                 return (false);
171             }
172             return (false);
173         }
174         return (true);
175     }
176
177     private void connectFinished(byte[] rx_body)
178     {
179         try
180         {
181             // ちょっと待つ
182             Thread.sleep(1000);
183             interfaceProvider.getAsyncEventCommunication().connect();
184             onConnectNotify();
185         }
186         catch (Exception e)
187         {
188             e.printStackTrace();
189         }
190     }
191
192
193     private void onConnectNotify()
194     {
195         try
196         {
197             final Thread thread = new Thread(new Runnable()
198             {
199                 @Override
200                 public void run()
201                 {
202                     // カメラとの接続確立を通知する
203                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
204                     cameraStatusReceiver.onCameraConnected();
205                     Log.v(TAG, "onConnectNotify()");
206                 }
207             });
208             thread.start();
209         }
210         catch (Exception e)
211         {
212             e.printStackTrace();
213         }
214     }
215
216 }