-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
212 lines (195 loc) · 5.47 KB
/
index.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
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
'use strict';
var discover = require('./lib/discover' ).discover
, log = require('debug')('index')
, EventEmitter = require('events').EventEmitter
var iKettle = function() {
this.KETTLE_NOT_CONNECTED = 'kettle-not-connected'
this.CR = String.fromCharCode(0x0d)
}
iKettle.prototype = new EventEmitter()
iKettle.prototype.errors = {
'-1': 'Improper command format',
'-2': 'Command not supported',
'-3': 'Improper operationa symbol',
'-4': 'Improper parameter',
'-5': 'Not permitted',
'-6': 'Lack of memory',
'-7': 'Flash memory error',
'-10': 'Join failed',
'-11': 'No available socket',
'-12': 'Improper socket value',
'-13': 'Socket connection failed',
'-100': 'Undefined'
}
iKettle.prototype._checkCall = function(callback) {
if (this.kettle) {
return true
}
if (!callback) {
this.emit('error', this.KETTLE_NOT_CONNECTED)
} else {
callback(this.KETTLE_NOT_CONNECTED)
}
return false
}
iKettle.prototype.discover = function(callback) {
var discoverStartTime = new Date().getTime()
var self = this
discover(function(error, socket) {
self._discovered(error, socket, discoverStartTime, callback)
})
}
iKettle.prototype._discovered = function(error, socket, discoverStartTime, callback) {
if (error) {
log(error)
return callback(error)
}
this.kettle = socket
this._setupListeners()
var discoveryTime = (new Date().getTime() - discoverStartTime)
log(
'Found kettle! Discovery took ' +
(discoveryTime / 1000) +
' seconds'
)
callback(null, true)
}
iKettle.prototype._setupListeners = function() {
this.kettle.on('data', this._handleData.bind(this))
}
iKettle.prototype._handleStatus = function(code) {
log('Status', code)
}
iKettle.prototype._handleData = function(data) {
log('incoming socket data', data)
if (-1 !== data.indexOf('sys status key')) {
return this._handleStatus(data.split('=')[1])
}
var code = data.toString('ascii').replace('\r', '').split(' ')
if (code[2] && code[2].match(/[0-9]*x[0-9]*/)) {
switch (code[2]) {
case '0x0':
log('Kettle off')
this.emit('off')
break
case '0x1':
this.emit('removed')
break
case '0x2':
this.emit('overheat')
break
case '0x3':
this.emit('boiled')
break
case '0x4':
this.emit('keep-warm-expired')
break
case '0x5':
log('Emitting boiling')
this.emit('boiling')
break
case '0x10':
this.emit('keep-warm', false)
break
case '0x11':
this.emit('keep-warm', true)
break
case '0x65':
this.emit('temperature', 65)
break
case '0x80':
this.emit('temperature', 80)
break
case '0x95':
this.emit('temperature', 95)
break
case '0x100':
this.emit('temperature', 100)
break
case '0x8005':
this.emit('keep-warm-time', 5 * 60 * 1000)
break
case '0x8010':
this.emit('keep-warm-time', 10 * 60 * 1000)
break
case '0x8020':
this.emit('keep-warm-time', 20 * 60 * 1000)
break
}
}
}
iKettle.prototype.getStatus = function(callback) {
if (!this._checkCall(callback)) return
log('Getting kettle status')
this.kettle.write('get sys status' + this.CR, function() {
if (callback) callback()
})
}
iKettle.prototype.boil = function(callback) {
if (!this._checkCall(callback)) return
this.kettle.write('set sys output 0x4' + this.CR, function() {
if (callback) callback()
})
}
iKettle.prototype.off = function(callback) {
if (!this._checkCall(callback)) return
this.kettle.write('set sys output 0x0' + this.CR, function() {
if (callback) callback()
})
}
iKettle.prototype.setTemperature = function(temperature, callback) {
if (!this._checkCall(callback)) return
var code = null
switch (temperature) {
case 100:
code = '0x80'
break
case 95:
code = '0x2'
break
case 80:
code = '0x4000'
break
case 65:
code = '0x200'
break
}
if (!code) {
callback(
'Invalid temperature provided, ' +
'valid values: 100, 95, 80, 65'
)
}
log('Setting temperature to ' + temperature)
var command = 'set sys output ' + code + this.CR
this.kettle.write(command, function() {
callback()
})
}
iKettle.prototype.keepWarm = function(callback) {
if (!this._checkCall(callback)) return
this.kettle.write('set sys output 0x8' + this.CR, function() {
if (callback) callback()
})
}
iKettle.prototype.setKeepWarmTime = function(time, callback) {
if (!this._checkCall(callback)) return
time = time / 60000
var key = null
switch (time) {
case 5:
key = '0x8005'
break
case 10:
key = '0x8010'
break
case 20:
key = '0x8020'
break
}
if (!key) {
return callback(this.INVALID_KEEP_WARM_TIME)
}
this.kettle.write('set sys output ' + key + this.CR)
}
module.exports = iKettle