OSDN Git Service

次バージョンの準備。
[gokigen/mangle.git] / app / src / main / java / jp / osdn / gokigen / gokigenassets / camera / vendor / ricohpentax / connection / RicohGr2CameraConnectSequence.kt
1 package jp.osdn.gokigen.gokigenassets.camera.vendor.ricohpentax.connection
2
3 import android.app.Activity
4 import android.util.Log
5 import androidx.preference.PreferenceManager
6 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraConnection
7 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraConnectionStatus
8 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatusReceiver
9 import jp.osdn.gokigen.gokigenassets.constants.IApplicationConstantConvert.Companion.ID_PREFERENCE_RICOH_GR2_LCD_SLEEP
10 import jp.osdn.gokigen.gokigenassets.constants.IApplicationConstantConvert.Companion.ID_PREFERENCE_USE_GR2_SPECIAL_COMMAND
11 import jp.osdn.gokigen.gokigenassets.constants.ICameraConstantConvert.Companion.ID_STRING_CAMERA_NOT_FOUND
12 import jp.osdn.gokigen.gokigenassets.constants.ICameraConstantConvert.Companion.ID_STRING_CONNECT_CONNECTED
13 import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleHttpClient
14
15 class RicohGr2CameraConnectSequence(private val context: Activity, private val cameraStatusReceiver: ICameraStatusReceiver, private val cameraConnection: ICameraConnection, private val gr2CommandNotify: IUseGR2CommandNotify, private val executeUrl : String = "http://192.168.0.1") : Runnable
16 {
17
18     companion object
19     {
20         private val TAG = RicohGr2CameraConnectSequence::class.java.simpleName
21         private const val TIMEOUT_MS = 5000
22     }
23     private val httpClient = SimpleHttpClient()
24
25     override fun run() {
26         val areYouThereUrl = "$executeUrl/v1/ping"
27         val grCommandUrl = "$executeUrl/_gr"
28         try {
29             val response: String = httpClient.httpGet(areYouThereUrl, TIMEOUT_MS)
30             Log.v(TAG, "$areYouThereUrl $response")
31             if (response.isNotEmpty())
32             {
33                 val preferences = PreferenceManager.getDefaultSharedPreferences(context)
34
35                 // 接続時、レンズロックOFF + GR2 コマンド有効/無効の確認
36                 run {
37                     val postData = "cmd=acclock off"
38                     val response0: String? = httpClient.httpPost(grCommandUrl, postData, TIMEOUT_MS)
39                     Log.v(TAG, "$grCommandUrl $response0")
40
41
42                     // GR2 専用コマンドを受け付けられるかどうかで、Preference を書き換える
43                     val enableGr2Command = !response0.isNullOrBlank()
44                     try
45                     {
46                         val editor = preferences.edit()
47                         editor.putBoolean(ID_PREFERENCE_USE_GR2_SPECIAL_COMMAND, enableGr2Command)
48                         editor.apply()
49                     }
50                     catch (e: Exception)
51                     {
52                         e.printStackTrace()
53                     }
54                     val gr2LcdSleep = preferences.getBoolean(ID_PREFERENCE_RICOH_GR2_LCD_SLEEP, false)
55                     gr2CommandNotify.setUseGR2Command(enableGr2Command, gr2LcdSleep)
56
57                     // 接続時、カメラの画面を消す
58                     if ((enableGr2Command)&&(gr2LcdSleep))
59                     {
60                         val postData0 = "cmd=lcd sleep on"
61                         val response1: String? = httpClient.httpPost(grCommandUrl, postData0, TIMEOUT_MS)
62                         Log.v(TAG, "$grCommandUrl $response1")
63                     }
64                 }
65                 onConnectNotify()
66             } else {
67                 onConnectError(context.getString(ID_STRING_CAMERA_NOT_FOUND))
68             }
69         }
70         catch (e: Exception)
71         {
72             e.printStackTrace()
73             onConnectError(e.localizedMessage)
74         }
75     }
76
77     private fun onConnectNotify()
78     {
79         try
80         {
81             val thread = Thread { // カメラとの接続確立を通知する
82                 cameraStatusReceiver.onStatusNotify(context.getString(ID_STRING_CONNECT_CONNECTED))
83                 cameraStatusReceiver.onCameraConnected()
84                 Log.v(TAG, "onConnectNotify()")
85                 cameraConnection.forceUpdateConnectionStatus(ICameraConnectionStatus.CameraConnectionStatus.CONNECTED)
86             }
87             thread.start()
88         }
89         catch (e: Exception)
90         {
91             e.printStackTrace()
92         }
93     }
94
95     private fun onConnectError(reason: String?)
96     {
97         cameraConnection.alertConnectingFailed(reason)
98     }
99 }