OSDN Git Service

シャッターや絞り、露出補正を変更できるようにした。
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / fuji_x / wrapper / FujiXStatusChecker.java
1 package net.osdn.gokigen.gr2control.camera.fuji_x.wrapper;
2
3 import android.content.SharedPreferences;
4 import android.util.Log;
5
6 import androidx.annotation.NonNull;
7 import androidx.fragment.app.FragmentActivity;
8 import androidx.preference.PreferenceManager;
9
10 import net.osdn.gokigen.gr2control.camera.ICameraStatus;
11 import net.osdn.gokigen.gr2control.camera.ICameraStatusWatcher;
12 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandCallback;
13 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.IFujiXCommandPublisher;
14 import net.osdn.gokigen.gr2control.camera.fuji_x.wrapper.command.messages.StatusRequestMessage;
15 import net.osdn.gokigen.gr2control.liveview.ICameraStatusUpdateNotify;
16 import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 public class FujiXStatusChecker implements ICameraStatusWatcher, ICameraStatus, IFujiXCommandCallback
22 {
23     private final String TAG = toString();
24     private static final int STATUS_MESSAGE_HEADER_SIZE = 14;
25     private int sleepMs;
26     private final IFujiXCommandPublisher issuer;
27     private ICameraStatusUpdateNotify notifier = null;
28     private FujiXStatusHolder statusHolder;
29     private boolean whileFetching = false;
30     private boolean logcat = false;
31
32
33     FujiXStatusChecker(@NonNull FragmentActivity activity, @NonNull IFujiXCommandPublisher issuer)
34     {
35         this.issuer = issuer;
36         this.statusHolder = new FujiXStatusHolder(activity, issuer);
37         try
38         {
39             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
40             String pollingWait = preferences.getString(IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT, IPreferencePropertyAccessor.FUJI_X_COMMAND_POLLING_WAIT_DEFAULT_VALUE);
41             this.sleepMs = Integer.parseInt(pollingWait);
42         }
43         catch (Exception e)
44         {
45             e.printStackTrace();
46             this.sleepMs = 400;
47         }
48         Log.v(TAG, "POLLING WAIT : " + sleepMs);
49     }
50
51     @Override
52     public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
53     {
54         Log.v(TAG, " " + currentBytes + "/" + totalBytes);
55     }
56
57     @Override
58     public boolean isReceiveMulti()
59     {
60         return (false);
61     }
62
63     @Override
64     public void receivedMessage(int id, byte[] data)
65     {
66         try
67         {
68             if (logcat)
69             {
70                 Log.v(TAG, " receivedMessage : " + id + ", length: " + data.length);
71             }
72             if (data.length < STATUS_MESSAGE_HEADER_SIZE)
73             {
74                 if (logcat)
75                 {
76                     Log.v(TAG, "received status length is short. (" + data.length + " bytes.)");
77                 }
78                 return;
79             }
80             int nofStatus = (data[13] * 256) + data[12];
81             int statusCount = 0;
82             int index = STATUS_MESSAGE_HEADER_SIZE;
83             while ((statusCount < nofStatus)&&(index < data.length))
84             {
85                 int dataId = ((((int)data[index + 1]) & 0xff) * 256) + (((int) data[index]) & 0xff);
86                 statusHolder.updateValue(notifier, dataId, data[index + 2], data[index + 3], data[index +4], data[index + 5]);
87                 index = index + 6;
88                 statusCount++;
89             }
90         }
91         catch (Exception e)
92         {
93             e.printStackTrace();
94         }
95     }
96
97     @Override
98     public @NonNull List<String> getStatusList(@NonNull String key)
99     {
100         try
101         {
102             if (statusHolder == null)
103             {
104                 return (new ArrayList<>());
105             }
106             return (statusHolder.getAvailableItemList(key));
107         }
108         catch (Exception e)
109         {
110             e.printStackTrace();
111         }
112         return (new ArrayList<>());
113     }
114
115     @Override
116     public String getStatus(@NonNull String key)
117     {
118         try
119         {
120             if (statusHolder == null)
121             {
122                 return ("");
123             }
124             return (statusHolder.getItemStatus(key));
125         }
126         catch (Exception e)
127         {
128             e.printStackTrace();
129         }
130         return ("");
131     }
132
133     @Override
134     public void setStatus(@NonNull final String key, @NonNull final String value)
135     {
136         try
137         {
138             Thread thread = new Thread(new Runnable() {
139                 @Override
140                 public void run() {
141                     if (statusHolder != null)
142                     {
143                         statusHolder.setItemStatus(key, value);
144                     }
145                 }
146             });
147             thread.start();
148         }
149         catch (Exception e)
150         {
151             e.printStackTrace();
152         }
153     }
154
155     @Override
156     public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
157     {
158         if (whileFetching)
159         {
160             Log.v(TAG, " startStatusWatch() already starting.");
161             return;
162         }
163         try
164         {
165             final IFujiXCommandCallback callback = this;
166             this.notifier = notifier;
167             whileFetching = true;
168             Thread thread = new Thread(new Runnable()
169             {
170                 @Override
171                 public void run()
172                 {
173                     Log.v(TAG, " Start status watch. : " + sleepMs + "ms");
174                     while (whileFetching)
175                     {
176                         try
177                         {
178                             issuer.enqueueCommand(new StatusRequestMessage(callback));
179                             Thread.sleep(sleepMs);
180                         }
181                         catch (Exception e)
182                         {
183                             e.printStackTrace();
184                         }
185                     }
186                     Log.v(TAG, "STATUS WATCH STOPPED.");
187                 }
188             });
189             thread.start();
190         }
191         catch (Exception e)
192         {
193             e.printStackTrace();
194         }
195     }
196
197     @Override
198     public void stopStatusWatch()
199     {
200         Log.v(TAG, "stoptStatusWatch()");
201         whileFetching = false;
202         this.notifier = null;
203     }
204
205     ICameraStatusUpdateNotify getStatusListener()
206     {
207         return (this.notifier);
208     }
209
210 }