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

This commit is contained in:
Maxime Alves LIRMM@home 2020-08-02 15:01:59 +02:00
commit 45303c5e7b
2 changed files with 45 additions and 9 deletions

View file

@ -10,6 +10,7 @@ from pprint import pprint
from pyheatpump.logger import logger_init
from pyheatpump.models import *
from pyheatpump.db import commit
CONTEXT_SETTINGS={
'default_map':{'run': {}}
@ -30,7 +31,7 @@ def cli(ctx, version):
def run(host, port):
logger = logger_init()
from pyheatpump.conf import (API_HOST, API_PORT)
from .config import (API_HOST, API_PORT)
if not host:
host = API_HOST
@ -46,7 +47,7 @@ def run(host, port):
host=host,
port=int(port),
log_level=log_level,
reload=False)
reload=True)
@cli.command()
@ -64,9 +65,18 @@ def fetch():
res = modbus.read_holding_registers(analog.start_address, analog.end_address)
for r in range(len(res)):
val = VariableValue(**{
var = Variable(**{
'type': analog,
'address': r + analog.start_address,
'address': r + analog.start_address})
if not var.exists():
logger.info('Insert variable {}:{}'.format(
var.type, var.address))
var.insert()
val = VariableValue(**{
'type': var.type,
'address': var.address,
'value': res[r]})
val.insert()
@ -79,9 +89,18 @@ def fetch():
res = modbus.read_holding_registers(integer.start_address, integer.end_address)
for r in range(len(res)):
val = VariableValue(**{
var = Variable(**{
'type': integer,
'address': r + integer.start_address,
'address': r + integer.start_address})
if not var.exists():
logger.info('Insert variable {}:{}'.format(
var.type, var.address))
var.insert()
val = VariableValue(**{
'type': var.type,
'address': var.address,
'value': res[r]})
val.insert()
@ -93,8 +112,20 @@ def fetch():
res = modbus.read_coils(digital.start_address, digital.end_address)
for r in range(len(res)):
val = VariableValue(**{
var = Variable(**{
'type': digital,
'address': r + digital.start_address,
'address': r + digital.start_address})
if not var.exists():
logger.info('Insert variable {}:{}'.format(
var.type, var.address))
var.insert()
val = VariableValue(**{
'type': var.type,
'address': var.address,
'value': res[r]})
val.insert()
commit()
logger.info('Successfully read all variables')

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
import sqlite3
from subprocess import Popen
from .conf import config
from .config import config
import sys
from pprint import pprint
@ -19,6 +19,11 @@ def connect():
conn.row_factory = sqlite3.Row
return conn
def commit():
global conn
logger.info('Commit database')
conn.commit()
def initialize(filename):
p = Popen(
'/usr/bin/env sqlite3 -init {} {}'.format(filename, config['heatpump']['database']),