-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoorlock.js
56 lines (48 loc) · 1.55 KB
/
doorlock.js
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
const axios = require('axios')
const aesCmac = require('node-aes-cmac').aesCmac
let sesame_id = process.env["SESAME_ID"] // uuid
let key_secret_hex = process.env["KEY_SECRET_HEX"]
let api_key = process.env["API_KEY"]
let getDoorStatus = async () => {
let after_cmd = await axios({
method: 'get',
url: `https://app.candyhouse.co/api/sesame2/${sesame_id}`,
headers: { 'x-api-key': api_key },
})
// console.log(after_cmd)
return after_cmd;
}
let openDoor = async () => {
let cmd = 89 //(toggle:88,lock:82,unlock:83)
let base64_history = Buffer.from('test2').toString('base64')
let sign = generateRandomTag(key_secret_hex)
let after_cmd = await axios({
method: 'post',
url: `https://app.candyhouse.co/api/sesame2/${sesame_id}/cmd`,
headers: { 'x-api-key': api_key },
data: {
cmd: cmd,
history: base64_history,
sign: sign,
},
})
// console.log(after_cmd)
return after_cmd;
}
function generateRandomTag(secret) {
// * key:key-secret_hex to data
let key = Buffer.from(secret, 'hex')
// message
// 1. timestamp (SECONDS SINCE JAN 01 1970. (UTC)) // 1621854456905
// 2. timestamp to uint32 (little endian) //f888ab60
// 3. remove most-significant byte //0x88ab60
const date = Math.floor(Date.now() / 1000)
const dateDate = Buffer.allocUnsafe(4)
dateDate.writeUInt32LE(date)
const message = Buffer.from(dateDate.slice(1, 4))
return aesCmac(key, message)
}
module.exports = {
getDoorStatus: getDoorStatus,
openDoor: openDoor
}