-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBleCharacteristicCache.js
More file actions
203 lines (172 loc) · 6.01 KB
/
BleCharacteristicCache.js
File metadata and controls
203 lines (172 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import * as uuIds from "../constants/uuids";
let cache = {};
export function cacheContainsCharacteristic(characteristicId) {
const characteristic = cache[characteristicId];
if (characteristic) {
return true;
}
return false;
}
export function clearCache() {
cache = {};
}
export function getCharacteristic(characteristicId) {
const characteristic = cache[characteristicId];
if (!characteristic) {
throw new Error("Characteristic not found in cache");
}
return characteristic;
}
function writeCharacteristicToCache(characteristic, characteristicId) {
cache[characteristicId] = characteristic;
}
export async function buildCacheFromBleDevice(bleDevice, gattRetryCount = 0) {
try {
clearCache();
writeCharacteristicToCache(bleDevice, uuIds.bleDeviceUuid);
const bleServer = await bleDevice.gatt.connect();
const primaryServiceUuidVolcano1 = await bleServer.getPrimaryService(
uuIds.primaryServiceUuidVolcano1
);
writeCharacteristicToCache(
primaryServiceUuidVolcano1,
uuIds.primaryServiceUuidVolcano1
);
const primaryServiceUuidVolcano2 = await bleServer.getPrimaryService(
uuIds.primaryServiceUuidVolcano2
);
writeCharacteristicToCache(
primaryServiceUuidVolcano2,
uuIds.primaryServiceUuidVolcano2
);
const primaryServiceUuidVolcano3 = await bleServer.getPrimaryService(
uuIds.primaryServiceUuidVolcano3
);
writeCharacteristicToCache(
primaryServiceUuidVolcano3,
uuIds.primaryServiceUuidVolcano3
);
const primaryServiceUuidVolcano4 = await bleServer.getPrimaryService(
uuIds.primaryServiceUuidVolcano4
);
writeCharacteristicToCache(
primaryServiceUuidVolcano4,
uuIds.primaryServiceUuidVolcano4
);
const heatOffCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(uuIds.heatOffUuid);
writeCharacteristicToCache(heatOffCharacteristic, uuIds.heatOffUuid);
const heatOnCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(uuIds.heatOnUuid);
writeCharacteristicToCache(heatOnCharacteristic, uuIds.heatOnUuid);
const fanOffCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(uuIds.fanOffUuid);
writeCharacteristicToCache(fanOffCharacteristic, uuIds.fanOffUuid);
const fanOnCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(uuIds.fanOnUuid);
writeCharacteristicToCache(fanOnCharacteristic, uuIds.fanOnUuid);
const hoursOfOperationCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.hoursOfOperationUuid
);
writeCharacteristicToCache(
hoursOfOperationCharacteristic,
uuIds.hoursOfOperationUuid
);
const minutesOfOperationCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.minutesOfOperationUuid
);
writeCharacteristicToCache(
minutesOfOperationCharacteristic,
uuIds.minutesOfOperationUuid
);
const bleFirmwareVersionCharacteristic =
await primaryServiceUuidVolcano3.getCharacteristic(
uuIds.bleFirmwareVersionUuid
);
writeCharacteristicToCache(
bleFirmwareVersionCharacteristic,
uuIds.bleFirmwareVersionUuid
);
const register2Characteristic =
await primaryServiceUuidVolcano3.getCharacteristic(uuIds.register2Uuid);
writeCharacteristicToCache(register2Characteristic, uuIds.register2Uuid);
const register1Characteristic =
await primaryServiceUuidVolcano3.getCharacteristic(uuIds.register1Uuid);
writeCharacteristicToCache(register1Characteristic, uuIds.register1Uuid);
const serialNumberCharacteristic =
await primaryServiceUuidVolcano3.getCharacteristic(
uuIds.serialNumberUuid
);
writeCharacteristicToCache(
serialNumberCharacteristic,
uuIds.serialNumberUuid
);
const volcanoFirmwareVersionCharacteristic =
await primaryServiceUuidVolcano3.getCharacteristic(
uuIds.volcanoFirmwareVersionUuid
);
writeCharacteristicToCache(
volcanoFirmwareVersionCharacteristic,
uuIds.volcanoFirmwareVersionUuid
);
const currentTemperatureCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.currentTemperatureUuid
);
writeCharacteristicToCache(
currentTemperatureCharacteristic,
uuIds.currentTemperatureUuid
);
const writeTemperatureCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.writeTemperatureUuid
);
writeCharacteristicToCache(
writeTemperatureCharacteristic,
uuIds.writeTemperatureUuid
);
const autoOffCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(uuIds.autoShutoffUuid);
writeCharacteristicToCache(autoOffCharacteristic, uuIds.autoShutoffUuid);
const register3 = await primaryServiceUuidVolcano3.getCharacteristic(
uuIds.register3Uuid
);
writeCharacteristicToCache(register3, uuIds.register3Uuid);
const LEDbrightnessCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.LEDbrightnessUuid
);
writeCharacteristicToCache(
LEDbrightnessCharacteristic,
uuIds.LEDbrightnessUuid
);
const autoShutoffSettingCharacteristic =
await primaryServiceUuidVolcano4.getCharacteristic(
uuIds.autoShutoffSettingUuid
);
writeCharacteristicToCache(
autoShutoffSettingCharacteristic,
uuIds.autoShutoffSettingUuid
);
return "Cache Built!";
} catch (error) {
console.warn(error);
console.warn(
`Error while building BLE caching on attempt #${
gattRetryCount + 1
}... Trying to establish cache again`
);
if (gattRetryCount < 3) {
await bleDevice.gatt.disconnect();
await buildCacheFromBleDevice(bleDevice, gattRetryCount + 1);
} else {
throw new Error(
`Could not establish Ble connection after ${
gattRetryCount + 1
} attemps. Refresh your browser and try again`
);
}
}
}