Small Bree based job runner + gui
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sensor.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const { unitScale } = require('./index')
  2. const interpolate = ({ x, y, from }) => {
  3. const [x1, x2] = x
  4. const [y1, y2] = y
  5. const to = y1 + (y2-y1) / (x2-x1) * (from-x1)
  6. return to
  7. }
  8. class Base {
  9. constructor({ name, min, max, unit, scale }) {
  10. this.name = name
  11. this.unit = unit
  12. this.scale = scale
  13. this.max = max
  14. this.min = min
  15. this._value = null
  16. }
  17. get value() { return this._value }
  18. get aboveRange() {
  19. return this._value > this.max
  20. }
  21. get belowRange() {
  22. return this._value < this.max
  23. }
  24. get inRange() {
  25. return this._value && this._value <= this.max && this._value >= this.min ? true : false
  26. }
  27. }
  28. class Channel extends Base {
  29. constructor({ name, min, max, interval=1, unit, scale }) {
  30. super({ name, min: min * interval, max: max * interval, unit, scale })
  31. this._interval = interval
  32. }
  33. readFrom(sensor) {
  34. const from = sensor.value
  35. this._value = interpolate({
  36. x: [sensor.min, sensor.max],
  37. y: [this.min, this.max],
  38. from
  39. })
  40. return Math.round(this.value)
  41. }
  42. }
  43. class Sensor extends Base {
  44. constructor({ name, min, max, interval=1, unit=unitScale.mV.unit, scale=unitScale.mV.scale }) {
  45. super({ name, min: min * interval, max: max * interval, unit, scale })
  46. this._interval = interval
  47. }
  48. }
  49. class TempSensor extends Sensor {
  50. constructor({ min, max, interval, unit, scale }) {
  51. super({ name: 'temperature_sensor', min, max, interval, unit, scale })
  52. }
  53. update() {
  54. // Store data in this._value
  55. this._value = 300
  56. return this
  57. }
  58. }
  59. class LevelSensor extends Sensor {
  60. constructor({ min, max, interval=1, unit="millimeter" }) {
  61. super({ name: 'level_sensor', min, max, interval, unit })
  62. }
  63. update() {
  64. // Store data in this._value
  65. return this
  66. }
  67. }
  68. module.exports = {
  69. Channel,
  70. TempSensor,
  71. }