@leonardojc/capacitor-ioboard 2.0.10 → 2.0.12

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.
@@ -7,6 +7,7 @@ import com.getcapacitor.Plugin;
7
7
  import com.getcapacitor.PluginCall;
8
8
  import com.getcapacitor.PluginMethod;
9
9
  import com.getcapacitor.annotation.CapacitorPlugin;
10
+ import org.json.JSONObject;
10
11
  import java.util.List;
11
12
  import java.util.ArrayList;
12
13
 
@@ -25,6 +26,21 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
25
26
  private String currentDeviceInfo = "";
26
27
  private String lastError = "";
27
28
 
29
+ // Helper class for pallet configuration
30
+ private static class PalletConfig {
31
+ String ledColor;
32
+ int intensity;
33
+ int blinkTimes;
34
+ int blinkSpeed;
35
+
36
+ PalletConfig(String ledColor, int intensity, int blinkTimes, int blinkSpeed) {
37
+ this.ledColor = ledColor;
38
+ this.intensity = intensity;
39
+ this.blinkTimes = blinkTimes;
40
+ this.blinkSpeed = blinkSpeed;
41
+ }
42
+ }
43
+
28
44
  // For response waiting in getStatus()
29
45
  private volatile byte[] pendingResponse = null;
30
46
  private final Object responseLock = new Object();
@@ -290,49 +306,52 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
290
306
  }
291
307
 
