Browse Source

modification related to VariableType.__str__

Maxime Alves LIRMM@home 3 years ago
parent
commit
bc60ac937a
3 changed files with 16 additions and 7 deletions
  1. 3
    3
      pyheatpump/cli.py
  2. 4
    4
      pyheatpump/models/variable.py
  3. 9
    0
      pyheatpump/models/variable_type.py

+ 3
- 3
pyheatpump/cli.py View File

@@ -60,7 +60,7 @@ def fetch():
60 60
     res = modbus.read_holding_registers(analog.start_address, analog.end_address)
61 61
 
62 62
     for r in range(len(res)):
63
-        val = VariableValue(str(analog), r + analog.start_address, res[r])
63
+        val = VariableValue(analog, r + analog.start_address, res[r])
64 64
         val.insert()
65 65
 
66 66
 
@@ -69,7 +69,7 @@ def fetch():
69 69
     res = modbus.read_holding_registers(integer.start_address, integer.end_address)
70 70
 
71 71
     for r in res:
72
-        val = VariableValue(str(integer), r + integer.start_address, res[r])
72
+        val = VariableValue(integer, r + integer.start_address, res[r])
73 73
         val.insert()
74 74
 
75 75
     # Digital - bool
@@ -77,5 +77,5 @@ def fetch():
77 77
     res = modbus.read_coils(digital.start_address, digital.end_address)
78 78
 
79 79
     for r in res:
80
-        val = VariableValue(str(integer), r + integer.start_address, res[r])
80
+        val = VariableValue(integer), r + integer.start_address, res[r])
81 81
         val.insert()

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

@@ -10,7 +10,7 @@ class Variable(RowClass):
10 10
     address: int = None
11 11
     unit: str = None
12 12
     last_update: date = None
13
-    type: str = None
13
+    type: VariableType = None
14 14
 
15 15
     def __init__(self, **kwargs):
16 16
         super().__init__(**kwargs)
@@ -19,7 +19,7 @@ class Variable(RowClass):
19 19
     @staticmethod
20 20
     def getall():
21 21
         return dict([
22
-            (row.slabel, Variable.getall_of_type(row))
22
+            (str(row), Variable.getall_of_type(row))
23 23
             for _, row in VariableType.getall().items()
24 24
         ])
25 25
 
@@ -34,7 +34,7 @@ class Variable(RowClass):
34 34
             WHERE type = '{}'
35 35
                 AND address >= {}
36 36
                 AND address <= {}
37
-            """.format(type.slabel, type.start_address, type.end_address))
37
+            """.format(type, type.start_address, type.end_address))
38 38
         ])
39 39
 
40 40
 
@@ -53,7 +53,7 @@ class Variable(RowClass):
53 53
                 var.type = '{}'
54 54
                 AND var.address >= {}
55 55
                 AND var.address <= {}
56
-            """.format(type.slabel, type.start_address, type.end_address))
56
+            """.format(type, type.start_address, type.end_address))
57 57
         ])
58 58
 
59 59
 

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

@@ -20,6 +20,15 @@ class VariableType(RowClass):
20 20
         return self.slabel or self.label[0]
21 21
 
22 22
 
23
+    def cast(self):
24
+        if self.type == 'float':
25
+            return float
26
+        if self.type == 'int':
27
+            return int
28
+        if self.type == 'bool':
29
+            return bool
30
+
31
+
23 32
     def select():
24 33
         try:
25 34
             elt = next(super().select('slabel', 'var_type'))

Loading…
Cancel
Save