-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisparkle-ble.js
85 lines (69 loc) · 1.74 KB
/
isparkle-ble.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
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
const BleUart = require('./ble-uart')
const VALID_TYPES = [
'PT',
'PW',
'SP',
'TM',
]
class iSparkleBle extends BleUart {
constructor() {
super()
this.count = 0
this.debug = false
}
getCmdString(value) {
// Default type prefix
let typeString = 'PT'
// Check if type prefix is set
if (VALID_TYPES.indexOf(value.slice(0, 2)) > -1) {
typeString = ''
}
// Prefix 2-digit count
let count = this.count
if (this.count < 10) {
count = `0${ count }`
} else if (this.count === 99) {
count = '00'
}
// Remove spaces from value
const valueString = `${ value.replace(/\s+/g, '') }`
return `${ count }${ typeString }${ valueString }`
}
sendCmd(commands) {
// single command
if (commands.length === 1) {
// Handle 'on' or 'off' text input
if (commands[0] === 'off' || commands[0] === 'on') {
this.writeString(this.getCmdString(commands[0] === 'off' ? 'PW0' : 'PW1'))
// Activate lights after turning on
if (commands[0] === 'on') {
this.writeString(this.getCmdString('1120005000050'))
}
// this.sendClearCmd()
return
}
this.writeString(this.getCmdString(commands[0]))
this.sendClearCmd()
return
}
// multiple commands
commands.forEach((command, i) => {
this.writeString(this.getCmdString(command))
// clear at end
if (i === commands.length - 1) {
this.sendClearCmd()
}
})
}
sendClearCmd() {
this.writeString(this.getCmdString('0000000000000'))
}
writeString(cmdString) {
if (this.debug) {
console.log(`write: ${ cmdString }`)
}
this.write(cmdString)
this.count++
}
}
module.exports = iSparkleBle