@leonardojc/capacitor-ioboard 2.0.1 → 2.0.3

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.
@@ -132,19 +132,77 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
132
132
 
133
133
  try {
134
134
  int address = call.getInt("address", 1);
135
+ int timeout = call.getInt("timeout", 1000); // 1 second default
136
+
135
137
  byte[] statusFrame = createStatusQueryFrame(address);
136
138
  boolean success = serialManager.writeData(statusFrame);
137
139
 
138
- if (success) {
139
- JSObject result = new JSObject();
140
- result.put("success", true);
141
- result.put("message", "Status query sent successfully");
142
- result.put("address", address);
143
- call.resolve(result);
144
- } else {
140
+ if (!success) {
145
141
  call.reject("Failed to send status query");
142
+ return;
143
+ }
144
+
145
+ // Wait for response
146
+ long startTime = System.currentTimeMillis();
147
+ byte[] response = null;
148
+
149
+ while (System.currentTimeMillis() - startTime < timeout) {
150
+ response = serialManager.readData(100); // Read with 100ms timeout
151
+ if (response != null && response.length > 0) {
152
+ break;
153
+ }
154
+ Thread.sleep(50); // Small delay between attempts
146
155
  }
147
156
 
157
+ if (response == null || response.length == 0) {
158
+ call.reject("No response from IOBoard (timeout after " + timeout + "ms)");
159
+ return;
160
+ }
161
+
162
+ // Parse response
163
+ IOBoardResponse parsedResponse = parseIOBoardResponse(response);
164
+
165
+ if (!parsedResponse.isValid) {
166
+ call.reject("Invalid response from IOBoard");
167
+ return;
168
+ }
169
+
170
+ // Build result with door status
171
+ JSObject result = new JSObject();
172
+ result.put("success", true);
173
+ result.put("address", parsedResponse.address);
174
+ result.put("firmware", parsedResponse.firmware);
175
+ result.put("doorStatus", parsedResponse.doorStatus);
176
+
177
+ // Interpret door status for user
178
+ String doorStatusText;
179
+ switch (parsedResponse.doorStatus) {
180
+ case 0xFE:
181
+ doorStatusText = "closed";
182
+ break;
183
+ case 0xFF:
184
+ doorStatusText = "open";
185
+ break;
186
+ case 0x00:
187
+ doorStatusText = "unknown";
188
+ break;
189
+ case 0x01:
190
+ doorStatusText = "moving";
191
+ break;
192
+ default:
193
+ doorStatusText = "custom";
194
+ break;
195
+ }
196
+ result.put("doorStatusText", doorStatusText);
197
+
198
+ Log.i(TAG, String.format("✅ IOBoard Status - Firmware: %s, Door: 0x%02X (%s)",
199
+ parsedResponse.firmware, parsedResponse.doorStatus, doorStatusText));
200
+
201
+ call.resolve(result);
202
+
203
+ } catch (InterruptedException e) {
204
+ Log.e(TAG, "Thread interrupted while waiting for status: " + e.getMessage(), e);
205
+ call.reject("Status query interrupted: " + e.getMessage());
148
206
  } catch (Exception e) {
149
207
  Log.e(TAG, "Error in getStatus: " + e.getMessage(), e);
150
208
  call.reject("Error getting status: " + e.getMessage());
@@ -187,33 +245,46 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
187
245
  }
188
246
 
189
247
  try {
190
- // TEMPORAL: Generar exactamente el frame de la documentación
191
- // Address: 01; Unlock: 01; Extended output: 80;
192
- // All 8 LEDs: flashing red, brightness 255, flashing 16 times, interval 1 second
193
- Log.d(TAG, "🧪 TESTING: Generating exact documentation frame");
194
-
195
- int address = 1;
196
- String color = "RED";
197
- int intensity = 255; // FF en hex
198
- int blinkTimes = 16; // 10 en hex
199
- int blinkSpeed = 1; // 01 en hex
200
-
201
- // Usar todos los pallets (1-8) como en el ejemplo
202
- List<Integer> allPallets = new ArrayList<>();
203
- for (int i = 1; i <= 8; i++) {
204
- allPallets.add(i);
248
+ // Leer parámetros del call
249
+ int address = call.getInt("address", 1);
250
+ String color = call.getString("color", "RED");
251
+ int intensity = call.getInt("intensity", 255);
252
+ int blinkTimes = call.getInt("blinkTimes", 16);
253
+ int blinkSpeed = call.getInt("blinkSpeed", 1);
254
+
255
+ // Leer array de pallets
256
+ JSArray palletsArray = call.getArray("pallets");
257
+ List<Integer> pallets = new ArrayList<>();
258
+ if (palletsArray != null) {
259
+ for (int i = 0; i < palletsArray.length(); i++) {
260
+ try {
261
+ pallets.add(palletsArray.getInt(i));
262
+ } catch (Exception e) {
263
+ Log.w(TAG, "Error reading pallet at index " + i);
264
+ }
265
+ }
266
+ }
267
+
268
+ // Si no se especificaron pallets, usar todos (1-8)
269
+ if (pallets.isEmpty()) {
270
+ for (int i = 1; i <= 8; i++) {
271
+ pallets.add(i);
272
+ }
205
273
  }
206
274
 
207
- byte[] frame = createMultiplePalletsFrame(address, color, intensity, blinkTimes, blinkSpeed, allPallets);
275
+ Log.d(TAG, String.format("Parameters - address: %d, color: %s, intensity: %d, blinkTimes: %d, blinkSpeed: %d, pallets: %s",
276
+ address, color, intensity, blinkTimes, blinkSpeed, pallets.toString()));
277
+
278
+ byte[] frame = createMultiplePalletsFrame(address, color, intensity, blinkTimes, blinkSpeed, pallets);
208
279
  boolean success = serialManager.writeData(frame);
209
280
 
210
281
  if (success) {
211
282
  JSObject result = new JSObject();
212
283
  result.put("success", true);
213
- result.put("message", String.format("TEST: Documentation frame sent - %d pallets with %s LEDs (intensity=%d, blink=%d)",
214
- allPallets.size(), color, intensity, blinkTimes));
284
+ result.put("message", String.format("Multiple pallets command sent - %d pallets with %s LEDs (intensity=%d, blink=%d)",
285
+ pallets.size(), color, intensity, blinkTimes));
215
286
  result.put("address", address);
216
- result.put("palletsCount", allPallets.size());
287
+ result.put("palletsCount", pallets.size());
217
288
  call.resolve(result);
218
289
  } else {
219
290
  call.reject("Failed to send multiple pallets command");
@@ -635,6 +706,80 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
635
706
  }
636
707
  }
637
708
 
709
+ // Helper method to parse RGB color from string
710
+ // Accepts: "RED", "255,128,64", "#FF8040", or "rgb(255,128,64)"
711
+ private int[] parseColorToRGB(String color) {
712
+ int[] rgb = new int[3]; // [R, G, B]
713
+
714
+ // Check if it's RGB format: "255,128,64" or "rgb(255,128,64)"
715
+ if (color.contains(",")) {
716
+ String rgbString = color.replace("rgb(", "").replace(")", "").trim();
717
+ String[] parts = rgbString.split(",");
718
+ if (parts.length == 3) {
719
+ try {
720
+ rgb[0] = Integer.parseInt(parts[0].trim());
721
+ rgb[1] = Integer.parseInt(parts[1].trim());
722
+ rgb[2] = Integer.parseInt(parts[2].trim());
723
+ Log.d(TAG, String.format("🎨 Parsed RGB: (%d, %d, %d)", rgb[0], rgb[1], rgb[2]));
724
+ return rgb;
725
+ } catch (NumberFormatException e) {
726
+ Log.w(TAG, "⚠️ Invalid RGB format: " + color);
727
+ }
728
+ }
729
+ }
730
+
731
+ // Check if it's hex format: "#FF8040" or "FF8040"
732
+ if (color.startsWith("#") || (color.length() == 6 && color.matches("[0-9A-Fa-f]+"))) {
733
+ try {
734
+ String hex = color.replace("#", "");
735
+ rgb[0] = Integer.parseInt(hex.substring(0, 2), 16);
736
+ rgb[1] = Integer.parseInt(hex.substring(2, 4), 16);
737
+ rgb[2] = Integer.parseInt(hex.substring(4, 6), 16);
738
+ Log.d(TAG, String.format("🎨 Parsed HEX %s: RGB(%d, %d, %d)", color, rgb[0], rgb[1], rgb[2]));
739
+ return rgb;
740
+ } catch (Exception e) {
741
+ Log.w(TAG, "⚠️ Invalid HEX format: " + color);
742
+ }
743
+ }
744
+
745
+ // Named colors
746
+ switch (color.toUpperCase()) {
747
+ case "RED":
748
+ rgb[0] = 255; rgb[1] = 0; rgb[2] = 0;
749
+ break;
750
+ case "GREEN":
751
+ rgb[0] = 0; rgb[1] = 255; rgb[2] = 0;
752
+ break;
753
+ case "BLUE":
754
+ rgb[0] = 0; rgb[1] = 0; rgb[2] = 255;
755
+ break;
756
+ case "WHITE":
757
+ rgb[0] = 255; rgb[1] = 255; rgb[2] = 255;
758
+ break;
759
+ case "YELLOW":
760
+ rgb[0] = 255; rgb[1] = 255; rgb[2] = 0;
761
+ break;
762
+ case "CYAN":
763
+ rgb[0] = 0; rgb[1] = 255; rgb[2] = 255;
764
+ break;
765
+ case "MAGENTA":
766
+ case "PURPLE":
767
+ rgb[0] = 255; rgb[1] = 0; rgb[2] = 255;
768
+ break;
769
+ case "ORANGE":
770
+ rgb[0] = 255; rgb[1] = 165; rgb[2] = 0;
771
+ break;
772
+ case "PINK":
773
+ rgb[0] = 255; rgb[1] = 192; rgb[2] = 203;
774
+ break;
775
+ default:
776
+ Log.w(TAG, "⚠️ Unknown color: " + color + ", using GREEN as default");
777
+ rgb[0] = 0; rgb[1] = 255; rgb[2] = 0;
778
+ break;
779
+ }
780
+ return rgb;
781
+ }
782
+
638
783
  // Create MTC3P08L protocol frame for unlock pallet command
639
784
  private byte[] createUnlockPalletFrame(int address, int palletNumber, String ledColor) {
640
785
  // MTC3P08L Protocol Frame Structure (según documentación):
@@ -654,33 +799,10 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
654
799
  byte doorLock = 0x01; // LOCK: 1=unlock, 0=no action
655
800
 
656
801
  // LED section (6 bytes): [RGB_R][RGB_G][RGB_B][Intensity][BlinkTimes][BlinkSpeed]
657
- byte red = 0, green = 0, blue = 0;
658
- switch (ledColor.toLowerCase()) {
659
- case "red":
660
- red = (byte) 255;
661
- break;
662
- case "green":
663
- green = (byte) 255;
664
- break;
665
- case "blue":
666
- blue = (byte) 255;
667
- break;
668
- case "white":
669
- red = green = blue = (byte) 255;
670
- break;
671
- case "yellow":
672
- red = green = (byte) 255;
673
- break;
674
- case "cyan":
675
- green = blue = (byte) 255;
676
- break;
677
- case "magenta":
678
- red = blue = (byte) 255;
679
- break;
680
- default:
681
- green = (byte) 255; // Default to green
682
- break;
683
- }
802
+ int[] rgb = parseColorToRGB(ledColor);
803
+ byte red = (byte) rgb[0];
804
+ byte green = (byte) rgb[1];
805
+ byte blue = (byte) rgb[2];
684
806
 
685
807
  byte intensity = (byte) 255; // Max intensity
686
808
  byte blinkTimes = 16; // 16 veces como en el ejemplo
@@ -730,40 +852,11 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
730
852
 
731
853
  byte extControl = (byte) 0x80; // Extended output control (fixed as per example)
732
854
 
733
- // Convert color to RGB
734
- byte red = 0x00, green = 0x00, blue = 0x00;
735
- switch (color.toUpperCase()) {
736
- case "RED":
737
- red = (byte) 0xFF;
738
- break;
739
- case "GREEN":
740
- green = (byte) 0xFF;
741
- break;
742
- case "BLUE":
743
- blue = (byte) 0xFF;
744
- break;
745
- case "YELLOW":
746
- red = (byte) 0xFF;
747
- green = (byte) 0xFF;
748
- break;
749
- case "PURPLE":
750
- red = (byte) 0xFF;
751
- blue = (byte) 0xFF;
752
- break;
753
- case "CYAN":
754
- green = (byte) 0xFF;
755
- blue = (byte) 0xFF;
756
- break;
757
- case "WHITE":
758
- red = (byte) 0xFF;
759
- green = (byte) 0xFF;
760
- blue = (byte) 0xFF;
761
- break;
762
- default:
763
- Log.w(TAG, "⚠️ Unknown color: " + color + ", using RED as default");
764
- red = (byte) 0xFF;
765
- break;
766
- }
855
+ // Convert color to RGB using helper method
856
+ int[] rgb = parseColorToRGB(color);
857
+ byte red = (byte) rgb[0];
858
+ byte green = (byte) rgb[1];
859
+ byte blue = (byte) rgb[2];
767
860
 
768
861
  // Build data array for CRC calculation
769
862
  // CRC includes SOI + all data up to (but not including) the CRC field itself
@@ -79,14 +79,10 @@ export interface IOBoardResponse {
79
79
  data?: any;
80
80
  }
81
81
  export interface IOBoardStatusResponse extends IOBoardResponse {
82
- data?: {
83
- doorLockStatus: number;
84
- softwareVersion: {
85
- major: number;
86
- minor: number;
87
- patch: number;
88
- };
89
- };
82
+ address?: number;
83
+ firmware?: string;
84
+ doorStatus?: number;
85
+ doorStatusText?: 'closed' | 'open' | 'unknown' | 'moving' | 'custom';
90
86
  }
91
87
  export interface SerialConnectionOptions {
92
88
  portPath?: string;
@@ -103,13 +99,13 @@ export interface DeviceOptions {
103
99
  }
104
100
  export interface UnlockPalletOptions extends DeviceOptions {
105
101
  palletNumber: number;
106
- ledColor?: 'red' | 'green' | 'blue' | 'yellow' | 'white' | 'off';
102
+ ledColor?: string;
107
103
  blinkTimes?: number;
108
104
  blinkSpeed?: number;
109
105
  }
110
106
  export interface MultiplePalletOptions extends DeviceOptions {
111
107
  doorLockMask: number;
112
- ledColor?: 'red' | 'green' | 'blue' | 'yellow' | 'white' | 'off';
108
+ ledColor?: string;
113
109
  ledControls?: LEDControlOptions[];
114
110
  }
115
111
  export interface ScanOptions {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonardojc/capacitor-ioboard",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
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",