Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

133 рядки
3.3KB

  1. from modules import cbpi
  2. from modules.core.props import StepProperty, Property
  3. import time
  4. class NotificationAPI(object):
  5. def notify(self, headline, message, type="success", timeout=5000):
  6. self.api.notify(headline, message, type, timeout)
  7. class ActorAPI(NotificationAPI):
  8. @cbpi.try_catch(None)
  9. def actor_on(self, id, power=100):
  10. self.api.switch_actor_on(int(id), power=power)
  11. @cbpi.try_catch(None)
  12. def actor_off(self, id):
  13. self.api.switch_actor_off(int(id))
  14. class SensorAPI(NotificationAPI):
  15. @cbpi.try_catch(None)
  16. def get_sensor_value(self, id):
  17. return cbpi.get_sensor_value(id)
  18. class KettleAPI(NotificationAPI):
  19. @cbpi.try_catch(None)
  20. def get_kettle_temp(self, id=None):
  21. id = int(id)
  22. if id is None:
  23. id = self.kettle_id
  24. return cbpi.get_sensor_value(int(self.api.cache.get("kettle").get(id).sensor))
  25. @cbpi.try_catch(None)
  26. def get_target_temp(self, id=None):
  27. id = int(id)
  28. if id is None:
  29. id = self.kettle_id
  30. return self.api.cache.get("kettle").get(id).target_temp
  31. def set_target_temp(self, temp, id=None):
  32. temp = int(temp)
  33. try:
  34. if id is None:
  35. self.api.emit_event("SET_TARGET_TEMP", id=self.kettle_id, temp=temp)
  36. else:
  37. self.api.emit_event("SET_TARGET_TEMP", id=id, temp=temp)
  38. except Exception as e:
  39. self.notify("Faild to set Target Temp", "", type="warning")
  40. class Timer(object):
  41. timer_end = Property.Number("TIMER_END", configurable=False)
  42. def start_timer(self, timer):
  43. if self.timer_end is not None:
  44. return
  45. self.timer_end = int(time.time()) + timer
  46. def stop_timer(self):
  47. if self.timer_end is not None:
  48. self.timer_end = None
  49. def is_timer_running(self):
  50. if self.timer_end is not None:
  51. return True
  52. else:
  53. return False
  54. def is_timer_finished(self):
  55. if self.timer_end is None:
  56. return None
  57. if self.timer_end <= int(time.time()):
  58. return True
  59. else:
  60. return False
  61. class StepBase(Timer, ActorAPI, SensorAPI, KettleAPI):
  62. __dirty = False
  63. managed_fields = []
  64. n = False
  65. def next(self):
  66. self.n = True
  67. def init(self):
  68. print "INIT STEP"
  69. def finish(self):
  70. print "FINSIH STEP"
  71. def reset(self):
  72. print "REST STEP"
  73. def execute(self):
  74. print "-------------"
  75. print "Step Info"
  76. print "Kettle ID: %s" % self.kettle_id
  77. print "ID: %s" % self.id
  78. def __init__(self, *args, **kwds):
  79. for a in kwds:
  80. super(StepBase, self).__setattr__(a, kwds.get(a))
  81. self.api = kwds.get("api")
  82. self.id = kwds.get("id")
  83. self.name = kwds.get("name")
  84. self.kettle_id = kwds.get("kettleid")
  85. self.value = None
  86. self.__dirty = False
  87. def is_dirty(self):
  88. return self.__dirty
  89. def reset_dirty(self):
  90. self.__dirty = False
  91. def __setattr__(self, name, value):
  92. if name != "_StepBase__dirty" and name in self.managed_fields:
  93. self.__dirty = True
  94. super(StepBase, self).__setattr__(name, value)
  95. else:
  96. super(StepBase, self).__setattr__(name, value)