change schema

This commit is contained in:
2023-05-23 15:57:50 +02:00
parent 863644fa56
commit 2d9372ea38
2 changed files with 33 additions and 22 deletions

View File

@ -17,29 +17,39 @@ create table variable_t (
variable_id varchar(16) not null,
label varchar(128) not null unique,
unit varchar(16),
quantity varchar(32),
unique (app, converter_id, device_id, variable_id)
);
create table measurement_t (
time timestamp without time zone not null,
application varchar(128) not null,
variable varchar(128) not null,
unit varchar(16),
variable integer references variable_t (id),
value float
);
select create_hypertable('measurement_t', 'time');
insert into application_t (app_id, label) values('ku226', 'Kupferdreher Str. 226, Essen');
insert into variable_t (app, converter_id, device_id, variable_id, label, unit)
values ((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'u', 'BHKW Spannung', 'V'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'p', 'BHKW Leistung', 'W'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'import', 'BHKW Energie Export', 'Wh'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'export', 'BHKW Energie Import', 'Wh'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'u', 'PV Spannung', 'V'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'p', 'PV Leistung', 'W'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'import', 'PV Energie Export', 'Wh'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'export', 'PV Energie Import', 'Wh')
insert into variable_t (app, converter_id, device_id, variable_id, label, unit, quantity)
values ((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'u', 'BHKW Spannung', 'V', 'voltage'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'p', 'BHKW Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'import', 'BHKW Energie Export', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'bhkw', 'export', 'BHKW Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'u', 'PV Spannung', 'V', 'voltage'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'p', 'PV Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'import', 'PV Energie Export', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'pv', 'export', 'PV Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '3', 'mains', 'import', 'Netz Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'w1', 'p', 'Wohnung 1 Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'w1', 'import', 'Wohnung 1 Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'w2', 'p', 'Wohnung 2 Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'w2', 'import', 'Wohnung 2 Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'w3', 'p', 'Wohnung 3 Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'w3', 'import', 'Wohnung 3 Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'w4', 'p', 'Wohnung 4 Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'w4', 'import', 'Wohnung 4 Energie Import', 'Wh', 'energy'),
((select id from application_t where app_id = 'ku226'), '1', 'w5', 'p', 'Wohnung 5 Leistung', 'W', 'power'),
((select id from application_t where app_id = 'ku226'), '1', 'w5', 'import', 'Wohnung 5 Energie Import', 'Wh', 'energy')
;
@ -49,4 +59,5 @@ insert into variable_t (app, converter_id, device_id, variable_id, label, unit)
grant select on application_t to preprocessor;
grant select on variable_t to preprocessor;
grant insert on measurement_t to preprocessor;
grant select on measurement_t to grafana;

View File

@ -46,7 +46,9 @@ class DbOp(object):
select
a.label as application,
v.label as variable,
v.unit as unit
v.quantity as quantity,
v.unit as unit,
v.id as vid
from application_t a, variable_t v
where a.app_id = %(appId)s and
v.app = a.id and
@ -60,7 +62,9 @@ class DbOp(object):
variable = {
'application': res[0],
'variable': res[1],
'unit': res[2]
'quantity': res[2]
'unit': res[3],
'vid': res[4]
}
logger.debug(f"{variable=}")
return variable
@ -78,14 +82,12 @@ class DbOp(object):
with conn:
with conn.cursor() as cur:
cur.execute("""
insert into measurement_t (time, application, variable, value, unit)
values (now(), %(application)s, %(variable)s, %(value)s, %(unit)s)
insert into measurement_t (time, variable, value)
values (now(), %(vid)s, %(value)s)
""",
{
'application': measurement['application'],
'variable': measurement['variable'],
'vid': measurement['vid'],
'value': measurement['value'],
'unit': measurement['unit']
}
)
except Exception as e:
@ -126,9 +128,7 @@ def mqttOnMessageCallback(client, userdata, message):
dbh = DbOp(config)
variable = dbh.getVariable(appId, converterId, deviceId, variableId)
measurement = {
"application": variable["application"],
"variable": variable["variable"],
"unit": variable["unit"],
"vid": variable["vid"],
"value": float(payload)
}