-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
140 lines (116 loc) · 4.19 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
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>BLE OTP</title>
<style>
@media (prefers-color-scheme: dark) {
html {
background: #222;
color: #eee;
}
}
button, input {
font-size: 1.5em;
padding: 1em;
}
textarea {
width: 100%;
margin: .5em 0;
}
</style>
<fieldset>
<button id="connectButton" onclick="connect()">Connect</button>
<button id="lockButton" onclick="lock()" style="display: none">Toggle</button>
</fieldset>
<!-- <fieldset>
<input id="topic" value="rolla/pulse" placeholder="Topic" />
<button id="listenButton" onclick="listen()">Listen</button>
<code id="messages">Messages</code>
</fieldset> -->
<script src="https://cdn.jsdelivr.net/npm/otpauth/dist/otpauth.umd.min.js"></script>
<!-- <script src="https://unpkg.com/mqtt@4.0.1/dist/mqtt.min.js"></script> -->
<script>
const serviceUuid = '5afe1eaf-f000-4ecb-ab75-f9ec2e1f1f10';
const lockUuid = '0be70cad-92aa-48c3-b26a-330e339aa163';
const otpUuid = 'e313b008-9fb4-4f5e-95a7-3dc5ee030543';
const nonceUuid = 'b0446719-6abf-4eac-8f15-9f94062b0763';
const connectButton = document.getElementById('connectButton');
const lockButton = document.getElementById('lockButton');
const secret = OTPAuth.Secret.fromUTF8('4x2%#$2_*f+4mi6t(&p*vl1u^3hkd79@+$-7(snt*^b13!*++');
const hotp = new OTPAuth.HOTP({ secret });
const textEncoder = new TextEncoder();
let otpCharacteristic;
let otpNonce;
let isLockOpen;
let device;
function setOtpNonce(dataView) {
if (!dataView) return;
otpNonce = dataView.getUint32(0, true);
console.log('OTP Nonce', otpNonce);
}
function setLock(value) {
isUnlocked = Boolean(value.getUint8());
lockButton.innerText = isUnlocked ? 'Lock' : 'Unlock';
if (isLockOpen === undefined) lockButton.removeAttribute('style');
}
async function connect() {
if (device) {
if (device.gatt.connected) {
device.gatt.disconnect();
onDisconnect();
}
return;
}
device = await navigator.bluetooth.requestDevice({
filters: [{
services: [serviceUuid]
}],
});
device.addEventListener('gattserverdisconnected', onDisconnect);
const server = await device.gatt.connect();
connectButton.innerText = "Disconnect";
const lockService = await server.getPrimaryService(serviceUuid);
const lockCharacteristic = await lockService.getCharacteristic(lockUuid)
const otpNonceCharacteristic = await lockService.getCharacteristic(nonceUuid);
otpCharacteristic = await lockService.getCharacteristic(otpUuid);
otpNonceCharacteristic
.readValue()
.then(value =>
!otpNonce && setOtpNonce(value));
otpNonceCharacteristic
.startNotifications()
.then(() =>
otpNonceCharacteristic.addEventListener(
'characteristicvaluechanged',
event => setOtpNonce(event.target.value)
));
lockCharacteristic
.readValue()
.then(value =>
isLockOpen === undefined && setLock(value));
lockCharacteristic
.startNotifications()
.then(value =>
lockCharacteristic.addEventListener(
'characteristicvaluechanged',
event => setLock(event.target.value)
));
}
async function lock() {
lockButton.setAttribute('disabled', true);
const otp = hotp.generate({ counter: otpNonce });
await otpCharacteristic.writeValueWithResponse(textEncoder.encode(otp))
lockButton.removeAttribute('disabled');
}
// function listen() {
// mqtt.connect('mqtt://test.mosquitto.org');
// }
function onDisconnect() {
connectButton.innerText = "Connect";
lockButton.style.display = "none";
lockButton.removeAttribute('disabled');
device = null;
}
</script>
</html>