Browse Source

[models] added a get static function for variable_type, set "type" attribute of variable_value to VariableType in __init__

Maxime Alves LIRMM@home 3 years ago
parent
commit
04922f4e93

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

@@ -63,3 +63,19 @@ class VariableType(RowClass):
63 63
         return dict([
64 64
             (row['label'], VariableType(**dict(row)))
65 65
             for row in sql('SELECT * FROM var_type') ])
66
+
67
+    @staticmethod
68
+    def get(slabel: str):
69
+        if len(slabel) > 1:
70
+            slabel = slabel[0]
71
+
72
+        try:
73
+            return VariableType(**dict(
74
+                next(sql(
75
+                """
76
+                SELECT * FROM var_type
77
+                WHERE slabel = '{}'
78
+                """.format(slabel)))))
79
+        except StopIteration:
80
+            raise NameError
81
+

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

@@ -3,13 +3,18 @@ from pyheatpump.db import sql
3 3
 from datetime import datetime
4 4
 from pprint import pprint
5 5
 
6
+from .variable_type import VariableType
7
+
6 8
 class VariableValue(RowClass):
7
-    type: str = None
9
+    type: VariableType = None
8 10
     address: int = None
9 11
     time: datetime = None
10 12
     value: int = None
11 13
 
12 14
     def __init__(self, **kwargs):
15
+        if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
16
+            kwargs['type'] = VariableType.get(kwargs['type'])
17
+
13 18
         super().__init__(**kwargs)
14 19
 
15 20
     def insert(self):
@@ -28,6 +33,10 @@ class VariableValue(RowClass):
28 33
             print(e)
29 34
             return False
30 35
 
36
+
37
+    def get_value(self):
38
+        return self.type.cast()(self.value)
39
+
31 40
     @staticmethod
32 41
     def get(type, address, time=datetime.now()):
33 42
         try:

+ 55
- 3
tests/models/test_variable_value.py View File

@@ -38,14 +38,19 @@ def set_test_db():
38 38
     os.unlink(tmpdb)
39 39
 
40 40
 
41
-def test_last_update(set_test_db):
42
-    for _, var_type in VariableType.getall().items():
41
+@pytest.fixture
42
+def var_types():
43
+    return VariableType.getall()
44
+
45
+
46
+def test_last_update(set_test_db, var_types):
47
+    for _, var_type in var_types.items():
43 48
         for _, variable in var_type.get_variables().items():
44 49
             try:
45 50
                 if not variable.last_update:
46 51
                     continue
47 52
                 time = datetime.fromtimestamp(variable.last_update)
48
-                
53
+
49 54
                 val = VariableValue.get(variable.type,
50 55
                     variable.address,
51 56
                     time)
@@ -53,3 +58,50 @@ def test_last_update(set_test_db):
53 58
                 assert variable.last_update == val.time
54 59
             except StopIteration:
55 60
                 assert False
61
+
62
+
63
+def test_var_value_getval(set_test_db, var_types):
64
+    var_type = var_types['Analog']
65
+    for _, variable in var_type.get_variables().items():
66
+        try:
67
+            if not variable.last_update:
68
+                continue
69
+            time = datetime.fromtimestamp(variable.last_update)
70
+
71
+            value = VariableValue.get(variable.type,
72
+                variable.address,
73
+                time).get_value()
74
+            assert type(value) == float
75
+        except StopIteration:
76
+            assert False
77
+
78
+
79
+    var_type = var_types['Integer']
80
+    for _, variable in var_type.get_variables().items():
81
+        try:
82
+            if not variable.last_update:
83
+                continue
84
+            time = datetime.fromtimestamp(variable.last_update)
85
+
86
+            value = VariableValue.get(variable.type,
87
+                variable.address,
88
+                time).get_value()
89
+            assert type(value) == int
90
+        except StopIteration:
91
+            assert False
92
+
93
+
94
+    var_type = var_types['Digital']
95
+    for _, variable in var_type.get_variables().items():
96
+        try:
97
+            if not variable.last_update:
98
+                continue
99
+            time = datetime.fromtimestamp(variable.last_update)
100
+
101
+            value = VariableValue.get(variable.type,
102
+                variable.address,
103
+                time).get_value()
104
+            assert type(value) == bool
105
+        except StopIteration:
106
+            assert False
107
+

Loading…
Cancel
Save