OSDN Git Service

露出補正値の変更ができるようになった。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / thetacamerawrapper / status / ThetaCameraStatusWatcher.kt
index 9c1c03b..5393e09 100644 (file)
@@ -3,22 +3,27 @@ package jp.sfjp.gokigen.a01c.thetacamerawrapper.status
 import android.util.Log
 import jp.sfjp.gokigen.a01c.ICameraStatusUpdateNotify
 import jp.sfjp.gokigen.a01c.ICameraStatusWatcher
+import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaStatusHolder
+import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaSessionIdProvider
 import jp.sfjp.gokigen.a01c.thetacamerawrapper.status.ICameraStatus.*
 import jp.sfjp.gokigen.a01c.utils.SimpleHttpClient
 import org.json.JSONObject
 
-class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
+class ThetaCameraStatusWatcher(private val sessionIdProvider: IThetaSessionIdProvider, private val statusHolder : IThetaStatusHolder) : ICameraStatus, ICameraStatusWatcher
 {
     private val httpClient = SimpleHttpClient()
     private var whileFetching = false
     private var currentBatteryLevel : Double = 0.0
+    private var currentExposureCompensation : Double = 0.0
+    private var currentCaptureMode : String = ""
     private var currentCaptureStatus : String = ""
     private var currentBatteryStatus : String = ""
+    private var currentWhiteBalance : String = ""
 
 
     override fun getStatusList(key: String): List<String>
     {
-        return (ArrayList<String>())
+        return (ArrayList())
     }
 
     override fun getStatus(key: String): String
@@ -28,6 +33,117 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
 
     override fun startStatusWatch(notifier: ICameraStatusUpdateNotify)
     {
+        startStatusWatch1(notifier)
+    }
+
+    private fun startStatusWatch1(notifier: ICameraStatusUpdateNotify)
+    {
+        if (whileFetching)
+        {
+            Log.v(TAG, "startStatusWatch() already starting.")
+            return
+        }
+        whileFetching = true
+        try
+        {
+            val thread = Thread {
+                try
+                {
+                    val getOptionsUrl = "http://192.168.1.1/osc/commands/execute"
+                    val postData = if (sessionIdProvider.sessionId.isEmpty()) "{\"name\":\"camera.getOptions\",\"parameters\":{\"timeout\":0, \"optionNames\" : [ \"aperture\",\"captureMode\",\"exposureCompensation\",\"exposureProgram\",\"iso\",\"shutterSpeed\",\"whiteBalance\"] }" else "{\"name\":\"camera.getOptions\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"optionNames\" : [ \"aperture\",\"captureMode\",\"exposureCompensation\",\"exposureProgram\",\"iso\",\"shutterSpeed\",\"whiteBalance\"] }}"
+
+                    Log.v(TAG, " >>>>> START STATUS WATCH : $getOptionsUrl $postData")
+                    while (whileFetching)
+                    {
+                        val response: String? = httpClient.httpPostWithHeader(getOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
+                        if (!(response.isNullOrEmpty()))
+                        {
+                            // ステータスデータ受信
+                            checkStatus1(response, notifier)
+                        }
+                        try
+                        {
+                            Thread.sleep(loopWaitMs)
+                        }
+                        catch (e: Exception)
+                        {
+                            e.printStackTrace()
+                        }
+                    }
+                }
+                catch (e: Exception)
+                {
+                    e.printStackTrace()
+                }
+            }
+            thread.start()
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
+    private fun checkStatus1(response : String, notifier: ICameraStatusUpdateNotify)
+    {
+        try
+        {
+            //Log.v(TAG, " STATUS : $response")
+            val stateObject = JSONObject(response).getJSONObject("results").getJSONObject("options")
+            try
+            {
+                val exposureCompensation = stateObject.getDouble(THETA_EXPOSURE_COMPENSATION)
+                if (exposureCompensation != currentExposureCompensation)
+                {
+                    Log.v(TAG, " EXPREV : $currentExposureCompensation => $exposureCompensation")
+                    currentExposureCompensation = exposureCompensation
+                    notifier.updatedExposureCompensation(String.format("%1.1f",currentExposureCompensation))
+                }
+            }
+            catch (e : Exception)
+            {
+                e.printStackTrace()
+            }
+
+            try
+            {
+                val whiteBalance = stateObject.getString(THETA_WHITE_BALANCE)
+                if (whiteBalance != currentWhiteBalance)
+                {
+                    Log.v(TAG, " WB : $currentWhiteBalance => $whiteBalance")
+                    currentWhiteBalance = whiteBalance
+                }
+            }
+            catch (e : Exception)
+            {
+                e.printStackTrace()
+            }
+
+            try
+            {
+                val captureMode = stateObject.getString(THETA_CAPTURE_MODE)
+                if (captureMode != currentCaptureMode)
+                {
+                    Log.v(TAG, " CAPTURE MODE : $currentCaptureMode -> $captureMode")
+                    notifier.updateCaptureMode(captureMode)
+                    currentCaptureMode = captureMode
+                    statusHolder.setCaptureMode(captureMode)
+                }
+            }
+            catch (e : Exception)
+            {
+                e.printStackTrace()
+            }
+        }
+        catch (ee : Exception)
+        {
+            ee.printStackTrace()
+        }
+    }
+
+
+    private fun startStatusWatch0(notifier: ICameraStatusUpdateNotify)
+    {
         if (whileFetching)
         {
             Log.v(TAG, "startStatusWatch() already starting.")
@@ -47,11 +163,11 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
                         if (!(response.isNullOrEmpty()))
                         {
                             // ステータスデータ受信
-                            checkStatus(response, notifier)
+                            checkStatus0(response, notifier)
                         }
                         try
                         {
-                            Thread.sleep(300)
+                            Thread.sleep(loopWaitMs)
                         }
                         catch (e: Exception)
                         {
@@ -72,7 +188,7 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
         }
     }
 
-    private fun checkStatus(response : String, notifier: ICameraStatusUpdateNotify)
+    private fun checkStatus0(response : String, notifier: ICameraStatusUpdateNotify)
     {
         try
         {
@@ -136,10 +252,15 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
         whileFetching = false
     }
 
+    override fun prepareStatusWatch()
+    {
+        //
+    }
+
     companion object
     {
         private val TAG = ThetaCameraStatusWatcher::class.java.simpleName
         private const val timeoutMs = 1500
+        private const val loopWaitMs : Long = 660
     }
-
-}
\ No newline at end of file
+}