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.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from pyheatpump.db import RowClass
  2. from pyheatpump.db import DB
  3. from datetime import date
  4. from pyheatpump.modbus import rtu
  5. from .variable_type import VariableType
  6. from .variable_value import VariableValue
  7. from pyheatpump.logger import logger_init
  8. logger = logger_init()
  9. class Variable(RowClass):
  10. address: int = None
  11. unit: str = None
  12. last_update: date = None
  13. type: VariableType = None
  14. def __init__(self, **kwargs):
  15. if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
  16. kwargs['type'] = VariableType.get(kwargs['type'])
  17. super().__init__(**kwargs)
  18. def insert(self):
  19. try:
  20. self.type_slabel = self.type.slabel
  21. DB.sql(
  22. """INSERT INTO variable
  23. (type, address)
  24. VALUES
  25. (:type_slabel, :address)
  26. """, self.__dict__)
  27. return True
  28. except Exception as e:
  29. print(e)
  30. return False
  31. def exists(self):
  32. try:
  33. self.type_slabel = self.type.slabel
  34. return bool(next(DB.sql(
  35. """SELECT 1 FROM variable
  36. WHERE type=:type_slabel AND address=:address
  37. """, self.__dict__)))
  38. except StopIteration:
  39. return False
  40. @staticmethod
  41. def getall():
  42. return dict([
  43. (str(row), Variable.getall_of_type(row))
  44. for _, row in VariableType.getall().items()
  45. ])
  46. @staticmethod
  47. def getall_of_type(type: VariableType) -> dict:
  48. return {
  49. row['address']: Variable(**dict(row))
  50. for row in DB.sql(
  51. """SELECT * FROM variable
  52. WHERE type = :slabel
  53. AND address >= :start_address
  54. AND address <= :end_address""",
  55. type.__dict__)
  56. }
  57. @staticmethod
  58. def getall_values_of_type(type: VariableType) -> dict:
  59. return Variable.getall_values_of_type_since(type, 0)
  60. @staticmethod
  61. def getall_values_of_type_since(type: VariableType, since: int) -> dict:
  62. floatcast = lambda x: round(float(x) / 10, 2)
  63. cast_fct = floatcast if type.slabel == 'A' else lambda x: x
  64. params = type.__dict__.copy()
  65. params.update({'since': since})
  66. return {
  67. row['address']: cast_fct(row['value'])
  68. for row in DB.sql(
  69. """SELECT var.address as address, val.value as value
  70. FROM variable var
  71. LEFT JOIN var_value val ON
  72. var.type = val.type
  73. AND var.address = val.address
  74. AND var.last_update = val.time
  75. WHERE
  76. var.type = :slabel
  77. AND var.address >= :start_address
  78. AND var.address <= :end_address
  79. AND var.last_update > :since""",
  80. params)
  81. if 'address' in row.keys() and 'value' in row.keys() and
  82. row['value'] is not None
  83. }
  84. def modbus_update(self):
  85. if self.type == 'A':
  86. pass
  87. elif self.type == 'I':
  88. pass
  89. elif self.type == 'D':
  90. pass