OSDN Git Service

カメラとの切断機能を追加してみる。
authorMRSa <mrsa@myad.jp>
Sun, 19 May 2019 12:04:47 +0000 (21:04 +0900)
committerMRSa <mrsa@myad.jp>
Sun, 19 May 2019 12:04:47 +0000 (21:04 +0900)
app/src/main/java/net/osdn/gokigen/cameratest/MainActivity.java
app/src/main/java/net/osdn/gokigen/cameratest/camtest/CamTest.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/Communication.java
app/src/main/java/net/osdn/gokigen/cameratest/fuji/Connection.java
app/src/main/res/menu/menu_main.xml
app/src/main/res/values/strings.xml

index 9f150a2..4265b7c 100644 (file)
@@ -168,18 +168,24 @@ public class MainActivity extends AppCompatActivity
             exitApplication();
             return (true);
         }
-/*
-        if (id == R.id.action_reset)
+        if (id == R.id.action_disconnect)
         {
-            testTarget.resetConnection();
+            testTarget.disconnect();
             return (true);
         }
-        //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings)
         {
             testTarget.settings();
             return (true);
         }
+
+/*
+        if (id == R.id.action_reset)
+        {
+            testTarget.resetConnection();
+            return (true);
+        }
+        //noinspection SimplifiableIfStatement
         if (id == R.id.action_up_value)
         {
             testTarget.valueUp();
index 67517fd..68a77b8 100644 (file)
@@ -71,9 +71,19 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
 
     public void disconnect()
     {
+        Log.v(TAG, "Disconnect");
+
+        showMessageText("DISCONNECT");
         try
         {
-            connection.disconnect();
+            Snackbar.make(activity.findViewById(R.id.constraintLayout), R.string.action_disconnect, Snackbar.LENGTH_SHORT).show();
+            Thread thread = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    connection.disconnect();
+                }
+            });
+            thread.start();
         }
         catch (Exception e)
         {
@@ -101,16 +111,16 @@ public class CamTest implements View.OnClickListener, View.OnTouchListener, ILiv
         {
             e.printStackTrace();
         }
-
     }
-/*
+
     public void settings()
     {
-        Log.v(TAG, "settings menu");
+        Log.v(TAG, "Show settings menu");
 
         showMessageText("BBBB");
     }
 
+/*
     public void valueUp()
     {
         Log.v(TAG, "value UP");
index 2328c0c..3613afd 100644 (file)
@@ -12,8 +12,8 @@ import java.net.Socket;
 class Communication
 {
     private final String TAG = toString();
-    private static final int BUFFER_SIZE = 1024 * 1024 + 8;
 
+    private static final int BUFFER_SIZE = 1024 * 1024 + 8;
     private static final int CONTROL_PORT = 55740;
     private static final int ASYNC_RESPONSE_PORT = 55741;
     private static final int STREAM_PORT = 55742;
@@ -24,6 +24,8 @@ class Communication
     private BufferedReader bufferedReader = null;
     private int sequenceNumber = 0x0000000a;
 
+    private boolean isDumpReceiveLog = true;
+
     private final FujiStreamReceiver stream;
     private final FujiAsyncResponseReceiver response;
 
@@ -47,7 +49,7 @@ class Communication
 
     void disconnect_socket()
     {
-        // ストリームを閉じる
+        // ストリームを全部閉じる
         try
         {
             dos.close();
@@ -57,6 +59,7 @@ class Communication
             e.printStackTrace();
         }
         dos = null;
+
         try
         {
             bufferedReader.close();
@@ -77,6 +80,7 @@ class Communication
             e.printStackTrace();
         }
         socket = null;
+        sequenceNumber = 0x0000000a;
         System.gc();
     }
 
@@ -102,8 +106,10 @@ class Communication
                 sendData[9]  = (byte) (((0x0000ff00 & sequenceNumber) >>> 8) & 0x000000ff);
                 sendData[10] = (byte) (((0x00ff0000 & sequenceNumber) >>> 16) & 0x000000ff);
                 sendData[11] = (byte) (((0xff000000 & sequenceNumber) >>> 24) & 0x000000ff);
-
-                Log.v(TAG, "SEQ No. : " + sequenceNumber);
+                if (isDumpReceiveLog)
+                {
+                    Log.v(TAG, "SEQ No. : " + sequenceNumber);
+                }
 
                 // シーケンス番号を増やす
                 sequenceNumber++;
@@ -111,7 +117,6 @@ class Communication
             // ログに送信メッセージを出力する
             dump_bytes("SEND[" + sendData.length + "] ", sendData);
 
-
             // (データを)送信
             dos.write(sendData);
             dos.flush();
@@ -131,7 +136,10 @@ class Communication
             if (is != null)
             {
                 int read_bytes = is.read(byte_array, 0, BUFFER_SIZE);
-                Log.v(TAG, "receive_from_camera() : " + read_bytes + " bytes.");
+                if (isDumpReceiveLog)
+                {
+                    Log.v(TAG, "receive_from_camera() : " + read_bytes + " bytes.");
+                }
                 return (new ReceivedDataHolder(byte_array, read_bytes));
             }
         }
@@ -142,8 +150,19 @@ class Communication
         return (new ReceivedDataHolder(new byte[0], 0));
     }
 
+    void dumpReceiveLog(boolean enable)
+    {
+        isDumpReceiveLog = enable;
+    }
+
     private void dump_bytes(String header, byte[] data)
     {
+        if (!isDumpReceiveLog)
+        {
+            // 受信ログを出さない
+            return;
+        }
+
         int index = 0;
         StringBuffer message;
         message = new StringBuffer();
index 0df760a..8f03b10 100644 (file)
@@ -157,8 +157,30 @@ public class Connection implements IFujiStatusRequest
     {
         try
         {
+            statusChecker.stop();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        try
+        {
             comm.stop_stream();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        try
+        {
             comm.stop_response();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        try
+        {
             comm.disconnect_socket();
         }
         catch (Exception e)
index 52c971c..3f56b52 100644 (file)
         android:orderInCategory="100"
         android:title="@string/action_wifi_settings"
         app:showAsAction="never" />
-<!--
     <item
-        android:id="@+id/action_reset"
+        android:id="@+id/action_settings"
         android:orderInCategory="100"
-        android:title="@string/action_reset"
+        android:title="@string/action_settings"
         app:showAsAction="never" />
     <item
-        android:id="@+id/action_settings"
+        android:id="@+id/action_disconnect"
         android:orderInCategory="100"
-        android:title="@string/action_settings"
+        android:title="@string/action_disconnect"
+        app:showAsAction="never" />
+
+<!--
+    <item
+        android:id="@+id/action_reset"
+        android:orderInCategory="100"
+        android:title="@string/action_reset"
         app:showAsAction="never" />
+
     <item
         android:id="@+id/action_up_value"
         android:orderInCategory="100"
index cc4b22f..0450647 100644 (file)
@@ -17,4 +17,5 @@
     <string name="action_up">Up</string>
     <string name="action_down">Down</string>
     <string name="action_reset">Reset Connection</string>
+    <string name="action_disconnect">Disconnect</string>
 </resources>