選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

147 行
3.6KB

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