Browse Source

[db] remove non-working trigger to manage it into python code

Maxime Alves LIRMM@home 3 years ago
parent
commit
83bf463cc0
2 changed files with 22 additions and 32 deletions
  1. 1
    19
      db/pyheatpump.sql
  2. 21
    13
      pyheatpump/models/variable_value.py

+ 1
- 19
db/pyheatpump.sql View File

@@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS variable (
21 21
 CREATE TABLE IF NOT EXISTS var_value (
22 22
   type CHAR(1) NOT NULL,
23 23
   address INT NOT NULL,
24
-  time INT DEFAULT CURRENT_TIMESTAMP,
24
+  time INT DEFAULT (strftime('%s', datetime('now'))),
25 25
   value INT NOT NULL,
26 26
 
27 27
   FOREIGN KEY (type) REFERENCES variable(type)
@@ -38,24 +38,6 @@ INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
38 38
 INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
39 39
   'D', 'Digital', 'bool', 1, 2048);
40 40
 
41
-CREATE TRIGGER variable_insert BEFORE INSERT ON var_value
42
-FOR EACH ROW
43
-WHEN
44
-  NOT EXISTS (
45
-    SELECT MAX(time) FROM var_value
46
-    WHERE
47
-      NEW.type = type
48
-      AND
49
-      NEW.address = address
50
-      AND
51
-      NEW.value = value
52
-  )
53
-BEGIN
54
-  INSERT INTO var_value
55
-  (type, address, value)
56
-  VALUES (NEW.type, NEW.address, NEW.value);
57
-END;
58
-
59 41
 CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
60 42
 FOR EACH ROW
61 43
 BEGIN

+ 21
- 13
pyheatpump/models/variable_value.py View File

@@ -27,14 +27,24 @@ class VariableValue(RowClass):
27 27
 
28 28
     def insert(self):
29 29
 
30
+        try:
31
+            old_value = VariableValue.get(
32
+                self.type,
33
+                self.address)
34
+            if old_value.value == self.value:
35
+                # last variable value is equal to current value
36
+                # so do not insert
37
+                return False
38
+        except StopIteration:
39
+            # variable value was never inserted
40
+            pass
41
+
30 42
         try:
31 43
             sql(
32
-            """
33
-            INSERT INTO var_value
44
+            """INSERT INTO var_value
34 45
             (type, address, value)
35 46
             VALUES
36
-            ('{}', {}, {})
37
-            """.format(
47
+            ('{}', {}, {})""".format(
38 48
                 self.type, self.address, self.value
39 49
             ))
40 50
             return True
@@ -50,15 +60,13 @@ class VariableValue(RowClass):
50 60
     def get(type, address, time=datetime.now()):
51 61
         try:
52 62
             row = next(sql(
53
-            """
54
-                SELECT * FROM var_value
55
-                WHERE
56
-                    type = '{}'
57
-                    AND address = {}
58
-                    AND time <= {}
59
-                ORDER BY time DESC
60
-                LIMIT 1
61
-            """.format(
63
+            """SELECT * FROM var_value
64
+            WHERE
65
+                type = '{}'
66
+                AND address = {}
67
+                AND time <= {}
68
+            ORDER BY time DESC
69
+            LIMIT 1""".format(
62 70
                 type, address, int(time.strftime('%s'))+1
63 71
             )))
64 72
             return VariableValue(**dict(row))

Loading…
Cancel
Save