Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

68 řádky
1.7KB

  1. import time
  2. from flask_classy import route
  3. from modules.core.core import cbpi
  4. from modules.core.db import DBModel
  5. from modules.core.baseview import BaseView
  6. from modules.database.dbmodel import Sensor
  7. class SensorView(BaseView):
  8. model = Sensor
  9. cache_key = "sensors"
  10. @route('<int:id>/action/<method>', methods=["POST"])
  11. def action(self, id, method):
  12. """
  13. Sensor Action
  14. ---
  15. tags:
  16. - sensor
  17. parameters:
  18. - in: path
  19. name: id
  20. schema:
  21. type: integer
  22. required: true
  23. description: Numeric ID of the sensor
  24. - in: path
  25. name: method
  26. schema:
  27. type: string
  28. required: true
  29. description: action method name
  30. responses:
  31. 200:
  32. description: Sensor Action called
  33. """
  34. cbpi.cache.get("sensors").get(id).instance.__getattribute__(method)()
  35. return ('', 204)
  36. def _post_post_callback(self, m):
  37. cbpi.sensor.init_one(m.id)
  38. def _post_put_callback(self, m):
  39. cbpi.sensor.stop_one(m.id)
  40. cbpi.sensor.init_one(m.id)
  41. def _pre_delete_callback(self, m):
  42. cbpi.sensor.stop_one(m.id)
  43. @cbpi.addon.core.initializer(order=1000)
  44. def init(cbpi):
  45. SensorView.register(cbpi._app, route_base='/api/sensor')
  46. SensorView.init_cache()
  47. #@cbpi.backgroundtask(key="read_passiv_sensor", interval=5)
  48. def read_passive_sensor(api):
  49. """
  50. background process that reads all passive sensors in interval of 1 second
  51. :return: None
  52. """
  53. for key, value in cbpi.cache.get("sensors").iteritems():
  54. if value.mode == "P":
  55. value.instance.read()