OSDN Git Service

すこしコードを整理。
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / Communication.java
1 package net.osdn.gokigen.cameratest.fuji;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import java.io.BufferedReader;
8 import java.io.DataOutputStream;
9 import java.io.InputStream;
10 import java.net.Socket;
11
12 class Communication
13 {
14     private final String TAG = toString();
15     private static final int BUFFER_SIZE = 1024 * 1024 + 8;
16
17     private static final int CONTROL_PORT = 55740;
18     private static final int ASYNC_RESPONSE_PORT = 55741;
19     private static final int STREAM_PORT = 55742;
20     private static final String CAMERA_IP = "192.168.0.1";
21
22     private Socket socket = null;
23     private DataOutputStream dos = null;
24     private BufferedReader bufferedReader = null;
25
26     private final FujiStreamReceiver stream;
27     private final FujiAsyncResponseReceiver response;
28
29     Communication(@NonNull ILiveViewImage imageViewer)
30     {
31         this.stream = new FujiStreamReceiver(CAMERA_IP, STREAM_PORT, imageViewer);
32         this.response = new FujiAsyncResponseReceiver(CAMERA_IP, ASYNC_RESPONSE_PORT);
33     }
34
35     void connect_socket()
36     {
37         try
38         {
39             socket = new Socket(CAMERA_IP, CONTROL_PORT);
40         }
41         catch (Exception e)
42         {
43             e.printStackTrace();
44         }
45     }
46
47     void disconnect_socket()
48     {
49         // ストリームを閉じる
50         try
51         {
52             dos.close();
53         }
54         catch (Exception e)
55         {
56             e.printStackTrace();
57         }
58         dos = null;
59         try
60         {
61             bufferedReader.close();
62         }
63         catch (Exception e)
64         {
65             e.printStackTrace();
66         }
67         bufferedReader = null;
68
69         try
70         {
71             socket.close();
72
73         }
74         catch (Exception e)
75         {
76             e.printStackTrace();
77         }
78         socket = null;
79         System.gc();
80     }
81
82     void send_to_camera(byte[] byte_array)
83     {
84         //Log.v(TAG, "send_to_camera() : " + byte_array.length + " bytes.");
85         try
86         {
87             dos = new DataOutputStream(socket.getOutputStream());
88
89             // 最初に4バイトのレングス長をつけて送る
90             byte[] sendData = new byte[byte_array.length + 4];
91
92             sendData[0] = (byte) (byte_array.length + 4);
93             sendData[1] = 0x00;
94             sendData[2] = 0x00;
95             sendData[3] = 0x00;
96             System.arraycopy(byte_array,0,sendData,4, byte_array.length);
97
98             // ログに送信メッセージを出力する
99             dump_bytes("SEND[" + sendData.length + "] ", sendData);
100
101             // (データを)送信
102             dos.write(sendData);
103             dos.flush();
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace();
108         }
109     }
110
111     ReceivedDataHolder receive_from_camera()
112     {
113         try
114         {
115             byte[] byte_array = new byte[BUFFER_SIZE];
116             InputStream is = socket.getInputStream();
117             if (is != null)
118             {
119                 int read_bytes = is.read(byte_array, 0, BUFFER_SIZE);
120                 Log.v(TAG, "receive_from_camera() : " + read_bytes + " bytes.");
121                 return (new ReceivedDataHolder(byte_array, read_bytes));
122             }
123         }
124         catch (Exception e)
125         {
126             e.printStackTrace();
127         }
128         return (new ReceivedDataHolder(new byte[0], 0));
129     }
130
131     private void dump_bytes(String header, byte[] data)
132     {
133         int index = 0;
134         StringBuffer message;
135         message = new StringBuffer();
136         for (byte item : data)
137         {
138             index++;
139             message.append(String.format("%02x ", item));
140             if (index >= 8)
141             {
142                 Log.v(TAG, header + " " + message);
143                 index = 0;
144                 message = new StringBuffer();
145             }
146         }
147         if (index != 0)
148         {
149             Log.v(TAG, header + " " + message);
150         }
151         System.gc();
152     }
153
154     void start_stream()
155     {
156         stream.start();
157     }
158
159     void stop_stream()
160     {
161         stream.stop();
162     }
163
164     void start_response()
165     {
166         response.start();
167     }
168
169     void stop_response()
170     {
171         response.stop();
172     }
173 }