1 package jp.sfjp.gokigen.a01c.thetacamerawrapper.status
3 import android.util.Log
4 import jp.sfjp.gokigen.a01c.ICameraStatusUpdateNotify
5 import jp.sfjp.gokigen.a01c.ICameraStatusWatcher
6 import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaStatusHolder
7 import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaSessionIdProvider
8 import jp.sfjp.gokigen.a01c.thetacamerawrapper.status.ICameraStatus.*
9 import jp.sfjp.gokigen.a01c.utils.SimpleHttpClient
10 import org.json.JSONObject
12 class ThetaCameraStatusWatcher(private val sessionIdProvider: IThetaSessionIdProvider, private val statusHolder : IThetaStatusHolder) : ICameraStatus, ICameraStatusWatcher
14 private val httpClient = SimpleHttpClient()
15 private var whileFetching = false
16 private var currentBatteryLevel : Double = 0.0
17 private var currentExposureCompensation : Double = 0.0
18 private var currentCaptureMode : String = ""
19 private var currentCaptureStatus : String = ""
20 private var currentBatteryStatus : String = ""
21 private var currentWhiteBalance : String = ""
24 override fun getStatusList(key: String): List<String>
29 override fun getStatus(key: String): String
34 override fun startStatusWatch(notifier: ICameraStatusUpdateNotify)
36 startStatusWatch1(notifier)
39 private fun startStatusWatch1(notifier: ICameraStatusUpdateNotify)
43 Log.v(TAG, "startStatusWatch() already starting.")
52 val getOptionsUrl = "http://192.168.1.1/osc/commands/execute"
53 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\"] }}"
55 Log.v(TAG, " >>>>> START STATUS WATCH : $getOptionsUrl $postData")
58 val response: String? = httpClient.httpPostWithHeader(getOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
59 if (!(response.isNullOrEmpty()))
62 checkStatus1(response, notifier)
66 Thread.sleep(loopWaitMs)
87 private fun checkStatus1(response : String, notifier: ICameraStatusUpdateNotify)
91 //Log.v(TAG, " STATUS : $response")
92 val stateObject = JSONObject(response).getJSONObject("results").getJSONObject("options")
95 val exposureCompensation = stateObject.getDouble(THETA_EXPOSURE_COMPENSATION)
96 if (exposureCompensation != currentExposureCompensation)
98 Log.v(TAG, " EXPREV : $currentExposureCompensation => $exposureCompensation")
99 currentExposureCompensation = exposureCompensation
100 notifier.updatedExposureCompensation(String.format("%1.1f",currentExposureCompensation))
103 catch (e : Exception)
110 val whiteBalance = stateObject.getString(THETA_WHITE_BALANCE)
111 if (whiteBalance != currentWhiteBalance)
113 Log.v(TAG, " WB : $currentWhiteBalance => $whiteBalance")
114 currentWhiteBalance = whiteBalance
117 catch (e : Exception)
124 val captureMode = stateObject.getString(THETA_CAPTURE_MODE)
125 if (captureMode != currentCaptureMode)
127 Log.v(TAG, " CAPTURE MODE : $currentCaptureMode -> $captureMode")
128 notifier.updateCaptureMode(captureMode)
129 currentCaptureMode = captureMode
130 statusHolder.setCaptureMode(captureMode)
133 catch (e : Exception)
138 catch (ee : Exception)
145 private fun startStatusWatch0(notifier: ICameraStatusUpdateNotify)
149 Log.v(TAG, "startStatusWatch() already starting.")
155 val thread = Thread {
158 val getStateUrl = "http://192.168.1.1/osc/state"
159 Log.v(TAG, " >>>>> START STATUS WATCH : $getStateUrl")
160 while (whileFetching)
162 val response: String? = httpClient.httpPostWithHeader(getStateUrl, "", null, "application/json;charset=utf-8", timeoutMs)
163 if (!(response.isNullOrEmpty()))
166 checkStatus0(response, notifier)
170 Thread.sleep(loopWaitMs)
191 private fun checkStatus0(response : String, notifier: ICameraStatusUpdateNotify)
195 //Log.v(TAG, " STATUS : $response")
196 val stateObject = JSONObject(response).getJSONObject("state")
199 val batteryLevel = stateObject.getDouble(THETA_BATTERY_LEVEL)
200 if (batteryLevel != currentBatteryLevel)
202 Log.v(TAG, " BATTERY : $currentBatteryLevel => $batteryLevel")
203 currentBatteryLevel = batteryLevel
204 notifier.updateRemainBattery(currentBatteryLevel)
207 catch (e : Exception)
214 val batteryStatus = stateObject.getString(THETA_CAPTURE_STATUS)
215 if (batteryStatus != currentBatteryStatus)
217 Log.v(TAG, " BATTERY STATUS : $currentBatteryStatus => $batteryStatus")
218 currentBatteryStatus = batteryStatus
221 catch (e : Exception)
228 val captureStatus = stateObject.getString(THETA_CAPTURE_STATUS)
229 if (captureStatus != currentCaptureStatus)
231 Log.v(TAG, " CAPTURE STATUS : $currentCaptureStatus -> $captureStatus")
232 if (captureStatus != "idle")
234 notifier.updateCameraStatus(captureStatus)
236 currentCaptureStatus = captureStatus
239 catch (e : Exception)
244 catch (ee : Exception)
250 override fun stopStatusWatch()
252 whileFetching = false
255 override fun prepareStatusWatch()
262 private val TAG = ThetaCameraStatusWatcher::class.java.simpleName
263 private const val timeoutMs = 1500
264 private const val loopWaitMs : Long = 660