Projet de remplacement du "RPiPasserelle" d'Otec.
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.

variable.py 3.0KB

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