@robdobsn/raftjs 1.1.1
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/.editorconfig +14 -0
- package/.gitattributes +11 -0
- package/.nvmrc +1 -0
- package/LICENSE +22 -0
- package/README.md +11 -0
- package/TODO.md +1 -0
- package/dist/RaftAttributeHandler.d.ts +12 -0
- package/dist/RaftAttributeHandler.js +241 -0
- package/dist/RaftAttributeHandler.js.map +1 -0
- package/dist/RaftChannel.d.ts +18 -0
- package/dist/RaftChannel.js +12 -0
- package/dist/RaftChannel.js.map +1 -0
- package/dist/RaftChannelWebBLE.d.ts +38 -0
- package/dist/RaftChannelWebBLE.js +274 -0
- package/dist/RaftChannelWebBLE.js.map +1 -0
- package/dist/RaftChannelWebSerial.d.ts +37 -0
- package/dist/RaftChannelWebSerial.js +319 -0
- package/dist/RaftChannelWebSerial.js.map +1 -0
- package/dist/RaftChannelWebSocket.d.ts +28 -0
- package/dist/RaftChannelWebSocket.js +197 -0
- package/dist/RaftChannelWebSocket.js.map +1 -0
- package/dist/RaftCommsStats.d.ts +39 -0
- package/dist/RaftCommsStats.js +128 -0
- package/dist/RaftCommsStats.js.map +1 -0
- package/dist/RaftConnEvents.d.ts +31 -0
- package/dist/RaftConnEvents.js +44 -0
- package/dist/RaftConnEvents.js.map +1 -0
- package/dist/RaftConnector.d.ts +242 -0
- package/dist/RaftConnector.js +613 -0
- package/dist/RaftConnector.js.map +1 -0
- package/dist/RaftCustomAttrHandler.d.ts +4 -0
- package/dist/RaftCustomAttrHandler.js +50 -0
- package/dist/RaftCustomAttrHandler.js.map +1 -0
- package/dist/RaftDeviceInfo.d.ts +64 -0
- package/dist/RaftDeviceInfo.js +36 -0
- package/dist/RaftDeviceInfo.js.map +1 -0
- package/dist/RaftDeviceManager.d.ts +37 -0
- package/dist/RaftDeviceManager.js +450 -0
- package/dist/RaftDeviceManager.js.map +1 -0
- package/dist/RaftDeviceMsg.d.ts +9 -0
- package/dist/RaftDeviceMsg.js +11 -0
- package/dist/RaftDeviceMsg.js.map +1 -0
- package/dist/RaftDeviceStates.d.ts +33 -0
- package/dist/RaftDeviceStates.js +60 -0
- package/dist/RaftDeviceStates.js.map +1 -0
- package/dist/RaftFileHandler.d.ts +52 -0
- package/dist/RaftFileHandler.js +502 -0
- package/dist/RaftFileHandler.js.map +1 -0
- package/dist/RaftLog.d.ts +22 -0
- package/dist/RaftLog.js +63 -0
- package/dist/RaftLog.js.map +1 -0
- package/dist/RaftMiniHDLC.d.ts +18 -0
- package/dist/RaftMiniHDLC.js +383 -0
- package/dist/RaftMiniHDLC.js.map +1 -0
- package/dist/RaftMsgHandler.d.ts +57 -0
- package/dist/RaftMsgHandler.js +480 -0
- package/dist/RaftMsgHandler.js.map +1 -0
- package/dist/RaftMsgTrackInfo.d.ts +17 -0
- package/dist/RaftMsgTrackInfo.js +42 -0
- package/dist/RaftMsgTrackInfo.js.map +1 -0
- package/dist/RaftProtocolDefs.d.ts +30 -0
- package/dist/RaftProtocolDefs.js +48 -0
- package/dist/RaftProtocolDefs.js.map +1 -0
- package/dist/RaftStreamHandler.d.ts +38 -0
- package/dist/RaftStreamHandler.js +257 -0
- package/dist/RaftStreamHandler.js.map +1 -0
- package/dist/RaftSystemType.d.ts +21 -0
- package/dist/RaftSystemType.js +3 -0
- package/dist/RaftSystemType.js.map +1 -0
- package/dist/RaftSystemUtils.d.ts +136 -0
- package/dist/RaftSystemUtils.js +410 -0
- package/dist/RaftSystemUtils.js.map +1 -0
- package/dist/RaftTypes.d.ts +184 -0
- package/dist/RaftTypes.js +157 -0
- package/dist/RaftTypes.js.map +1 -0
- package/dist/RaftUpdateEvents.d.ts +33 -0
- package/dist/RaftUpdateEvents.js +46 -0
- package/dist/RaftUpdateEvents.js.map +1 -0
- package/dist/RaftUpdateManager.d.ts +61 -0
- package/dist/RaftUpdateManager.js +618 -0
- package/dist/RaftUpdateManager.js.map +1 -0
- package/dist/RaftUtils.d.ts +125 -0
- package/dist/RaftUtils.js +454 -0
- package/dist/RaftUtils.js.map +1 -0
- package/dist/RaftWifiTypes.d.ts +23 -0
- package/dist/RaftWifiTypes.js +43 -0
- package/dist/RaftWifiTypes.js.map +1 -0
- package/dist/TestDataGen.d.ts +7 -0
- package/dist/TestDataGen.js +133 -0
- package/dist/TestDataGen.js.map +1 -0
- package/dist/main.d.ts +18 -0
- package/dist/main.js +42 -0
- package/dist/main.js.map +1 -0
- package/eslint.config.mjs +33 -0
- package/examples/dashboard/package.json +39 -0
- package/examples/dashboard/src/ConnManager.ts +86 -0
- package/examples/dashboard/src/Main.tsx +100 -0
- package/examples/dashboard/src/StatusScreen.tsx +72 -0
- package/examples/dashboard/src/SystemTypeCog/CogStateInfo.ts +144 -0
- package/examples/dashboard/src/SystemTypeCog/SystemTypeCog.ts +77 -0
- package/examples/dashboard/src/SystemTypeMarty/RICAddOn.ts +70 -0
- package/examples/dashboard/src/SystemTypeMarty/RICAddOnBase.ts +33 -0
- package/examples/dashboard/src/SystemTypeMarty/RICAddOnManager.ts +342 -0
- package/examples/dashboard/src/SystemTypeMarty/RICCommsStats.ts +170 -0
- package/examples/dashboard/src/SystemTypeMarty/RICHWElem.ts +123 -0
- package/examples/dashboard/src/SystemTypeMarty/RICLEDPatternChecker.ts +207 -0
- package/examples/dashboard/src/SystemTypeMarty/RICROSSerial.ts +464 -0
- package/examples/dashboard/src/SystemTypeMarty/RICServoFaultDetector.ts +146 -0
- package/examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts +32 -0
- package/examples/dashboard/src/SystemTypeMarty/RICSystemUtils.ts +371 -0
- package/examples/dashboard/src/SystemTypeMarty/RICTypes.ts +20 -0
- package/examples/dashboard/src/SystemTypeMarty/SystemTypeMarty.ts +113 -0
- package/examples/dashboard/src/index.html +15 -0
- package/examples/dashboard/src/index.tsx +15 -0
- package/examples/dashboard/src/styles.css +122 -0
- package/examples/dashboard/tsconfig.json +18 -0
- package/jest.config.js +11 -0
- package/package.json +50 -0
- package/src/RaftAttributeHandler.ts +289 -0
- package/src/RaftChannel.ts +30 -0
- package/src/RaftChannelWebBLE.ts +342 -0
- package/src/RaftChannelWebSerial.ts +408 -0
- package/src/RaftChannelWebSocket.ts +245 -0
- package/src/RaftCommsStats.ts +142 -0
- package/src/RaftConnEvents.ts +46 -0
- package/src/RaftConnector.ts +745 -0
- package/src/RaftCustomAttrHandler.ts +54 -0
- package/src/RaftDeviceInfo.ts +104 -0
- package/src/RaftDeviceManager.ts +542 -0
- package/src/RaftDeviceMsg.ts +20 -0
- package/src/RaftDeviceStates.ts +89 -0
- package/src/RaftFileHandler.ts +668 -0
- package/src/RaftLog.ts +70 -0
- package/src/RaftMiniHDLC.ts +396 -0
- package/src/RaftMsgHandler.ts +778 -0
- package/src/RaftMsgTrackInfo.ts +51 -0
- package/src/RaftProtocolDefs.ts +46 -0
- package/src/RaftStreamHandler.ts +328 -0
- package/src/RaftSystemType.ts +25 -0
- package/src/RaftSystemUtils.ts +487 -0
- package/src/RaftTypes.ts +250 -0
- package/src/RaftUpdateEvents.ts +48 -0
- package/src/RaftUpdateManager.ts +778 -0
- package/src/RaftUtils.ts +484 -0
- package/src/RaftWifiTypes.ts +36 -0
- package/src/TestDataGen.ts +157 -0
- package/src/main.ts +28 -0
- package/testdata/TestDeviceTypeRecs.json +492 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// RICJS
|
|
4
|
+
// Communications Library
|
|
5
|
+
//
|
|
6
|
+
// Rob Dobson & Chris Greening 2020-2022
|
|
7
|
+
// (C) 2020-2022
|
|
8
|
+
//
|
|
9
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
10
|
+
|
|
11
|
+
export default class RICCommsStats {
|
|
12
|
+
|
|
13
|
+
_msgSmartServos = 0;
|
|
14
|
+
_msgIMU = 0;
|
|
15
|
+
_msgPowerStatus = 0;
|
|
16
|
+
_msgAddOnPub = 0;
|
|
17
|
+
_msgRobotStatus = 0;
|
|
18
|
+
|
|
19
|
+
_msgSmartServosPS = 0;
|
|
20
|
+
_msgIMUPS = 0;
|
|
21
|
+
_msgPowerStatusPS = 0;
|
|
22
|
+
_msgAddOnPubPS = 0;
|
|
23
|
+
_msgRobotStatusPS = 0;
|
|
24
|
+
|
|
25
|
+
_msgSmartServosCountInWindow = 0;
|
|
26
|
+
_msgIMUCountInWindow = 0;
|
|
27
|
+
_msgPowerStatusCountInWindow = 0;
|
|
28
|
+
_msgAddOnPubCountInWindow = 0;
|
|
29
|
+
_msgRobotStatusCountInWindow = 0;
|
|
30
|
+
|
|
31
|
+
_msgSmartServosLastCalcMs = 0;
|
|
32
|
+
_msgIMULastCalcMs = 0;
|
|
33
|
+
_msgPowerStatusLastCalcMs = 0;
|
|
34
|
+
_msgAddOnPubLastCalcMs = 0;
|
|
35
|
+
_msgRobotStatusLastCalcMs = 0;
|
|
36
|
+
|
|
37
|
+
_msgOtherTopic = 0;
|
|
38
|
+
|
|
39
|
+
_rosSerialRxRate = 0;
|
|
40
|
+
_rosSerialRxTotalBytes = 0;
|
|
41
|
+
_rosSerialRxTotalTimeMs = 0;
|
|
42
|
+
|
|
43
|
+
clear() {
|
|
44
|
+
this._msgSmartServos = 0;
|
|
45
|
+
this._msgIMU = 0;
|
|
46
|
+
this._msgPowerStatus = 0;
|
|
47
|
+
this._msgAddOnPub = 0;
|
|
48
|
+
this._msgRobotStatus = 0;
|
|
49
|
+
this._msgSmartServosPS = 0;
|
|
50
|
+
this._msgIMUPS = 0;
|
|
51
|
+
this._msgPowerStatusPS = 0;
|
|
52
|
+
this._msgAddOnPubPS = 0;
|
|
53
|
+
this._msgRobotStatusPS = 0;
|
|
54
|
+
this._msgSmartServosCountInWindow = 0;
|
|
55
|
+
this._msgIMUCountInWindow = 0;
|
|
56
|
+
this._msgPowerStatusCountInWindow = 0;
|
|
57
|
+
this._msgAddOnPubCountInWindow = 0;
|
|
58
|
+
this._msgRobotStatusCountInWindow = 0;
|
|
59
|
+
this._msgSmartServosLastCalcMs = Date.now();
|
|
60
|
+
this._msgIMULastCalcMs = Date.now();
|
|
61
|
+
this._msgPowerStatusLastCalcMs = Date.now();
|
|
62
|
+
this._msgAddOnPubLastCalcMs = Date.now();
|
|
63
|
+
this._msgRobotStatusLastCalcMs = Date.now();
|
|
64
|
+
this._rosSerialRxRate = 0;
|
|
65
|
+
this._rosSerialRxTotalBytes = 0;
|
|
66
|
+
this._rosSerialRxTotalTimeMs = 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getSmartServosRate(): number {
|
|
70
|
+
if (this._msgSmartServosLastCalcMs + 1000 < Date.now()) {
|
|
71
|
+
this._msgSmartServosPS =
|
|
72
|
+
(1000.0 * this._msgSmartServosCountInWindow) /
|
|
73
|
+
(Date.now() - this._msgSmartServosLastCalcMs);
|
|
74
|
+
this._msgSmartServosLastCalcMs = Date.now();
|
|
75
|
+
this._msgSmartServosCountInWindow = 0;
|
|
76
|
+
}
|
|
77
|
+
return this._msgSmartServosPS;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getIMURate(): number {
|
|
81
|
+
if (this._msgIMULastCalcMs + 1000 < Date.now()) {
|
|
82
|
+
this._msgIMUPS =
|
|
83
|
+
(1000.0 * this._msgIMUCountInWindow) /
|
|
84
|
+
(Date.now() - this._msgIMULastCalcMs);
|
|
85
|
+
this._msgIMULastCalcMs = Date.now();
|
|
86
|
+
this._msgIMUCountInWindow = 0;
|
|
87
|
+
}
|
|
88
|
+
return this._msgIMUPS;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getPowerStatusRate(): number {
|
|
92
|
+
if (this._msgPowerStatusLastCalcMs + 1000 < Date.now()) {
|
|
93
|
+
this._msgPowerStatusPS =
|
|
94
|
+
(1000.0 * this._msgPowerStatusCountInWindow) /
|
|
95
|
+
(Date.now() - this._msgPowerStatusLastCalcMs);
|
|
96
|
+
this._msgPowerStatusLastCalcMs = Date.now();
|
|
97
|
+
this._msgPowerStatusCountInWindow = 0;
|
|
98
|
+
}
|
|
99
|
+
return this._msgPowerStatusPS;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
getAddOnPubRate(): number {
|
|
103
|
+
if (this._msgAddOnPubLastCalcMs + 1000 < Date.now()) {
|
|
104
|
+
this._msgAddOnPubPS =
|
|
105
|
+
(1000.0 * this._msgAddOnPubCountInWindow) /
|
|
106
|
+
(Date.now() - this._msgAddOnPubLastCalcMs);
|
|
107
|
+
this._msgAddOnPubLastCalcMs = Date.now();
|
|
108
|
+
this._msgAddOnPubCountInWindow = 0;
|
|
109
|
+
}
|
|
110
|
+
return this._msgAddOnPubPS;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getRobotStatusRate(): number {
|
|
114
|
+
if (this._msgRobotStatusLastCalcMs + 1000 < Date.now()) {
|
|
115
|
+
this._msgRobotStatusPS =
|
|
116
|
+
(1000.0 * this._msgRobotStatusCountInWindow) /
|
|
117
|
+
(Date.now() - this._msgRobotStatusLastCalcMs);
|
|
118
|
+
this._msgRobotStatusLastCalcMs = Date.now();
|
|
119
|
+
this._msgRobotStatusCountInWindow = 0;
|
|
120
|
+
}
|
|
121
|
+
return this._msgRobotStatusPS;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
getROSSerialRate(): number {
|
|
125
|
+
if (this._rosSerialRxTotalTimeMs > 0) {
|
|
126
|
+
return (
|
|
127
|
+
(1000.0 * this._rosSerialRxTotalBytes) / this._rosSerialRxTotalTimeMs
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
recordSmartServos(): void {
|
|
134
|
+
this._msgSmartServos++;
|
|
135
|
+
this._msgSmartServosCountInWindow++;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
recordIMU(): void {
|
|
139
|
+
this._msgIMU++;
|
|
140
|
+
this._msgIMUCountInWindow++;
|
|
141
|
+
// Don't call msgRx() as double counting msgs with smartServos
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
recordPowerStatus(): void {
|
|
145
|
+
this._msgPowerStatus++;
|
|
146
|
+
this._msgPowerStatusCountInWindow++;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
recordAddOnPub(): void {
|
|
150
|
+
this._msgAddOnPub++;
|
|
151
|
+
this._msgAddOnPubCountInWindow++;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
recordRobotStatus(): void {
|
|
155
|
+
this._msgRobotStatus++;
|
|
156
|
+
this._msgRobotStatusCountInWindow++;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
recordOtherTopic(): void {
|
|
160
|
+
this._msgOtherTopic++;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
updateROSSerialRxRate(frameLen: number, timeMs: number): void {
|
|
164
|
+
if ((frameLen != 0) && (timeMs != 0)) {
|
|
165
|
+
this._rosSerialRxRate = (1000*frameLen) / timeMs;
|
|
166
|
+
this._rosSerialRxTotalTimeMs += timeMs;
|
|
167
|
+
this._rosSerialRxTotalBytes += frameLen;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RICHWElem
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* Information about a hardware element
|
|
6
|
+
*
|
|
7
|
+
* @field name: string - element name
|
|
8
|
+
* @field type: string - element type
|
|
9
|
+
* @field busName: string - name of bus (e.g. I2C) attached to
|
|
10
|
+
* @field addr: string - address of element on the bus
|
|
11
|
+
* @field addrValid: number - 1 if address is valid
|
|
12
|
+
* @field IDNo: string - unique identifier of element
|
|
13
|
+
* @field whoAmI: string - name of element type
|
|
14
|
+
* @field whoAmITypeCode: string - number of element type
|
|
15
|
+
* @field SN: string - Serial number
|
|
16
|
+
* @field versionStr: string - Version
|
|
17
|
+
* @field commsOk: number - 1 if communications OK, 0 if not, -1 if device is invalid
|
|
18
|
+
*/
|
|
19
|
+
export type RICHWElem = {
|
|
20
|
+
name: string;
|
|
21
|
+
type: string;
|
|
22
|
+
busName: string;
|
|
23
|
+
addr: string;
|
|
24
|
+
addrValid: number;
|
|
25
|
+
IDNo: string;
|
|
26
|
+
whoAmI: string;
|
|
27
|
+
whoAmITypeCode: string;
|
|
28
|
+
SN: string;
|
|
29
|
+
versionStr: string;
|
|
30
|
+
commsOk: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export class RICHWElemList {
|
|
34
|
+
req = '';
|
|
35
|
+
rslt = 'ok';
|
|
36
|
+
hw: Array<RICHWElem> = [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Minimum key length version of RICHWElem
|
|
40
|
+
export type RICHWElem_Min = {
|
|
41
|
+
n: string;
|
|
42
|
+
t: string;
|
|
43
|
+
I: string;
|
|
44
|
+
w: string;
|
|
45
|
+
W: string;
|
|
46
|
+
S: string;
|
|
47
|
+
v: string;
|
|
48
|
+
c: number;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export class RICHWElemList_Min {
|
|
52
|
+
// Members
|
|
53
|
+
req = '';
|
|
54
|
+
rslt = 'ok';
|
|
55
|
+
hw: Array<RICHWElem_Min> = [];
|
|
56
|
+
|
|
57
|
+
// Method to convert to RICHWElemList
|
|
58
|
+
static expand(hwMin: RICHWElemList_Min): RICHWElemList {
|
|
59
|
+
const hwList = new RICHWElemList();
|
|
60
|
+
for (const hwElem of hwMin.hw) {
|
|
61
|
+
hwList.hw.push({
|
|
62
|
+
name: hwElem.n,
|
|
63
|
+
type: hwElem.t,
|
|
64
|
+
busName: '',
|
|
65
|
+
addr: '',
|
|
66
|
+
addrValid: 0,
|
|
67
|
+
IDNo: hwElem.I,
|
|
68
|
+
whoAmI: hwElem.w,
|
|
69
|
+
whoAmITypeCode: hwElem.W,
|
|
70
|
+
SN: hwElem.S,
|
|
71
|
+
versionStr: hwElem.v,
|
|
72
|
+
commsOk: hwElem.c,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return hwList;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Single encoded string version of RICHWElem
|
|
80
|
+
export type RICHWElem_Str = {
|
|
81
|
+
a: string;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type RICHWElemList_Name = {
|
|
85
|
+
rslt: string;
|
|
86
|
+
hw: Array<string>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// RICHWElemList containing coded strings for each HWElem field
|
|
90
|
+
export class RICHWElemList_Str {
|
|
91
|
+
req = '';
|
|
92
|
+
rslt = 'ok';
|
|
93
|
+
hw: Array<RICHWElem_Str> = [];
|
|
94
|
+
|
|
95
|
+
// Method to convert to RICHWElemList
|
|
96
|
+
static expand(hwStr: RICHWElemList_Str): RICHWElemList {
|
|
97
|
+
const hwList = new RICHWElemList();
|
|
98
|
+
for (const hwElem of hwStr.hw) {
|
|
99
|
+
if (hwElem.a) {
|
|
100
|
+
const hwElemStr = hwElem.a.split('|');
|
|
101
|
+
hwList.hw.push({
|
|
102
|
+
name: RICHWElemList_Str.unesc(hwElemStr[0]),
|
|
103
|
+
type: RICHWElemList_Str.unesc(hwElemStr[1]),
|
|
104
|
+
busName: "",
|
|
105
|
+
addr: "",
|
|
106
|
+
addrValid: 0,
|
|
107
|
+
IDNo: RICHWElemList_Str.unesc(hwElemStr[2]),
|
|
108
|
+
whoAmI: RICHWElemList_Str.unesc(hwElemStr[3]),
|
|
109
|
+
whoAmITypeCode: RICHWElemList_Str.unesc(hwElemStr[4]),
|
|
110
|
+
SN: RICHWElemList_Str.unesc(hwElemStr[5]),
|
|
111
|
+
versionStr: RICHWElemList_Str.unesc(hwElemStr[6]),
|
|
112
|
+
commsOk: Number(hwElemStr[7]),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return hwList;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Method to unescape a pipe character
|
|
120
|
+
static unesc(s: string): string {
|
|
121
|
+
return s.replace(/\/x7c/g, '|');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
//
|
|
3
|
+
// RICJS
|
|
4
|
+
// Communications Library
|
|
5
|
+
//
|
|
6
|
+
// Rob Dobson & Chris Greening 2020-2022
|
|
7
|
+
// (C) 2020-2022
|
|
8
|
+
//
|
|
9
|
+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
10
|
+
|
|
11
|
+
import { RaftConnEvent, RaftConnEventFn } from '../../../../src/RaftConnEvents';
|
|
12
|
+
import RaftLog from '../../../../src/RaftLog';
|
|
13
|
+
import RaftMsgHandler from '../../../../src/RaftMsgHandler';
|
|
14
|
+
import { RaftOKFail } from '../../../../src/RaftTypes';
|
|
15
|
+
import { RICLEDPatternCheckerColour, RICLedLcdColours } from './RICTypes';
|
|
16
|
+
|
|
17
|
+
export default class RICLEDPatternChecker {
|
|
18
|
+
|
|
19
|
+
// LED pattern refresh
|
|
20
|
+
private _ledPatternTimeoutMs = 10000;
|
|
21
|
+
private _ledPatternRefreshTimer: ReturnType<typeof setTimeout> | null = null;
|
|
22
|
+
|
|
23
|
+
// Verification of correct device
|
|
24
|
+
private _ledColours: Array<string> = new Array<string>();
|
|
25
|
+
private _lcdColours: Array<string> = new Array<string>();
|
|
26
|
+
private _bleVerifActive = false;
|
|
27
|
+
|
|
28
|
+
// Message handler, etc
|
|
29
|
+
private _msgHandler: RaftMsgHandler | null = null;
|
|
30
|
+
private _onConnEvent: RaftConnEventFn | null = null;
|
|
31
|
+
private _isConnectedFn: () => boolean = () => false;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Start checking correct RIC using LED pattern
|
|
35
|
+
*
|
|
36
|
+
* @param {RICLedLcdColours} ricLedLcdColours - available colours
|
|
37
|
+
* @param {RaftMsgHandler} msgHandler - message handler
|
|
38
|
+
* @param {RaftConnEventFn} onConnEvent - connection event handler
|
|
39
|
+
* @param {() => boolean} isConnectedFn - function to check if connected
|
|
40
|
+
* @return boolean - true if started ok
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
async checkCorrectRICStart(ricLedLcdColours: RICLedLcdColours,
|
|
44
|
+
msgHandler: RaftMsgHandler,
|
|
45
|
+
onConnEvent: RaftConnEventFn,
|
|
46
|
+
isConnectedFn: () => boolean): Promise<boolean> {
|
|
47
|
+
|
|
48
|
+
// Store the onConnEvent, etc
|
|
49
|
+
this._msgHandler = msgHandler;
|
|
50
|
+
this._onConnEvent = onConnEvent;
|
|
51
|
+
this._isConnectedFn = isConnectedFn;
|
|
52
|
+
|
|
53
|
+
// Set colour pattern checker colours
|
|
54
|
+
const randomColours = this.setupColours(ricLedLcdColours);
|
|
55
|
+
|
|
56
|
+
// Start timer to repeat checking LED pattern
|
|
57
|
+
RaftLog.debug(`checkCorrectRICStart: starting LED pattern checker`);
|
|
58
|
+
if (!await this._checkCorrectRICRefreshLEDs()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Event
|
|
63
|
+
onConnEvent(RaftConnEvent.CONN_VERIFYING_CORRECT, randomColours);
|
|
64
|
+
|
|
65
|
+
// Start timer to repeat sending of LED pattern
|
|
66
|
+
// This is because RIC's LED pattern override times out after a while
|
|
67
|
+
// so has to be refreshed periodically
|
|
68
|
+
this._ledPatternRefreshTimer = setInterval(async () => {
|
|
69
|
+
RaftLog.verbose(`checkCorrectRICStart: loop`);
|
|
70
|
+
if (!this._checkCorrectRICRefreshLEDs()) {
|
|
71
|
+
RaftLog.debug('checkCorrectRICStart no longer active - clearing timer');
|
|
72
|
+
this._clearLedPatternRefreshTimer();
|
|
73
|
+
}
|
|
74
|
+
}, this._ledPatternTimeoutMs / 2.1);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Stop checking correct RIC
|
|
80
|
+
* @param {boolean} confirmCorrectRIC - true if user confirmed correct RIC
|
|
81
|
+
* @return void
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
async checkCorrectRICStop(confirmCorrectRIC: boolean): Promise<boolean> {
|
|
85
|
+
|
|
86
|
+
// Stop refreshing LED pattern on RIC
|
|
87
|
+
this._clearLedPatternRefreshTimer();
|
|
88
|
+
|
|
89
|
+
// Stop the LED pattern checker if connected
|
|
90
|
+
if (this._isConnectedFn()) {
|
|
91
|
+
await this.clearRICColors();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check correct
|
|
95
|
+
if (!confirmCorrectRIC) {
|
|
96
|
+
// Event
|
|
97
|
+
if (this._onConnEvent)
|
|
98
|
+
this._onConnEvent(RaftConnEvent.CONN_REJECTED);
|
|
99
|
+
// Indicate as rejected if we're not connected or if user didn't confirm
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
// Event
|
|
103
|
+
if (this._onConnEvent)
|
|
104
|
+
this._onConnEvent(RaftConnEvent.CONN_VERIFIED_CORRECT);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Refresh LED pattern on RIC
|
|
110
|
+
*
|
|
111
|
+
* @return boolean - true if checking still active
|
|
112
|
+
*
|
|
113
|
+
*/
|
|
114
|
+
async _checkCorrectRICRefreshLEDs(): Promise<boolean> {
|
|
115
|
+
// Check LED pattern is active
|
|
116
|
+
if (!this.isActive()) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Check connected
|
|
121
|
+
RaftLog.debug(`_verificationRepeat getting isConnected`);
|
|
122
|
+
if (!this._isConnectedFn()) {
|
|
123
|
+
console.warn('_verificationRepeat not connected');
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Repeat the LED pattern (RIC times out the LED override after ~10 seconds)
|
|
128
|
+
RaftLog.debug(`_verificationRepeat setting pattern`);
|
|
129
|
+
return await this.setRICColors();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
_clearLedPatternRefreshTimer(): void {
|
|
133
|
+
if (this._ledPatternRefreshTimer) {
|
|
134
|
+
clearInterval(this._ledPatternRefreshTimer);
|
|
135
|
+
this._ledPatternRefreshTimer = null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
isActive(): boolean {
|
|
140
|
+
return this._bleVerifActive;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
clear(): void {
|
|
144
|
+
this._bleVerifActive = false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setupColours(availableColors: RICLEDPatternCheckerColour[]): string[] {
|
|
148
|
+
|
|
149
|
+
// Check length of available colours
|
|
150
|
+
if (availableColors.length == 0) {
|
|
151
|
+
RaftLog.warn('start no available colours');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Random colour selection
|
|
155
|
+
const LED_1 =
|
|
156
|
+
availableColors[Math.floor(Math.random() * availableColors.length)];
|
|
157
|
+
const LED_2 =
|
|
158
|
+
availableColors[Math.floor(Math.random() * availableColors.length)];
|
|
159
|
+
const LED_3 =
|
|
160
|
+
availableColors[Math.floor(Math.random() * availableColors.length)];
|
|
161
|
+
|
|
162
|
+
// LED and LCD colours are different to attempt to be visually similar
|
|
163
|
+
this._ledColours = [LED_1.led, LED_2.led, LED_3.led];
|
|
164
|
+
this._lcdColours = [LED_1.lcd, LED_2.lcd, LED_3.lcd];
|
|
165
|
+
|
|
166
|
+
// Set the colours to display on LEDs
|
|
167
|
+
this._bleVerifActive = true;
|
|
168
|
+
|
|
169
|
+
// Return LCD colours to display
|
|
170
|
+
return this._lcdColours;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async setRICColors(): Promise<boolean> {
|
|
174
|
+
// Set bonding colours
|
|
175
|
+
let colourSetStr = '';
|
|
176
|
+
for (let i = 0; i < this._ledColours.length; i++) {
|
|
177
|
+
if (i != 0) {
|
|
178
|
+
colourSetStr += '&';
|
|
179
|
+
}
|
|
180
|
+
let colr = this._ledColours[i];
|
|
181
|
+
if (colr.startsWith('#')) colr = colr.slice(1);
|
|
182
|
+
colourSetStr += `c${i}=${colr}`;
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
RaftLog.debug('setRICColors setting colours');
|
|
186
|
+
if (this._msgHandler) {
|
|
187
|
+
await this._msgHandler.sendRICRESTURL<RaftOKFail>(`indicator/set?${colourSetStr}&ms=${this._ledPatternTimeoutMs}`);
|
|
188
|
+
}
|
|
189
|
+
} catch (error) {
|
|
190
|
+
RaftLog.debug(`setRICColors failed to send ${error}`);
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async clearRICColors(): Promise<void> {
|
|
197
|
+
// Clear the LED colours
|
|
198
|
+
RaftLog.debug('clearRICColors');
|
|
199
|
+
try {
|
|
200
|
+
if (this._msgHandler) {
|
|
201
|
+
await this._msgHandler.sendRICRESTURL<RaftOKFail>(`indicator/resume`);
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
RaftLog.debug(`clearRICColors failed to send ${error}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|