let allWarnings = [] class Warning { constructor(msg, channelOrSensor) { this.warning = msg this.outOfRange = channelOrSensor.inRange this.value = channelOrSensor.value this.unit = channelOrSensor.unit this.scale = channelOrSensor.scale this.created = Date.now() } } class Reading { constructor({ from, value, unit, scale }) { this.value = value this.unit = unit this.scale = scale this.from = from this.created = Date.now() } } class Controller { constructor({ type, channel, sensor, action=null }) { this.type = type this.channel = channel this.sensor = sensor this.action = action } get inRange() { return this.channel.inRange && this.sensor.inRange } _checkRanges(all) { all.forEach(channelOrSensor => { if(channelOrSensor.inRange) return allWarnings.push( new Warning(`${channelOrSensor} out of range.`, channelOrSensor) ) }) } readout() { const value = this.channel.readFrom(this.sensor.update()) this._checkRanges([this.channel, this.sensor]) return new Reading({ value, from: this.channel.name, unit: this.channel.unit, scale: this.channel.scale }) } } module.exports = { Controller, allWarnings }