Smarthome Functionen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

db.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from function.modules import heating_function
  2. import os
  3. import sqlite3
  4. db_file = os.path.join(os.path.dirname(__file__), '..', 'database.db')
  5. db_mapping_radiator = {
  6. 0: heating_function.KEY_AWAY_MODE,
  7. 1: heating_function.KEY_SUMMER_MODE,
  8. 2: heating_function.KEY_USER_TEMPERATURE_SETPOINT,
  9. 3: heating_function.KEY_TEMPERATURE_SETPOINT
  10. }
  11. def get_radiator_data(topic):
  12. db_data = __storage__().get_radiator_data(topic)
  13. rv = {}
  14. for index in db_mapping_radiator:
  15. rv[db_mapping_radiator[index]] = db_data[index]
  16. return rv
  17. def set_radiator_data(device, key, data):
  18. if key in db_mapping_radiator.values():
  19. db_data = []
  20. for index in range(0, len(db_mapping_radiator)):
  21. db_data.append(device.get(db_mapping_radiator[index]))
  22. return __storage__().store_radiator_data(device.heating_valve.topic, db_data)
  23. class __storage__(object):
  24. def __init__(self):
  25. self.conn = sqlite3.connect(db_file)
  26. self.c = self.conn.cursor()
  27. with self.conn:
  28. self.c.execute("""CREATE TABLE IF NOT EXISTS radiator (
  29. topic text PRIMARY KEY,
  30. away_mode integer,
  31. summer_mode integer,
  32. user_temperatur_setpoint real,
  33. temperatur_setpoint real
  34. )""")
  35. def store_radiator_data(self, topic, target_data):
  36. try:
  37. with self.conn:
  38. self.c.execute(
  39. 'INSERT INTO radiator VALUES (?, ?, ?, ?, ?)', [topic] + target_data)
  40. except sqlite3.IntegrityError:
  41. db_data = self.get_radiator_data(topic)
  42. if db_data != target_data:
  43. with self.conn:
  44. self.c.execute(
  45. 'UPDATE radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ?, temperatur_setpoint = ? WHERE topic = ?', target_data + [topic])
  46. def get_radiator_data(self, topic):
  47. """ returns a list [away_mode, summer_mode, user_temperatur_setpoint, temperatur_setpoint] or [None, None, None, None]"""
  48. self.c.execute("SELECT * FROM radiator WHERE topic=?", (topic, ))
  49. data = self.c.fetchone()
  50. if data is not None:
  51. data = list(data)
  52. data[1] = data[1] == 1
  53. data[2] = data[2] == 1
  54. return data[1:]
  55. else:
  56. return [None, None, None, None]
  57. def __del__(self):
  58. self.conn.close()