292
308
  try {
293
- // Leer parámetros del call
294
309
  int address = call.getInt("address", 1);
295
- String color = call.getString("color", "RED");
296
- int intensity = call.getInt("intensity", 255);
297
- int blinkTimes = call.getInt("blinkTimes", 16);
298
- int blinkSpeed = call.getInt("blinkSpeed", 1);
310
+ int lockMask = call.getInt("lockMask", 0);
311
+ int extendedControl = call.getInt("extendedControl", 0);
299
312
 
300
- // Leer array de pallets
313
+ // Leer array de pallets (objetos con ledColor, intensity, blinkTimes, blinkSpeed)
301
314
  JSArray palletsArray = call.getArray("pallets");
302
- List<Integer> pallets = new ArrayList<>();
303
- if (palletsArray != null) {
304
- for (int i = 0; i < palletsArray.length(); i++) {
305
- try {
306
- pallets.add(palletsArray.getInt(i));
307
- } catch (Exception e) {
308
- Log.w(TAG, "Error reading pallet at index " + i);
309
- }
310
- }
315
+
316
+ if (palletsArray == null || palletsArray.length() != 8) {
317
+ call.reject("pallets array must contain exactly 8 pallet objects");
318
+ return;
311
319
  }
312
320
 
313
- // Si no se especificaron pallets, usar todos (1-8)
314
- if (pallets.isEmpty()) {
315
- for (int i = 1; i <= 8; i++) {
316
- pallets.add(i);
321
+ // Parsear los 8 objetos pallet
322
+ List<PalletConfig> pallets = new ArrayList<>();
323
+ for (int i = 0; i < 8; i++) {
324
+ try {
325
+ JSONObject palletJson = palletsArray.getJSONObject(i);
326
+ JSObject pallet = JSObject.fromJSONObject(palletJson);
327
+
328
+ String ledColor = pallet.getString("ledColor", "OFF");
329
+ int intensity = pallet.getInteger("intensity", 0);
330
+ int blinkTimes = pallet.getInteger("blinkTimes", 0);
331
+ int blinkSpeed = pallet.getInteger("blinkSpeed", 1);
332
+
333
+ pallets.add(new PalletConfig(ledColor, intensity, blinkTimes, blinkSpeed));
334
+
335
+ Log.d(TAG, String.format("Pallet %d: color=%s, intensity=%d, blink=%d, speed=%d",
336
+ i+1, ledColor, intensity, blinkTimes, blinkSpeed));
337
+ } catch (Exception e) {
338
+ Log.e(TAG, "Error reading pallet at index " + i + ": " + e.getMessage(), e);
339
+ call.reject("Error parsing pallet at index " + i);
340
+ return;
317
341
  }
318
342
  }
319
343
 
320
- Log.d(TAG, String.format("Parameters - address: %d, color: %s, intensity: %d, blinkTimes: %d, blinkSpeed: %d, pallets: %s",
321
- address, color, intensity, blinkTimes, blinkSpeed, pallets.toString()));
322
-
323
- byte[] frame = createMultiplePalletsFrame(address, color, intensity, blinkTimes, blinkSpeed, pallets);
344
+ byte[] frame = createFullPalletControlFrame(address, lockMask, extendedControl, pallets);
324
345
  boolean success = serialManager.writeData(frame);
325
346
 
326
347
  if (success) {
327
348
  JSObject result = new JSObject();
328
349
  result.put("success", true);
329
- result.put("message", String.format("Multiple pallets command sent - %d pallets with %s LEDs (intensity=%d, blink=%d)",
330
- pallets.size(), color, intensity, blinkTimes));
350
+ result.put("message", "Full pallet control command sent successfully");
331
351
  result.put("address", address);
332
- result.put("palletsCount", pallets.size());
333
352
  call.resolve(result);
334
353
  } else {
335
- call.reject("Failed to send multiple pallets command");
354
+ call.reject("Failed to send full pallet control command");
336
355
  }
337
356
 
338
357
  } catch (Exception e) {
@@ -374,7 +393,8 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
374
393
 
375
394
  for (int i = 0; i < actionsArray.length(); i++) {
376
395
  try {
377
- JSObject action = actionsArray.getJSObject(i);
396
+ JSONObject actionJson = actionsArray.getJSONObject(i);
397
+ JSObject action = JSObject.fromJSONObject(actionJson);
378
398
 
379
399
  int palletNumber = action.getInteger("palletNumber", 0);
380
400
  boolean unlock = action.has("unlock") ? action.getBoolean("unlock") : true;
@@ -1111,6 +1131,73 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
1111
1131
  return frameArray;
1112
1132
  }
1113
1133
 
1134
+ // New method for Full Pallet Control with individual pallet configurations
1135
+ private byte[] createFullPalletControlFrame(int address, int lockMask, int extendedControl, List<PalletConfig> pallets) {
1136
+ Log.d(TAG, "🎨 Creating Full Pallet Control frame with individual configs: address=" + address +
1137
+ ", lockMask=0x" + String.format("%02X", lockMask) +
1138
+ ", extControl=0x" + String.format("%02X", extendedControl));
1139
+
1140
+ byte soi = 0x0D;
1141
+ byte eoi = 0x0A;
1142
+ byte len = 0x39; // 57 bytes
1143
+ byte addr = (byte) address;
1144
+ byte type = 0x02;
1145
+ byte lockCtrl = (byte) lockMask;
1146
+ byte extCtrl = (byte) extendedControl;
1147
+
1148
+ // Build data for CRC
1149
+ List<Byte> dataForCrc = new ArrayList<>();
1150
+ dataForCrc.add(soi);
1151
+ dataForCrc.add(len);
1152
+ dataForCrc.add(addr);
1153
+ dataForCrc.add(type);
1154
+ dataForCrc.add(lockCtrl);
1155
+ dataForCrc.add(extCtrl);
1156
+
1157
+ // Add 8 LED groups (6 bytes each)
1158
+ for (int i = 0; i < 8; i++) {
1159
+ PalletConfig pallet = pallets.get(i);
1160
+ int[] rgb = parseColorToRGB(pallet.ledColor);
1161
+
1162
+ dataForCrc.add((byte) rgb[0]); // R
1163
+ dataForCrc.add((byte) rgb[1]); // G
1164
+ dataForCrc.add((byte) rgb[2]); // B
1165
+ dataForCrc.add((byte) pallet.intensity);
1166
+ dataForCrc.add((byte) pallet.blinkTimes);
1167
+ dataForCrc.add((byte) pallet.blinkSpeed);
1168
+ }
1169
+
1170
+ // Calculate CRC16
1171
+ byte[] crcData = new byte[dataForCrc.size()];
1172
+ for (int i = 0; i < dataForCrc.size(); i++) {
1173
+ crcData[i] = dataForCrc.get(i);
1174
+ }
1175
+ int crc16 = calculateCRC16Modbus(crcData);
1176
+ byte crcLow = (byte) (crc16 & 0xFF);
1177
+ byte crcHigh = (byte) ((crc16 >> 8) & 0xFF);
1178
+
1179
+ // Build final frame
1180
+ List<Byte> frame = new ArrayList<>();
1181
+ frame.addAll(dataForCrc);
1182
+ frame.add(crcLow);
1183
+ frame.add(crcHigh);
1184
+ frame.add(eoi);
1185
+
1186
+ byte[] frameArray = new byte[frame.size()];
1187
+ for (int i = 0; i < frame.size(); i++) {
1188
+ frameArray[i] = frame.get(i);
1189
+ }
1190
+
1191
+ // Log frame
1192
+ StringBuilder hexFrame = new StringBuilder();
1193
+ for (byte b : frameArray) {
1194
+ hexFrame.append(String.format("%02X ", b & 0xFF));
1195
+ }
1196
+ Log.d(TAG, "🎯 Full Pallet Control frame created: " + hexFrame.toString().trim());
1197
+
1198
+ return frameArray;
1199
+ }
1200
+
1114
1201
  // Implementación CRC16-Modbus (x16+x15+x2+1) según documentación MTC3P08L
1115
1202
  private static int calculateCRC16Modbus(byte[] data) {
1116
1203
  int crc = 0xFFFF; // Initial value
@@ -194,10 +194,10 @@ public class SerialConnectionManager {
194
194
  try {
195
195
  // Enhanced logging with hex dump (DEBUG only)
196
196
  logDebug("=== WRITING TO SERIAL PORT ===");
197
- logDebug("Port: " + currentPortPath);
198
- logDebug("Data length: " + data.length + " bytes");
197
+ //logDebug("Port: " + currentPortPath);
198
+ //logDebug("Data length: " + data.length + " bytes");
199
199
  logDebug("HEX data: " + bytesToHex(data));
200
- logDebug("Raw bytes: " + java.util.Arrays.toString(data));
200
+ //logDebug("Raw bytes: " + java.util.Arrays.toString(data));
201
201
 
202
202
  // Clear any pending input first
203
203
  if (serialInputStream != null && serialInputStream.available() > 0) {
@@ -351,10 +351,10 @@ public class SerialConnectionManager {
351
351
 
352
352
  // Enhanced logging for received data (DEBUG only for detailed hex)
353
353
  logDebug("=== 📥 RECEIVED FROM IOBOARD ===");
354
- logDebug("Port: " + currentPortPath);
355
- logDebug("Received " + bytesRead + " bytes");
354
+ //logDebug("Port: " + currentPortPath);
355
+ //logDebug("Received " + bytesRead + " bytes");
356
356
  logDebug("HEX received: " + bytesToHex(data));
357
- logDebug("Raw bytes: " + java.util.Arrays.toString(data));
357
+ //logDebug("Raw bytes: " + java.util.Arrays.toString(data));
358
358
 
359
359
  // Parser automático de respuesta del IOBoard
360
360
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonardojc/capacitor-ioboard",
3
- "version": "2.0.10",
3
+ "version": "2.0.12",
4
4
  "description": "A comprehensive Capacitor plugin for IOBoard devices with integrated serial communication and complete MTC3P08L protocol support including OTA updates",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",