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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from pyheatpump.db import RowClass
  2. from pyheatpump.db import sql
  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. sql(
  19. """INSERT INTO variable
  20. (type, address)
  21. VALUES
  22. ('{}', {})
  23. """.format(
  24. self.type, self.address
  25. ))
  26. return True
  27. except Exception as e:
  28. print(e)
  29. return False
  30. def exists(self):
  31. try:
  32. return bool(next(sql(
  33. """SELECT 1 FROM variable
  34. WHERE type = '{}' AND address = {}
  35. """.format(self.type, self.address))))
  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 dict([
  47. (row['address'], Variable(**dict(row)))
  48. for row in sql(
  49. """SELECT * FROM variable
  50. WHERE type = '{}'
  51. AND address >= {}
  52. AND address <= {}""".format(
  53. type, type.start_address, type.end_address))
  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. #offset = 10000 if (type.slabel == 'D') else 0
  63. return dict([
  64. (row['address'], cast_fct(row['value']))
  65. for row in sql(
  66. """SELECT var.address as address, val.value
  67. FROM variable var
  68. LEFT JOIN var_value val ON
  69. var.type = val.type
  70. AND var.address = val.address
  71. AND var.last_update = val.time
  72. WHERE
  73. var.type = '{}'
  74. AND var.address >= {}
  75. AND var.address <= {}
  76. AND var.last_update > {}""".format(
  77. type, type.start_address, type.end_address, since))
  78. ])
  79. def modbus_update(self):
  80. if self.type == 'A':
  81. pass
  82. elif self.type == 'I':
  83. pass
  84. elif self.type == 'D':
  85. pass