| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- const { unitScale } = require('./index')
-
- const interpolate = ({ x, y, from }) => {
- const [x1, x2] = x
- const [y1, y2] = y
- const to = y1 + (y2-y1) / (x2-x1) * (from-x1)
- return to
- }
-
- class Base {
- constructor({ name, min, max, unit, scale }) {
- this.name = name
- this.unit = unit
- this.scale = scale
- this.max = max
- this.min = min
- this._value = null
- }
- get value() { return this._value }
- get aboveRange() {
- return this._value > this.max
- }
- get belowRange() {
- return this._value < this.max
- }
- get inRange() {
- return this._value && this._value <= this.max && this._value >= this.min ? true : false
- }
- }
-
- class Channel extends Base {
- constructor({ name, min, max, interval=1, unit, scale }) {
- super({ name, min: min * interval, max: max * interval, unit, scale })
- this._interval = interval
- }
- readFrom(sensor) {
- const from = sensor.value
- this._value = interpolate({
- x: [sensor.min, sensor.max],
- y: [this.min, this.max],
- from
- })
- return Math.round(this.value)
- }
- }
-
- class Sensor extends Base {
- constructor({ name, min, max, interval=1, unit=unitScale.mV.unit, scale=unitScale.mV.scale }) {
- super({ name, min: min * interval, max: max * interval, unit, scale })
- this._interval = interval
- }
- }
- class TempSensor extends Sensor {
- constructor({ min, max, interval, unit, scale }) {
- super({ name: 'temperature_sensor', min, max, interval, unit, scale })
- }
- update() {
- // Store data in this._value
- this._value = 300
- return this
- }
- }
- class LevelSensor extends Sensor {
- constructor({ min, max, interval=1, unit="millimeter" }) {
- super({ name: 'level_sensor', min, max, interval, unit })
- }
- update() {
- // Store data in this._value
- return this
- }
- }
-
- module.exports = {
- Channel,
- TempSensor,
- }
|