Просмотр исходного кода

[models] added heatpump model and added "since" function to variables models

Maxime Alves LIRMM@home 4 лет назад
Родитель
Сommit
4ca3f53750

+ 2
- 1
pyheatpump/models/__init__.py Просмотреть файл

@@ -1,4 +1,5 @@
1
+from .heatpump import Heatpump
1 2
 from .variable import Variable
2 3
 from .variable_type import VariableType
3 4
 from .variable_value import VariableValue
4
-__all__ = ['Variable', 'VariableType', 'VariableValue']
5
+__all__ = ['Heatpump', 'Variable', 'VariableType', 'VariableValue']

+ 42
- 0
pyheatpump/models/heatpump.py Просмотреть файл

@@ -0,0 +1,42 @@
1
+#!/usr/bin/env python3
2
+from typing import Dict
3
+from datetime import datetime
4
+
5
+from .variable_type import VariableType
6
+from .variable import Variable
7
+from .variable_value import VariableValue
8
+
9
+class Heatpump:
10
+    mac_address: str
11
+    var_types: Dict
12
+    last_update: int = None
13
+
14
+    def __init__(self, mac_address, last_update):
15
+        self.mac_address = mac_address
16
+        self.last_update = last_update
17
+        self.var_types = VariableType.getall()
18
+
19
+    def __str__(self):
20
+        return str(self.__dict__())
21
+
22
+    def __dict__(self):
23
+        r = {'macAddress': self.macformat}
24
+        if self.last_update is None:
25
+            return { **r, **self.get_var_values() }
26
+        return { **r, **self.get_var_values_since() }
27
+
28
+    @property
29
+    def macformat(self):
30
+        return ''.join(self.mac_address.split(':')).lower()
31
+
32
+    def get_var_values(self):
33
+        r = {}
34
+        for label, var_type in self.var_types.items():
35
+            r[label] = var_type.get_variables_values()
36
+        return r
37
+
38
+    def get_var_values_since(self):
39
+        r = {}
40
+        for label, var_type in self.var_types.items():
41
+            r[label] = var_type.get_variables_values_since(self.last_update)
42
+        return r

+ 17
- 5
pyheatpump/models/variable.py Просмотреть файл

@@ -68,10 +68,20 @@ class Variable(RowClass):
68 68
 
69 69
     @staticmethod
70 70
     def getall_values_of_type(type: VariableType) -> dict:
71
-         return dict([
72
-            (row['address'], row['value'])
71
+        return Variable.getall_values_of_type_since(type, 0) 
72
+
73
+
74
+    @staticmethod
75
+    def getall_values_of_type_since(type: VariableType, since: int) -> dict:
76
+        floatcast = lambda x: round(float(x)/10, 2)
77
+        cast_fct = floatcast if type.slabel == 'F' else lambda x: x
78
+
79
+        offset = 10000 if (type.slabel == 'D') else 0
80
+        return dict([
81
+            (row['address'], cast_fct(row['value']))
73 82
             for row in sql(
74
-            """SELECT var.address, val.value FROM variable var
83
+            """SELECT (var.address + {}) as address, val.value
84
+            FROM variable var
75 85
             LEFT JOIN var_value val ON
76 86
                 var.type = val.type
77 87
                 AND var.address = val.address
@@ -79,11 +89,13 @@ class Variable(RowClass):
79 89
             WHERE
80 90
                 var.type = '{}'
81 91
                 AND var.address >= {}
82
-                AND var.address <= {}""".format(
83
-            type, type.start_address, type.end_address))
92
+                AND var.address <= {}
93
+                AND var.last_update > {}""".format(
94
+            offset, type, type.start_address, type.end_address, since))
84 95
         ])
85 96
 
86 97
 
98
+
87 99
     def modbus_update(self):
88 100
         if self.type == 'A':
89 101
             pass

+ 5
- 0
pyheatpump/models/variable_type.py Просмотреть файл

@@ -59,6 +59,11 @@ class VariableType(RowClass):
59 59
         from .variable import Variable
60 60
         return Variable.getall_values_of_type(self)
61 61
 
62
+    def get_variables_values_since(self, since: int):
63
+        from .variable import Variable
64
+        return Variable.getall_values_of_type_since(self, since)
65
+
66
+
62 67
     @staticmethod
63 68
     def getall():
64 69
         return dict([

Загрузка…
Отмена
Сохранить