Projet de remplacement du "RPiPasserelle" d'Otec.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

variable_value.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from pyheatpump.db import RowClass
  2. from pyheatpump.db import sql
  3. from datetime import datetime
  4. from pprint import pprint
  5. from .variable_type import VariableType
  6. from pyheatpump.logger import logger_init
  7. logger = logger_init()
  8. class VariableValue(RowClass):
  9. type: VariableType = None
  10. address: int = None
  11. time: datetime = None
  12. value: int = None
  13. def __init__(self, **kwargs):
  14. logger.debug("""Create VariableValue object with attributes\n
  15. :type:{}
  16. :address:{}
  17. :value:{}""".format(*kwargs.values()))
  18. if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
  19. kwargs['type'] = VariableType.get(kwargs['type'])
  20. super().__init__(**kwargs)
  21. def insert(self):
  22. try:
  23. sql(
  24. """
  25. INSERT INTO var_value
  26. (type, address, value)
  27. VALUES
  28. ('{}', {}, {})
  29. """.format(
  30. self.type, self.address, self.value
  31. ))
  32. return True
  33. except Exception as e:
  34. print(e)
  35. return False
  36. def get_value(self):
  37. return self.type.cast()(self.value)
  38. @staticmethod
  39. def get(type, address, time=datetime.now()):
  40. try:
  41. row = next(sql(
  42. """
  43. SELECT * FROM var_value
  44. WHERE
  45. type = '{}'
  46. AND address = {}
  47. AND time <= {}
  48. ORDER BY time DESC
  49. LIMIT 1
  50. """.format(
  51. type, address, int(time.strftime('%s'))+1
  52. )))
  53. return VariableValue(**dict(row))
  54. except StopIteration as e:
  55. raise e