OSDN Git Service

dbdadf9a19e35ecb85f85a7bb3af3ed0ac400e60
[gokigen/mangle.git] / app / src / main / java / jp / osdn / gokigen / gokigenassets / camera / panasonic / status / CameraStatusHolder.kt
1 package jp.osdn.gokigen.gokigenassets.camera.panasonic.status
2
3 import android.content.Context
4 import android.util.Log
5 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICameraStatus
6 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICardSlotSelectionReceiver
7 import jp.osdn.gokigen.gokigenassets.camera.interfaces.ICardSlotSelector
8 import jp.osdn.gokigen.gokigenassets.camera.panasonic.ICameraChangeListener
9 import jp.osdn.gokigen.gokigenassets.camera.panasonic.IPanasonicCamera
10 import jp.osdn.gokigen.gokigenassets.utils.communication.SimpleHttpClient
11 import java.util.ArrayList
12
13 class CameraStatusHolder(private val context: Context, private val remote: IPanasonicCamera, private val cardSlotSelector: ICardSlotSelector) : ICardSlotSelectionReceiver, ICameraStatusHolder
14 {
15     private var listener: ICameraChangeListener? = null
16     private var currentSd = "sd1"
17     private var isInitialized = false
18     private var isDualSlot = false
19     private var remainBattery : Int = 0
20
21     fun parse(reply: String)
22     {
23         try
24         {
25             parseReceivedStatus(reply)
26             var isEnableDualSlot = false
27             if (reply.contains("<sd_memory>set</sd_memory>") && reply.contains("<sd2_memory>set</sd2_memory>")) {
28                 // カードが2枚刺さっている場合...
29                 isEnableDualSlot = true
30             }
31             if (!isInitialized || isDualSlot != isEnableDualSlot)
32             {
33                 // 初回だけの実行...
34                 if (isEnableDualSlot)
35                 {
36                     // カードが2枚刺さっている場合...
37                     cardSlotSelector.setupSlotSelector(true, this)
38                 }
39                 else
40                 {
41                     // カードが1つしか刺さっていない場合...
42                     cardSlotSelector.setupSlotSelector(false, null)
43                 }
44                 isInitialized = true
45                 isDualSlot = isEnableDualSlot
46             }
47             checkCurrentSlot(reply)
48         }
49         catch (e: Exception)
50         {
51             e.printStackTrace()
52         }
53     }
54
55     private fun checkCurrentSlot(reply: String)
56     {
57         try
58         {
59             val header = "<current_sd>"
60             val indexStart = reply.indexOf(header)
61             val indexEnd = reply.indexOf("</current_sd>")
62             if (indexStart > 0 && indexEnd > 0 && indexStart < indexEnd)
63             {
64                 val currentSlot = reply.substring(indexStart + header.length, indexEnd)
65                 if (currentSd != currentSlot)
66                 {
67                     currentSd = currentSlot
68                     cardSlotSelector.changedCardSlot(currentSd)
69                 }
70             }
71         }
72         catch (e: Exception)
73         {
74             e.printStackTrace()
75         }
76     }
77
78     fun setEventChangeListener(listener: ICameraChangeListener)
79     {
80         this.listener = listener
81     }
82
83     fun clearEventChangeListener()
84     {
85         listener = null
86     }
87
88     override fun getCameraStatus(): String? {
89         return null
90     }
91
92     override fun getLiveviewStatus(): Boolean {
93         return false
94     }
95
96     override fun getShootMode(): String?
97     {
98         return null
99     }
100
101     override fun getAvailableShootModes(): List<String?>? {
102         return null
103     }
104
105     override fun getZoomPosition(): Int {
106         return 0
107     }
108
109     override fun getStorageId(): String {
110         return currentSd
111     }
112
113     override fun slotSelected(slotId: String)
114     {
115         Log.v(TAG, " slotSelected : $slotId")
116         if (currentSd != slotId)
117         {
118             // スロットを変更したい!
119             requestToChangeSlot(slotId)
120         }
121     }
122     /**
123      *
124      *
125      */
126     fun getAvailableItemList(key: String): List<String>
127     {
128         val itemList: MutableList<String> = ArrayList()
129         try
130         {
131 /*
132             val array = latestResultObject!!.getJSONArray(key) ?: return itemList
133             val nofItems = array.length()
134             for (index in 0 until nofItems) {
135                 try {
136                     itemList.add(array.getString(index))
137                 } catch (e: Exception) {
138                     e.printStackTrace()
139                 }
140             }
141 */
142         }
143         catch (e: Exception)
144         {
145             e.printStackTrace()
146         }
147         return itemList
148     }
149
150     fun getItemStatus(key: String): String
151     {
152 /*
153         try
154         {
155             return latestResultObject!!.getString(key)
156         }
157         catch (e: Exception)
158         {
159             e.printStackTrace()
160         }
161 */
162         return ""
163     }
164
165     private fun requestToChangeSlot(slotId: String)
166     {
167         try
168         {
169             val thread = Thread {
170                 try
171                 {
172                     var loop = true
173                     val http = SimpleHttpClient()
174                     while (loop)
175                     {
176                         val reply: String = http.httpGet(remote.getCmdUrl() + "cam.cgi?mode=setsetting&type=current_sd&value=" + slotId, TIMEOUT_MS)
177                         if (reply.indexOf("<result>ok</result>") > 0)
178                         {
179                             loop = false
180                             cardSlotSelector.selectSlot(slotId)
181                         }
182                         else
183                         {
184                             Thread.sleep(1000) // 1秒待つ
185                         }
186                     }
187                 }
188                 catch (e: Exception)
189                 {
190                     e.printStackTrace()
191                 }
192             }
193             thread.start()
194         }
195         catch (e: Exception)
196         {
197             e.printStackTrace()
198         }
199     }
200
201     private fun parseReceivedStatus(eventData : String)
202     {
203         try
204         {
205             //Log.v(TAG, " parseReceivedStatus : $eventData")
206             parseBatteryInfo(eventData)
207         }
208         catch (e : Exception)
209         {
210             e.printStackTrace()
211         }
212     }
213
214     private fun parseBatteryInfo(eventData : String)
215     {
216         try
217         {
218             val header = "<batt>"
219             val indexStart = eventData.indexOf(header)
220             val indexEnd = eventData.indexOf("</batt>")
221             if (indexStart > 0 && indexEnd > 0 && indexStart < indexEnd)
222             {
223                 val batteryInfo = eventData.substring(indexStart + header.length, indexEnd)
224                 val indexMiddle = batteryInfo.indexOf("/")
225                 val numerator  = batteryInfo.substring(0, indexMiddle).toFloat()
226                 val denominator = batteryInfo.substring(indexMiddle + 1, batteryInfo.length).toFloat()
227                 remainBattery = ((numerator/denominator) * 100.0f).toInt()
228                 // Log.v(TAG, "  ======  BATTERY INFORMATION :  $numerator / $denominator  ($remainBattery%)  ====== ")
229             }
230         }
231         catch (e: Exception)
232         {
233             e.printStackTrace()
234         }
235     }
236
237     fun getCurrentStatus(key: String) : String
238     {
239         return (when (key) {
240             ICameraStatus.BATTERY -> "$remainBattery"
241             else -> ""
242         })
243     }
244
245     companion object
246     {
247         private val TAG = CameraStatusHolder::class.java.simpleName
248         private const val TIMEOUT_MS = 3000
249     }
250 }