@digipair/skill-serialport 0.131.8

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.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # skill-serialport
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build skill-serialport` to build the library.
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ var serialport = require('serialport');
4
+
5
+ let SerialPortService = class SerialPortService {
6
+ sleep(ms) {
7
+ return new Promise((resolve)=>setTimeout(resolve, ms));
8
+ }
9
+ async read(ser, timeout) {
10
+ return new Promise((resolve)=>{
11
+ const buffer = [];
12
+ const instance = setTimeout(()=>{
13
+ ser.removeListener('data', onData);
14
+ resolve(buffer);
15
+ }, timeout);
16
+ const onData = (chunk)=>{
17
+ for (const byte of chunk){
18
+ buffer.push(byte);
19
+ }
20
+ if (buffer.length >= 8) {
21
+ clearTimeout(instance);
22
+ ser.removeListener('data', onData);
23
+ resolve(buffer);
24
+ }
25
+ };
26
+ ser.on('data', onData);
27
+ });
28
+ }
29
+ async command(params, _pinsSettingsList, context) {
30
+ const { data, sleep = 1000, timeout = 3000, config = context.privates.SERIALPORT_CONFIG } = params;
31
+ const command = Buffer.from(data);
32
+ return new Promise((resolve, reject)=>{
33
+ const ser = new serialport.SerialPort(config);
34
+ ser.on('error', (err)=>{
35
+ reject(err);
36
+ });
37
+ ser.on('open', async ()=>{
38
+ await ser.write(command);
39
+ await this.sleep(sleep);
40
+ const reponse = await this.read(ser, timeout);
41
+ ser.close();
42
+ resolve(reponse);
43
+ });
44
+ });
45
+ }
46
+ async write(params, _pinsSettingsList, context) {
47
+ const { data, sleep = 1000, config = context.privates.SERIALPORT_CONFIG } = params;
48
+ const command = Buffer.from(data);
49
+ return new Promise((resolve, reject)=>{
50
+ const ser = new serialport.SerialPort(config);
51
+ ser.on('error', (err)=>{
52
+ reject(err);
53
+ });
54
+ ser.on('open', async ()=>{
55
+ await ser.write(command);
56
+ await this.sleep(sleep);
57
+ ser.close();
58
+ resolve({});
59
+ });
60
+ });
61
+ }
62
+ };
63
+ // Export helpers
64
+ const write = (params, pinsSettingsList, context)=>new SerialPortService().write(params, pinsSettingsList, context);
65
+ const command = (params, pinsSettingsList, context)=>new SerialPortService().command(params, pinsSettingsList, context);
66
+
67
+ exports.command = command;
68
+ exports.write = write;
@@ -0,0 +1 @@
1
+ export * from "./src/index";