@leonardojc/capacitor-ioboard 2.0.2 → 2.0.4
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.
|
@@ -19,6 +19,10 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
|
|
|
19
19
|
private boolean isConnected = false;
|
|
20
20
|
private String currentDeviceInfo = "";
|
|
21
21
|
private String lastError = "";
|
|
22
|
+
|
|
23
|
+
// For response waiting in getStatus()
|
|
24
|
+
private volatile byte[] pendingResponse = null;
|
|
25
|
+
private final Object responseLock = new Object();
|
|
22
26
|
|
|
23
27
|
// Helper method to convert bytes to hex string for debugging
|
|
24
28
|
private String bytesToHex(byte[] bytes) {
|
|
@@ -132,19 +136,81 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
|
|
|
132
136
|
|
|
133
137
|
try {
|
|
134
138
|
int address = call.getInt("address", 1);
|
|
139
|
+
int timeout = call.getInt("timeout", 1000); // 1 second default
|
|
140
|
+
|
|
141
|
+
// Clear previous response
|
|
142
|
+
synchronized (responseLock) {
|
|
143
|
+
pendingResponse = null;
|
|
144
|
+
}
|
|
145
|
+
|
|
135
146
|
byte[] statusFrame = createStatusQueryFrame(address);
|
|
136
147
|
boolean success = serialManager.writeData(statusFrame);
|
|
137
148
|
|
|
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 {
|
|
149
|
+
if (!success) {
|
|
145
150
|
call.reject("Failed to send status query");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Wait for response with timeout
|
|
155
|
+
byte[] response;
|
|
156
|
+
synchronized (responseLock) {
|
|
157
|
+
long startTime = System.currentTimeMillis();
|
|
158
|
+
while (pendingResponse == null) {
|
|
159
|
+
long elapsed = System.currentTimeMillis() - startTime;
|
|
160
|
+
if (elapsed >= timeout) {
|
|
161
|
+
call.reject("No response from IOBoard (timeout after " + timeout + "ms)");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
responseLock.wait(100); // Wait with 100ms intervals
|
|
165
|
+
}
|
|
166
|
+
response = pendingResponse;
|
|
167
|
+
pendingResponse = null; // Clear after reading
|
|
146
168
|
}
|
|
147
169
|
|
|
170
|
+
// Parse response
|
|
171
|
+
IOBoardResponse parsedResponse = parseIOBoardResponse(response);
|
|
172
|
+
|
|
173
|
+
if (!parsedResponse.isValid) {
|
|
174
|
+
call.reject("Invalid response from IOBoard");
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Build result with door status
|
|
179
|
+
JSObject result = new JSObject();
|
|
180
|
+
result.put("success", true);
|
|
181
|
+
result.put("address", parsedResponse.address);
|
|
182
|
+
result.put("firmware", parsedResponse.firmware);
|
|
183
|
+
result.put("doorStatus", parsedResponse.doorStatus);
|
|
184
|
+
|
|
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;
|
|
203
|
+
}
|
|
204
|
+
result.put("doorStatusText", doorStatusText);
|
|
205
|
+
|
|
206
|
+
Log.i(TAG, String.format("✅ IOBoard Status - Firmware: %s, Door: 0x%02X (%s)",
|
|
207
|
+
parsedResponse.firmware, parsedResponse.doorStatus, doorStatusText));
|
|
208
|
+
|
|
209
|
+
call.resolve(result);
|
|
210
|
+
|
|
211
|
+
} catch (InterruptedException e) {
|
|
212
|
+
Log.e(TAG, "Thread interrupted while waiting for status: " + e.getMessage(), e);
|
|
213
|
+
call.reject("Status query interrupted: " + e.getMessage());
|
|
148
214
|
} catch (Exception e) {
|
|
149
215
|
Log.e(TAG, "Error in getStatus: " + e.getMessage(), e);
|
|
150
216
|
call.reject("Error getting status: " + e.getMessage());
|
|
@@ -324,6 +390,12 @@ public class CapacitorIoboardPlugin extends Plugin implements SerialConnectionMa
|
|
|
324
390
|
String dataString = new String(data);
|
|
325
391
|
Log.d(TAG, "Data received: " + dataString);
|
|
326
392
|
|
|
393
|
+
// Store response for getStatus() waiting
|
|
394
|
+
synchronized (responseLock) {
|
|
395
|
+
pendingResponse = data;
|
|
396
|
+
responseLock.notifyAll();
|
|
397
|
+
}
|
|
398
|
+
|
|
327
399
|
JSObject result = new JSObject();
|
|
328
400
|
result.put("rawData", dataString);
|
|
329
401
|
result.put("timestamp", System.currentTimeMillis());
|
|
@@ -79,14 +79,10 @@ export interface IOBoardResponse {
|
|
|
79
79
|
data?: any;
|
|
80
80
|
}
|
|
81
81
|
export interface IOBoardStatusResponse extends IOBoardResponse {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leonardojc/capacitor-ioboard",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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",
|