Browse Source

[models] update of the model classes

Maxime Alves LIRMM@home 3 years ago
parent
commit
6133b81b05
2 changed files with 37 additions and 9 deletions
  1. 36
    9
      pyheatpump/models/variable.py
  2. 1
    0
      pyheatpump/models/variable_type.py

+ 36
- 9
pyheatpump/models/variable.py View File

@@ -13,9 +13,38 @@ class Variable(RowClass):
13 13
     type: VariableType = None
14 14
 
15 15
     def __init__(self, **kwargs):
16
+        if 'type' in kwargs.keys() and type(kwargs['type']) != VariableType:
17
+            kwargs['type'] = VariableType.get(kwargs['type'])
18
+
16 19
         super().__init__(**kwargs)
17 20
 
18 21
 
22
+    def insert(self):
23
+
24
+        try:
25
+            sql(
26
+            """INSERT INTO variable
27
+            (type, address)
28
+            VALUES
29
+            ('{}', {})
30
+            """.format(
31
+                self.type, self.address
32
+            ))
33
+            return True
34
+        except Exception as e:
35
+            print(e)
36
+            return False
37
+
38
+
39
+    def exists(self):
40
+        try:
41
+            return bool(next(sql(
42
+            """SELECT 1 FROM variable
43
+            WHERE type = '{}' AND address = {}
44
+            """.format(self.type, self.address))))
45
+        except StopIteration:
46
+            return False
47
+
19 48
     @staticmethod
20 49
     def getall():
21 50
         return dict([
@@ -29,12 +58,11 @@ class Variable(RowClass):
29 58
         return dict([
30 59
             (row['address'], Variable(**dict(row)))
31 60
             for row in sql(
32
-            """
33
-            SELECT * FROM variable
61
+            """SELECT * FROM variable
34 62
             WHERE type = '{}'
35 63
                 AND address >= {}
36
-                AND address <= {}
37
-            """.format(type, type.start_address, type.end_address))
64
+                AND address <= {}""".format(
65
+            type, type.start_address, type.end_address))
38 66
         ])
39 67
 
40 68
 
@@ -43,17 +71,16 @@ class Variable(RowClass):
43 71
          return dict([
44 72
             (row['address'], row['value'])
45 73
             for row in sql(
46
-            """
47
-            SELECT var.address, val.value FROM variable var
74
+            """SELECT var.address, val.value FROM variable var
48 75
             LEFT JOIN var_value val ON
49 76
                 var.type = val.type
50 77
                 AND var.address = val.address
51
-                AND var.last_update = var.time
78
+                AND var.last_update = val.time
52 79
             WHERE
53 80
                 var.type = '{}'
54 81
                 AND var.address >= {}
55
-                AND var.address <= {}
56
-            """.format(type, type.start_address, type.end_address))
82
+                AND var.address <= {}""".format(
83
+            type, type.start_address, type.end_address))
57 84
         ])
58 85
 
59 86
 

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

@@ -56,6 +56,7 @@ class VariableType(RowClass):
56 56
         return Variable.getall_of_type(self)
57 57
 
58 58
     def get_variables_values(self):
59
+        from .variable import Variable
59 60
         return Variable.getall_values_of_type(self)
60 61
 
61 62
     @staticmethod

Loading…
Cancel
Save