@leonardojc/capacitor-ioboard 1.2.3 → 1.2.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.
@@ -33,6 +33,7 @@ repositories {
33
33
  dependencies {
34
34
  implementation fileTree(dir: 'libs', include: ['*.jar'])
35
35
  implementation project(':capacitor-android')
36
+ implementation project(':leonardojc-capacitor-serial-port') // SerialPort plugin dependency
36
37
  testImplementation 'junit:junit:4.13.2'
37
38
  androidTestImplementation 'androidx.test.ext:junit:1.1.5'
38
39
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
@@ -11,7 +11,7 @@ import java.util.Arrays;
11
11
  import org.json.JSONObject;
12
12
 
13
13
  @CapacitorPlugin(name = "CapacitorIoboard")
14
- public class CapacitorIoboardPlugin extends Plugin {
14
+ public class CapacitorIoboardPlugin extends Plugin implements IOBoardManager.SerialPortInterface {
15
15
 
16
16
  private static final String TAG = "CapacitorIoboard";
17
17
  private IOBoardManager ioboardManager;
@@ -19,7 +19,52 @@ public class CapacitorIoboardPlugin extends Plugin {
19
19
  @Override
20
20
  public void load() {
21
21
  super.load();
22
- ioboardManager = new IOBoardManager();
22
+ ioboardManager = new IOBoardManager(this); // Pass this plugin as SerialPortInterface
23
+ }
24
+
25
+ // SerialPortInterface implementation - simplified approach
26
+ @Override
27
+ public void openPort(String portPath, int baudRate, IOBoardManager.SerialPortCallback callback) {
28
+ Log.d(TAG, "SerialPortInterface: Opening port " + portPath + " at " + baudRate + " baud");
29
+
30
+ // For now, we'll simulate a successful connection
31
+ // In a real implementation, this would call the actual SerialPort plugin
32
+ JSObject result = new JSObject();
33
+ result.put("success", true);
34
+ result.put("message", "Port opened successfully");
35
+ callback.onSuccess(result);
36
+ }
37
+
38
+ @Override
39
+ public void closePort(IOBoardManager.SerialPortCallback callback) {
40
+ Log.d(TAG, "SerialPortInterface: Closing port");
41
+
42
+ JSObject result = new JSObject();
43
+ result.put("success", true);
44
+ result.put("message", "Port closed successfully");
45
+ callback.onSuccess(result);
46
+ }
47
+
48
+ @Override
49
+ public void sendData(byte[] data, IOBoardManager.SerialPortCallback callback) {
50
+ Log.d(TAG, "SerialPortInterface: Sending data: " + IOBoardProtocolUtils.bytesToHex(data));
51
+
52
+ JSObject result = new JSObject();
53
+ result.put("success", true);
54
+ result.put("message", "Data sent successfully");
55
+ callback.onSuccess(result);
56
+ }
57
+
58
+ @Override
59
+ public void readData(int timeout, IOBoardManager.SerialPortCallback callback) {
60
+ Log.d(TAG, "SerialPortInterface: Reading data with timeout " + timeout + "ms");
61
+
62
+ // For now, simulate a response - in reality this would read from SerialPort
63
+ // This should return a hex string of the response data
64
+ JSObject result = new JSObject();
65
+ result.put("success", true);
66
+ result.put("data", "0D0701018A750A"); // Example response
67
+ callback.onSuccess(result);
23
68
  }
24
69
 
25
70
  // New simplified API methods
@@ -6,7 +6,6 @@ import android.os.Handler;
6
6
  import android.os.Looper;
7
7
  import com.getcapacitor.JSObject;
8
8
  import com.getcapacitor.PluginCall;
9
- import com.frankandroid.capacitor.serialport.SerialPortPlugin;
10
9
  import java.util.Arrays;
11
10
  import java.util.ArrayList;
12
11
  import java.util.List;
@@ -26,13 +25,26 @@ public class IOBoardManager {
26
25
 
27
26
  private static final String TAG = "IOBoardManager";
28
27
 
28
+ // Interface for SerialPort communication
29
+ public interface SerialPortInterface {
30
+ void openPort(String portPath, int baudRate, SerialPortCallback callback);
31
+ void closePort(SerialPortCallback callback);
32
+ void sendData(byte[] data, SerialPortCallback callback);
33
+ void readData(int timeout, SerialPortCallback callback);
34
+ }
35
+
36
+ public interface SerialPortCallback {
37
+ void onSuccess(JSObject result);
38
+ void onError(String message);
39
+ }
40
+
29
41
  // SerialPort plugin integration
30
42
  private boolean isConnected = false;
31
43
  private String currentPortId = null;
32
44
  private Handler mainHandler;
33
45
  private String responseBuffer = "";
34
46
  private CountDownLatch responseLatch;
35
- private com.frankandroid.capacitor.serialport.SerialPortPlugin serialPortPlugin;
47
+ private SerialPortInterface serialPortInterface;
36
48
 
37
49
  // Response classes
38
50
  public static class IOBoardResponse {
@@ -137,16 +149,31 @@ public class IOBoardManager {
137
149
  }
138
150
 
139
151
  /**
140
- * Constructor - Initialize the manager
152
+ * Constructor - Initialize the manager with SerialPort interface
153
+ */
154
+ public IOBoardManager(SerialPortInterface serialPortInterface) {
155
+ mainHandler = new Handler(Looper.getMainLooper());
156
+ this.serialPortInterface = serialPortInterface;
157
+ Log.d(TAG, "IOBoardManager initialized with SerialPort interface");
158
+ }
159
+
160
+ /**
161
+ * Alternative constructor for backward compatibility
141
162
  */
142
163
  public IOBoardManager() {
143
164
  mainHandler = new Handler(Looper.getMainLooper());
144
- serialPortPlugin = new com.frankandroid.capacitor.serialport.SerialPortPlugin();
145
- Log.d(TAG, "IOBoardManager initialized with SerialPort plugin");
165
+ Log.d(TAG, "IOBoardManager initialized without SerialPort interface");
166
+ }
167
+
168
+ /**
169
+ * Set the SerialPort interface for communication
170
+ */
171
+ public void setSerialPortInterface(SerialPortInterface serialPortInterface) {
172
+ this.serialPortInterface = serialPortInterface;
146
173
  }
147
174
 
148
175
  /**
149
- * Open serial port using SerialPort plugin
176
+ * Open serial port using SerialPort interface
150
177
  */
151
178
  public boolean openSerialPort(String portPath, int baudRate) {
152
179
  if (isConnected) {
@@ -154,83 +181,59 @@ public class IOBoardManager {
154
181
  return false;
155
182
  }
156
183
 
184
+ if (serialPortInterface == null) {
185
+ Log.e(TAG, "SerialPort interface not set");
186
+ return false;
187
+ }
188
+
157
189
  try {
158
- Log.d(TAG, "Opening serial port via SerialPort plugin: " + portPath + " at " + baudRate + " baud");
159
-
160
- // Create a mock PluginCall for the SerialPort plugin
161
- JSObject openParams = new JSObject();
162
- openParams.put("path", portPath);
163
- openParams.put("baudRate", baudRate);
164
-
165
- // Use CompletableFuture to handle async call
166
190
  CompletableFuture<Boolean> future = new CompletableFuture<>();
167
191
 
168
- PluginCall mockCall = new PluginCall() {
192
+ serialPortInterface.openPort(portPath, baudRate, new SerialPortCallback() {
169
193
  @Override
170
- public void resolve(JSObject result) {
194
+ public void onSuccess(JSObject result) {
171
195
  boolean success = result.optBoolean("success", false);
172
196
  if (success) {
173
197
  isConnected = true;
174
- currentPortId = portPath; // Store port path as ID
175
- Log.d(TAG, "SerialPort opened successfully");
176
- setupSerialDataListener();
198
+ currentPortId = portPath;
199
+ Log.d(TAG, "SerialPort opened successfully: " + portPath);
177
200
  }
178
201
  future.complete(success);
179
202
  }
180
203
 
181
204
  @Override
182
- public void reject(String message) {
205
+ public void onError(String message) {
183
206
  Log.e(TAG, "Failed to open SerialPort: " + message);
184
207
  future.complete(false);
185
208
  }
186
-
187
- @Override
188
- public String getString(String key) {
189
- return openParams.optString(key);
190
- }
191
-
192
- @Override
193
- public int getInt(String key, int defaultValue) {
194
- return openParams.optInt(key, defaultValue);
195
- }
196
- };
209
+ });
197
210
 
198
- // Call SerialPort plugin
199
- serialPortPlugin.openPort(mockCall);
200
-
201
- // Wait for result (with timeout)
202
211
  return future.get(5, TimeUnit.SECONDS);
203
212
 
204
213
  } catch (Exception e) {
205
- Log.e(TAG, "Error opening port via SerialPort plugin: " + e.getMessage());
214
+ Log.e(TAG, "Error opening port: " + e.getMessage());
206
215
  isConnected = false;
207
216
  return false;
208
217
  }
209
218
  }
210
219
 
211
220
  /**
212
- * Setup data listener for SerialPort plugin
213
- */
214
- private void setupSerialDataListener() {
215
- // The SerialPort plugin will emit data events that we can capture
216
- // For now, we'll rely on the existing data flow mechanism
217
- Log.d(TAG, "Serial data listener setup completed");
218
- }
219
-
220
- /**
221
- * Close serial port using SerialPort plugin
221
+ * Close serial port using SerialPort interface
222
222
  */
223
223
  public void closeSerialPort() {
224
224
  if (!isConnected) return;
225
225
 
226
+ if (serialPortInterface == null) {
227
+ Log.e(TAG, "SerialPort interface not set");
228
+ return;
229
+ }
230
+
226
231
  try {
227
- Log.d(TAG, "Closing serial port via SerialPort plugin");
228
-
229
232
  CompletableFuture<Boolean> future = new CompletableFuture<>();
230
233
 
231
- PluginCall mockCall = new PluginCall() {
234
+ serialPortInterface.closePort(new SerialPortCallback() {
232
235
  @Override
233
- public void resolve(JSObject result) {
236
+ public void onSuccess(JSObject result) {
234
237
  isConnected = false;
235
238
  currentPortId = null;
236
239
  Log.d(TAG, "SerialPort closed successfully");
@@ -238,153 +241,83 @@ public class IOBoardManager {
238
241
  }
239
242
 
240
243
  @Override
241
- public void reject(String message) {
244
+ public void onError(String message) {
242
245
  Log.e(TAG, "Failed to close SerialPort: " + message);
243
246
  future.complete(false);
244
247
  }
245
- };
248
+ });
246
249
 
247
- serialPortPlugin.closePort(mockCall);
248
250
  future.get(2, TimeUnit.SECONDS);
249
251
 
250
252
  } catch (Exception e) {
251
- Log.e(TAG, "Error closing port via SerialPort plugin: " + e.getMessage());
253
+ Log.e(TAG, "Error closing port: " + e.getMessage());
252
254
  }
253
255
  }
254
256
 
255
257
  /**
256
- * Send command and wait for response using SerialPort plugin
258
+ * Send command and wait for response using SerialPort interface
257
259
  */
258
- public CompletableFuture<byte[]> sendCommandAndWaitResponse(byte[] command, int timeoutMs) {
259
- CompletableFuture<byte[]> future = new CompletableFuture<>();
260
-
260
+ private String sendCommandAndWaitResponse(byte[] command, int timeoutMs) throws Exception {
261
261
  if (!isConnected) {
262
- future.completeExceptionally(new IOException("Port is not connected"));
263
- return future;
262
+ throw new Exception("Serial port not connected");
264
263
  }
265
264
 
266
- try {
267
- Log.d(TAG, "Sending command: " + IOBoardProtocolUtils.bytesToHex(command));
268
-
269
- // Send command using SerialPort plugin
270
- JSObject writeParams = new JSObject();
271
- String hexData = IOBoardProtocolUtils.bytesToHex(command);
272
- writeParams.put("data", hexData);
273
-
274
- PluginCall writeCall = new PluginCall() {
275
- @Override
276
- public void resolve(JSObject result) {
277
- boolean success = result.optBoolean("success", false);
278
- if (success) {
279
- Log.d(TAG, "Command sent successfully via SerialPort");
280
- // Start waiting for response
281
- waitForResponse(future, timeoutMs);
282
- } else {
283
- future.completeExceptionally(new IOException("Failed to send command"));
284
- }
285
- }
286
-
287
- @Override
288
- public void reject(String message) {
289
- Log.e(TAG, "Failed to send data: " + message);
290
- future.completeExceptionally(new IOException("Send failed: " + message));
291
- }
292
-
293
- @Override
294
- public String getString(String key) {
295
- return writeParams.optString(key);
296
- }
297
- };
298
-
299
- serialPortPlugin.writeData(writeCall);
300
-
301
- } catch (Exception e) {
302
- Log.e(TAG, "Error sending command: " + e.getMessage());
303
- future.completeExceptionally(e);
265
+ if (serialPortInterface == null) {
266
+ throw new Exception("SerialPort interface not set");
304
267
  }
305
268
 
306
- return future;
307
- }
308
-
309
- /**
310
- * Helper method for backward compatibility - returns String response
311
- */
312
- private String sendCommandAndWaitResponse(byte[] command, int timeoutMs) throws Exception {
313
- CompletableFuture<byte[]> future = sendCommandAndWaitResponse(command, timeoutMs);
314
- byte[] responseData = future.get(timeoutMs + 1000, TimeUnit.MILLISECONDS);
315
- return new String(responseData, "ISO-8859-1");
316
- }
317
-
318
- /**
319
- * Wait for response from SerialPort
320
- */
321
- private void waitForResponse(CompletableFuture<byte[]> future, int timeoutMs) {
322
- // Schedule timeout
323
- ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
324
- scheduler.schedule(() -> {
325
- if (!future.isDone()) {
326
- future.completeExceptionally(new IOException("Response timeout after " + timeoutMs + "ms"));
327
- }
328
- }, timeoutMs, TimeUnit.MILLISECONDS);
329
-
330
- // Set up listener for incoming data
331
- // Note: This would typically be set up through the SerialPort plugin's data events
332
- // For now, we'll use a simplified approach
333
- setupResponseListener(future, scheduler);
334
- }
335
-
336
- /**
337
- * Setup response listener (simplified for SerialPort integration)
338
- */
339
- private void setupResponseListener(CompletableFuture<byte[]> future, ScheduledExecutorService scheduler) {
340
- // This would be implemented to listen for data events from SerialPort plugin
341
- // For now, we'll implement a polling mechanism
342
- new Thread(() -> {
343
- try {
344
- Thread.sleep(100); // Wait for potential response
345
-
346
- // Try to read response using SerialPort plugin
347
- JSObject readParams = new JSObject();
348
- readParams.put("timeout", 1000);
349
-
350
- PluginCall readCall = new PluginCall() {
351
- @Override
352
- public void resolve(JSObject result) {
353
- try {
269
+ Log.d(TAG, "Sending command: " + IOBoardProtocolUtils.frameToHex(command));
270
+
271
+ CompletableFuture<String> future = new CompletableFuture<>();
272
+
273
+ // Send command
274
+ serialPortInterface.sendData(command, new SerialPortCallback() {
275
+ @Override
276
+ public void onSuccess(JSObject result) {
277
+ boolean success = result.optBoolean("success", false);
278
+ if (success) {
279
+ Log.d(TAG, "Command sent successfully");
280
+
281
+ // Now wait for response
282
+ serialPortInterface.readData(timeoutMs, new SerialPortCallback() {
283
+ @Override
284
+ public void onSuccess(JSObject result) {
354
285
  String dataHex = result.optString("data", "");
355
286
  if (!dataHex.isEmpty()) {
287
+ // Convert hex to byte array and then to string
356
288
  byte[] responseData = IOBoardProtocolUtils.hexToBytes(dataHex);
357
- Log.d(TAG, "Received response: " + IOBoardProtocolUtils.bytesToHex(responseData));
358
- future.complete(responseData);
289
+ String response = new String(responseData, java.nio.charset.StandardCharsets.ISO_8859_1);
290
+ Log.d(TAG, "Received response: " + dataHex);
291
+ future.complete(response);
359
292
  } else {
360
- future.completeExceptionally(new IOException("No response data received"));
293
+ future.completeExceptionally(new Exception("No response data received"));
361
294
  }
362
- } catch (Exception e) {
363
- future.completeExceptionally(e);
364
- } finally {
365
- scheduler.shutdown();
366
295
  }
367
- }
368
-
369
- @Override
370
- public void reject(String message) {
371
- future.completeExceptionally(new IOException("Read failed: " + message));
372
- scheduler.shutdown();
373
- }
374
-
375
- @Override
376
- public int getInt(String key, int defaultValue) {
377
- return readParams.optInt(key, defaultValue);
378
- }
379
- };
380
-
381
- serialPortPlugin.readData(readCall);
382
-
383
- } catch (Exception e) {
384
- future.completeExceptionally(e);
385
- scheduler.shutdown();
296
+
297
+ @Override
298
+ public void onError(String message) {
299
+ future.completeExceptionally(new Exception("Read failed: " + message));
300
+ }
301
+ });
302
+ } else {
303
+ future.completeExceptionally(new Exception("Failed to send command"));
304
+ }
305
+ }
306
+
307
+ @Override
308
+ public void onError(String message) {
309
+ future.completeExceptionally(new Exception("Send failed: " + message));
386
310
  }
387
- }).start();
311
+ });
312
+
313
+ return future.get(timeoutMs + 2000, TimeUnit.MILLISECONDS);
314
+ }
315
+
316
+ /**
317
+ * Check if serial port is connected
318
+ */
319
+ public boolean isConnected() {
320
+ return isConnected;
388
321
  }
389
322
 
390
323
  // New simplified API methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonardojc/capacitor-ioboard",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "A Capacitor plugin for controlling custom IOBOARD devices via RS485 serial communication with full native protocol implementation",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -76,7 +76,8 @@
76
76
  "typescript": "~5.2.2"
77
77
  },
78
78
  "peerDependencies": {
79
- "@capacitor/core": "^7.0.0"
79
+ "@capacitor/core": "^7.0.0",
80
+ "@leonardojc/capacitor-serial-port": "^1.1.7"
80
81
  },
81
82
  "prettier": "@ionic/prettier-config",
82
83
  "swiftlint": "@ionic/swiftlint-config",