-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
513 lines (412 loc) · 13.3 KB
/
index.html
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Web BLE TPMS reader</title>
<style>
:root {
--primaryColor: #1f6ec9;
--successColor: #43ef00;
--warningColor: #e06f0d;
--dangerColor: #ba140b;
--borderColor: #808080;
--backgroundColor: #111;
--textColor: #EEE;
}
body {
margin: 0;
background: #111;
color: #EEE;
overflow: hidden;
font-family: sans-serif;
font-size: 12pt;
}
body * {
font-size: 1em;
}
body > main {
display: flex;
position: absolute;
flex-flow: column;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
#output-container {
flex: 1;
overflow-x: hidden;
overflow-y: auto;
}
#output-container > *:not(#scroll-anchor) {
padding: 0.5em;
}
#output-container * {
overflow-anchor: none;
white-space: pre-wrap;
}
#scroll-anchor {
overflow-anchor: auto;
height: 1px;
}
#output-container > *:nth-child(even) {
background: rgba(255,255,255,0.2);
}
main > nav {
display: flex;
flex-flow: row;
background: var(--textColor);
color: var(--bgColor);
padding: 0.5em;
}
main > nav .sensorOptions {
flex: 1;
}
.inputWrapper {
display: inline-block;
}
.tpmsIdField {
position: relative;
z-index: 0;
}
.tpmsIdField label {
z-index: 1;
position: relative;
width: 1em;
display: inline-block;
margin-left: 0.5em;
}
.tpmsIdField input[type="text"] {
z-index: 0;
border: 0.1em solid gray;
border-radius: 0.25em;
}
.tpmsIdField input[type="text"]:valid {
border-color: var(--primaryColor);
box-shadow: -2em 0em 0em 0em var(--primaryColor);
}
.tpmsIdField input[type="text"]:invalid {
border-color: var(--dangerColor);
box-shadow: -2em 0em 0em 0em var(--dangerColor);
}
.tpmsIdField input[type="text"]:placeholder-shown {
border-color: var(--warningColor);
box-shadow: -2em 0em 0em 0em var(--warningColor);
}
main:not(.scanning) #stopScan {
display: none;
}
main.scanning #startScan {
display: none;
}
@media (max-width: 35em) {
.inputWrapper {
display: flex;
flex-flow: row;
}
.inputWrapper input {
flex: 1;
}
.inputWrapper label {
top: 0.15em;
}
main > nav > button {
padding: 1em;
}
.tpmsIdField input[type="text"]:valid {
border-color: var(--primaryColor);
box-shadow: -1.5em 0em 0em 0em var(--primaryColor);
}
.tpmsIdField input[type="text"]:invalid {
border-color: var(--dangerColor);
box-shadow: -1.5em 0em 0em 0em var(--dangerColor);
}
.tpmsIdField input[type="text"]:placeholder-shown {
border-color: var(--warningColor);
box-shadow: -1.5em 0em 0em 0em var(--warningColor);
}
}
main > nav > button {
color: var(--textColor);
background: var(--primaryColor);
border: 0.1em solid #808080;
border-radius: 0.25em;
cursor: pointer;
}
main > nav > button#stopScan {
color: var(--textColor);
background: var(--warningColor);
border: 0.1em solid #808080;
border-radius: 0.25em;
cursor: pointer;
}
.sensorOptions > * {
margin: 0.25em;
}
main > nav > input + label {
margin-right: 0.25em;
}
</style>
</head>
<body>
<main>
<nav class="controls">
<div class="sensorOptions">
<div class="inputWrapper tpmsIdField">
<label for="tpmsId_frontLeft">↖</label>
<input type="text" id="tpmsId_frontLeft" name="tpmsId_frontLeft" value="11ADA3" placeholder="A1B2C3" size="6" maxlength="6" pattern="^[0-9a-fA-F]{6}$">
</div>
<div class="inputWrapper tpmsIdField">
<label for="tpmsId_frontRight">↗</label>
<input type="text" id="tpmsId_frontRight" name="tpmsId_frontRight" value="21AD18" placeholder="A1B2C3" size="6" maxlength="6" pattern="^[0-9a-fA-F]{6}$">
</div>
<div class="inputWrapper tpmsIdField">
<label for="tpmsId_rearLeft">↙</label>
<input type="text" id="tpmsId_rearLeft" name="tpmsId_rearLeft" value="31A515" placeholder="A1B2C3" size="6" maxlength="6" pattern="^[0-9a-fA-F]{6}$">
</div>
<div class="inputWrapper tpmsIdField">
<label for="tpmsId_rearRight">↘</label>
<input type="text" id="tpmsId_rearRight" name="tpmsId_rearRight" value="41A44B" placeholder="A1B2C3" size="6" maxlength="6" pattern="^[0-9a-fA-F]{6}$">
</div>
</div>
<button id="startScan">Scan</button>
<button id="stopScan">Stop</button>
</nav>
<div id="output-container">
<!-- lets scroll position stick to bottom when content is appended-->
<div id="scroll-anchor"></div>
</div>
</main>
<script>
const SCAN_START_TIMEOUT = 10; // seconds
const SCAN_CHECK_INTERVAL = 500; // milliseconds
let scrollingEnabled = false;
let bleScan;
let scanStartCheck;
let bleScanPreviouslyActive = false;
let bleScanChecker = setInterval(doBleScanCheck, SCAN_CHECK_INTERVAL);
/* 471 471 471 471 471 471 471 471 471 471 471 471 471 471 */
function doBleScanCheck() {
if (typeof bleScan !== "undefined" && bleScan.active) {
document.querySelector("main").classList.add("scanning");
if (!bleScanPreviouslyActive) {
/* oh boy we're scanning */
}
bleScanPreviouslyActive = true;
} else {
document.querySelector("main").classList.remove("scanning");
if (bleScanPreviouslyActive) {
log("BLE scan stopped.");
}
bleScanPreviouslyActive = false;
}
}
function isOverflown(element) {
return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}
function log(dataOrPrefix, data) {
let prefix = "";
let outputData = dataOrPrefix;
if (typeof data !== "undefined") {
prefix = dataOrPrefix;
outputData = data;
}
console.log(outputData);
let outputString = outputData;
if (typeof outputData !== "string") {
try {
outputString = JSON.stringify(outputData, null, 4);
} catch(error) {
outputString = "Could not stringify object " + data.toString() + ", error: " + error.toString();
}
}
if (typeof outputString === "undefined") {
outputString = outputData.toString();
}
outputString = prefix + outputString;
const outputContainer = document.querySelector("#output-container");
const now = new Date();
const humanFriendlyTime = (
now.getHours().toString().padStart(2, '0') + ":" +
now.getMinutes().toString().padStart(2, '0') + ":" +
now.getSeconds().toString().padStart(2, '0')
);
outputContainer.querySelector("#scroll-anchor").insertAdjacentHTML("beforebegin",
`<div><code>[${humanFriendlyTime}]</code> ${outputString}</div>`
);
if (!scrollingEnabled && isOverflown(outputContainer)) {
outputContainer.scrollTop = outputContainer.scrollHeight;
scrollingEnabled = true;
}
}
const fromHexString = (hexString) =>
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const toHexString = (bytes) =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
const isHex = (maybeHex) =>
maybeHex.length !== 0 && maybeHex.length % 2 === 0 && !/[^a-fA-F0-9]/u.test(maybeHex);
/* Usage
log(toHexString(Uint8Array.from([0, 1, 2, 42, 100, 101, 102, 255])));
log(fromHexString('0001022a646566ff'));
const missingLetter = "abc";
if (!isHex(missingLetter)) {
log(`The string "${missingLetter}" is not valid hex.`)
} else {
fromHexString(missingLetter);
}
*/
function getTpmsData(tpmsBuffer) {
const data = {};
data.sensorAddress = toHexString(new Uint8Array(tpmsBuffer, 0, 6));
data.sensorId = toHexString(new Uint8Array(tpmsBuffer, 3, 3));
data.alarm = {
raw: 0,
id: 0
};
data.battery = {
raw: 0,
percentage: 0,
voltage: 0
};
data.pressure = {
raw: 0,
kPa: 0,
bar: 0,
psi: 0
};
data.temperature = {
raw: 0,
degC: 0,
degF: 0
};
try {
const expectedTpmsBufferLength = 16;
if (tpmsBuffer.byteLength !== expectedTpmsBufferLength) {
log(`Expected TPMS buffer length of ${expectedTpmsBufferLength}, got ${tpmsBuffer.byteLength}`);
if (tpmsBuffer.byteLength < expectedTpmsBufferLength) {
throw "TPMS buffer too short to decode, aborting!";
}
}
const dataView = new DataView(tpmsBuffer);
const pressure = dataView.getInt32(6, true);
data.pressure.raw = pressure;
data.pressure.kPa = pressure/100;
data.pressure.bar = pressure/100000;
data.pressure.psi = 14.5038 * data.pressure.bar;
const temperature = dataView.getInt32(10, true);
data.temperature.raw = temperature;
data.temperature.degC = temperature/100;
data.temperature.degF = 1.8 * data.temperature.degC + 32;
const battery = dataView.getInt8(14);
data.battery.raw = battery;
data.battery.percentage = battery/100;
data.battery.voltage = 3 * data.battery.percentage;
const alarm = dataView.getInt8(15);
data.alarm.raw = alarm;
data.alarm.id = (alarm === 0) ? "NO_ALARM" : "PRESSURE_ALARM";
} catch(error) {
console.error(error);
log("Error: ", error.toString());
}
return data;
}
function getTpmsDataSummary(tpmsData) {
return `${tpmsData.sensorId}: ${tpmsData.pressure.bar.toPrecision(3)} bar, ${tpmsData.temperature.degC.toPrecision(3)}°C`;
}
function getTpmsDataLog(tpmsData) {
return (
`<details><summary>Data from sensor ${getTpmsDataSummary(tpmsData)}</summary>` +
JSON.stringify(tpmsData, null, 4) +
"</details>"
);
}
function startTpmsListener(ids) {
log("Listening for sensor IDs \"", ids.join("\", \"")+ "\"");
const filters = ids.map((id, index) => new Object({
manufacturerData: [{
companyIdentifier: 0x0100,
dataPrefix: fromHexString(`8${index}eaca${id.toLowerCase()}`),
}],
services: [0xfbb0]
}));
const LEScanOptions = {
filters,
keepRepeatedDevices: true
};
log("BLE scan options: ", LEScanOptions);
navigator.bluetooth.requestLEScan(LEScanOptions).then((bleScanInstance) => {
bleScan = bleScanInstance;
let advertisementsReceived = 0;
log("BLE scan started, awaiting BLE advertisements");
navigator.bluetooth.addEventListener("advertisementreceived", (event) => {
try {
advertisementsReceived++;
log("New BLE advert: ", event);
const tpmsBuffer = event.manufacturerData.get(0x0100).buffer;
const tpmsData = getTpmsData(tpmsBuffer);
tpmsData.meta = {
id: advertisementsReceived,
time: Date.now()
};
// Reference: https://webbluetoothcg.github.io/web-bluetooth/#advertising-event-algorithm
tpmsData.meta.deviceName = event.device.name;
tpmsData.meta.deviceId = event.device.id;
tpmsData.meta.rssi = event.rssi; // dBm
tpmsData.meta.txPower = event.txPower;
tpmsData.meta.uuids = event.uuids;
// Reference: https://dom.spec.whatwg.org/#event
tpmsData.meta.uaTime = event.timeStamp; // https://dom.spec.whatwg.org/#dom-event-timestamp
tpmsData.meta.uaIsTrusted = event.isTrusted; // https://dom.spec.whatwg.org/#dom-event-istrusted
log(getTpmsDataLog(tpmsData));
} catch(error) {
console.error(error);
log("Error: ", error.toString());
}
});
});
}
/* 471 471 471 471 471 471 471 471 471 471 471 471 471 471 */
log(
"Web BLE scanning is an experimental web standard." +
"\n" +
"You likely need to be using a recent version of Chrome with Experimental Platform Features enabled for this to work!" +
"\n" +
"See <code>chrome://flags/#enable-experimental-web-platform-features</code> for more information."
);
const testString = (
"80" + // 80, 81, 82, 83 maps to sensor ID 1, 2, 3, 4 \
"ea ca" + // no known meaning, fixed part of sensor address | - > sensor address
"11 ad a3" + // sensor ID /
"0e a9 04 00" + // pressure as little endian int32, divide by 100 to get kPa (here 3054.22 kPa)
"5f 09 00 00" + // temperature as little endian int32, divide by 100 to get degrees C (here 23.99 degrees C)
"5e" + // battery percentage as int8, divide by 100 to get an actual percentage (here 94%)
"00" // alarm, 01 seems to indicate a low/no pressure alarm
).replace(/\s/g, "");
const testBuffer = fromHexString(testString).buffer;
const testData = getTpmsData(testBuffer);
log("Testing TPMS data parser with input 80eaca11ada30ea904005f0900005e00:", getTpmsDataLog(testData));
document.querySelector("#startScan").addEventListener("click", () => {
const frontLeftId = document.querySelector("input#tpmsId_frontLeft").value;
const frontRightId = document.querySelector("input#tpmsId_frontRight").value;
const rearLeftId = document.querySelector("input#tpmsId_rearLeft").value;
const rearRightId = document.querySelector("input#tpmsId_rearRight").value;
startTpmsListener([frontLeftId, frontRightId, rearLeftId, rearRightId]);
scanStartCheck = setTimeout(() => {
if (typeof bleScan === "undefined" || !bleScan.active) {
log(
`Scan did not start after ${SCAN_START_TIMEOUT} seconds.` +
"\n" +
"Your user agent may not support the Web BLE scanning API, or you may not have accepted the BLE scan permission request."
);
}
}, 1000 * SCAN_START_TIMEOUT);
});
document.querySelector("#stopScan").addEventListener("click", () => {
clearTimeout(scanStartCheck);
bleScan.stop();
document.querySelector("main").classList.remove("scanning");
});
</script>
</body>
</html>