Browse Source

[models] added control functions

Maxime Alves LIRMM@home 3 years ago
parent
commit
8a7a076fd7

+ 12
- 0
pyheatpump/models/heatpump.py View File

@@ -40,3 +40,15 @@ class Heatpump:
40 40
         for label, var_type in self.var_types.items():
41 41
             r[label] = var_type.get_variables_values_since(self.last_update)
42 42
         return r
43
+
44
+    def control(self, data):
45
+        if data['mac_address'] != self.mac_address:
46
+            logger.warn(f'Orders are not for the current heatpump ({self.mac_address} != {data["mac_address"]})')
47
+            raise Exception('Wrong mac_address')
48
+
49
+        for label, var_type in self.var_types.items():
50
+            if var_type.label not in data.keys():
51
+                continue
52
+
53
+            var_type.control(data[var_type.label])
54
+        return True

+ 1
- 1
pyheatpump/models/variable.py View File

@@ -73,7 +73,7 @@ class Variable(RowClass):
73 73
 
74 74
     @staticmethod
75 75
     def getall_values_of_type_since(type: VariableType, since: int) -> dict:
76
-        floatcast = lambda x: round(float(x)/10, 2)
76
+        floatcast = type
77 77
         cast_fct = floatcast if type.slabel == 'F' else lambda x: x
78 78
 
79 79
         offset = 10000 if (type.slabel == 'D') else 0

+ 32
- 0
pyheatpump/models/variable_type.py View File

@@ -63,6 +63,38 @@ class VariableType(RowClass):
63 63
         from .variable import Variable
64 64
         return Variable.getall_values_of_type_since(self, since)
65 65
 
66
+    def control(self, data):
67
+        for s_address, s_value in data.items():
68
+            address = int(s_address)
69
+
70
+            # All values are stored in strings that represent floats
71
+            # We have to convert them
72
+            f_value = float(s_value)
73
+
74
+            if self.slabel == 'A':
75
+                f_value = f_value * 10
76
+
77
+            # Value is stored as integers in database
78
+            value = int(f_value)
79
+
80
+            if address not in range(self.start_address, self.end_address + 1):
81
+                continue
82
+
83
+            if not Variable(self, address).exists():
84
+                continue
85
+
86
+            new_var_value = VariableValue({
87
+                'type': self,
88
+                'address': address,
89
+                'value': value})
90
+
91
+            old_var_value = VariableValue.get(
92
+                self, address)
93
+
94
+            if old_var_value.equals(new_var_value):
95
+                continue
96
+
97
+            new_var_value.set()
66 98
 
67 99
     @staticmethod
68 100
     def getall():

+ 12
- 0
pyheatpump/models/variable_value.py View File

@@ -5,6 +5,7 @@ from pprint import pprint
5 5
 
6 6
 from .variable_type import VariableType
7 7
 from pyheatpump.logger import logger_init
8
+from pyheatpump.modbus import write_coil, write_holding_register
8 9
 
9 10
 logger = logger_init()
10 11
 
@@ -56,6 +57,17 @@ class VariableValue(RowClass):
56 57
     def get_value(self):
57 58
         return self.type.cast()(self.value)
58 59
 
60
+    def equals(self, var_value):
61
+        return self.get_value() == var_value.get_value()
62
+
63
+    def set(self):
64
+        if self.type == 'D':
65
+            write_coil(self)
66
+        else:
67
+            write_holding_register(self)
68
+
69
+        return self.insert()
70
+
59 71
     @staticmethod
60 72
     def get(type, address, time=datetime.now()):
61 73
         try:

Loading…
Cancel
Save