OSDN Git Service

若干整理。
[gokigen/A01d.git] / app / src / main / java / net / osdn / gokigen / a01d / camera / nikon / wrapper / connection / NikonCameraConnectSequence.java
1 package net.osdn.gokigen.a01d.camera.nikon.wrapper.connection;
2
3 import android.app.Activity;
4 import android.graphics.Color;
5 import android.util.Log;
6
7 import androidx.annotation.NonNull;
8
9 import net.osdn.gokigen.a01d.R;
10 import net.osdn.gokigen.a01d.camera.ICameraConnection;
11 import net.osdn.gokigen.a01d.camera.ICameraStatusReceiver;
12 import net.osdn.gokigen.a01d.camera.nikon.wrapper.command.messages.specific.NikonRegistrationMessage;
13 import net.osdn.gokigen.a01d.camera.nikon.wrapper.status.NikonStatusChecker;
14 import net.osdn.gokigen.a01d.camera.ptpip.IPtpIpInterfaceProvider;
15 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandCallback;
16 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpCommandPublisher;
17 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.IPtpIpMessages;
18 import net.osdn.gokigen.a01d.camera.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
19
20 public class NikonCameraConnectSequence implements Runnable, IPtpIpCommandCallback, IPtpIpMessages
21 {
22     private final String TAG = this.toString();
23
24     private final Activity context;
25     private final ICameraConnection cameraConnection;
26     private final ICameraStatusReceiver cameraStatusReceiver;
27     private final IPtpIpInterfaceProvider interfaceProvider;
28     private final IPtpIpCommandPublisher commandIssuer;
29     private final NikonStatusChecker statusChecker;
30     private boolean isDumpLog = false;
31
32     NikonCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull NikonStatusChecker statusChecker)
33     {
34         Log.v(TAG, " NikonCameraConnectSequenceForPlayback");
35         this.context = context;
36         this.cameraConnection = cameraConnection;
37         this.cameraStatusReceiver = statusReceiver;
38         this.interfaceProvider = interfaceProvider;
39         this.commandIssuer = interfaceProvider.getCommandPublisher();
40         this.statusChecker = statusChecker;
41     }
42
43     @Override
44     public void run()
45     {
46         try
47         {
48             // カメラとTCP接続
49             IPtpIpCommandPublisher issuer = interfaceProvider.getCommandPublisher();
50             if (!issuer.isConnected())
51             {
52                 if (!interfaceProvider.getCommandCommunication().connect())
53                 {
54                     // 接続失敗...
55                     interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
56                     onConnectError(context.getString(R.string.dialog_title_connect_failed_nikon));
57                     return;
58                 }
59             }
60             else
61             {
62                 Log.v(TAG, "SOCKET IS ALREADY CONNECTED...");
63             }
64             // コマンドタスクの実行開始
65             issuer.start();
66
67             // 接続シーケンスの開始
68             sendRegistrationMessage();
69
70         }
71         catch (Exception e)
72         {
73             e.printStackTrace();
74             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.dialog_title_connect_failed_nikon), false, true, Color.RED);
75             onConnectError(e.getLocalizedMessage());
76         }
77     }
78
79     private void onConnectError(String reason)
80     {
81         cameraConnection.alertConnectingFailed(reason);
82     }
83
84     @Override
85     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
86     {
87         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
88     }
89
90     @Override
91     public boolean isReceiveMulti()
92     {
93         return (false);
94     }
95
96     @Override
97     public void receivedMessage(int id, byte[] rx_body)
98     {
99         switch (id)
100         {
101             case SEQ_REGISTRATION:
102                 if (checkRegistrationMessage(rx_body))
103                 {
104                     sendInitEventRequest(rx_body);
105                 }
106                 else
107                 {
108                     onConnectError(context.getString(R.string.connect_error_message));
109                 }
110                 break;
111
112             case SEQ_EVENT_INITIALIZE:
113                 if (checkEventInitialize(rx_body))
114                 {
115                     interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
116                     commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0));  // OpenSession
117                 }
118                 else
119                 {
120                     onConnectError(context.getString(R.string.connect_error_message));
121                 }
122                 break;
123
124             case SEQ_OPEN_SESSION:
125                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
126                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, 50, isDumpLog, 0, 0x1001, 0, 0, 0, 0, 0));  // GetDeviceInfo
127                 break;
128
129             case SEQ_INIT_SESSION:
130             case SEQ_CHANGE_REMOTE:
131             case SEQ_SET_EVENT_MODE:
132                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
133                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
134                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
135                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
136                 connectFinished();
137                 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
138                 break;
139
140             default:
141                 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
142                 onConnectError(context.getString(R.string.connect_receive_unknown_message));
143                 break;
144         }
145     }
146
147     private void sendRegistrationMessage()
148     {
149         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
150         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
151         commandIssuer.enqueueCommand(new NikonRegistrationMessage(this));
152     }
153
154     private void sendInitEventRequest(byte[] receiveData)
155     {
156         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
157         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
158         try
159         {
160             int eventConnectionNumber = (receiveData[8] & 0xff);
161             eventConnectionNumber = eventConnectionNumber + ((receiveData[9]  & 0xff) << 8);
162             eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
163             eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
164             statusChecker.setEventConnectionNumber(eventConnectionNumber);
165             interfaceProvider.getCameraStatusWatcher().startStatusWatch(null);
166
167             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0));
168         }
169         catch (Exception e)
170         {
171             e.printStackTrace();
172         }
173     }
174
175     private boolean checkRegistrationMessage(byte[] receiveData)
176     {
177         // データ(Connection Number)がないときにはエラーと判断する
178         return (!((receiveData == null)||(receiveData.length < 12)));
179     }
180
181     private boolean checkEventInitialize(byte[] receiveData)
182     {
183         Log.v(TAG, "checkEventInitialize() ");
184         return (!(receiveData == null));
185     }
186
187     private void connectFinished()
188     {
189         try
190         {
191             // 接続成功のメッセージを出す
192             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
193
194             // ちょっと待つ
195             Thread.sleep(1000);
196
197             //interfaceProvider.getAsyncEventCommunication().connect();
198             //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());  ステータスの定期確認は実施しない
199
200             // 接続成功!のメッセージを出す
201             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
202
203             onConnectNotify();
204         }
205         catch (Exception e)
206         {
207             e.printStackTrace();
208         }
209     }
210
211     private void onConnectNotify()
212     {
213         try
214         {
215             final Thread thread = new Thread(new Runnable()
216             {
217                 @Override
218                 public void run()
219                 {
220                     // カメラとの接続確立を通知する
221                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
222                     cameraStatusReceiver.onCameraConnected();
223                     Log.v(TAG, " onConnectNotify()");
224                 }
225             });
226             thread.start();
227         }
228         catch (Exception e)
229         {
230             e.printStackTrace();
231         }
232     }
233 }