OSDN Git Service

NIKON対応途中。
[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 = true;
31
32     NikonCameraConnectSequence(@NonNull Activity context, @NonNull ICameraStatusReceiver statusReceiver, @NonNull final ICameraConnection cameraConnection, @NonNull IPtpIpInterfaceProvider interfaceProvider, @NonNull NikonStatusChecker statusChecker)
33     {
34         Log.v(TAG, " NikonCameraConnectSequence");
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                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting1), false, false, 0);
114                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_INIT_SESSION, 50, isDumpLog, 0, 0x1001, 0, 0, 0, 0, 0));  // GetDeviceInfo
115                 break;
116
117             case SEQ_INIT_SESSION:
118                 if (checkEventInitialize(rx_body))
119                 {
120                     interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting2), false, false, 0);
121                     commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0));  // OpenSession
122                 }
123                 else
124                 {
125                     onConnectError(context.getString(R.string.connect_error_message));
126                 }
127                 break;
128             case SEQ_OPEN_SESSION:
129                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting3), false, false, 0);
130                 commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_CHANGE_REMOTE, 50, isDumpLog, 0, 0x902c, 4, 0x01, 0, 0, 0));  //
131                 break;
132
133             case SEQ_CHANGE_REMOTE:
134             case SEQ_SET_EVENT_MODE:
135                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting4), false, false, 0);
136                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.canon_connect_connecting5), false, false, 0);
137                 interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connect_finished), false, false, 0);
138                 connectFinished();
139                 Log.v(TAG, "CHANGED PLAYBACK MODE : DONE.");
140                 break;
141
142             default:
143                 Log.v(TAG, "RECEIVED UNKNOWN ID : " + id);
144                 onConnectError(context.getString(R.string.connect_receive_unknown_message));
145                 break;
146         }
147     }
148
149     private void sendRegistrationMessage()
150     {
151         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start), false, false, 0);
152         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start));
153         commandIssuer.enqueueCommand(new NikonRegistrationMessage(this));
154     }
155
156     private void sendInitEventRequest(byte[] receiveData)
157     {
158         interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_start_2), false, false, 0);
159         cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_start_2));
160         try
161         {
162             int eventConnectionNumber = (receiveData[8] & 0xff);
163             eventConnectionNumber = eventConnectionNumber + ((receiveData[9]  & 0xff) << 8);
164             eventConnectionNumber = eventConnectionNumber + ((receiveData[10] & 0xff) << 16);
165             eventConnectionNumber = eventConnectionNumber + ((receiveData[11] & 0xff) << 24);
166             statusChecker.setEventConnectionNumber(eventConnectionNumber);
167             interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());
168
169             commandIssuer.enqueueCommand(new PtpIpCommandGeneric(this, SEQ_OPEN_SESSION, 50, isDumpLog, 0, 0x1002, 4, 0x41, 0, 0, 0));
170         }
171         catch (Exception e)
172         {
173             e.printStackTrace();
174         }
175     }
176
177     private boolean checkRegistrationMessage(byte[] receiveData)
178     {
179         // データ(Connection Number)がないときにはエラーと判断する
180         return (!((receiveData == null)||(receiveData.length < 12)));
181     }
182
183     private boolean checkEventInitialize(byte[] receiveData)
184     {
185         Log.v(TAG, "checkEventInitialize() ");
186         return (!(receiveData == null));
187     }
188
189     private void connectFinished()
190     {
191         try
192         {
193             // 接続成功のメッセージを出す
194             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
195
196             // ちょっと待つ
197             Thread.sleep(1000);
198
199             //interfaceProvider.getAsyncEventCommunication().connect();
200             //interfaceProvider.getCameraStatusWatcher().startStatusWatch(interfaceProvider.getStatusListener());  ステータスの定期確認は実施しない
201
202             // 接続成功!のメッセージを出す
203             interfaceProvider.getInformationReceiver().updateMessage(context.getString(R.string.connect_connected), false, false, 0);
204
205             onConnectNotify();
206         }
207         catch (Exception e)
208         {
209             e.printStackTrace();
210         }
211     }
212
213     private void onConnectNotify()
214     {
215         try
216         {
217             final Thread thread = new Thread(new Runnable()
218             {
219                 @Override
220                 public void run()
221                 {
222                     // カメラとの接続確立を通知する
223                     cameraStatusReceiver.onStatusNotify(context.getString(R.string.connect_connected));
224                     cameraStatusReceiver.onCameraConnected();
225                     Log.v(TAG, " onConnectNotify()");
226                 }
227             });
228             thread.start();
229         }
230         catch (Exception e)
231         {
232             e.printStackTrace();
233         }
234     }
235 }