OSDN Git Service

カメラプロパティの値を作りこみ。
[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.support.annotation.NonNull;
4 import android.util.Log;
5
6 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
7 import net.osdn.gokigen.gr2control.camera.ICameraStatusWatcher;
8 import net.osdn.gokigen.gr2control.camera.utils.SimpleHttpClient;
9 import net.osdn.gokigen.gr2control.liveview.ICameraStatusUpdateNotify;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  *
16  *
17  */
18 public class RicohGr2StatusChecker implements ICameraStatusWatcher, ICameraStatus
19 {
20     private final String TAG = toString();
21     private final String statusCheckUrl = "http://192.168.0.1/v1/props";
22     private final String statusSetUrl = "http://192.168.0.1/v1/params/camera";
23     private final String grCommandUrl = "http://192.168.0.1/_gr";
24     private final int sleepMs;
25
26     private int timeoutMs = 5000;
27     private boolean whileFetching = false;
28     private RicohGr2StatusHolder statusHolder;
29
30     /**
31      *
32      *
33      */
34     RicohGr2StatusChecker(int sleepMs)
35     {
36         this.sleepMs = sleepMs;
37     }
38
39     /**
40      *
41      *
42      */
43     @Override
44     public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
45     {
46         Log.v(TAG, "startStatusWatch()");
47         try
48         {
49             this.statusHolder = new RicohGr2StatusHolder(notifier);
50             Thread thread = new Thread(new Runnable()
51             {
52                 @Override
53                 public void run()
54                 {
55                     try
56                     {
57                         start(statusCheckUrl);
58                     }
59                     catch (Exception e)
60                     {
61                         e.printStackTrace();
62                     }
63                 }
64             });
65             thread.start();
66         }
67         catch (Exception e)
68         {
69             e.printStackTrace();
70         }
71     }
72
73     /**
74      *
75      *
76      */
77     @Override
78     public void stoptStatusWatch()
79     {
80         Log.v(TAG, "stoptStatusWatch()");
81         whileFetching = false;
82     }
83
84     /**
85      *
86      *
87      */
88     private void start(@NonNull final String watchUrl)
89     {
90         if (whileFetching)
91         {
92             Log.v(TAG, "start() already starting.");
93             return;
94         }
95
96         try
97         {
98             whileFetching = true;
99             Thread thread = new Thread(new Runnable()
100             {
101                 @Override
102                 public void run()
103                 {
104                     Log.d(TAG, "Start status watch.");
105                     while (whileFetching)
106                     {
107                         try
108                         {
109                             statusHolder.updateStatus(SimpleHttpClient.httpGet(watchUrl, timeoutMs));
110                             Thread.sleep(sleepMs);
111                         }
112                         catch (Exception e)
113                         {
114                             e.printStackTrace();
115                         }
116                     }
117                     Log.v(TAG, "STATUS WATCH STOPPED.");
118                 }
119             });
120             thread.start();
121         }
122         catch (Exception e)
123         {
124             e.printStackTrace();
125         }
126     }
127
128     @Override
129     public @NonNull List<String> getStatusList(@NonNull final String key)
130     {
131         try
132         {
133             if (statusHolder == null)
134             {
135                 return (new ArrayList<>());
136             }
137             String listKey = key + "List";
138             return (statusHolder.getAvailableItemList(listKey));
139         }
140         catch (Exception e)
141         {
142             e.printStackTrace();
143         }
144         return (new ArrayList<>());
145     }
146
147     @Override
148     public String getStatus(@NonNull String key)
149     {
150         try
151         {
152             if (statusHolder == null)
153             {
154                 return ("");
155             }
156             return (statusHolder.getItemStatus(key));
157         }
158         catch (Exception e)
159         {
160             e.printStackTrace();
161         }
162         return ("");
163     }
164
165     @Override
166     public void setStatus(@NonNull final String key, @NonNull final String value)
167     {
168         Thread thread = new Thread(new Runnable() {
169             @Override
170             public void run() {
171                 try
172                 {
173                     String postData = key + "=" + value;
174                     String response = SimpleHttpClient.httpPut(statusSetUrl, postData, timeoutMs);
175                     Log.v(TAG, "SET PROPERTY : " + postData + " resp. (" + response.length() + "bytes.)");
176
177                     //  GR専用コマンドで、画面表示をリフレッシュ
178                     response = SimpleHttpClient.httpPost(grCommandUrl, "cmd=mode refresh", timeoutMs);
179                     Log.v(TAG, "refresh resp. (" + response.length() + "bytes.)");
180                 }
181                 catch (Exception e)
182                 {
183                     e.printStackTrace();
184                 }
185             }
186         });
187         try
188         {
189             thread.start();
190         }
191         catch (Exception e)
192         {
193             e.printStackTrace();
194         }
195     }
196 }