Browse Source

[models] update models

Maxime Alves LIRMM@home 4 years ago
parent
commit
fb57af3592

+ 4
- 0
pyheatpump/models/__init__.py View File

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

+ 47
- 8
pyheatpump/models/variable.py View File

@@ -1,27 +1,66 @@
1 1
 from pyheatpump.db import RowClass
2 2
 from pyheatpump.db import sql
3 3
 from datetime import date
4
+from pyheatpump.modbus import rtu
4 5
 
5 6
 from .variable_type import VariableType
7
+from .variable_value import VariableValue
6 8
 
7 9
 class Variable(RowClass):
8 10
     address: int = None
9 11
     unit: str = None
10 12
     last_update: date = None
11 13
     type: str = None
12
- 
14
+
13 15
     def __init__(self, **kwargs):
14 16
         super().__init__(**kwargs)
15 17
 
18
+
16 19
     @staticmethod
17 20
     def getall():
18 21
         return dict([
19
-            (row.slabel, Variable.getall_of_type(row.slabel))
20
-            for row in VariableType.getall().values()
22
+            (row.slabel, Variable.getall_of_type(row))
23
+            for _, row in VariableType.getall().items()
24
+        ])
25
+
26
+
27
+    @staticmethod
28
+    def getall_of_type(type: VariableType) -> dict:
29
+        return dict([
30
+            (row['address'], Variable(**dict(row)))
31
+            for row in sql(
32
+            """
33
+            SELECT * FROM variable
34
+            WHERE type = '{}'
35
+                AND address >= {}
36
+                AND address <= {}
37
+            """.format(type.slabel, type.start_address, type.end_address))
38
+        ])
39
+
40
+
41
+    @staticmethod
42
+    def getall_values_of_type(type: VariableType) -> dict:
43
+         return dict([
44
+            (row['address'], row['value'])
45
+            for row in sql(
46
+            """
47
+            SELECT var.address, val.value FROM variable var
48
+            LEFT JOIN var_value val ON
49
+                var.type = val.type
50
+                AND var.address = val.address
51
+                AND var.last_update = var.time
52
+            WHERE
53
+                var.type = '{}'
54
+                AND var.address >= {}
55
+                AND var.address <= {}
56
+            """.format(type.slabel, type.start_address, type.end_address))
21 57
         ])
22 58
 
23
-    def getall_of_type(type: str):
24
-        return [
25
-            row['address'] for row in
26
-            sql(f"SELECT address FROM variable WHERE type LIKE '{type}'")
27
-        ]
59
+
60
+    def modbus_update(self):
61
+        if self.type == 'A':
62
+            pass
63
+        elif self.type == 'I':
64
+            pass
65
+        elif self.type == 'D':
66
+            pass

+ 11
- 2
pyheatpump/models/variable_type.py View File

@@ -1,13 +1,14 @@
1 1
 from pyheatpump.db import RowClass
2 2
 from pyheatpump.db import sql
3 3
 
4
+
4 5
 class VariableType(RowClass):
5 6
     slabel: str = None
6 7
     label: str = None
7 8
     type: str = None
8 9
     start_address: int = None
9 10
     end_address: int = None
10
- 
11
+
11 12
     def __init__(self, **kwargs):
12 13
         super().__init__(**kwargs)
13 14
 
@@ -31,10 +32,18 @@ class VariableType(RowClass):
31 32
         if len(updates) == 0:
32 33
             return
33 34
         q.append(','.join(updates))
34
-        q.append(f"WHERE slabel LIKE '{self.slabel}'")
35
+        q.append(f"WHERE slabel = '{self.slabel}'")
35 36
 
36 37
         return sql(' '.join(q))
37 38
 
39
+
40
+    def get_variables(self):
41
+        from .variable import Variable
42
+        return Variable.getall_of_type(self)
43
+
44
+    def get_variables_values(self):
45
+        return Variable.getall_values_of_type(self)
46
+
38 47
     @staticmethod
39 48
     def getall():
40 49
         return dict([

+ 24
- 10
pyheatpump/models/variable_value.py View File

@@ -1,18 +1,32 @@
1 1
 from pyheatpump.db import RowClass
2 2
 from pyheatpump.db import sql
3
+from datetime import datetime
4
+from pprint import pprint
3 5
 
4
-class VariableType(RowClass):
5
-    slabel: str = None
6
-    label: str = None
6
+class VariableValue(RowClass):
7 7
     type: str = None
8
-    start_address: int = None
9
-    end_address: int = None
10
- 
8
+    address: int = None
9
+    time: datetime = None
10
+    value: int = None
11
+
11 12
     def __init__(self, **kwargs):
12 13
         super().__init__(**kwargs)
13 14
 
14 15
     @staticmethod
15
-    def getall():
16
-        return dict([
17
-            (row['label'], VariableType(**dict(row)))
18
-            for row in sql('SELECT * FROM var_type') ])
16
+    def get(type, address, time=datetime.now()):
17
+        try:
18
+            row = next(sql(
19
+            """
20
+                SELECT * FROM var_value
21
+                WHERE
22
+                    type = '{}'
23
+                    AND address = {}
24
+                    AND time <= {}
25
+                ORDER BY time DESC
26
+                LIMIT 1
27
+            """.format(
28
+                type, address, int(time.strftime('%s'))+1)
29
+            ))
30
+            return VariableValue(**dict(row))
31
+        except StopIteration as e:
32
+            raise e

Loading…
Cancel
Save