OSDN Git Service

カメラとの切断機能を追加してみる。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / Connection.java
1 package net.osdn.gokigen.cameratest.fuji;
2
3 import android.graphics.PointF;
4 import android.util.Log;
5
6 import androidx.annotation.NonNull;
7
8 import net.osdn.gokigen.cameratest.fuji.statuses.FujiStatusChecker;
9 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusNotify;
10 import net.osdn.gokigen.cameratest.fuji.statuses.IFujiStatusRequest;
11
12 public class Connection implements IFujiStatusRequest
13 {
14     private final String TAG = toString();
15     private final MessageSequence sequence;
16     private final Communication comm;
17     private final FujiStatusChecker statusChecker;
18
19     public Connection(@NonNull ILiveViewImage imageViewer, @NonNull IFujiStatusNotify notify)
20     {
21         this.comm = new Communication(imageViewer);
22         this.sequence = new MessageSequence();
23         this.statusChecker = new FujiStatusChecker(this, notify);
24     }
25
26     public boolean start_connect()
27     {
28         boolean ret = false;
29
30         if (connect_to_camera())
31         {
32             ret = requestStatus();
33             if (ret)
34             {
35                 // 定期監視の開始
36                 statusChecker.start();
37             }
38         }
39         return (ret);
40     }
41
42     private boolean connect_to_camera()
43     {
44         try
45         {
46             ReceivedDataHolder rx_bytes;
47             comm.connect_socket();
48
49             comm.send_to_camera(sequence.registration_message(), false);
50             rx_bytes = comm.receive_from_camera();
51             dump_bytes(0, rx_bytes);
52             Thread.sleep(50);
53
54             // 応答エラーの場合は この値が返ってくるはず  = {0x05, 0x00, 0x00, 0x00, 0x19, 0x20, 0x00, 0x00};
55
56             // start_messageを送信
57             comm.send_to_camera(sequence.start_message(), false);
58             rx_bytes = comm.receive_from_camera();
59             dump_bytes(1, rx_bytes);
60             Thread.sleep(50);
61
62             //  なんだろう?? (送信が必要なようだが)
63             comm.send_to_camera(sequence.start_message2(), false);
64             rx_bytes = comm.receive_from_camera();
65             dump_bytes(2, rx_bytes);
66             Thread.sleep(50);
67
68             // two_part messageを発行 (その1)
69             comm.send_to_camera(sequence.start_message3_1(), false);
70             rx_bytes = comm.receive_from_camera();
71             dump_bytes(3, rx_bytes);
72             Thread.sleep(50);
73
74             // two_part messageを発行 (その2)
75             comm.send_to_camera(sequence.start_message3_2(), false);
76             rx_bytes = comm.receive_from_camera();
77             dump_bytes(4, rx_bytes);
78             Thread.sleep(50);
79
80             // remote mode
81             comm.send_to_camera(sequence.start_message4(), false);
82             rx_bytes = comm.receive_from_camera();
83             dump_bytes(5, rx_bytes);
84             Thread.sleep(50);
85
86             // two_part messageを発行 (その1)
87             comm.send_to_camera(sequence.start_message5_1(), false);
88             rx_bytes = comm.receive_from_camera();
89             dump_bytes(6, rx_bytes);
90             Thread.sleep(50);
91
92             // two_part messageを発行 (その2)
93             comm.send_to_camera(sequence.start_message5_2(), false);
94             rx_bytes = comm.receive_from_camera();
95             dump_bytes(7, rx_bytes);
96             Thread.sleep(50);
97
98             // ????
99             comm.send_to_camera(sequence.start_message6(), false);
100             rx_bytes = comm.receive_from_camera();
101             dump_bytes(8, rx_bytes);
102             Thread.sleep(50);
103
104             // ????
105             comm.send_to_camera(sequence.start_message7(), false);
106             rx_bytes = comm.receive_from_camera();
107             dump_bytes(9, rx_bytes);
108             Thread.sleep(50);
109
110             // ????
111             comm.send_to_camera(sequence.start_message8(), false);
112             rx_bytes = comm.receive_from_camera();
113             dump_bytes(10, rx_bytes);
114             Thread.sleep(50);
115
116             // ????
117             comm.send_to_camera(sequence.start_message9(), false);
118
119             // 応答OKの場合は、8バイト ({0x03, 0x00, 0x01, 0x20} + {0x10, 0x02, 0x00, 0x00} )が応答されるはず
120             rx_bytes = comm.receive_from_camera();
121             dump_bytes(11, rx_bytes);
122             Thread.sleep(150);
123
124
125             // 別のポートもオープンして動作を行う。 (1500ms程度待つといけるみたいだ...)
126             Thread.sleep(2000);
127             comm.start_stream();
128             comm.start_response();
129
130             Log.v(TAG, "connect_to_camera DONE.");
131             return (true);
132         }
133         catch (Exception e)
134         {
135             e.printStackTrace();
136         }
137         return (false);
138     }
139
140     public void reset_to_camera()
141     {
142         try
143         {
144             comm.send_to_camera(sequence.reset_message(), true);
145             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
146             dump_bytes(0, rx_bytes);
147             statusChecker.stop();
148             Thread.sleep(150);
149         }
150         catch (Exception e)
151         {
152             e.printStackTrace();
153         }
154     }
155
156     public void disconnect()
157     {
158         try
159         {
160             statusChecker.stop();
161         }
162         catch (Exception e)
163         {
164             e.printStackTrace();
165         }
166         try
167         {
168             comm.stop_stream();
169         }
170         catch (Exception e)
171         {
172             e.printStackTrace();
173         }
174         try
175         {
176             comm.stop_response();
177         }
178         catch (Exception e)
179         {
180             e.printStackTrace();
181         }
182         try
183         {
184             comm.disconnect_socket();
185         }
186         catch (Exception e)
187         {
188             e.printStackTrace();
189         }
190     }
191
192     @Override
193     public boolean requestStatus()
194     {
195         try
196         {
197             comm.send_to_camera(sequence.status_request_message(), true);
198             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
199             if (rx_bytes.getData().length > 0)
200             {
201                 // 受信したステータス情報を渡す
202                 statusChecker.statusReceived(rx_bytes);
203             }
204             dump_bytes(12, rx_bytes);
205
206             return (true);
207         }
208         catch (Exception e)
209         {
210             e.printStackTrace();
211         }
212         return (false);
213     }
214
215     private void dump_bytes(int indexNumber, ReceivedDataHolder data)
216     {
217         int index = 0;
218         StringBuffer message;
219         message = new StringBuffer();
220         for (byte item : data.getData())
221         {
222             index++;
223             message.append(String.format("%02x ", item));
224             if (index >= 8)
225             {
226                 Log.v(TAG, " RX [" + indexNumber + "] " + message);
227                 index = 0;
228                 message = new StringBuffer();
229             }
230         }
231         if (index != 0)
232         {
233             Log.v(TAG, " RX [" + indexNumber + "] "  + message);
234         }
235         System.gc();
236     }
237
238     public boolean execute_focus_point(PointF point)
239     {
240         try
241         {
242             byte x = (byte) (0x000000ff & Math.round(point.x));
243             byte y = (byte) (0x000000ff & Math.round(point.y));
244             Log.v(TAG, "DRIVE AF (" + x + "," + y + ")");
245
246             comm.send_to_camera(sequence.execute_focus_lock(x, y), true);
247
248             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
249             dump_bytes(16, rx_bytes);
250         }
251         catch (Exception e)
252         {
253             e.printStackTrace();
254         }
255         return (false);
256     }
257
258     public boolean execute_unlock_focus()
259     {
260         try
261         {
262             comm.send_to_camera(sequence.execute_focus_unlock(), true);
263
264             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
265             dump_bytes(17, rx_bytes);
266         }
267         catch (Exception e)
268         {
269             e.printStackTrace();
270         }
271         return (false);
272     }
273
274     public boolean execute_shutter()
275     {
276         try
277         {
278             comm.send_to_camera(sequence.execute_shutter_message(), true);
279
280             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
281             dump_bytes(14, rx_bytes);
282         }
283         catch (Exception e)
284         {
285             e.printStackTrace();
286         }
287         return (false);
288     }
289 }