You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 line
3.4KB

  1. from modules import cbpi
  2. class ActorController(object):
  3. @cbpi.try_catch(None)
  4. def actor_on(self, power=100, id=None):
  5. if id is None:
  6. id = self.heater
  7. self.api.switch_actor_on(int(id), power=power)
  8. @cbpi.try_catch(None)
  9. def actor_off(self, id=None):
  10. if id is None:
  11. id = self.heater
  12. self.api.switch_actor_off(int(id))
  13. class SensorController(object):
  14. @cbpi.try_catch(None)
  15. def get_sensor_value(self, id=None):
  16. if id is None:
  17. id = self.sensor
  18. return cbpi.get_sensor_value(id)
  19. class ControllerBase(object):
  20. __dirty = False
  21. __running = False
  22. @staticmethod
  23. def init_global():
  24. print "GLOBAL CONTROLLER INIT"
  25. def notify(self, headline, message, type="success", timeout=5000):
  26. self.api.notify(headline, message, type, timeout)
  27. def is_running(self):
  28. return self.__running
  29. def init(self):
  30. self.__running = True
  31. def sleep(self, seconds):
  32. self.api.socketio.sleep(seconds)
  33. def stop(self):
  34. self.__running = False
  35. def __init__(self, *args, **kwds):
  36. for a in kwds:
  37. super(ControllerBase, self).__setattr__(a, kwds.get(a))
  38. self.api = kwds.get("api")
  39. self.heater = kwds.get("heater")
  40. self.sensor = kwds.get("sensor")
  41. def run(self):
  42. pass
  43. class KettleController(ControllerBase, ActorController, SensorController):
  44. def __init__(self, *args, **kwds):
  45. ControllerBase.__init__(self, *args, **kwds)
  46. self.kettle_id = kwds.get("kettle_id")
  47. @cbpi.try_catch(None)
  48. def heater_on(self, power=100):
  49. k = self.api.cache.get("kettle").get(self.kettle_id)
  50. if k.heater is not None:
  51. self.actor_on(power, int(k.heater))
  52. @cbpi.try_catch(None)
  53. def heater_off(self):
  54. k = self.api.cache.get("kettle").get(self.kettle_id)
  55. if k.heater is not None:
  56. self.actor_off(int(k.heater))
  57. @cbpi.try_catch(None)
  58. def get_temp(self, id=None):
  59. if id is None:
  60. id = self.kettle_id
  61. return self.get_sensor_value(int(self.api.cache.get("kettle").get(id).sensor))
  62. @cbpi.try_catch(None)
  63. def get_target_temp(self, id=None):
  64. if id is None:
  65. id = self.kettle_id
  66. return self.api.cache.get("kettle").get(id).target_temp
  67. class FermenterController(ControllerBase, ActorController, SensorController):
  68. def __init__(self, *args, **kwds):
  69. ControllerBase.__init__(self, *args, **kwds)
  70. self.fermenter_id = kwds.get("fermenter_id")
  71. self.cooler = kwds.get("cooler")
  72. @cbpi.try_catch(None)
  73. def get_target_temp(self, id=None):
  74. if id is None:
  75. id = self.fermenter_id
  76. return self.api.cache.get("fermenter").get(id).target_temp
  77. @cbpi.try_catch(None)
  78. def heater_on(self, power=100):
  79. f = self.api.cache.get("fermenter").get(self.fermenter_id)
  80. if f.heater is not None:
  81. self.actor_on(int(f.heater))
  82. @cbpi.try_catch(None)
  83. def heater_off(self):
  84. f = self.api.cache.get("fermenter").get(self.fermenter_id)
  85. if f.heater is not None:
  86. self.actor_off(int(f.heater))
  87. @cbpi.try_catch(None)
  88. def get_temp(self, id=None):
  89. if id is None:
  90. id = self.fermenter_id
  91. return self.get_sensor_value(int(self.api.cache.get("fermenter").get(id).sensor))