1 package net.osdn.gokigen.gr2control.camera.fuji_x.wrapper;
3 import android.content.SharedPreferences;
4 import android.util.Log;
6 import androidx.annotation.NonNull;
7 import androidx.fragment.app.FragmentActivity;
8 import androidx.preference.PreferenceManager;
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;
18 import java.util.ArrayList;
19 import java.util.List;
21 public class FujiXStatusChecker implements ICameraStatusWatcher, ICameraStatus, IFujiXCommandCallback
23 private final String TAG = toString();
24 private static final int STATUS_MESSAGE_HEADER_SIZE = 14;
26 private final IFujiXCommandPublisher issuer;
27 private ICameraStatusUpdateNotify notifier = null;
28 private FujiXStatusHolder statusHolder;
29 private boolean whileFetching = false;
30 private boolean logcat = false;
33 FujiXStatusChecker(@NonNull FragmentActivity activity, @NonNull IFujiXCommandPublisher issuer)
36 this.statusHolder = new FujiXStatusHolder(activity, issuer);
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);
48 Log.v(TAG, "POLLING WAIT : " + sleepMs);
52 public void onReceiveProgress(int currentBytes, int totalBytes, byte[] body)
54 Log.v(TAG, " " + currentBytes + "/" + totalBytes);
58 public boolean isReceiveMulti()
64 public void receivedMessage(int id, byte[] data)
70 Log.v(TAG, " receivedMessage : " + id + ", length: " + data.length);
72 if (data.length < STATUS_MESSAGE_HEADER_SIZE)
76 Log.v(TAG, "received status length is short. (" + data.length + " bytes.)");
80 int nofStatus = (data[13] * 256) + data[12];
82 int index = STATUS_MESSAGE_HEADER_SIZE;
83 while ((statusCount < nofStatus)&&(index < data.length))
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]);
98 public @NonNull List<String> getStatusList(@NonNull String key)
102 if (statusHolder == null)
104 return (new ArrayList<>());
106 return (statusHolder.getAvailableItemList(key));
112 return (new ArrayList<>());
116 public String getStatus(@NonNull String key)
120 if (statusHolder == null)
124 return (statusHolder.getItemStatus(key));
134 public void setStatus(@NonNull final String key, @NonNull final String value)
138 Thread thread = new Thread(new Runnable() {
141 if (statusHolder != null)
143 statusHolder.setItemStatus(key, value);
156 public void startStatusWatch(@NonNull ICameraStatusUpdateNotify notifier)
160 Log.v(TAG, " startStatusWatch() already starting.");
165 final IFujiXCommandCallback callback = this;
166 this.notifier = notifier;
167 whileFetching = true;
168 Thread thread = new Thread(new Runnable()
173 Log.v(TAG, " Start status watch. : " + sleepMs + "ms");
174 while (whileFetching)
178 issuer.enqueueCommand(new StatusRequestMessage(callback));
179 Thread.sleep(sleepMs);
186 Log.v(TAG, "STATUS WATCH STOPPED.");
198 public void stopStatusWatch()
200 Log.v(TAG, "stoptStatusWatch()");
201 whileFetching = false;
202 this.notifier = null;
205 ICameraStatusUpdateNotify getStatusListener()
207 return (this.notifier);