OSDN Git Service

Thetaのオプション設定をすこし整理と追加。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / thetacamerawrapper / status / ThetaCameraStatusWatcher.kt
index 9c1c03b..8c99d7a 100644 (file)
 package jp.sfjp.gokigen.a01c.thetacamerawrapper.status
 
+import android.graphics.Color
 import android.util.Log
-import jp.sfjp.gokigen.a01c.ICameraStatusUpdateNotify
 import jp.sfjp.gokigen.a01c.ICameraStatusWatcher
+import jp.sfjp.gokigen.a01c.IShowInformation
+import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaSessionIdProvider
+import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaStatusHolder
 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, private val showInformation: IShowInformation) : 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 currentExposureProgram : String = ""
     private var currentCaptureStatus : String = ""
     private var currentBatteryStatus : String = ""
+    private var currentWhiteBalance : String = ""
+    private var currentFilter : String = ""
 
+    override fun startStatusWatch()
+    {
+        startStatusWatch1()
+    }
 
-    override fun getStatusList(key: String): List<String>
+    private fun startStatusWatch1()
     {
-        return (ArrayList<String>())
+        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\",\"_filter\",\"whiteBalance\"] }" else "{\"name\":\"camera.getOptions\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"optionNames\" : [ \"aperture\",\"captureMode\",\"exposureCompensation\",\"exposureProgram\",\"iso\",\"shutterSpeed\",\"_filter\",\"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)
+                        }
+                        try
+                        {
+                            Thread.sleep(loopWaitMs)
+                        }
+                        catch (e: Exception)
+                        {
+                            e.printStackTrace()
+                        }
+                    }
+                }
+                catch (e: Exception)
+                {
+                    e.printStackTrace()
+                }
+            }
+            thread.start()
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
     }
 
-    override fun getStatus(key: String): String
+    private fun checkStatus1(response: String)
     {
-        return ("")
+        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
+                    if (currentExposureCompensation == 0.0)
+                    {
+                        // 補正なしの時には数値を表示しない
+                        showInformation.setMessage(IShowInformation.AREA_6, Color.WHITE, "")
+                    }
+                    else
+                    {
+                        showInformation.setMessage(IShowInformation.AREA_6, Color.WHITE, 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
+                    if (currentWhiteBalance == "auto")
+                    {
+                        showInformation.setMessage(IShowInformation.AREA_7, Color.WHITE, "")
+                    }
+                    else
+                    {
+                        showInformation.setMessage(IShowInformation.AREA_7, Color.WHITE, currentWhiteBalance)
+                    }
+                }
+            }
+            catch (e: Exception)
+            {
+                e.printStackTrace()
+            }
+
+            try
+            {
+                val captureMode = stateObject.getString(THETA_CAPTURE_MODE)
+                if (captureMode != currentCaptureMode)
+                {
+                    Log.v(TAG, " CAPTURE MODE : $currentCaptureMode -> $captureMode")
+                    currentCaptureMode = captureMode
+                    statusHolder.captureMode = captureMode
+                    showInformation.setMessage(IShowInformation.AREA_1, Color.WHITE, captureMode)
+                }
+            }
+            catch (e: Exception)
+            {
+                e.printStackTrace()
+            }
+
+            try
+            {
+                val exposureProgram = stateObject.getString(THETA_EXPOSURE_PROGRAM)
+                if (exposureProgram != currentExposureProgram)
+                {
+                    Log.v(TAG, " EXPOSURE PROGRAM : $currentExposureProgram -> $exposureProgram")
+                    currentExposureProgram = exposureProgram
+                    //showInformation.setMessage(IShowInformation.AREA_8, Color.WHITE, currentExposureProgram)
+                }
+            }
+            catch (e: Exception)
+            {
+                e.printStackTrace()
+            }
+
+            try
+            {
+                val filterValue = stateObject.getString(THETA_FILTER)
+                if (filterValue != currentFilter)
+                {
+                    Log.v(TAG, " FILTER : $currentFilter -> $filterValue")
+                    currentFilter = filterValue
+                    if (currentFilter == "off")
+                    {
+                        showInformation.setMessage(IShowInformation.AREA_5, Color.WHITE, "")
+                    }
+                    else
+                    {
+                        showInformation.setMessage(IShowInformation.AREA_5, Color.WHITE, currentFilter)
+                    }
+                }
+            }
+            catch (e: Exception)
+            {
+                e.printStackTrace()
+            }
+        }
+        catch (ee: Exception)
+        {
+            ee.printStackTrace()
+        }
     }
 
-    override fun startStatusWatch(notifier: ICameraStatusUpdateNotify)
+
+    private fun startStatusWatch0()
     {
         if (whileFetching)
         {
@@ -47,11 +208,11 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
                         if (!(response.isNullOrEmpty()))
                         {
                             // ステータスデータ受信
-                            checkStatus(response, notifier)
+                            checkStatus0(response)
                         }
                         try
                         {
-                            Thread.sleep(300)
+                            Thread.sleep(loopWaitMs)
                         }
                         catch (e: Exception)
                         {
@@ -72,7 +233,7 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
         }
     }
 
-    private fun checkStatus(response : String, notifier: ICameraStatusUpdateNotify)
+    private fun checkStatus0(response: String)
     {
         try
         {
@@ -85,10 +246,10 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
                 {
                     Log.v(TAG, " BATTERY : $currentBatteryLevel => $batteryLevel")
                     currentBatteryLevel = batteryLevel
-                    notifier.updateRemainBattery(currentBatteryLevel)
+                    updateRemainBattery(currentBatteryLevel)
                 }
             }
-            catch (e : Exception)
+            catch (e: Exception)
             {
                 e.printStackTrace()
             }
@@ -102,7 +263,7 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
                     currentBatteryStatus = batteryStatus
                 }
             }
-            catch (e : Exception)
+            catch (e: Exception)
             {
                 e.printStackTrace()
             }
@@ -115,22 +276,43 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
                     Log.v(TAG, " CAPTURE STATUS : $currentCaptureStatus -> $captureStatus")
                     if (captureStatus != "idle")
                     {
-                        notifier.updateCameraStatus(captureStatus)
+                        showInformation.setMessage(IShowInformation.AREA_5, Color.WHITE, captureStatus)
                     }
                     currentCaptureStatus = captureStatus
                 }
             }
-            catch (e : Exception)
+            catch (e: Exception)
             {
                 e.printStackTrace()
             }
         }
-        catch (ee : Exception)
+        catch (ee: Exception)
         {
             ee.printStackTrace()
         }
     }
 
+    private fun updateRemainBattery(percentageDouble: Double)
+    {
+        var color = Color.YELLOW
+        if (percentageDouble < 0.5)
+        {
+            if (percentageDouble < 0.3)
+            {
+                color = Color.RED
+            }
+            try
+            {
+                val percentage = Math.ceil(percentageDouble * 100.0).toInt()
+                showInformation.setMessage(IShowInformation.AREA_7, color, "Bat: $percentage%")
+            }
+            catch (ee: java.lang.Exception)
+            {
+                ee.printStackTrace()
+            }
+        }
+    }
+
     override fun stopStatusWatch()
     {
         whileFetching = false
@@ -140,6 +322,6 @@ class ThetaCameraStatusWatcher : ICameraStatus, ICameraStatusWatcher
     {
         private val TAG = ThetaCameraStatusWatcher::class.java.simpleName
         private const val timeoutMs = 1500
+        private const val loopWaitMs : Long = 350
     }
-
-}
\ No newline at end of file
+}