Browse Source

[models] fix type casting (issue #30)

Maxime Alves LIRMM@home 3 years ago
parent
commit
c2ffe4c5cf

+ 12
- 6
pyheatpump/models/variable.py View File

@@ -43,12 +43,13 @@ class Variable(RowClass):
43 43
 
44 44
     def exists(self):
45 45
         try:
46
-            self.type_slabel = self.type.slabel
47
-
48 46
             return bool(next(DB.sql(
49 47
             """SELECT 1 FROM variable
50
-            WHERE type=:type_slabel AND address=:address
51
-            """, self.__dict__)))
48
+            WHERE type=:type AND address=:address
49
+            """, {
50
+                'type': self.type.slabel,
51
+                'address': self.address
52
+            })))
52 53
         except StopIteration:
53 54
             return False
54 55
 
@@ -66,7 +67,7 @@ class Variable(RowClass):
66 67
             row['address']: Variable(**dict(row))
67 68
             for row in DB.sql(
68 69
             """SELECT * FROM variable
69
-            WHERE var_type = :slabel
70
+            WHERE type = :slabel
70 71
                 AND address >= :start_address
71 72
                 AND address <= :end_address""", {
72 73
 
@@ -84,8 +85,13 @@ class Variable(RowClass):
84 85
 
85 86
     @staticmethod
86 87
     def getall_values_of_type_since(var_type: VariableType, since: int) -> dict:
88
+        """
87 89
         floatcast = lambda x: round(float(x) / 10, 2)
88 90
         cast_fct = floatcast if type.slabel == 'A' else lambda x: x
91
+        """
92
+        if not var_type.cast:
93
+            raise Exception('var_type has no cast property')
94
+
89 95
         params = {
90 96
             'slabel': var_type.slabel,
91 97
             'start_address': var_type.start_address,
@@ -94,7 +100,7 @@ class Variable(RowClass):
94 100
         }
95 101
 
96 102
         return {
97
-            row['address']: cast_fct(row['value'])
103
+            row['address']: var_type.cast(row['value'])
98 104
 
99 105
             for row in DB.sql(
100 106
             """SELECT var.address as address, val.value as value

+ 15
- 6
pyheatpump/models/variable_type.py View File

@@ -19,13 +19,22 @@ class VariableType(RowClass):
19 19
         return self.slabel or self.label[0]
20 20
 
21 21
 
22
+    @property
22 23
     def cast(self):
23
-        return ({
24
-            'float': float,
25
-            'int': int,
26
-            'bool': bool
27
-        }).get(self.type, None)
28
-
24
+        complement = lambda x: x - (1 << 16 if x & (1 << 15) else 0)
25
+
26
+        # Function to convert numbers > 2**15 to negative numbers (issue #30)
27
+        if self.type == 'bool':
28
+            # returns a boolean
29
+            return lambda x: bool(x)
30
+        elif self.type == 'float':
31
+            # returns a signed float
32
+            return lambda x: round(complement(x) / 10, 2)
33
+        elif self.type == 'int':
34
+            # returns the signed integer
35
+            return lambda x: int(complement(x))
36
+
37
+        return None
29 38
 
30 39
     def select(self):
31 40
         try:

+ 3
- 3
pyheatpump/models/variable_value.py View File

@@ -14,7 +14,7 @@ class VariableValue(RowClass):
14 14
     value: int = None
15 15
 
16 16
     def __init__(self, **kwargs):
17
-        if 'type' in kwargs.keys() and isinstance(kwargs['type'], VariableType):
17
+        if 'type' in kwargs.keys() and not isinstance(kwargs['type'], VariableType):
18 18
             kwargs['type'] = VariableType.get(kwargs['type'])
19 19
 
20 20
 
@@ -51,7 +51,7 @@ class VariableValue(RowClass):
51 51
 
52 52
 
53 53
     def get_value(self):
54
-        return self.type.cast()(self.value)
54
+        return self.type.cast(self.value)
55 55
 
56 56
     def equals(self, var_value):
57 57
         return self.get_value() == var_value.get_value()
@@ -77,6 +77,6 @@ class VariableValue(RowClass):
77 77
             LIMIT 1""", {
78 78
                 'type':str(var_type), 'address':address, 'time':int(time.strftime('%s'))+1
79 79
             }))
80
-            return VariableValue(**dict(row))
80
+            return VariableValue(**{ **dict(row), **{ 'type': var_type } })
81 81
         except StopIteration as e:
82 82
             raise e

Loading…
Cancel
Save