OSDN Git Service

とりあえずコミット。
authorMRSa <mrsa@myad.jp>
Fri, 26 Feb 2021 15:38:16 +0000 (00:38 +0900)
committerMRSa <mrsa@myad.jp>
Fri, 26 Feb 2021 15:38:16 +0000 (00:38 +0900)
app/src/main/java/jp/osdn/gokigen/thetaview/bluetooth/ThetaSetupBluetoothSPP.kt [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/ThetaControl.kt
app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/connection/ThetaCameraConnection.kt
app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/operation/ThetaOptionGetControl.kt [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/operation/ThetaOptionSetControl.kt [moved from app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/operation/ThetaOptionUpdateControl.kt with 87% similarity]

diff --git a/app/src/main/java/jp/osdn/gokigen/thetaview/bluetooth/ThetaSetupBluetoothSPP.kt b/app/src/main/java/jp/osdn/gokigen/thetaview/bluetooth/ThetaSetupBluetoothSPP.kt
new file mode 100644 (file)
index 0000000..5505f36
--- /dev/null
@@ -0,0 +1,150 @@
+package jp.osdn.gokigen.thetaview.bluetooth
+
+
+import android.util.Log
+import jp.osdn.gokigen.thetaview.camera.theta.operation.IOperationCallback
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionGetControl
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionSetControl
+import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
+import org.json.JSONObject
+
+class ThetaSetupBluetoothSPP(private val sessionIdProvider: IThetaSessionIdProvider, executeUrl : String = "http://192.168.1.1") : IOperationCallback
+{
+    private val getOption = ThetaOptionGetControl(sessionIdProvider, executeUrl)
+    private val setOption = ThetaOptionSetControl(sessionIdProvider, executeUrl)
+    private var readyCallback : IOperationCallback? = null
+
+    companion object
+    {
+        private val TAG = ThetaSetupBluetoothSPP::class.java.simpleName
+    }
+
+    fun setupBluetoothSPP(callback : IOperationCallback?)
+    {
+        this.readyCallback = callback
+        getOption.getOptions("[ \"_bluetoothRole\", \"_bluetoothPower\", \"_bluetoothClassicEnable\" ]", this)
+    }
+
+    private fun setBluetoothPowerOn(bluetoothPower : String?)
+    {
+        if (bluetoothPower == null)
+        {
+            readyCallback?.operationExecuted(-1, "")
+            return
+        }
+        try
+        {
+            if (!bluetoothPower.contains("ON"))
+            {
+                //
+                Log.v(TAG, " --- Bluetooth Power ON ---")
+                setOption.setOptions("\"_bluetoothPower\" : \"ON\"", (sessionIdProvider.sessionId.isEmpty()), object : IOperationCallback {
+                    override fun operationExecuted(result: Int, resultStr: String?)
+                    {
+                        readyCallback?.operationExecuted(result, resultStr)
+                    }
+                })
+            }
+            readyCallback?.operationExecuted(0, "OK")
+            return
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, "")
+    }
+
+    private fun setBluetoothClassicEnable(bluetoothClassicEnable: Boolean, bluetoothPower: String?)
+    {
+        try
+        {
+            if (!bluetoothClassicEnable)
+            {
+                //
+                Log.v(TAG, " --- Bluetooth Classic Enable ---")
+                setOption.setOptions("\"_bluetoothClassicEnable\" : \"true\"",(sessionIdProvider.sessionId.isEmpty()), object : IOperationCallback {
+                    override fun operationExecuted(result: Int, resultStr: String?)
+                    {
+                        if (resultStr != null)
+                        {
+                            Log.v(TAG, "_bluetoothClassicEnable : true  $resultStr ")
+                            setBluetoothPowerOn(bluetoothPower)
+                        }
+                        else
+                        {
+                            readyCallback?.operationExecuted(-1, resultStr)
+                        }
+                    }
+                })
+            }
+            else
+            {
+                setBluetoothPowerOn(bluetoothPower)
+            }
+            return
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, "")
+    }
+
+    override fun operationExecuted(result: Int, resultStr: String?)
+    {
+        Log.v(TAG, " optionSet.getOptions(Bluetooth) : $resultStr? ")
+        if (resultStr == null)
+        {
+            readyCallback?.operationExecuted(-1, resultStr)
+            return
+        }
+        try
+        {
+            val stateObject = JSONObject(resultStr).getJSONObject("results").getJSONObject("options")
+            try
+            {
+                // Bluetoothの状態を確認
+                val bluetoothClassicEnable = stateObject.getBoolean("_bluetoothClassicEnable")
+                val bluetoothPower = stateObject.getString("_bluetoothPower")
+                val bluetoothRole = stateObject.getString("_bluetoothRole")
+
+                Log.v(TAG, " BLUETOOTH CLASSIC : $bluetoothClassicEnable  POWER: $bluetoothPower  ROLE: $bluetoothRole")
+
+                if (bluetoothRole.contains("Central"))
+                {
+                    // Central: ON ⇒ OFF にする
+                    Log.v(TAG, " --- CHANGE TO 'Peripheral' ---")
+                    setOption.setOptions("\"_bluetoothRole\" : \"Peripheral\"", (sessionIdProvider.sessionId.isEmpty()), object : IOperationCallback {
+                        override fun operationExecuted(result: Int, resultStr: String?)
+                        {
+                            if (resultStr != null)
+                            {
+                                Log.v(TAG, "_bluetoothRole : Peripheral  $resultStr ")
+                                setBluetoothClassicEnable(bluetoothClassicEnable, bluetoothPower)
+                            }
+                            else
+                            {
+                                readyCallback?.operationExecuted(-1, resultStr)
+                            }
+                        }
+                    })
+                }
+                else
+                {
+                    setBluetoothClassicEnable(bluetoothClassicEnable, bluetoothPower)
+                }
+                return
+            }
+            catch (e: Exception)
+            {
+                e.printStackTrace()
+            }
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+        readyCallback?.operationExecuted(-1, resultStr)
+    }
+}
index 4a2d36e..4af6c5b 100644 (file)
@@ -11,7 +11,7 @@ import jp.osdn.gokigen.thetaview.camera.ICameraStatusReceiver
 import jp.osdn.gokigen.thetaview.camera.theta.connection.ThetaCameraConnection
 import jp.osdn.gokigen.thetaview.camera.theta.liveview.ThetaLiveViewControl
 import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaMovieRecordingControl
-import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionUpdateControl
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionSetControl
 import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaSingleShotControl
 import jp.osdn.gokigen.thetaview.camera.theta.status.ICaptureModeReceiver
 import jp.osdn.gokigen.thetaview.camera.theta.status.ThetaCameraStatusWatcher
@@ -52,7 +52,7 @@ class ThetaControl(private val context: AppCompatActivity, private val showInfor
             // video -> image
             "\"captureMode\" : \"image\""
         }
-        ThetaOptionUpdateControl(sessionIdHolder).setOptions(options, sessionIdHolder.isApiLevelV21())
+        ThetaOptionSetControl(sessionIdHolder).setOptions(options, sessionIdHolder.isApiLevelV21())
     }
 
     override fun changedCaptureMode(captureMode : String)
index 1a347dc..cf16187 100644 (file)
@@ -10,7 +10,7 @@ import androidx.appcompat.app.AppCompatActivity
 import jp.osdn.gokigen.thetaview.R
 import jp.osdn.gokigen.thetaview.camera.ICameraStatusReceiver
 import jp.osdn.gokigen.thetaview.camera.theta.operation.IOperationCallback
-import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionUpdateControl
+import jp.osdn.gokigen.thetaview.camera.theta.operation.ThetaOptionSetControl
 import jp.osdn.gokigen.thetaview.scene.ICameraConnection
 import jp.osdn.gokigen.thetaview.scene.ICameraConnectionStatus
 import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdNotifier
@@ -224,7 +224,7 @@ class ThetaCameraConnection(private val context: AppCompatActivity, private val
     {
         try
         {
-            val optionSet = ThetaOptionUpdateControl(sessionIdProvider)
+            val optionSet = ThetaOptionSetControl(sessionIdProvider)
             optionSet.setOptions("\"captureMode\" : \"image\"", (sessionIdProvider.sessionId.isBlank()),
                     object : IOperationCallback { override fun operationExecuted(result: Int, resultStr: String?)
                     {
diff --git a/app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/operation/ThetaOptionGetControl.kt b/app/src/main/java/jp/osdn/gokigen/thetaview/camera/theta/operation/ThetaOptionGetControl.kt
new file mode 100644 (file)
index 0000000..638f5a1
--- /dev/null
@@ -0,0 +1,59 @@
+package jp.osdn.gokigen.thetaview.camera.theta.operation
+
+import android.util.Log
+import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
+import jp.osdn.gokigen.thetaview.utils.communication.SimpleHttpClient
+
+class ThetaOptionGetControl(private val sessionIdProvider: IThetaSessionIdProvider, private val executeUrl : String = "http://192.168.1.1")
+{
+    private val httpClient = SimpleHttpClient()
+
+    /**
+     *
+     *
+     */
+    fun getOptions(options: String, callBack: IOperationCallback? = null)
+    {
+        //Log.v(TAG, "getOptions()  MSG : $options")
+        try
+        {
+            val thread = Thread {
+                try
+                {
+                    val setOptionsUrl = "${executeUrl}/osc/commands/execute"
+                    val postData = "{\"name\":\"camera.getOptions\",\"parameters\":{\"timeout\":0, \"optionNames\": $options}}"
+                    val result: String? = httpClient.httpPostWithHeader(setOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
+                    if ((result != null) && (result.isNotEmpty()))
+                    {
+                        Log.v(TAG, " getOptions() : $result (${setOptionsUrl})")
+                        callBack?.operationExecuted(0, result)
+                    }
+                    else
+                    {
+                        Log.v(TAG, "getOptions() reply is null or empty.  $postData (${setOptionsUrl})")
+                        callBack?.operationExecuted(-1, "")
+                    }
+                }
+                catch (e: Exception)
+                {
+                    Log.v(TAG, "getOptions() Exception : $options")
+                    e.printStackTrace()
+                    callBack?.operationExecuted(-1, e.localizedMessage)
+                }
+            }
+            thread.start()
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+            callBack?.operationExecuted(-1, e.localizedMessage)
+        }
+    }
+
+    companion object
+    {
+        private val TAG = ThetaOptionGetControl::class.java.simpleName
+        private const val timeoutMs = 1500
+    }
+
+}
\ No newline at end of file
@@ -4,7 +4,7 @@ import android.util.Log
 import jp.osdn.gokigen.thetaview.camera.theta.status.IThetaSessionIdProvider
 import jp.osdn.gokigen.thetaview.utils.communication.SimpleHttpClient
 
-class ThetaOptionUpdateControl(private val sessionIdProvider: IThetaSessionIdProvider)
+class ThetaOptionSetControl(private val sessionIdProvider: IThetaSessionIdProvider, private val executeUrl : String = "http://192.168.1.1")
 {
     private val httpClient = SimpleHttpClient()
 
@@ -20,7 +20,7 @@ class ThetaOptionUpdateControl(private val sessionIdProvider: IThetaSessionIdPro
             val thread = Thread {
                 try
                 {
-                    val setOptionsUrl = "http://192.168.1.1/osc/commands/execute"
+                    val setOptionsUrl = "${executeUrl}/osc/commands/execute"
                     val postData = if (useOSCv2) "{\"name\":\"camera.setOptions\",\"parameters\":{\"timeout\":0, \"options\": {$options}}}" else "{\"name\":\"camera.setOptions\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"options\": { $options }}}"
                     val result: String? = httpClient.httpPostWithHeader(setOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
                     if ((result != null) && (result.isNotEmpty()))
@@ -51,7 +51,7 @@ class ThetaOptionUpdateControl(private val sessionIdProvider: IThetaSessionIdPro
 
     companion object
     {
-        private val TAG = ThetaOptionUpdateControl::class.java.simpleName
+        private val TAG = ThetaOptionSetControl::class.java.simpleName
         private const val timeoutMs = 1500
     }
 }