您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

134 行
4.4KB

  1. from modules.core.db import DBModel, get_db
  2. from flask import json
  3. class Kettle(DBModel):
  4. __fields__ = ["name","sensor", "heater", "automatic", "logic", "config", "agitator", "target_temp"]
  5. __table_name__ = "kettle"
  6. __json_fields__ = ["config"]
  7. class Sensor(DBModel):
  8. __fields__ = ["name","type", "config", "hide"]
  9. __table_name__ = "sensor"
  10. __json_fields__ = ["config"]
  11. class Config(DBModel):
  12. __fields__ = ["type", "value", "description", "options"]
  13. __table_name__ = "config"
  14. __json_fields__ = ["options"]
  15. __priamry_key__ = "name"
  16. class Actor(DBModel):
  17. __fields__ = ["name","type", "config", "hide"]
  18. __table_name__ = "actor"
  19. __json_fields__ = ["config"]
  20. class Step(DBModel):
  21. __fields__ = ["name","type", "stepstate", "state", "start", "end", "order", "config"]
  22. __table_name__ = "step"
  23. __json_fields__ = ["config", "stepstate"]
  24. __order_by__ = "order"
  25. __as_array__ = True
  26. @classmethod
  27. def get_max_order(cls):
  28. cur = get_db().cursor()
  29. cur.execute("SELECT max(step.'order') as 'order' FROM %s" % cls.__table_name__)
  30. r = cur.fetchone()
  31. return r.get("order")
  32. @classmethod
  33. def get_by_state(cls, state, order=True):
  34. cur = get_db().cursor()
  35. cur.execute("SELECT * FROM %s WHERE state = ? ORDER BY %s.'order'" % (cls.__table_name__,cls.__table_name__,), state)
  36. r = cur.fetchone()
  37. if r is not None:
  38. return cls(r)
  39. else:
  40. return None
  41. @classmethod
  42. def delete_all(cls):
  43. cur = get_db().cursor()
  44. cur.execute("DELETE FROM %s" % cls.__table_name__)
  45. get_db().commit()
  46. @classmethod
  47. def reset_all_steps(cls):
  48. cur = get_db().cursor()
  49. cur.execute("UPDATE %s SET state = 'I', stepstate = NULL , start = NULL, end = NULL " % cls.__table_name__)
  50. get_db().commit()
  51. @classmethod
  52. def update_state(cls, id, state):
  53. cur = get_db().cursor()
  54. cur.execute("UPDATE %s SET state = ? WHERE id =?" % cls.__table_name__, (state, id))
  55. get_db().commit()
  56. @classmethod
  57. def update_step_state(cls, id, state):
  58. cur = get_db().cursor()
  59. cur.execute("UPDATE %s SET stepstate = ? WHERE id =?" % cls.__table_name__, (json.dumps(state),id))
  60. get_db().commit()
  61. @classmethod
  62. def sort(cls, new_order):
  63. cur = get_db().cursor()
  64. for e in new_order:
  65. cur.execute("UPDATE %s SET '%s' = ? WHERE id = ?" % (cls.__table_name__, "order"), (e[1], e[0]))
  66. get_db().commit()
  67. class Fermenter(DBModel):
  68. __fields__ = ["name", "brewname", "sensor", "sensor2", "sensor3", "heater", "cooler", "logic", "config", "target_temp"]
  69. __table_name__ = "fermenter"
  70. __json_fields__ = ["config"]
  71. class FermenterStep(DBModel):
  72. __fields__ = ["name", "days", "hours", "minutes", "temp", "direction", "order", "state", "start", "end", "timer_start", "fermenter_id"]
  73. __table_name__ = "fermenter_step"
  74. @classmethod
  75. def get_by_fermenter_id(cls, id):
  76. cur = get_db().cursor()
  77. cur.execute("SELECT * FROM %s WHERE fermenter_id = ?" % cls.__table_name__,(id,))
  78. result = []
  79. for r in cur.fetchall():
  80. result.append(cls(r))
  81. return result
  82. @classmethod
  83. def get_max_order(cls,id):
  84. cur = get_db().cursor()
  85. cur.execute("SELECT max(fermenter_step.'order') as 'order' FROM %s WHERE fermenter_id = ?" % cls.__table_name__, (id,))
  86. r = cur.fetchone()
  87. return r.get("order")
  88. @classmethod
  89. def update_state(cls, id, state):
  90. cur = get_db().cursor()
  91. cur.execute("UPDATE %s SET state = ? WHERE id =?" % cls.__table_name__, (state, id))
  92. get_db().commit()
  93. @classmethod
  94. def update_timer(cls, id, timer):
  95. cur = get_db().cursor()
  96. cur.execute("UPDATE %s SET timer_start = ? WHERE id =?" % cls.__table_name__, (timer, id))
  97. get_db().commit()
  98. @classmethod
  99. def get_by_state(cls, state):
  100. cur = get_db().cursor()
  101. cur.execute("SELECT * FROM %s WHERE state = ?" % cls.__table_name__, state)
  102. r = cur.fetchone()
  103. if r is not None:
  104. return cls(r)
  105. else:
  106. return None
  107. @classmethod
  108. def reset_all_steps(cls,id):
  109. cur = get_db().cursor()
  110. cur.execute("UPDATE %s SET state = 'I', start = NULL, end = NULL, timer_start = NULL WHERE fermenter_id = ?" % cls.__table_name__, (id,))
  111. get_db().commit()