OSDN Git Service

ソースコード整理と微修正。
[gokigen/A01c.git] / wear / src / main / java / jp / sfjp / gokigen / a01c / thetacamerawrapper / operation / ThetaBracketingControl.kt
1 package jp.sfjp.gokigen.a01c.thetacamerawrapper.operation
2
3 import android.content.Context
4 import android.graphics.Color
5 import android.util.Log
6 import jp.sfjp.gokigen.a01c.ICameraController
7 import jp.sfjp.gokigen.a01c.IShowInformation
8 import jp.sfjp.gokigen.a01c.R
9 import jp.sfjp.gokigen.a01c.thetacamerawrapper.IThetaSessionIdProvider
10 import jp.sfjp.gokigen.a01c.utils.SimpleHttpClient
11
12 class ThetaBracketingControl(val context: Context, private val sessionIdProvider: IThetaSessionIdProvider, private val statusDrawer: IShowInformation, private val liveViewControl: ICameraController)
13 {
14     private val httpClient = SimpleHttpClient()
15     private var isCapturing = false
16
17     fun bracketingControl(useOSCv2 : Boolean)
18     {
19         try
20         {
21             if (!(isCapturing))
22             {
23                 startCapture(useOSCv2)
24                 statusDrawer.setMessage(IShowInformation.AREA_9, Color.YELLOW, context.getString(R.string.capturing))
25             }
26             else
27             {
28                 stopCapture(useOSCv2)
29                 statusDrawer.setMessage(IShowInformation.AREA_9, Color.WHITE, "")
30             }
31             statusDrawer.invalidate()
32             isCapturing = !isCapturing
33         }
34         catch (e: Exception)
35         {
36             e.printStackTrace()
37         }
38     }
39
40     private fun startCapture(useOSCv2 : Boolean)
41     {
42         Log.v(TAG, "startCapture() (API v2.1 : $useOSCv2)")
43         try
44         {
45             val thread = Thread {
46                 try
47                 {
48                     val shootUrl = "http://192.168.1.1/osc/commands/execute"
49 //                    val postData = if (useOSCv2) "{\"name\":\"camera.startCapture\",\"parameters\":{\"timeout\":0, \"mode\":\"bracket\"}}" else "{\"name\":\"camera._startCapture\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"mode\":\"bracket\"}}"
50 //                    val postData = if (useOSCv2) "{\"name\":\"camera.startCapture\",\"parameters\":{\"timeout\":0, \"mode\":\"composite\"}}" else "{\"name\":\"camera._startCapture\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"mode\":\"composite\"}}"
51                     val postData = if (useOSCv2) "{\"name\":\"camera.startCapture\",\"parameters\":{\"timeout\":0, \"mode\":\"interval\"}}" else "{\"name\":\"camera._startCapture\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\", \"mode\":\"interval\"}}"
52                     Log.v(TAG, " start Capture : $postData")
53
54                     val result: String? = httpClient.httpPostWithHeader(shootUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
55                     if ((result != null)&&(result.isNotEmpty()))
56                     {
57                         Log.v(TAG, " startCapture() : $result")
58 /*
59                         if (!useOSCv2)
60                         {
61                             liveViewControl.stopLiveView()
62                             waitMs(300) // ちょっと待つ...
63                             liveViewControl.startLiveView()
64                         }
65 */
66                     }
67                     else
68                     {
69                         Log.v(TAG, "startCapture() reply is null.  $postData")
70                     }
71                 }
72                 catch (e: Exception)
73                 {
74                     e.printStackTrace()
75                 }
76             }
77             thread.start()
78         }
79         catch (e: Exception)
80         {
81             e.printStackTrace()
82         }
83     }
84
85     private fun stopCapture(useOSCv2 : Boolean)
86     {
87         Log.v(TAG, "stopCapture() (API v2.1 : $useOSCv2)")
88         try
89         {
90             val thread = Thread {
91                 try
92                 {
93                     val shootUrl = "http://192.168.1.1/osc/commands/execute"
94                     val postData = if (useOSCv2) "{\"name\":\"camera.stopCapture\",\"parameters\":{\"timeout\":0}}" else "{\"name\":\"camera._stopCapture\",\"parameters\":{\"sessionId\": \"" + sessionIdProvider.sessionId + "\"}}"
95
96                     Log.v(TAG, " stop Capture : $postData")
97
98                     val result: String? = httpClient.httpPostWithHeader(shootUrl, postData, null, "application/json;charset=utf-8", timeoutMs)
99                     if ((result != null)&&(result.isNotEmpty()))
100                     {
101                         Log.v(TAG, " stopCapture() : $result")
102
103                         if (!useOSCv2)
104                         {
105                             // THETA V / THETA Z1 は、videoモードでライブビューができるので...
106                             liveViewControl.stopLiveView()
107                             waitMs() // ちょっと待つ...
108                             liveViewControl.startLiveView()
109                         }
110                     }
111                     else
112                     {
113                         Log.v(TAG, "stopCapture() reply is null.  $postData")
114                     }
115                 }
116                 catch (e: Exception)
117                 {
118                     e.printStackTrace()
119                 }
120             }
121             thread.start()
122         }
123         catch (e: Exception)
124         {
125             e.printStackTrace()
126         }
127     }
128
129     /**
130      *
131      *
132      */
133     private fun waitMs(waitMs: Int = 300)
134     {
135         try
136         {
137             Thread.sleep(waitMs.toLong())
138         }
139         catch (e: Exception)
140         {
141             e.printStackTrace()
142         }
143     }
144
145     companion object
146     {
147         private val TAG = ThetaBracketingControl::class.java.simpleName
148         private const val timeoutMs = 6000
149     }
150 }