Browse Source

[db][fetch] fixed db insertion of new Variable objects

Maxime Alves LIRMM@home 3 years ago
parent
commit
45303c5e7b
2 changed files with 45 additions and 9 deletions
  1. 39
    8
      pyheatpump/cli.py
  2. 6
    1
      pyheatpump/db.py

+ 39
- 8
pyheatpump/cli.py View File

@@ -10,6 +10,7 @@ from pprint import pprint
10 10
 
11 11
 from pyheatpump.logger import logger_init
12 12
 from pyheatpump.models import *
13
+from pyheatpump.db import commit
13 14
 
14 15
 CONTEXT_SETTINGS={
15 16
     'default_map':{'run': {}}
@@ -30,7 +31,7 @@ def cli(ctx, version):
30 31
 def run(host, port):
31 32
     logger = logger_init()
32 33
 
33
-    from pyheatpump.conf import (API_HOST, API_PORT)
34
+    from .config import (API_HOST, API_PORT)
34 35
 
35 36
     if not host:
36 37
         host = API_HOST
@@ -46,7 +47,7 @@ def run(host, port):
46 47
         host=host,
47 48
         port=int(port),
48 49
         log_level=log_level,
49
-        reload=False)
50
+        reload=True)
50 51
 
51 52
 
52 53
 @cli.command()
@@ -64,9 +65,18 @@ def fetch():
64 65
     res = modbus.read_holding_registers(analog.start_address, analog.end_address)
65 66
 
66 67
     for r in range(len(res)):
67
-        val = VariableValue(**{
68
+        var = Variable(**{
68 69
             'type': analog,
69
-            'address': r + analog.start_address,
70
+            'address': r + analog.start_address})
71
+
72
+        if not var.exists():
73
+            logger.info('Insert variable {}:{}'.format(
74
+                var.type, var.address))
75
+            var.insert()
76
+
77
+        val = VariableValue(**{
78
+            'type': var.type,
79
+            'address': var.address,
70 80
             'value': res[r]})
71 81
         val.insert()
72 82
 
@@ -79,9 +89,18 @@ def fetch():
79 89
     res = modbus.read_holding_registers(integer.start_address, integer.end_address)
80 90
 
81 91
     for r in range(len(res)):
82
-        val = VariableValue(**{
92
+        var = Variable(**{
83 93
             'type': integer,
84
-            'address': r + integer.start_address,
94
+            'address': r + integer.start_address})
95
+
96
+        if not var.exists():
97
+            logger.info('Insert variable {}:{}'.format(
98
+                var.type, var.address))
99
+            var.insert()
100
+
101
+        val = VariableValue(**{
102
+            'type': var.type,
103
+            'address': var.address,
85 104
             'value': res[r]})
86 105
         val.insert()
87 106
 
@@ -93,8 +112,20 @@ def fetch():
93 112
     res = modbus.read_coils(digital.start_address, digital.end_address)
94 113
 
95 114
     for r in range(len(res)):
96
-        val = VariableValue(**{
115
+        var = Variable(**{
97 116
             'type': digital,
98
-            'address': r + digital.start_address,
117
+            'address': r + digital.start_address})
118
+
119
+        if not var.exists():
120
+            logger.info('Insert variable {}:{}'.format(
121
+                var.type, var.address))
122
+            var.insert()
123
+
124
+        val = VariableValue(**{
125
+            'type': var.type,
126
+            'address': var.address,
99 127
             'value': res[r]})
100 128
         val.insert()
129
+
130
+    commit()
131
+    logger.info('Successfully read all variables')

+ 6
- 1
pyheatpump/db.py View File

@@ -1,7 +1,7 @@
1 1
 #!/usr/bin/env python3
2 2
 import sqlite3
3 3
 from subprocess import Popen
4
-from .conf import config
4
+from .config import config
5 5
 import sys
6 6
 from pprint import pprint
7 7
 
@@ -19,6 +19,11 @@ def connect():
19 19
         conn.row_factory = sqlite3.Row
20 20
     return conn
21 21
 
22
+def commit():
23
+    global conn
24
+    logger.info('Commit database')
25
+    conn.commit()
26
+
22 27
 def initialize(filename):
23 28
     p = Popen(
24 29
         '/usr/bin/env sqlite3 -init {} {}'.format(filename, config['heatpump']['database']),

Loading…
Cancel
Save