OSDN Git Service

f1a8889d502ea83db44ea14b036bd5a00c7be6ad
[gokigen/FujiCam.git] / app / src / main / java / net / osdn / gokigen / cameratest / fuji / statuses / FujiStatusChecker.java
1 package net.osdn.gokigen.cameratest.fuji.statuses;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import net.osdn.gokigen.cameratest.fuji.ReceivedDataHolder;
8
9 public class FujiStatusChecker implements IFujiStatusReceiver
10 {
11     private final String TAG = toString();
12     private final IFujiStatusRequest comm;
13     private final IFujiStatusNotify notify;
14     private final FujiStatusHolder statusHolder;
15     private static final int  ERROR_LIMIT = 10;
16     private boolean threadIsRunning = false;
17     private final int WAIT_MS = 400;
18
19     public FujiStatusChecker(@NonNull IFujiStatusRequest comm, @NonNull IFujiStatusNotify notify)
20     {
21         this.comm = comm;
22         this.notify = notify;
23         this.statusHolder = new FujiStatusHolder();
24     }
25
26     @Override
27     public void statusReceived(ReceivedDataHolder data)
28     {
29         statusReceivedImpl(data.getData());
30     }
31
32     @Override
33     public void start()
34     {
35         Thread thread = new Thread(new Runnable() {
36             @Override
37             public void run()
38             {
39
40                 int errorCount = 0;
41                 threadIsRunning = true;
42                 while (threadIsRunning)
43                 {
44                     try
45                     {
46                         comm.requestStatus();
47                         Thread.sleep(WAIT_MS);
48                         errorCount = 0;
49                     }
50                     catch (Exception e)
51                     {
52                         e.printStackTrace();
53                         errorCount++;
54                     }
55                     if (errorCount > ERROR_LIMIT)
56                     {
57                         threadIsRunning = false;
58                     }
59                 }
60                 Log.v(TAG, "--- FINISH STATUS WATCH ---");
61             }
62         });
63         try
64         {
65             if (!threadIsRunning)
66             {
67                 Log.v(TAG, "--- START STATUS WATCH ---");
68                 thread.start();
69             }
70         }
71         catch (Exception e)
72         {
73             e.printStackTrace();
74         }
75     }
76
77     @Override
78     public void stop()
79     {
80         threadIsRunning = false;
81     }
82
83
84
85     private void statusReceivedImpl(byte[] data)
86     {
87         boolean isStatusUpdated = false;
88         try
89         {
90             if (data.length < 14)
91             {
92                 Log.v(TAG, "received status length is short. (" + data.length + " bytes.)");
93                 return;
94             }
95
96             int nofStatus = (data[13] * 256) + data[12];
97             //Log.v(TAG, "status Received. " + data.length + " bytes. [status : " + nofStatus + "]");
98
99             int statusCount = 0;
100             int index = 14;
101             while ((statusCount < nofStatus)&&(index < data.length))
102             {
103                 int dataId = ((((int)data[index + 1]) & 0xff) * 256) + (((int) data[index]) & 0xff);
104                 statusHolder.updateValue(dataId, data[index + 2], data[index + 3], data[index +4], data[index + 5]);
105                 index = index + 6;
106                 statusCount++;
107                 isStatusUpdated = true;
108             }
109             if (isStatusUpdated)
110             {
111                 notify.statusUpdated(statusHolder);
112             }
113         }
114         catch (Exception e)
115         {
116             e.printStackTrace();
117         }
118     }
119 }