@leonardojc/capacitor-ioboard 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,336 @@
1
+ package com.leonardojc.capacitor.ioboard;
2
+
3
+ import com.getcapacitor.JSArray;
4
+ import com.getcapacitor.JSObject;
5
+ import com.getcapacitor.Plugin;
6
+ import com.getcapacitor.PluginCall;
7
+ import com.getcapacitor.PluginMethod;
8
+ import com.getcapacitor.annotation.CapacitorPlugin;
9
+ import android.util.Log;
10
+ import java.util.Arrays;
11
+ import org.json.JSONObject;
12
+
13
+ @CapacitorPlugin(name = "CapacitorIoboard")
14
+ public class CapacitorIoboardPlugin extends Plugin {
15
+
16
+ private static final String TAG = "CapacitorIoboard";
17
+ private IOBoardManager ioboardManager;
18
+
19
+ @Override
20
+ public void load() {
21
+ super.load();
22
+ ioboardManager = new IOBoardManager();
23
+ }
24
+
25
+ // New simplified API methods
26
+ @PluginMethod
27
+ public void connect(PluginCall call) {
28
+ String portPath = call.getString("portPath", "/dev/ttyS2");
29
+ Integer baudRate = call.getInt("baudRate", 115200);
30
+ Integer dataBits = call.getInt("dataBits", 8);
31
+ Integer stopBits = call.getInt("stopBits", 1);
32
+ String parity = call.getString("parity", "none");
33
+ String flowControl = call.getString("flowControl", "none");
34
+ Integer timeout = call.getInt("timeout", 5000);
35
+
36
+ try {
37
+ IOBoardManager.SerialConfig config = new IOBoardManager.SerialConfig(
38
+ portPath, baudRate, dataBits, stopBits, parity, flowControl, timeout
39
+ );
40
+
41
+ IOBoardManager.IOBoardResponse response = ioboardManager.connect(config);
42
+
43
+ JSObject result = new JSObject();
44
+ result.put("success", response.success);
45
+ result.put("message", response.message);
46
+
47
+ call.resolve(result);
48
+ } catch (Exception e) {
49
+ Log.e(TAG, "Error connecting", e);
50
+ call.reject("Failed to connect: " + e.getMessage());
51
+ }
52
+ }
53
+
54
+ @PluginMethod
55
+ public void disconnect(PluginCall call) {
56
+ try {
57
+ IOBoardManager.IOBoardResponse response = ioboardManager.disconnect();
58
+
59
+ JSObject result = new JSObject();
60
+ result.put("success", response.success);
61
+ result.put("message", response.message);
62
+
63
+ call.resolve(result);
64
+ } catch (Exception e) {
65
+ Log.e(TAG, "Error disconnecting", e);
66
+ call.reject("Failed to disconnect: " + e.getMessage());
67
+ }
68
+ }
69
+
70
+ @PluginMethod
71
+ public void isConnected(PluginCall call) {
72
+ try {
73
+ boolean connected = ioboardManager.isConnected();
74
+ JSObject result = new JSObject();
75
+ result.put("connected", connected);
76
+ call.resolve(result);
77
+ } catch (Exception e) {
78
+ Log.e(TAG, "Error checking connection status", e);
79
+ call.reject("Failed to check connection status: " + e.getMessage());
80
+ }
81
+ }
82
+
83
+ @PluginMethod
84
+ public void getStatus(PluginCall call) {
85
+ Integer address = call.getInt("address");
86
+ Integer timeout = call.getInt("timeout", 5000);
87
+
88
+ if (address == null || address < 1 || address > 63) {
89
+ call.reject("Invalid address. Must be between 1 and 63");
90
+ return;
91
+ }
92
+
93
+ try {
94
+ IOBoardManager.StatusResponse response = ioboardManager.getStatus(address, timeout);
95
+
96
+ JSObject result = new JSObject();
97
+ result.put("success", response.success);
98
+ result.put("message", response.message);
99
+
100
+ if (response.success && response.data != null) {
101
+ JSObject data = new JSObject();
102
+ data.put("doorLockStatus", response.data.doorLockStatus);
103
+
104
+ JSObject version = new JSObject();
105
+ version.put("major", response.data.softwareVersion.major);
106
+ version.put("minor", response.data.softwareVersion.minor);
107
+ version.put("patch", response.data.softwareVersion.patch);
108
+ data.put("softwareVersion", version);
109
+
110
+ result.put("data", data);
111
+ }
112
+
113
+ call.resolve(result);
114
+ } catch (Exception e) {
115
+ Log.e(TAG, "Error getting status", e);
116
+ call.reject("Failed to get status: " + e.getMessage());
117
+ }
118
+ }
119
+
120
+ @PluginMethod
121
+ public void unlockPallet(PluginCall call) {
122
+ Integer address = call.getInt("address");
123
+ Integer palletNumber = call.getInt("palletNumber");
124
+ String ledColor = call.getString("ledColor", "green");
125
+ Integer timeout = call.getInt("timeout", 5000);
126
+
127
+ if (address == null || address < 1 || address > 63) {
128
+ call.reject("Invalid address. Must be between 1 and 63");
129
+ return;
130
+ }
131
+
132
+ if (palletNumber == null || palletNumber < 0 || palletNumber > 7) {
133
+ call.reject("Invalid pallet number. Must be between 0 and 7");
134
+ return;
135
+ }
136
+
137
+ try {
138
+ IOBoardManager.IOBoardResponse response = ioboardManager.unlockPallet(
139
+ address, palletNumber, ledColor, timeout
140
+ );
141
+
142
+ JSObject result = new JSObject();
143
+ result.put("success", response.success);
144
+ result.put("message", response.message);
145
+
146
+ if (response.success) {
147
+ JSObject data = new JSObject();
148
+ data.put("address", address);
149
+ data.put("palletNumber", palletNumber);
150
+ data.put("ledColor", ledColor);
151
+ result.put("data", data);
152
+ }
153
+
154
+ call.resolve(result);
155
+ } catch (Exception e) {
156
+ Log.e(TAG, "Error unlocking pallet", e);
157
+ call.reject("Failed to unlock pallet: " + e.getMessage());
158
+ }
159
+ }
160
+
161
+ @PluginMethod
162
+ public void controlMultiplePallets(PluginCall call) {
163
+ Integer address = call.getInt("address");
164
+ Integer doorLockMask = call.getInt("doorLockMask", 0);
165
+ String ledColor = call.getString("ledColor", "green");
166
+ Integer timeout = call.getInt("timeout", 5000);
167
+
168
+ if (address == null || address < 1 || address > 63) {
169
+ call.reject("Invalid address. Must be between 1 and 63");
170
+ return;
171
+ }
172
+
173
+ try {
174
+ IOBoardManager.IOBoardResponse response = ioboardManager.controlMultiplePallets(
175
+ address, doorLockMask, ledColor, timeout
176
+ );
177
+
178
+ JSObject result = new JSObject();
179
+ result.put("success", response.success);
180
+ result.put("message", response.message);
181
+
182
+ if (response.success) {
183
+ JSObject data = new JSObject();
184
+ data.put("address", address);
185
+ data.put("doorLockMask", doorLockMask);
186
+
187
+ // Calculate affected pallets from mask
188
+ JSArray affectedPallets = new JSArray();
189
+ for (int i = 0; i < 8; i++) {
190
+ if ((doorLockMask & (1 << i)) != 0) {
191
+ affectedPallets.put(i);
192
+ }
193
+ }
194
+ data.put("affectedPallets", affectedPallets);
195
+ result.put("data", data);
196
+ }
197
+
198
+ call.resolve(result);
199
+ } catch (Exception e) {
200
+ Log.e(TAG, "Error controlling multiple pallets", e);
201
+ call.reject("Failed to control multiple pallets: " + e.getMessage());
202
+ }
203
+ }
204
+
205
+ @PluginMethod
206
+ public void scanDevices(PluginCall call) {
207
+ Integer startAddress = call.getInt("startAddress", 1);
208
+ Integer endAddress = call.getInt("endAddress", 63);
209
+ Integer timeout = call.getInt("timeout", 1000);
210
+
211
+ if (startAddress < 1 || startAddress > 63) {
212
+ call.reject("Invalid start address. Must be between 1 and 63");
213
+ return;
214
+ }
215
+
216
+ if (endAddress < 1 || endAddress > 63 || endAddress < startAddress) {
217
+ call.reject("Invalid end address. Must be between 1 and 63 and >= start address");
218
+ return;
219
+ }
220
+
221
+ try {
222
+ IOBoardManager.ScanResponse response = ioboardManager.scanDevices(
223
+ startAddress, endAddress, timeout
224
+ );
225
+
226
+ JSObject result = new JSObject();
227
+ result.put("success", response.success);
228
+ result.put("message", response.message);
229
+
230
+ if (response.success && response.devices != null) {
231
+ JSArray devicesArray = new JSArray();
232
+ for (IOBoardManager.DeviceInfo device : response.devices) {
233
+ JSObject deviceObj = new JSObject();
234
+ deviceObj.put("address", device.address);
235
+ deviceObj.put("responding", device.responding);
236
+
237
+ if (device.status != null) {
238
+ JSObject statusObj = new JSObject();
239
+ statusObj.put("doorLockStatus", device.status.doorLockStatus);
240
+ deviceObj.put("status", statusObj);
241
+ }
242
+
243
+ devicesArray.put(deviceObj);
244
+ }
245
+ result.put("devices", devicesArray);
246
+ }
247
+
248
+ call.resolve(result);
249
+ } catch (Exception e) {
250
+ Log.e(TAG, "Error scanning devices", e);
251
+ call.reject("Failed to scan devices: " + e.getMessage());
252
+ }
253
+ }
254
+
255
+ @PluginMethod
256
+ public void sendOTANotification(PluginCall call) {
257
+ Integer address = call.getInt("address");
258
+ Integer majorVersion = call.getInt("majorVersion");
259
+ Integer minorVersion = call.getInt("minorVersion");
260
+ Integer patchVersion = call.getInt("patchVersion");
261
+ Integer firmwareSize = call.getInt("firmwareSize");
262
+
263
+ if (address == null || address < 1 || address > 63) {
264
+ call.reject("Invalid address. Must be between 1 and 63");
265
+ return;
266
+ }
267
+
268
+ if (majorVersion == null || minorVersion == null || patchVersion == null || firmwareSize == null) {
269
+ call.reject("All OTA parameters are required");
270
+ return;
271
+ }
272
+
273
+ try {
274
+ IOBoardManager.IOBoardResponse response = ioboardManager.sendOTANotification(
275
+ address, majorVersion, minorVersion, patchVersion, firmwareSize
276
+ );
277
+
278
+ JSObject result = new JSObject();
279
+ result.put("success", response.success);
280
+ result.put("message", response.message);
281
+
282
+ if (response.success) {
283
+ JSObject data = new JSObject();
284
+ data.put("address", address);
285
+ data.put("version", majorVersion + "." + minorVersion + "." + patchVersion);
286
+ data.put("firmwareSize", firmwareSize);
287
+ result.put("data", data);
288
+ }
289
+
290
+ call.resolve(result);
291
+ } catch (Exception e) {
292
+ Log.e(TAG, "Error sending OTA notification", e);
293
+ call.reject("Failed to send OTA notification: " + e.getMessage());
294
+ }
295
+ }
296
+
297
+ @PluginMethod
298
+ public void sendOTAData(PluginCall call) {
299
+ Integer address = call.getInt("address");
300
+ Integer packetNumber = call.getInt("packetNumber");
301
+ String dataBase64 = call.getString("data");
302
+
303
+ if (address == null || address < 1 || address > 63) {
304
+ call.reject("Invalid address. Must be between 1 and 63");
305
+ return;
306
+ }
307
+
308
+ if (packetNumber == null || dataBase64 == null) {
309
+ call.reject("Packet number and data are required");
310
+ return;
311
+ }
312
+
313
+ try {
314
+ IOBoardManager.IOBoardResponse response = ioboardManager.sendOTAData(
315
+ address, packetNumber, dataBase64
316
+ );
317
+
318
+ JSObject result = new JSObject();
319
+ result.put("success", response.success);
320
+ result.put("message", response.message);
321
+
322
+ if (response.success) {
323
+ JSObject data = new JSObject();
324
+ data.put("address", address);
325
+ data.put("packetNumber", packetNumber);
326
+ data.put("dataLength", dataBase64.length());
327
+ result.put("data", data);
328
+ }
329
+
330
+ call.resolve(result);
331
+ } catch (Exception e) {
332
+ Log.e(TAG, "Error sending OTA data", e);
333
+ call.reject("Failed to send OTA data: " + e.getMessage());
334
+ }
335
+ }
336
+ }