Small Bree based job runner + gui
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

controller.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. let allWarnings = []
  2. class Warning {
  3. constructor(msg, channelOrSensor) {
  4. this.warning = msg
  5. this.outOfRange = channelOrSensor.inRange
  6. this.value = channelOrSensor.value
  7. this.unit = channelOrSensor.unit
  8. this.scale = channelOrSensor.scale
  9. this.created = Date.now()
  10. }
  11. }
  12. class Reading {
  13. constructor({ from, value, unit, scale }) {
  14. this.value = value
  15. this.unit = unit
  16. this.scale = scale
  17. this.from = from
  18. this.created = Date.now()
  19. }
  20. }
  21. class Controller {
  22. constructor({ type, channel, sensor, action=null }) {
  23. this.type = type
  24. this.channel = channel
  25. this.sensor = sensor
  26. this.action = action
  27. }
  28. get inRange() {
  29. return this.channel.inRange && this.sensor.inRange
  30. }
  31. _checkRanges(all) {
  32. all.forEach(channelOrSensor => {
  33. if(channelOrSensor.inRange) return
  34. allWarnings.push(
  35. new Warning(`${channelOrSensor} out of range.`, channelOrSensor)
  36. )
  37. })
  38. }
  39. readout() {
  40. const value = this.channel.readFrom(this.sensor.update())
  41. this._checkRanges([this.channel, this.sensor])
  42. return new Reading({
  43. value,
  44. from: this.channel.name,
  45. unit: this.channel.unit,
  46. scale: this.channel.scale
  47. })
  48. }
  49. }
  50. module.exports = {
  51. Controller,
  52. allWarnings
  53. }