Smarthome Functionen
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import sqlite3
  2. def get_gui_radiator_data(topic):
  3. return __storage__().get_gui_radiator_data(topic)
  4. def set_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint):
  5. return __storage__().store_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint)
  6. class __storage__(object):
  7. def __init__(self):
  8. self.conn = sqlite3.connect("database.db")
  9. self.c = self.conn.cursor()
  10. with self.conn:
  11. self.c.execute("""CREATE TABLE IF NOT EXISTS gui_radiator (
  12. topic text PRIMARY KEY,
  13. away_mode integer,
  14. summer_mode integer,
  15. user_temperatur_setpoint real
  16. )""")
  17. def store_gui_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint):
  18. data = [topic, away_mode, summer_mode, user_temperatur_setpoint]
  19. try:
  20. with self.conn:
  21. self.c.execute(
  22. 'INSERT INTO gui_radiator VALUES (?, ?, ?, ?)', data)
  23. except sqlite3.IntegrityError:
  24. data = [away_mode, summer_mode, user_temperatur_setpoint]
  25. db_data = self.get_gui_radiator_data(topic)
  26. if db_data != data:
  27. with self.conn:
  28. self.c.execute(
  29. 'UPDATE gui_radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ? WHERE topic = ?', data + [topic])
  30. def get_gui_radiator_data(self, topic):
  31. """ returns a list [away_mode, summer_mode, user_temperatur_setpoint] or [None, None, None]"""
  32. self.c.execute("SELECT * FROM gui_radiator WHERE topic=?", (topic, ))
  33. data = self.c.fetchone()
  34. if data is not None:
  35. data = list(data)
  36. data[1] = data[1] == 1
  37. data[2] = data[2] == 1
  38. return data[1:]
  39. else:
  40. return [None, None, None]
  41. def __del__(self):
  42. self.conn.close()