OSDN Git Service

露出補正値の変更ができるようになった。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / thetacamerawrapper / status / ThetaCameraStatusWatcher.kt
1 package jp.sfjp.gokigen.a01c.thetacamerawrapper.status
2
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
11
12 class ThetaCameraStatusWatcher(private val sessionIdProvider: IThetaSessionIdProvider, private val statusHolder : IThetaStatusHolder) : ICameraStatus, ICameraStatusWatcher
13 {
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 = ""
22
23
24     override fun getStatusList(key: String): List<String>
25     {
26         return (ArrayList())
27     }
28
29     override fun getStatus(key: String): String
30     {
31         return ("")
32     }
33
34     override fun startStatusWatch(notifier: ICameraStatusUpdateNotify)
35     {
36         startStatusWatch1(notifier)
37     }
38
39     private fun startStatusWatch1(notifier: ICameraStatusUpdateNotify)
40     {
41         if (whileFetching)
42         {
43             Log.v(TAG, "startStatusWatch() already starting.")
44             return
45         }
46         whileFetching = true
47         try
48         {
49             val thread = Thread {
50                 try
51                 {
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\"] }}"
54
55                     Log.v(TAG, " >>>>> START STATUS WATCH : $getOptionsUrl $postData")
56                     while (whileFetching)
57                     {
58                         val response: String? = httpClient.httpPostWithHeader(getOptionsUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
59                         if (!(response.isNullOrEmpty()))
60                         {
61                             // ステータスデータ受信
62                             checkStatus1(response, notifier)
63                         }
64                         try
65                         {
66                             Thread.sleep(loopWaitMs)
67                         }
68                         catch (e: Exception)
69                         {
70                             e.printStackTrace()
71                         }
72                     }
73                 }
74                 catch (e: Exception)
75                 {
76                     e.printStackTrace()
77                 }
78             }
79             thread.start()
80         }
81         catch (e: Exception)
82         {
83             e.printStackTrace()
84         }
85     }
86
87     private fun checkStatus1(response : String, notifier: ICameraStatusUpdateNotify)
88     {
89         try
90         {
91             //Log.v(TAG, " STATUS : $response")
92             val stateObject = JSONObject(response).getJSONObject("results").getJSONObject("options")
93             try
94             {
95                 val exposureCompensation = stateObject.getDouble(THETA_EXPOSURE_COMPENSATION)
96                 if (exposureCompensation != currentExposureCompensation)
97                 {
98                     Log.v(TAG, " EXPREV : $currentExposureCompensation => $exposureCompensation")
99                     currentExposureCompensation = exposureCompensation
100                     notifier.updatedExposureCompensation(String.format("%1.1f",currentExposureCompensation))
101                 }
102             }
103             catch (e : Exception)
104             {
105                 e.printStackTrace()
106             }
107
108             try
109             {
110                 val whiteBalance = stateObject.getString(THETA_WHITE_BALANCE)
111                 if (whiteBalance != currentWhiteBalance)
112                 {
113                     Log.v(TAG, " WB : $currentWhiteBalance => $whiteBalance")
114                     currentWhiteBalance = whiteBalance
115                 }
116             }
117             catch (e : Exception)
118             {
119                 e.printStackTrace()
120             }
121
122             try
123             {
124                 val captureMode = stateObject.getString(THETA_CAPTURE_MODE)
125                 if (captureMode != currentCaptureMode)
126                 {
127                     Log.v(TAG, " CAPTURE MODE : $currentCaptureMode -> $captureMode")
128                     notifier.updateCaptureMode(captureMode)
129                     currentCaptureMode = captureMode
130                     statusHolder.setCaptureMode(captureMode)
131                 }
132             }
133             catch (e : Exception)
134             {
135                 e.printStackTrace()
136             }
137         }
138         catch (ee : Exception)
139         {
140             ee.printStackTrace()
141         }
142     }
143
144
145     private fun startStatusWatch0(notifier: ICameraStatusUpdateNotify)
146     {
147         if (whileFetching)
148         {
149             Log.v(TAG, "startStatusWatch() already starting.")
150             return
151         }
152         whileFetching = true
153         try
154         {
155             val thread = Thread {
156                 try
157                 {
158                     val getStateUrl = "http://192.168.1.1/osc/state"
159                     Log.v(TAG, " >>>>> START STATUS WATCH : $getStateUrl")
160                     while (whileFetching)
161                     {
162                         val response: String? = httpClient.httpPostWithHeader(getStateUrl, "", null, "application/json;charset=utf-8", timeoutMs)
163                         if (!(response.isNullOrEmpty()))
164                         {
165                             // ステータスデータ受信
166                             checkStatus0(response, notifier)
167                         }
168                         try
169                         {
170                             Thread.sleep(loopWaitMs)
171                         }
172                         catch (e: Exception)
173                         {
174                             e.printStackTrace()
175                         }
176                     }
177                 }
178                 catch (e: Exception)
179                 {
180                     e.printStackTrace()
181                 }
182             }
183             thread.start()
184         }
185         catch (e: Exception)
186         {
187             e.printStackTrace()
188         }
189     }
190
191     private fun checkStatus0(response : String, notifier: ICameraStatusUpdateNotify)
192     {
193         try
194         {
195             //Log.v(TAG, " STATUS : $response")
196             val stateObject = JSONObject(response).getJSONObject("state")
197             try
198             {
199                 val batteryLevel = stateObject.getDouble(THETA_BATTERY_LEVEL)
200                 if (batteryLevel != currentBatteryLevel)
201                 {
202                     Log.v(TAG, " BATTERY : $currentBatteryLevel => $batteryLevel")
203                     currentBatteryLevel = batteryLevel
204                     notifier.updateRemainBattery(currentBatteryLevel)
205                 }
206             }
207             catch (e : Exception)
208             {
209                 e.printStackTrace()
210             }
211
212             try
213             {
214                 val batteryStatus = stateObject.getString(THETA_CAPTURE_STATUS)
215                 if (batteryStatus != currentBatteryStatus)
216                 {
217                     Log.v(TAG, " BATTERY STATUS : $currentBatteryStatus => $batteryStatus")
218                     currentBatteryStatus = batteryStatus
219                 }
220             }
221             catch (e : Exception)
222             {
223                 e.printStackTrace()
224             }
225
226             try
227             {
228                 val captureStatus = stateObject.getString(THETA_CAPTURE_STATUS)
229                 if (captureStatus != currentCaptureStatus)
230                 {
231                     Log.v(TAG, " CAPTURE STATUS : $currentCaptureStatus -> $captureStatus")
232                     if (captureStatus != "idle")
233                     {
234                         notifier.updateCameraStatus(captureStatus)
235                     }
236                     currentCaptureStatus = captureStatus
237                 }
238             }
239             catch (e : Exception)
240             {
241                 e.printStackTrace()
242             }
243         }
244         catch (ee : Exception)
245         {
246             ee.printStackTrace()
247         }
248     }
249
250     override fun stopStatusWatch()
251     {
252         whileFetching = false
253     }
254
255     override fun prepareStatusWatch()
256     {
257         //
258     }
259
260     companion object
261     {
262         private val TAG = ThetaCameraStatusWatcher::class.java.simpleName
263         private const val timeoutMs = 1500
264         private const val loopWaitMs : Long = 660
265     }
266 }