OSDN Git Service

GR2専用コマンドを、GR2と接続したときにだけ有効にするように仕様を変更した。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / ricohgr2 / wrapper / RicohGr2StatusChecker.java
1 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper;
2
3 import android.util.Log;
4
5 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
6 import net.osdn.gokigen.gr2control.camera.ICameraStatusWatcher;
7 import net.osdn.gokigen.gr2control.camera.utils.SimpleHttpClient;
8 import net.osdn.gokigen.gr2control.liveview.ICameraStatusUpdateNotify;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import androidx.annotation.NonNull;
14
15 /**
16  *
17  *
18  */
19 public class RicohGr2StatusChecker implements ICameraStatusWatcher, ICameraStatus
20 {
21     private final String TAG = toString();
22     private final String statusCheckUrl = "http://192.168.0.1/v1/props";
23     private final String statusSetUrl = "http://192.168.0.1/v1/params/camera";
24     private final String grCommandUrl = "http://192.168.0.1/_gr";
25     private final int sleepMs;
26
27     private int timeoutMs = 5000;
28     private boolean whileFetching = false;
29     private RicohGr2StatusHolder statusHolder;
30     private boolean useGR2command = false;
31
32     /**
33      *
34      *
35      */
36     RicohGr2StatusChecker(int sleepMs)
37     {
38         this.sleepMs = sleepMs;
39     }
40
41     public void setUseGR2Command(boolean useGR2command)
42     {
43         this.useGR2command = useGR2command;
44     }
45
46     /**
47      *
48      *
49      */
50     @Override
51     public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
52     {
53         Log.v(TAG, "startStatusWatch()");
54         try
55         {
56             this.statusHolder = new RicohGr2StatusHolder(notifier);
57             Thread thread = new Thread(new Runnable()
58             {
59                 @Override
60                 public void run()
61                 {
62                     try
63                     {
64                         start(statusCheckUrl);
65                     }
66                     catch (Exception e)
67                     {
68                         e.printStackTrace();
69                     }
70                 }
71             });
72             thread.start();
73         }
74         catch (Exception e)
75         {
76             e.printStackTrace();
77         }
78     }
79
80     /**
81      *
82      *
83      */
84     @Override
85     public void stopStatusWatch()
86     {
87         Log.v(TAG, "stoptStatusWatch()");
88         whileFetching = false;
89     }
90
91     /**
92      *
93      *
94      */
95     private void start(@NonNull final String watchUrl)
96     {
97         if (whileFetching)
98         {
99             Log.v(TAG, "start() already starting.");
100             return;
101         }
102
103         try
104         {
105             whileFetching = true;
106             Thread thread = new Thread(new Runnable()
107             {
108                 @Override
109                 public void run()
110                 {
111                     Log.d(TAG, "Start status watch.");
112                     while (whileFetching)
113                     {
114                         try
115                         {
116                             statusHolder.updateStatus(SimpleHttpClient.httpGet(watchUrl, timeoutMs));
117                             Thread.sleep(sleepMs);
118                         }
119                         catch (Exception e)
120                         {
121                             e.printStackTrace();
122                         }
123                     }
124                     Log.v(TAG, "STATUS WATCH STOPPED.");
125                 }
126             });
127             thread.start();
128         }
129         catch (Exception e)
130         {
131             e.printStackTrace();
132         }
133     }
134
135     @Override
136     public @NonNull List<String> getStatusList(@NonNull final String key)
137     {
138         try
139         {
140             if (statusHolder == null)
141             {
142                 return (new ArrayList<>());
143             }
144             String listKey = key + "List";
145             return (statusHolder.getAvailableItemList(listKey));
146         }
147         catch (Exception e)
148         {
149             e.printStackTrace();
150         }
151         return (new ArrayList<>());
152     }
153
154     @Override
155     public String getStatus(@NonNull String key)
156     {
157         try
158         {
159             if (statusHolder == null)
160             {
161                 return ("");
162             }
163             return (statusHolder.getItemStatus(key));
164         }
165         catch (Exception e)
166         {
167             e.printStackTrace();
168         }
169         return ("");
170     }
171
172     @Override
173     public void setStatus(@NonNull final String key, @NonNull final String value)
174     {
175         Thread thread = new Thread(new Runnable() {
176             @Override
177             public void run() {
178                 try
179                 {
180                     String response;
181                     String postData = key + "=" + value;
182                     if ((useGR2command)&&(key.equals("exposureMode")))
183                     {
184                         //  撮影モードを変更するときは、GR専用コマンドを送ることにする。
185                         postData = "cmd=" + decideButtonCode(value);
186                         response = SimpleHttpClient.httpPost(grCommandUrl, postData, timeoutMs);
187                         Log.v(TAG, "CHANGE MODE : " + postData + " resp. (" + response.length() + "bytes.)");
188                     } else {
189                         // 通常の変更コマンド
190                         response = SimpleHttpClient.httpPut(statusSetUrl, postData, timeoutMs);
191                         Log.v(TAG, "SET PROPERTY : " + postData + " resp. (" + response.length() + "bytes.)");
192                     }
193                     if (useGR2command)
194                     {
195                         //  GR専用コマンドで、画面表示をリフレッシュ
196                         response = SimpleHttpClient.httpPost(grCommandUrl, "cmd=mode refresh", timeoutMs);
197                         Log.v(TAG, "refresh resp. (" + response.length() + "bytes.)");
198                     }
199                 }
200                 catch (Exception e)
201                 {
202                     e.printStackTrace();
203                 }
204             }
205         });
206         try
207         {
208             thread.start();
209         }
210         catch (Exception e)
211         {
212             e.printStackTrace();
213         }
214     }
215
216     /**
217      *   撮影モードをGRのダイアルコマンドに変更する
218      *
219      */
220     private String decideButtonCode(String exposureMode)
221     {
222         String buttonCode = "bdial AUTO";
223         if (exposureMode == null)
224         {
225             return (buttonCode);
226         }
227         switch (exposureMode)
228         {
229             case "movie":
230                 buttonCode = "bdial MOVIE";
231                 break;
232             case "M":
233                 buttonCode = "bdial M";
234                 break;
235             case "TAV":
236                 buttonCode = "bdial TAV";
237                 break;
238             case "AV":
239                 buttonCode = "bdial AV";
240                 break;
241             case "TV":
242                 buttonCode = "bdial TV";
243                 break;
244             case "P":
245                 buttonCode = "bdial P";
246                 break;
247             case "auto":
248                 buttonCode = "bdial AUTO";
249                 break;
250         }
251         return (buttonCode);
252     }
253 }