25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0KB

  1. import time
  2. from flask_classy import route
  3. from modules import DBModel, cbpi
  4. from modules.core.baseview import BaseView
  5. class Actor(DBModel):
  6. __fields__ = ["name","type", "config", "hide"]
  7. __table_name__ = "actor"
  8. __json_fields__ = ["config"]
  9. class ActorView(BaseView):
  10. model = Actor
  11. cache_key = "actors"
  12. @classmethod
  13. def post_init_callback(self, obj):
  14. obj.state = 0
  15. obj.power = 100
  16. def _post_post_callback(self, m):
  17. self.api.init_actor(m.id)
  18. def _post_put_callback(self, m):
  19. self.api.init_actor(m.id)
  20. @route("<int:id>/switch/on", methods=["POST"])
  21. def on(self, id):
  22. self.api.switch_actor_on(id)
  23. return ('', 204)
  24. @route("<int:id>/switch/off", methods=["POST"])
  25. def off(self, id):
  26. self.api.switch_actor_off(id)
  27. return ('', 204)
  28. @route("<int:id>/power/<int:power>", methods=["POST"])
  29. def power(self, id, power):
  30. self.api.actor_power(id, power)
  31. return ('', 204)
  32. @route("<int:id>/toggle", methods=["POST"])
  33. def toggle(self, id):
  34. if self.api.cache.get("actors").get(id).state == 0:
  35. self.on(id)
  36. else:
  37. self.off(id)
  38. return ('', 204)
  39. def toggleTimeJob(self, id, t):
  40. self.api.cache.get("actors").get(int(id)).timer = int(time.time()) + int(t)
  41. self.toggle(int(id))
  42. self.api.socketio.sleep(t)
  43. self.api.cache.get("actors").get(int(id)).timer = None
  44. self.toggle(int(id))
  45. @route("/<id>/toggle/<int:t>", methods=["POST"])
  46. def toggleTime(self, id, t):
  47. t = self.api.socketio.start_background_task(target=self.toggleTimeJob, id=id, t=t)
  48. return ('', 204)
  49. @route('<int:id>/action/<method>', methods=["POST"])
  50. def action(self, id, method):
  51. cbpi.cache.get("actors").get(id).instance.__getattribute__(method)()
  52. return ('', 204)
  53. @cbpi.initalizer(order=1000)
  54. def init(cbpi):
  55. ActorView.register(cbpi.app, route_base='/api/actor')
  56. ActorView.init_cache()
  57. cbpi.init_actors()