瀏覽代碼

[models] fix type casting (issue #30)

Maxime Alves LIRMM@home 3 年之前
父節點
當前提交
c2ffe4c5cf
共有 3 個檔案被更改,包括 30 行新增15 行删除
  1. 12
    6
      pyheatpump/models/variable.py
  2. 15
    6
      pyheatpump/models/variable_type.py
  3. 3
    3
      pyheatpump/models/variable_value.py

+ 12
- 6
pyheatpump/models/variable.py 查看文件

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

+ 15
- 6
pyheatpump/models/variable_type.py 查看文件

19
         return self.slabel or self.label[0]
19
         return self.slabel or self.label[0]
20
 
20
 
21
 
21
 
22
+    @property
22
     def cast(self):
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
     def select(self):
39
     def select(self):
31
         try:
40
         try:

+ 3
- 3
pyheatpump/models/variable_value.py 查看文件

14
     value: int = None
14
     value: int = None
15
 
15
 
16
     def __init__(self, **kwargs):
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
             kwargs['type'] = VariableType.get(kwargs['type'])
18
             kwargs['type'] = VariableType.get(kwargs['type'])
19
 
19
 
20
 
20
 
51
 
51
 
52
 
52
 
53
     def get_value(self):
53
     def get_value(self):
54
-        return self.type.cast()(self.value)
54
+        return self.type.cast(self.value)
55
 
55
 
56
     def equals(self, var_value):
56
     def equals(self, var_value):
57
         return self.get_value() == var_value.get_value()
57
         return self.get_value() == var_value.get_value()
77
             LIMIT 1""", {
77
             LIMIT 1""", {
78
                 'type':str(var_type), 'address':address, 'time':int(time.strftime('%s'))+1
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
         except StopIteration as e:
81
         except StopIteration as e:
82
             raise e
82
             raise e

Loading…
取消
儲存