@leonardojc/capacitor-ioboard 2.0.4 → 2.0.6

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.
@@ -14,6 +14,11 @@ import java.util.ArrayList;
14
14
  public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionManager.SerialDataListener {
15
15
 
16
16
  private static final String TAG = "CapacitorIoboard";
17
+
18
+ // Debug mode control
19
+ // Set to false for production to minimize logs
20
+ private static final boolean DEBUG_MODE = BuildConfig.DEBUG;
21
+
17
22
  private SerialConnectionManager serialManager;
18
23
  private IOBoardManager ioBoardManager;
19
24
  private boolean isConnected = false;
@@ -23,6 +28,33 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
23
28
  // For response waiting in getStatus()
24
29
  private volatile byte[] pendingResponse = null;
25
30
  private final Object responseLock = new Object();
31
+
32
+ // Helper methods for conditional logging
33
+ private void logDebug(String message) {
34
+ if (DEBUG_MODE) {
35
+ Log.d(TAG, message);
36
+ }
37
+ }
38
+
39
+ private void logInfo(String message) {
40
+ // Always log INFO level (important events)
41
+ Log.i(TAG, message);
42
+ }
43
+
44
+ private void logWarning(String message) {
45
+ // Always log warnings
46
+ Log.w(TAG, message);
47
+ }
48
+
49
+ private void logError(String message) {
50
+ // Always log errors
51
+ Log.e(TAG, message);
52
+ }
53
+
54
+ private void logError(String message, Throwable throwable) {
55
+ // Always log errors with exceptions
56
+ Log.e(TAG, message, throwable);
57
+ }
26
58
 
27
59
  // Helper method to convert bytes to hex string for debugging
28
60
  private String bytesToHex(byte[] bytes) {
@@ -122,14 +154,16 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
122
154
 
123
155
  @PluginMethod
124
156
  public void getStatus(PluginCall call) {
125
- Log.d(TAG, "📊 getStatus called");
157
+ logDebug("📊 getStatus called");
126
158
 
127
159
  if (serialManager == null) {
160
+ logError("Serial connection not initialized");
128
161
  call.reject("Serial connection not initialized");
129
162
  return;
130
163
  }
131
164
 
132
165
  if (!serialManager.isConnected()) {
166
+ logError("Not connected to serial port");
133
167
  call.reject("Not connected to serial port");
134
168
  return;
135
169
  }
@@ -182,34 +216,37 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
182
216
  result.put("firmware", parsedResponse.firmware);
183
217
  result.put("doorStatus", parsedResponse.doorStatus);
184
218
 
185
- // Interpret door status for user
186
- String doorStatusText;
187
- switch (parsedResponse.doorStatus) {
188
- case 0xFE:
189
- doorStatusText = "closed";
190
- break;
191
- case 0xFF:
192
- doorStatusText = "open";
193
- break;
194
- case 0x00:
195
- doorStatusText = "unknown";
196
- break;
197
- case 0x01:
198
- doorStatusText = "moving";
199
- break;
200
- default:
201
- doorStatusText = "custom";
202
- break;
219
+ // Parse door status bitmask (each bit represents a door)
220
+ // Bit 0 = Door 1, Bit 1 = Door 2, etc.
221
+ // 0 = closed, 1 = open
222
+ // Example: 0xF8 (11111000) = doors 1,2,3 closed, rest open
223
+ JSArray doorsArray = new JSArray();
224
+ for (int i = 0; i < 8; i++) {
225
+ boolean isOpen = ((parsedResponse.doorStatus & (1 << i)) != 0);
226
+ JSObject doorInfo = new JSObject();
227
+ doorInfo.put("doorNumber", i + 1);
228
+ doorInfo.put("isOpen", isOpen);
229
+ doorInfo.put("status", isOpen ? "open" : "closed");
230
+ doorsArray.put(doorInfo);
231
+ }
232
+ result.put("doors", doorsArray);
233
+
234
+ // Summary text
235
+ StringBuilder doorStatusText = new StringBuilder();
236
+ for (int i = 0; i < 8; i++) {
237
+ boolean isOpen = ((parsedResponse.doorStatus & (1 << i)) != 0);
238
+ if (i > 0) doorStatusText.append(", ");
239
+ doorStatusText.append("D").append(i + 1).append(":").append(isOpen ? "open" : "closed");
203
240
  }
204
- result.put("doorStatusText", doorStatusText);
241
+ result.put("doorStatusText", doorStatusText.toString());
205
242
 
206
- Log.i(TAG, String.format("✅ IOBoard Status - Firmware: %s, Door: 0x%02X (%s)",
207
- parsedResponse.firmware, parsedResponse.doorStatus, doorStatusText));
243
+ logInfo(String.format("✅ IOBoard Status - Firmware: %s, Door Status: 0x%02X (%s)",
244
+ parsedResponse.firmware, parsedResponse.doorStatus, doorStatusText.toString()));
208
245
 
209
246
  call.resolve(result);
210
247
 
211
248
  } catch (InterruptedException e) {
212
- Log.e(TAG, "Thread interrupted while waiting for status: " + e.getMessage(), e);
249
+ logError("Thread interrupted while waiting for status: " + e.getMessage(), e);
213
250
  call.reject("Status query interrupted: " + e.getMessage());
214
251
  } catch (Exception e) {
215
252
  Log.e(TAG, "Error in getStatus: " + e.getMessage(), e);
@@ -19,6 +19,9 @@ import java.util.concurrent.Executors;
19
19
  public class SerialConnectionManager {
20
20
  private static final String TAG = "SerialConnectionManager";
21
21
 
22
+ // Debug mode control - matches CapacitorIoboardPlugin
23
+ private static final boolean DEBUG_MODE = BuildConfig.DEBUG;
24
+
22
25
  public interface SerialDataListener {
23
26
  void onDataReceived(byte[] data);
24
27
  void onConnectionStateChanged(boolean connected);
@@ -37,7 +40,30 @@ public class SerialConnectionManager {
37
40
  public SerialConnectionManager() {
38
41
  mainHandler = new Handler(Looper.getMainLooper());
39
42
  executorService = Executors.newSingleThreadExecutor();
40
- Log.d(TAG, "SerialConnectionManager initialized");
43
+ logDebug("SerialConnectionManager initialized");
44
+ }
45
+
46
+ // Helper methods for conditional logging
47
+ private void logDebug(String message) {
48
+ if (DEBUG_MODE) {
49
+ Log.d(TAG, message);
50
+ }
51
+ }
52
+
53
+ private void logInfo(String message) {
54
+ Log.i(TAG, message);
55
+ }
56
+
57
+ private void logWarning(String message) {
58
+ Log.w(TAG, message);
59
+ }
60
+
61
+ private void logError(String message) {
62
+ Log.e(TAG, message);
63
+ }
64
+
65
+ private void logError(String message, Throwable throwable) {
66
+ Log.e(TAG, message, throwable);
41
67
  }
42
68
 
43
69
  public void setDataListener(SerialDataListener listener) {
@@ -166,32 +192,35 @@ public class SerialConnectionManager {
166
192
  }
167
193
 
168
194
  try {
169
- // Enhanced logging with hex dump
170
- Log.d(TAG, "=== WRITING TO SERIAL PORT ===");
171
- Log.d(TAG, "Port: " + currentPortPath);
172
- Log.d(TAG, "Data length: " + data.length + " bytes");
173
- Log.d(TAG, "HEX data: " + bytesToHex(data));
174
- Log.d(TAG, "Raw bytes: " + java.util.Arrays.toString(data));
195
+ // Enhanced logging with hex dump (DEBUG only)
196
+ logDebug("=== WRITING TO SERIAL PORT ===");
197
+ logDebug("Port: " + currentPortPath);
198
+ logDebug("Data length: " + data.length + " bytes");
199
+ logDebug("HEX data: " + bytesToHex(data));
200
+ logDebug("Raw bytes: " + java.util.Arrays.toString(data));
175
201
 
176
202
  // Clear any pending input first
177
203
  if (serialInputStream != null && serialInputStream.available() > 0) {
178
204
  int available = serialInputStream.available();
179
205
  byte[] discarded = new byte[available];
180
206
  serialInputStream.read(discarded);
181
- Log.d(TAG, "Discarded " + available + " pending bytes before write");
207
+ logDebug("Discarded " + available + " pending bytes before write");
182
208
  }
183
209
 
184
210
  long startTime = System.currentTimeMillis();
185
211
 
186
212
  // Write entire frame at once (IOBoard expects complete frame)
187
- Log.d(TAG, "Sending complete frame as single write operation...");
213
+ logDebug("Sending complete frame as single write operation...");
188
214
  serialOutputStream.write(data);
189
215
  serialOutputStream.flush();
190
216
 
191
217
  long endTime = System.currentTimeMillis();
192
218
 
193
- Log.i(TAG, "✅ Successfully wrote " + data.length + " bytes in " + (endTime - startTime) + "ms");
194
- Log.i(TAG, "HEX written: " + bytesToHex(data));
219
+ // Only log summary in production
220
+ if (DEBUG_MODE) {
221
+ logInfo("✅ Successfully wrote " + data.length + " bytes in " + (endTime - startTime) + "ms");
222
+ logInfo("HEX written: " + bytesToHex(data));
223
+ }
195
224
 
196
225
  // Wait longer for IOBoard response (some devices need more processing time)
197
226
  Thread.sleep(200); // Increased from 50ms to 200ms
@@ -320,38 +349,42 @@ public class SerialConnectionManager {
320
349
  byte[] data = new byte[bytesRead];
321
350
  System.arraycopy(buffer, 0, data, 0, bytesRead);
322
351
 
323
- // Enhanced logging for received data
324
- Log.d(TAG, "=== 📥 RECEIVED FROM IOBOARD ===");
325
- Log.d(TAG, "Port: " + currentPortPath);
326
- Log.d(TAG, "Received " + bytesRead + " bytes");
327
- Log.d(TAG, "HEX received: " + bytesToHex(data));
328
- Log.d(TAG, "Raw bytes: " + java.util.Arrays.toString(data));
352
+ // Enhanced logging for received data (DEBUG only for detailed hex)
353
+ logDebug("=== 📥 RECEIVED FROM IOBOARD ===");
354
+ logDebug("Port: " + currentPortPath);
355
+ logDebug("Received " + bytesRead + " bytes");
356
+ logDebug("HEX received: " + bytesToHex(data));
357
+ logDebug("Raw bytes: " + java.util.Arrays.toString(data));
329
358
 
330
359
  // Parser automático de respuesta del IOBoard
331
360
  try {
332
361
  CapacitorIoboardPlugin.IOBoardResponse response = CapacitorIoboardPlugin.parseIOBoardResponse(data);
333
362
  if (response.isValid) {
334
- Log.i(TAG, String.format("🎯 IOBoard Response Parsed Successfully: %s", response.toString()));
363
+ logDebug(String.format("🎯 IOBoard Response Parsed Successfully: %s", response.toString()));
335
364
 
336
365
  // Logging específico según tipo de respuesta
337
366
  if (response.type == 0x00) { // Status Response
338
- Log.i(TAG, String.format("📊 IOBoard Status - Firmware: %s, Door Status: 0x%02X",
367
+ logInfo(String.format("📊 IOBoard Status - Firmware: %s, Door Status: 0x%02X",
339
368
  response.firmware, response.doorStatus));
340
369
 
341
- // Interpretar door status
342
- String doorStatusDesc = interpretDoorStatus(response.doorStatus);
343
- Log.i(TAG, String.format("🚪 Door Status: %s", doorStatusDesc));
370
+ // Interpretar door status (DEBUG only)
371
+ if (DEBUG_MODE) {
372
+ String doorStatusDesc = interpretDoorStatus(response.doorStatus);
373
+ logInfo(String.format("🚪 Door Status: %s", doorStatusDesc));
374
+ }
344
375
  }
345
376
  } else {
346
- Log.w(TAG, "⚠️ Received invalid IOBoard response - trying ASCII interpretation");
377
+ logWarning("⚠️ Received invalid IOBoard response - trying ASCII interpretation");
347
378
 
348
- // Try to interpret as ASCII if printable
349
- String ascii = new String(data);
350
- boolean isPrintable = ascii.chars().allMatch(c -> c >= 32 && c <= 126);
351
- if (isPrintable) {
352
- Log.d(TAG, "ASCII: '" + ascii + "'");
353
- } else {
354
- Log.d(TAG, "Binary data (non-ASCII)");
379
+ // Try to interpret as ASCII if printable (DEBUG only)
380
+ if (DEBUG_MODE) {
381
+ String ascii = new String(data);
382
+ boolean isPrintable = ascii.chars().allMatch(c -> c >= 32 && c <= 126);
383
+ if (isPrintable) {
384
+ logDebug("ASCII: '" + ascii + "'");
385
+ } else {
386
+ logDebug("Binary data (non-ASCII)");
387
+ }
355
388
  }
356
389
  }
357
390
  } catch (Exception e) {
@@ -78,11 +78,17 @@ export interface IOBoardResponse {
78
78
  message?: string;
79
79
  data?: any;
80
80
  }
81
+ export interface DoorInfo {
82
+ doorNumber: number;
83
+ isOpen: boolean;
84
+ status: 'open' | 'closed';
85
+ }
81
86
  export interface IOBoardStatusResponse extends IOBoardResponse {
82
87
  address?: number;
83
88
  firmware?: string;
84
89
  doorStatus?: number;
85
- doorStatusText?: 'closed' | 'open' | 'unknown' | 'moving' | 'custom';
90
+ doors?: DoorInfo[];
91
+ doorStatusText?: string;
86
92
  }
87
93
  export interface SerialConnectionOptions {
88
94
  portPath?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonardojc/capacitor-ioboard",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
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",