@leonardojc/capacitor-ioboard 1.2.0 → 1.2.2
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.
|
@@ -2,10 +2,17 @@ package com.leonardojc.capacitor.ioboard;
|
|
|
2
2
|
|
|
3
3
|
import android.util.Log;
|
|
4
4
|
import android.util.Base64;
|
|
5
|
+
import android.os.Handler;
|
|
6
|
+
import android.os.Looper;
|
|
5
7
|
import java.util.Arrays;
|
|
6
8
|
import java.util.ArrayList;
|
|
7
9
|
import java.util.List;
|
|
8
10
|
import java.util.concurrent.TimeUnit;
|
|
11
|
+
import java.util.concurrent.CountDownLatch;
|
|
12
|
+
import java.io.File;
|
|
13
|
+
import java.io.FileInputStream;
|
|
14
|
+
import java.io.FileOutputStream;
|
|
15
|
+
import java.io.IOException;
|
|
9
16
|
|
|
10
17
|
/**
|
|
11
18
|
* Manager class for IOBOARD communication protocol
|
|
@@ -16,8 +23,15 @@ public class IOBoardManager {
|
|
|
16
23
|
|
|
17
24
|
private static final String TAG = "IOBoardManager";
|
|
18
25
|
|
|
26
|
+
// Serial communication variables
|
|
27
|
+
private FileInputStream serialInputStream;
|
|
28
|
+
private FileOutputStream serialOutputStream;
|
|
29
|
+
private String currentPortPath;
|
|
19
30
|
private boolean isConnected = false;
|
|
20
|
-
private
|
|
31
|
+
private Thread readerThread;
|
|
32
|
+
private Handler mainHandler;
|
|
33
|
+
private String responseBuffer = "";
|
|
34
|
+
private CountDownLatch responseLatch;
|
|
21
35
|
|
|
22
36
|
// Response classes
|
|
23
37
|
public static class IOBoardResponse {
|
|
@@ -122,18 +136,177 @@ public class IOBoardManager {
|
|
|
122
136
|
}
|
|
123
137
|
|
|
124
138
|
/**
|
|
125
|
-
*
|
|
139
|
+
* Constructor - Initialize the manager
|
|
126
140
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
141
|
+
public IOBoardManager() {
|
|
142
|
+
mainHandler = new Handler(Looper.getMainLooper());
|
|
143
|
+
Log.d(TAG, "IOBoardManager initialized");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Open serial port for communication
|
|
148
|
+
*/
|
|
149
|
+
public boolean openSerialPort(String portPath, int baudRate) {
|
|
150
|
+
if (isConnected) {
|
|
151
|
+
Log.w(TAG, "Port already connected. Close current connection first.");
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
// Verificar que el puerto existe
|
|
157
|
+
File portFile = new File(portPath);
|
|
158
|
+
if (!portFile.exists()) {
|
|
159
|
+
Log.e(TAG, "Port " + portPath + " does not exist");
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Intentar abrir streams
|
|
164
|
+
serialInputStream = new FileInputStream(portFile);
|
|
165
|
+
serialOutputStream = new FileOutputStream(portFile);
|
|
166
|
+
|
|
167
|
+
currentPortPath = portPath;
|
|
168
|
+
isConnected = true;
|
|
169
|
+
|
|
170
|
+
// Iniciar hilo de lectura
|
|
171
|
+
startReaderThread();
|
|
172
|
+
|
|
173
|
+
Log.d(TAG, "Port opened successfully: " + portPath + " at " + baudRate + " baud");
|
|
174
|
+
return true;
|
|
175
|
+
|
|
176
|
+
} catch (IOException e) {
|
|
177
|
+
Log.e(TAG, "Error opening port: " + e.getMessage());
|
|
178
|
+
isConnected = false;
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Close serial port
|
|
185
|
+
*/
|
|
186
|
+
public void closeSerialPort() {
|
|
187
|
+
if (!isConnected) return;
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
// Detener hilo de lectura
|
|
191
|
+
stopReaderThread();
|
|
192
|
+
|
|
193
|
+
// Cerrar streams
|
|
194
|
+
if (serialInputStream != null) {
|
|
195
|
+
serialInputStream.close();
|
|
196
|
+
serialInputStream = null;
|
|
197
|
+
}
|
|
198
|
+
if (serialOutputStream != null) {
|
|
199
|
+
serialOutputStream.close();
|
|
200
|
+
serialOutputStream = null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
isConnected = false;
|
|
204
|
+
currentPortPath = null;
|
|
205
|
+
Log.d(TAG, "Port closed successfully");
|
|
206
|
+
|
|
207
|
+
} catch (IOException e) {
|
|
208
|
+
Log.e(TAG, "Error closing port: " + e.getMessage());
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Start reader thread for incoming data
|
|
214
|
+
*/
|
|
215
|
+
private void startReaderThread() {
|
|
216
|
+
if (readerThread != null && readerThread.isAlive()) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
readerThread = new Thread(() -> {
|
|
221
|
+
byte[] buffer = new byte[1024];
|
|
222
|
+
|
|
223
|
+
while (isConnected && !Thread.currentThread().isInterrupted()) {
|
|
224
|
+
try {
|
|
225
|
+
if (serialInputStream.available() > 0) {
|
|
226
|
+
int bytesRead = serialInputStream.read(buffer);
|
|
227
|
+
if (bytesRead > 0) {
|
|
228
|
+
String receivedData = new String(buffer, 0, bytesRead, "ISO-8859-1");
|
|
229
|
+
synchronized (this) {
|
|
230
|
+
responseBuffer += receivedData;
|
|
231
|
+
if (responseLatch != null) {
|
|
232
|
+
responseLatch.countDown();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
Log.d(TAG, "Data received (length): " + bytesRead);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
Thread.sleep(10); // Small delay to prevent excessive CPU usage
|
|
239
|
+
} catch (IOException | InterruptedException e) {
|
|
240
|
+
if (isConnected) {
|
|
241
|
+
Log.e(TAG, "Error in reader thread: " + e.getMessage());
|
|
242
|
+
}
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
134
247
|
|
|
135
|
-
|
|
136
|
-
|
|
248
|
+
readerThread.start();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Stop reader thread
|
|
253
|
+
*/
|
|
254
|
+
private void stopReaderThread() {
|
|
255
|
+
if (readerThread != null && readerThread.isAlive()) {
|
|
256
|
+
readerThread.interrupt();
|
|
257
|
+
try {
|
|
258
|
+
readerThread.join(1000); // Wait up to 1 second
|
|
259
|
+
} catch (InterruptedException e) {
|
|
260
|
+
Thread.currentThread().interrupt();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Send command via serial port and wait for response
|
|
267
|
+
*/
|
|
268
|
+
private String sendCommandAndWaitResponse(byte[] command, int timeoutMs) throws Exception {
|
|
269
|
+
if (!isConnected || serialOutputStream == null) {
|
|
270
|
+
throw new Exception("Serial port not connected");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
String hexCommand = IOBoardProtocolUtils.frameToHex(command);
|
|
274
|
+
Log.d(TAG, "Sending command (HEX): " + hexCommand);
|
|
275
|
+
|
|
276
|
+
synchronized (this) {
|
|
277
|
+
// Clear response buffer
|
|
278
|
+
responseBuffer = "";
|
|
279
|
+
responseLatch = new CountDownLatch(1);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
// Convert command to Latin-1 string and write to serial port
|
|
284
|
+
String commandString = IOBoardProtocolUtils.frameToLatin1String(command);
|
|
285
|
+
byte[] bytes = commandString.getBytes("ISO-8859-1");
|
|
286
|
+
serialOutputStream.write(bytes);
|
|
287
|
+
serialOutputStream.flush();
|
|
288
|
+
|
|
289
|
+
Log.d(TAG, "Command sent, waiting for response...");
|
|
290
|
+
|
|
291
|
+
// Wait for response with timeout
|
|
292
|
+
boolean responseReceived = responseLatch.await(timeoutMs, TimeUnit.MILLISECONDS);
|
|
293
|
+
|
|
294
|
+
if (responseReceived) {
|
|
295
|
+
synchronized (this) {
|
|
296
|
+
String response = responseBuffer;
|
|
297
|
+
Log.d(TAG, "Response received (length): " + response.length());
|
|
298
|
+
return response;
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
throw new Exception("Timeout waiting for response");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
} catch (IOException e) {
|
|
305
|
+
throw new Exception("Error sending command: " + e.getMessage());
|
|
306
|
+
} catch (InterruptedException e) {
|
|
307
|
+
Thread.currentThread().interrupt();
|
|
308
|
+
throw new Exception("Command interrupted");
|
|
309
|
+
}
|
|
137
310
|
}
|
|
138
311
|
|
|
139
312
|
// New simplified API methods
|
|
@@ -145,15 +318,12 @@ public class IOBoardManager {
|
|
|
145
318
|
Log.d(TAG, "Connecting to serial port: " + config.portPath + " at " + config.baudRate + " baud");
|
|
146
319
|
|
|
147
320
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
currentSerialPortId = "mock_port_id";
|
|
155
|
-
|
|
156
|
-
return new IOBoardResponse(true, "Connected to " + config.portPath + " successfully");
|
|
321
|
+
boolean success = openSerialPort(config.portPath, config.baudRate);
|
|
322
|
+
if (success) {
|
|
323
|
+
return new IOBoardResponse(true, "Connected to " + config.portPath + " successfully");
|
|
324
|
+
} else {
|
|
325
|
+
return new IOBoardResponse(false, "Failed to open serial port: " + config.portPath);
|
|
326
|
+
}
|
|
157
327
|
|
|
158
328
|
} catch (Exception e) {
|
|
159
329
|
Log.e(TAG, "Error connecting", e);
|
|
@@ -168,12 +338,7 @@ public class IOBoardManager {
|
|
|
168
338
|
Log.d(TAG, "Disconnecting from serial port");
|
|
169
339
|
|
|
170
340
|
try {
|
|
171
|
-
|
|
172
|
-
// Call SerialPort.closeConnection(currentSerialPortId)
|
|
173
|
-
|
|
174
|
-
isConnected = false;
|
|
175
|
-
currentSerialPortId = null;
|
|
176
|
-
|
|
341
|
+
closeSerialPort();
|
|
177
342
|
return new IOBoardResponse(true, "Disconnected successfully");
|
|
178
343
|
|
|
179
344
|
} catch (Exception e) {
|
|
@@ -656,49 +821,6 @@ public class IOBoardManager {
|
|
|
656
821
|
}
|
|
657
822
|
}
|
|
658
823
|
|
|
659
|
-
/**
|
|
660
|
-
* Open serial connection
|
|
661
|
-
*/
|
|
662
|
-
public IOBoardResponse openConnection(SerialConfig config) {
|
|
663
|
-
Log.d(TAG, "Opening serial connection - Port: " + config.portPath + ", BaudRate: " + config.baudRate);
|
|
664
|
-
|
|
665
|
-
try {
|
|
666
|
-
// TODO: Initialize connection with SerialPort plugin
|
|
667
|
-
// Call SerialPort.openConnection() with config parameters
|
|
668
|
-
// Store the returned port ID for future operations
|
|
669
|
-
|
|
670
|
-
isConnected = true;
|
|
671
|
-
currentSerialPortId = "mock_port_id";
|
|
672
|
-
|
|
673
|
-
return new IOBoardResponse(true, "Serial connection opened successfully");
|
|
674
|
-
|
|
675
|
-
} catch (Exception e) {
|
|
676
|
-
Log.e(TAG, "Error opening connection", e);
|
|
677
|
-
return new IOBoardResponse(false, "Error opening connection: " + e.getMessage());
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Close serial connection
|
|
683
|
-
*/
|
|
684
|
-
public IOBoardResponse closeConnection() {
|
|
685
|
-
Log.d(TAG, "Closing serial connection");
|
|
686
|
-
|
|
687
|
-
try {
|
|
688
|
-
// TODO: Close connection with SerialPort plugin
|
|
689
|
-
// Call SerialPort.closeConnection(currentSerialPortId)
|
|
690
|
-
|
|
691
|
-
isConnected = false;
|
|
692
|
-
currentSerialPortId = null;
|
|
693
|
-
|
|
694
|
-
return new IOBoardResponse(true, "Serial connection closed successfully");
|
|
695
|
-
|
|
696
|
-
} catch (Exception e) {
|
|
697
|
-
Log.e(TAG, "Error closing connection", e);
|
|
698
|
-
return new IOBoardResponse(false, "Error closing connection: " + e.getMessage());
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
|
|
702
824
|
/**
|
|
703
825
|
* Check if serial connection is open
|
|
704
826
|
*/
|
|
@@ -81,6 +81,7 @@ export interface UnlockPalletOptions extends DeviceOptions {
|
|
|
81
81
|
}
|
|
82
82
|
export interface MultiplePalletOptions extends DeviceOptions {
|
|
83
83
|
doorLockMask: number;
|
|
84
|
+
ledColor?: 'red' | 'green' | 'blue' | 'yellow' | 'white' | 'off';
|
|
84
85
|
ledControls?: LEDControlOptions[];
|
|
85
86
|
}
|
|
86
87
|
export interface ScanOptions {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leonardojc/capacitor-ioboard",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
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",
|