1 package jp.osdn.gokigen.gokigenassets.camera.vendor.theta.connection
3 import android.util.Log
4 import androidx.appcompat.app.AppCompatActivity
5 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraConnection
6 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraConnectionStatus
7 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusReceiver
8 import jp.osdn.gokigen.gokigenassets.camera.vendor.theta.status.IThetaSessionIdNotifier
9 import jp.osdn.gokigen.gokigenassets.constants.IStringResourceConstantConvert.Companion.ID_STRING_CAMERA_CONNECT_RESPONSE_NG
10 import jp.osdn.gokigen.gokigenassets.constants.IStringResourceConstantConvert.Companion.ID_STRING_CAMERA_NOT_FOUND
11 import jp.osdn.gokigen.gokigenassets.constants.IStringResourceConstantConvert.Companion.ID_STRING_CONNECT_CONNECTED
12 import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleHttpClient
13 import org.json.JSONObject
15 class ThetaCameraConnectSequence(private val context: AppCompatActivity, private val cameraStatusReceiver: ICameraStatusReceiver, private val sessionIdNotifier: IThetaSessionIdNotifier, private val cameraConnection : ICameraConnection, private val executeUrl : String = "http://192.168.1.1") : Runnable
17 private var useThetaV21: Boolean = false
18 private val httpClient = SimpleHttpClient()
23 useThetaV21 = decideApiLevel()
26 Log.v(TAG, "Theta API v2.1 : $useThetaV21")
29 // API Level V2.1を使用して通信する
34 // API Level V2 を使用して通信する
47 private fun decideApiLevel(): Boolean
49 var apiLevelIsV21 = false
52 val oscInfoUrl = "$executeUrl/osc/info"
54 val response: String = httpClient.httpGet(oscInfoUrl, timeoutMs)
55 Log.v(TAG, " $oscInfoUrl $response")
56 if (response.isNotEmpty())
58 val apiLevelArray = JSONObject(response).getJSONArray("apiLevel")
59 val size = apiLevelArray.length()
60 for (index in 0 until size)
62 val api = apiLevelArray.getInt(index)
63 if (api == 1) //if (api == 1 && useThetaV21)
68 if (api == 2) //if (api == 2 && useThetaV21)
80 return (apiLevelIsV21)
86 private fun connectApiV2()
88 val commandsExecuteUrl = "$executeUrl/osc/commands/execute"
89 val startSessionData = "{\"name\":\"camera.startSession\",\"parameters\":{\"timeout\":0}}"
90 val getStateUrl = "$executeUrl/osc/state"
94 val response: String? = httpClient.httpPost(commandsExecuteUrl, startSessionData, timeoutMs)
95 Log.v(TAG, " $commandsExecuteUrl $startSessionData $response")
96 val response2: String? = httpClient.httpPost(getStateUrl, "", timeoutMs)
97 Log.v(TAG, " $getStateUrl $response2")
98 if ((response2 != null) && (response2.isNotEmpty()))
102 val jsonObject = JSONObject(response2)
103 val sessionId = jsonObject.getJSONObject("state").getString("sessionId")
104 sessionIdNotifier.receivedSessionId(sessionId)
114 cameraConnection.alertConnectingFailed(context.getString(ID_STRING_CAMERA_CONNECT_RESPONSE_NG))
119 cameraConnection.alertConnectingFailed(e.localizedMessage)
123 private fun connectApiV21()
125 val commandsExecuteUrl = "$executeUrl/osc/commands/execute"
126 val startSessionData = "{\"name\":\"camera.startSession\",\"parameters\":{\"timeout\":0}}"
127 val getStateUrl = "$executeUrl/osc/state"
131 val responseS: String? = httpClient.httpPostWithHeader(
135 "application/json;charset=utf-8",
138 Log.v(TAG, " [ $commandsExecuteUrl ] $startSessionData ::: $responseS")
139 val response: String? =
140 httpClient.httpPostWithHeader(getStateUrl, "", null, null, timeoutMs)
141 Log.v(TAG, " ($getStateUrl) $response")
142 if ((response != null) && (response.isNotEmpty()))
145 var sessionId: String? = null
146 val jsonObject = JSONObject(response)
149 apiLevel = jsonObject.getJSONObject("state").getInt("_apiVersion")
159 sessionId = jsonObject.getJSONObject("state").getString("sessionId")
160 sessionIdNotifier.receivedSessionId(sessionId)
169 // API Levelを 1 から 2 に変える
170 val setApiLevelData = "{\"name\":\"camera.setOptions\",\"parameters\":{\"sessionId\" : \"$sessionId\", \"options\":{ \"clientVersion\":2}}}"
171 val response3: String? = httpClient.httpPostWithHeader(
175 "application/json;charset=utf-8",
178 Log.v(TAG, " $commandsExecuteUrl $setApiLevelData $response3")
184 cameraConnection.alertConnectingFailed(context.getString(ID_STRING_CAMERA_NOT_FOUND))
190 cameraConnection.alertConnectingFailed(e.localizedMessage)
194 private fun onConnectNotify()
198 val thread = Thread { // カメラとの接続確立を通知する
199 cameraStatusReceiver.onStatusNotify(context.getString(ID_STRING_CONNECT_CONNECTED))
200 cameraStatusReceiver.onCameraConnected()
201 Log.v(TAG, "onConnectNotify()")
202 cameraConnection.forceUpdateConnectionStatus(ICameraConnectionStatus.CameraConnectionStatus.CONNECTED)
214 private val TAG = ThetaCameraConnectSequence::class.java.simpleName