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             comm.stop_stream();
161             comm.stop_response();
162             comm.disconnect_socket();
163         }
164         catch (Exception e)
165         {
166             e.printStackTrace();
167         }
168     }
169
170     @Override
171     public boolean requestStatus()
172     {
173         try
174         {
175             comm.send_to_camera(sequence.status_request_message(), true);
176             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
177             if (rx_bytes.getData().length > 0)
178             {
179                 // 受信したステータス情報を渡す
180                 statusChecker.statusReceived(rx_bytes);
181             }
182             dump_bytes(12, rx_bytes);
183
184             return (true);
185         }
186         catch (Exception e)
187         {
188             e.printStackTrace();
189         }
190         return (false);
191     }
192
193     private void dump_bytes(int indexNumber, ReceivedDataHolder data)
194     {
195         int index = 0;
196         StringBuffer message;
197         message = new StringBuffer();
198         for (byte item : data.getData())
199         {
200             index++;
201             message.append(String.format("%02x ", item));
202             if (index >= 8)
203             {
204                 Log.v(TAG, " RX [" + indexNumber + "] " + message);
205                 index = 0;
206                 message = new StringBuffer();
207             }
208         }
209         if (index != 0)
210         {
211             Log.v(TAG, " RX [" + indexNumber + "] "  + message);
212         }
213         System.gc();
214     }
215
216     public boolean execute_focus_point(PointF point)
217     {
218         try
219         {
220             byte x = (byte) (0x000000ff & Math.round(point.x));
221             byte y = (byte) (0x000000ff & Math.round(point.y));
222             Log.v(TAG, "DRIVE AF (" + x + "," + y + ")");
223
224             comm.send_to_camera(sequence.execute_focus_lock(x, y), true);
225
226             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
227             dump_bytes(16, rx_bytes);
228         }
229         catch (Exception e)
230         {
231             e.printStackTrace();
232         }
233         return (false);
234     }
235
236     public boolean execute_unlock_focus()
237     {
238         try
239         {
240             comm.send_to_camera(sequence.execute_focus_unlock(), true);
241
242             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
243             dump_bytes(17, rx_bytes);
244         }
245         catch (Exception e)
246         {
247             e.printStackTrace();
248         }
249         return (false);
250     }
251
252     public boolean execute_shutter()
253     {
254         try
255         {
256             comm.send_to_camera(sequence.execute_shutter_message(), true);
257
258             ReceivedDataHolder rx_bytes = comm.receive_from_camera();
259             dump_bytes(14, rx_bytes);
260         }
261         catch (Exception e)
262         {
263             e.printStackTrace();
264         }
265         return (false);
266     }
267 }