Projet de remplacement du "RPiPasserelle" d'Otec.
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. from typing import Dict
  3. from .variable_type import VariableType
  4. from pyheatpump.logger import logger_init
  5. logger = logger_init()
  6. class Heatpump:
  7. mac_address: str
  8. var_types: Dict
  9. last_update: int = None
  10. def __init__(self, mac_address, last_update=0, types=None):
  11. self.mac_address = mac_address
  12. self.last_update = last_update
  13. if types is not None:
  14. self.var_types = {
  15. key: val
  16. for key,val in VariableType.getall().items()
  17. if key in types
  18. }
  19. else:
  20. self.var_types = VariableType.getall()
  21. def __str__(self):
  22. res = f'{self.mac_address} ['
  23. res += ', '.join([
  24. var_type.slabel
  25. for var_type in self.var_types
  26. ])
  27. res += ']'
  28. @property
  29. def packet(self):
  30. """
  31. Returns an object containing all values and the current macAddress
  32. """
  33. r = {'macAddress': self.macformat}
  34. if not self.last_update:
  35. # Since beginning
  36. return { **r, **self.get_var_values() }
  37. return { **r, **self.get_var_values_since() }
  38. @property
  39. def macformat(self):
  40. return ''.join(self.mac_address.split(':')).lower()
  41. def get_var_values(self):
  42. return {
  43. var_type.label: var_type.get_variables_values()
  44. for var_type in self.var_types.values()
  45. }
  46. def get_var_values_since(self):
  47. r = {}
  48. for label, var_type in self.var_types.items():
  49. r[label] = var_type.get_variables_values_since(self.last_update)
  50. return r
  51. def control(self, data):
  52. logger.warning('Received orders data :')
  53. logger.warning(data)
  54. if 'macAddress' not in data.keys():
  55. logger.info('No orders')
  56. return True
  57. if data['macAddress'] != self.macformat:
  58. logger.warning('Orders are not for the current heatpump (%s != %s)',
  59. self.mac_address, data['macAddress'])
  60. raise Exception('Wrong mac_address')
  61. for _, var_type in self.var_types.items():
  62. if var_type.label not in data.keys():
  63. continue
  64. var_type.control(data[var_type.label])
  65. return True