-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensors.js
308 lines (271 loc) · 5.68 KB
/
sensors.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
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
const PIN_MUX_X = 18
const PIN_MUX_Y = 23
const PIN_FLOW_VALUE = 10
// x=0, y=0: temp
// x=0, y=1: pH
// x=1, y=0: orp
const SENSORS = {
TEMP: {x:0,y:0},
PH: {x:0,y:1},
ORP: {x:1,y:0}
}
const EventEmitter = require('events')
const fs = require('fs')
const async = require('async')
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const Pins = require('./pins')
const RESPONSE_TIMEOUT = 1000 // ms
const READING_TIMEOUT = 2000 // ms
const RETRIES = 2
class Sensors extends EventEmitter {
constructor () {
super()
var self = this
self.temp = null
self.ph = null
self.orp = null
self.flow = null
self.enabled = false
self._ready = false
self._running = false
self._errorCount = 0
self._flowCount = 0
self._flowTime = null
var pinNums = {}
pinNums[PIN_MUX_X] = { in: false }
pinNums[PIN_MUX_Y] = { in: false }
pinNums[PIN_FLOW_VALUE] = { in: true, edge: 'rising' }
self._pins = new Pins(pinNums)
function ready () {
if (self._uart.isOpen && self._pins.ready) {
self._ready = true
self._loop()
}
}
self._pins.on('ready', ready)
function flowEdge () {
self._flowCount++
}
self._pins.on('edge', flowEdge)
self._uart = new SerialPort('/dev/ttyAMA0', {
baudRate: 9600,
})
self._parser = new Readline({
delimiter: '\r'
})
self._uart.pipe(self._parser)
self._lines = []
self._lineCbs = []
self._parser.on('open', ready)
self._parser.on('data', function (data) {
if (self._lineCbs.length) {
self._lineCbs.shift()(null, data)
} else {
self._lines.push(data)
}
})
self._parser.on('error', function (err) {
self._lineCbs.forEach(function (cb) {
cb(err)
})
self._lines = []
self._lineCbs = []
})
}
enable(on) {
var self = this
self.enabled = !!on
self._loop()
}
_loop(err) {
var self = this
if (err) {
if (self._errorCount > RETRIES) {
return self.emit('error', err)
}
}
if (!self.enabled || self._running || !self._ready)
return
self._running = true
self._flowCount = 0
self._flowTime = Date.now()
async.series([
function (cb) {
self._readTemperature(function (err, temp) {
if (err)
return cb(err)
self.temp = temp
cb()
})
},
function (cb) {
self._readPH(self.temp, function (err, ph) {
if (err)
return cb(err)
self.ph = ph
cb()
})
},
function (cb) {
self._readORP(function (err, orp) {
if (err)
return cb(err)
self.orp = orp
self.flow = Math.round(self._flowCount * 1000 / (Date.now() - self._flowTime))
if (self.enabled) {
self.emit('reading', {
temp: self.temp,
ph: self.ph,
orp: self.orp,
flow: self.flow
})
}
cb()
})
}
], function (err) {
self._running = false
if (err) {
self._errorCount++
console.error('Error reading sensor; errorCount:', self._errorCount)
} else {
self._errorCount = 0
}
self._loop(err)
})
}
_clearBuffer(cb) {
var self = this
self._lines = []
self._lineCbs = []
self._uart.flush(cb)
}
_readLine(timeout, cb) {
var self = this
if (self._lines.length)
return cb(null, self._lines.shift())
setTimeout(function () {
if (cb)
cb(new Error('timed out'))
cb = null
}, timeout)
self._lineCbs.push(function (err, line) {
if (cb)
cb(err, line)
cb = null
})
}
_selectSensor(sensor, cb) {
var self = this
async.parallel([
function (cb) {
self._pins.set(PIN_MUX_X, SENSORS[sensor].x, cb)
},
function (cb) {
self._pins.set(PIN_MUX_Y, SENSORS[sensor].y, cb)
}
], cb)
}
_readSensor(sensor, cb) {
var self = this
var reading
async.series([
function (cb) {
if (sensor)
self._selectSensor(sensor, cb)
else
cb()
},
self._clearBuffer.bind(self),
function (cb) {
self._uart.write('R\r', cb)
},
function (cb) {
self._readLine(READING_TIMEOUT, function (err, line) {
if (err)
return cb(err)
if (line.length === 0) {
return cb(new Error('Bad reading'))
}
reading = parseFloat(line)
cb()
})
},
function (cb) {
self._readLine(RESPONSE_TIMEOUT, function (err, line) {
if (err)
return cb(err)
if (line !== '*OK') {
return cb(new Error('Bad response line: "' + line + '"'))
}
cb()
})
}
], function (err) {
if (err)
return cb(err)
cb(null, reading)
})
}
_readTemperature(cb) {
var self = this
self._readSensor('TEMP', cb)
}
_readPH(temp, cb) {
var self = this
async.series([
function (cb) {
self._selectSensor('PH', cb)
},
self._clearBuffer.bind(self),
function (cb) {
self._uart.write('T,' + temp.toString() + '\r', cb)
},
function (cb) {
self._readLine(RESPONSE_TIMEOUT, function (err, line) {
if (err)
return cb(err)
if (line !== '*OK')
return cb(new Error('Bad response line: "' + line + '"'))
cb()
})
}, function (cb) {
self._readSensor(null, cb)
}
], function (err, data) {
if (err)
return cb(err)
cb(null, data[data.length - 1])
})
}
_readORP(cb) {
var self = this
self._readSensor('ORP', cb)
}
}
// For debugging
class FakeSensors extends EventEmitter {
constructor () {
super()
this.enabled = false
const readings = require('./fakeReadings.json')
let index = 0
setInterval(() => {
if (this.enabled) {
const reading = readings[index]
index = (index + 1) % readings.length
if (!reading) {
this.emit('error', new Error('no fake readings'))
} else {
this.emit('reading', reading)
}
}
}, 2000)
}
enable (enabled) {
this.enabled = enabled
}
}
// module.exports = FakeSensors
module.exports = Sensors