OSDN Git Service

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