Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

249 Zeilen
7.1KB

  1. import time
  2. from flask import json, request
  3. from flask_classy import route
  4. from modules import DBModel, cbpi, get_db
  5. from modules.core.baseview import BaseView
  6. class Step(DBModel):
  7. __fields__ = ["name","type", "stepstate", "state", "start", "end", "order", "config"]
  8. __table_name__ = "step"
  9. __json_fields__ = ["config", "stepstate"]
  10. __order_by__ = "order"
  11. __as_array__ = True
  12. @classmethod
  13. def get_max_order(cls):
  14. cur = get_db().cursor()
  15. cur.execute("SELECT max(step.'order') as 'order' FROM %s" % cls.__table_name__)
  16. r = cur.fetchone()
  17. return r.get("order")
  18. @classmethod
  19. def get_by_state(cls, state, order=True):
  20. cur = get_db().cursor()
  21. cur.execute("SELECT * FROM %s WHERE state = ? ORDER BY %s.'order'" % (cls.__table_name__,cls.__table_name__,), state)
  22. r = cur.fetchone()
  23. if r is not None:
  24. return cls(r)
  25. else:
  26. return None
  27. @classmethod
  28. def delete_all(cls):
  29. cur = get_db().cursor()
  30. cur.execute("DELETE FROM %s" % cls.__table_name__)
  31. get_db().commit()
  32. @classmethod
  33. def reset_all_steps(cls):
  34. cur = get_db().cursor()
  35. cur.execute("UPDATE %s SET state = 'I', stepstate = NULL , start = NULL, end = NULL " % cls.__table_name__)
  36. get_db().commit()
  37. @classmethod
  38. def update_state(cls, id, state):
  39. cur = get_db().cursor()
  40. cur.execute("UPDATE %s SET state = ? WHERE id =?" % cls.__table_name__, (state, id))
  41. get_db().commit()
  42. @classmethod
  43. def update_step_state(cls, id, state):
  44. cur = get_db().cursor()
  45. cur.execute("UPDATE %s SET stepstate = ? WHERE id =?" % cls.__table_name__, (json.dumps(state),id))
  46. get_db().commit()
  47. @classmethod
  48. def sort(cls, new_order):
  49. cur = get_db().cursor()
  50. for e in new_order:
  51. cur.execute("UPDATE %s SET '%s' = ? WHERE id = ?" % (cls.__table_name__, "order"), (e[1], e[0]))
  52. get_db().commit()
  53. class StepView(BaseView):
  54. model = Step
  55. def pre_post_callback(self, data):
  56. order = self.model.get_max_order()
  57. data["order"] = 1 if order is None else order + 1
  58. data["state"] = "I"
  59. @route('/sort', methods=["POST"])
  60. def sort_steps(self):
  61. Step.sort(request.json)
  62. cbpi.emit("UPDATE_ALL_STEPS", self.model.get_all())
  63. return ('', 204)
  64. @route('/', methods=["DELETE"])
  65. def deleteAll(self):
  66. self.model.delete_all()
  67. cbpi.emit("UPDATE_ALL_STEPS", self.model.get_all())
  68. return ('', 204)
  69. @route('/action/<method>', methods=["POST"])
  70. def action(self, method):
  71. cbpi.cache["active_step"].__getattribute__(method)()
  72. return ('', 204)
  73. @route('/reset', methods=["POST"])
  74. def reset(self):
  75. self.model.reset_all_steps()
  76. #db.session.commit()
  77. self.stop_step()
  78. cbpi.emit("UPDATE_ALL_STEPS", self.model.get_all())
  79. return ('', 204)
  80. def stop_step(self):
  81. '''
  82. stop active step
  83. :return:
  84. '''
  85. step = cbpi.cache.get("active_step")
  86. cbpi.cache["active_step"] = None
  87. if step is not None:
  88. step.finish()
  89. @route('/reset/current', methods=['POST'])
  90. def resetCurrentStep(self):
  91. '''
  92. Reset current step
  93. :return:
  94. '''
  95. step = cbpi.cache.get("active_step")
  96. if step is not None:
  97. step.reset()
  98. if step.is_dirty():
  99. state = {}
  100. for field in step.managed_fields:
  101. state[field] = step.__getattribute__(field)
  102. Step.update_step_state(step.id, state)
  103. step.reset_dirty()
  104. cbpi.emit("UPDATE_ALL_STEPS", self.model.get_all())
  105. return ('', 204)
  106. def init_step(self, step):
  107. cbpi.log_action("Start Step %s" % step.name)
  108. type_cfg = cbpi.cache.get("step_types").get(step.type)
  109. if type_cfg is None:
  110. # if type not found
  111. return
  112. # copy config to stepstate
  113. # init step
  114. cfg = step.config.copy()
  115. cfg.update(dict(name=step.name, api=cbpi, id=step.id, timer_end=None, managed_fields=get_manged_fields_as_array(type_cfg)))
  116. instance = type_cfg.get("class")(**cfg)
  117. instance.init()
  118. # set step instance to ache
  119. cbpi.cache["active_step"] = instance
  120. @route('/next', methods=['POST'])
  121. @route('/start', methods=['POST'])
  122. def start(self):
  123. active = Step.get_by_state("A")
  124. inactive = Step.get_by_state('I')
  125. if (active is not None):
  126. active.state = 'D'
  127. active.end = int(time.time())
  128. self.stop_step()
  129. Step.update(**active.__dict__)
  130. if (inactive is not None):
  131. self.init_step(inactive)
  132. inactive.state = 'A'
  133. inactive.stepstate = inactive.config
  134. inactive.start = int(time.time())
  135. Step.update(**inactive.__dict__)
  136. else:
  137. cbpi.log_action("Brewing Finished")
  138. cbpi.notify("Brewing Finished", "You are done!", timeout=None)
  139. cbpi.emit("UPDATE_ALL_STEPS", Step.get_all())
  140. return ('', 204)
  141. def get_manged_fields_as_array(type_cfg):
  142. result = []
  143. for f in type_cfg.get("properties"):
  144. result.append(f.get("name"))
  145. return result
  146. @cbpi.try_catch(None)
  147. def init_after_startup():
  148. '''
  149. Restart after startup. Check is a step is in state A and reinitialize
  150. :return: None
  151. '''
  152. step = Step.get_by_state('A')
  153. # We have an active step
  154. if step is not None:
  155. # get the type
  156. type_cfg = cbpi.cache.get("step_types").get(step.type)
  157. if type_cfg is None:
  158. # step type not found. cant restart step
  159. return
  160. print "STEP SATE......", step.stepstate
  161. cfg = step.stepstate.copy()
  162. cfg.update(dict(api=cbpi, id=step.id, managed_fields=get_manged_fields_as_array(type_cfg)))
  163. instance = type_cfg.get("class")(**cfg)
  164. instance.init()
  165. cbpi.cache["active_step"] = instance
  166. @cbpi.initalizer(order=2000)
  167. def init(cbpi):
  168. print "INITIALIZE STEPS MODULE"
  169. StepView.register(cbpi.app, route_base='/api/step')
  170. def get_all():
  171. with cbpi.app.app_context():
  172. return Step.get_all()
  173. with cbpi.app.app_context():
  174. init_after_startup()
  175. cbpi.add_cache_callback("steps", get_all)
  176. @cbpi.backgroundtask(key="step_task", interval=0.1)
  177. def execute_step():
  178. '''
  179. Background job which executes the step
  180. :return:
  181. '''
  182. with cbpi.app.app_context():
  183. step = cbpi.cache.get("active_step")
  184. if step is not None:
  185. step.execute()
  186. if step.is_dirty():
  187. state = {}
  188. for field in step.managed_fields:
  189. state[field] = step.__getattribute__(field)
  190. Step.update_step_state(step.id, state)
  191. step.reset_dirty()
  192. cbpi.emit("UPDATE_ALL_STEPS", Step.get_all())
  193. if step.n is True:
  194. StepView().start()
  195. cbpi.emit("UPDATE_ALL_STEPS", Step.get_all())