Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
42b307ff7b
|
|||
943516f1ac
|
|||
1a8e76dc32
|
|||
3e4c621645
|
|||
c8e60df30b
|
|||
664a2831ab
|
|||
00524c0a3f
|
|||
3af9482880
|
|||
df353d4f6c
|
|||
d1bbbeaccf
|
|||
8cfc92c226
|
|||
08e81e309c
|
|||
f44664eaad
|
|||
15458b9955
|
|||
f55990cc57
|
|||
766355f85d
|
|||
73aaa2225d
|
|||
44b7461d19
|
|||
c54b335e5f
|
|||
956d1bdcdb
|
|||
b938d48c7f
|
|||
b374b7f49d
|
|||
879825a260
|
|||
b6132afb11
|
|||
5e94782575
|
|||
57c63adeb2
|
@ -2,7 +2,7 @@ steps:
|
|||||||
build:
|
build:
|
||||||
image: plugins/kaniko
|
image: plugins/kaniko
|
||||||
settings:
|
settings:
|
||||||
repo: gitea.hottis.de/wn/udi
|
repo: ${FORGE_NAME}/${CI_REPO}
|
||||||
registry:
|
registry:
|
||||||
from_secret: container_registry
|
from_secret: container_registry
|
||||||
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
||||||
@ -14,6 +14,13 @@ steps:
|
|||||||
when:
|
when:
|
||||||
- event: [push, tag]
|
- event: [push, tag]
|
||||||
|
|
||||||
|
scan_image:
|
||||||
|
image: aquasec/trivy
|
||||||
|
commands:
|
||||||
|
- trivy image $FORGE_NAME/$CI_REPO:$CI_COMMIT_SHA --quiet --exit-code 1
|
||||||
|
when:
|
||||||
|
- event: [push, tag]
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
image: portainer/kubectl-shell:latest
|
image: portainer/kubectl-shell:latest
|
||||||
secrets:
|
secrets:
|
||||||
|
@ -13,4 +13,3 @@ ENV UDI_CONF ""
|
|||||||
COPY --from=builder /go/src/udi ./
|
COPY --from=builder /go/src/udi ./
|
||||||
ENTRYPOINT ["./udi"]
|
ENTRYPOINT ["./udi"]
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ if [ "$IMAGE_TAG" == "" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
IMAGE_NAME=gitea.hottis.de/wn/udi
|
IMAGE_NAME=$FORGE_NAME/$CI_REPO
|
||||||
|
|
||||||
CONFIG_FILE=config.json
|
CONFIG_FILE=config.json
|
||||||
|
|
||||||
|
@ -4,6 +4,15 @@
|
|||||||
"tlsEnable": "false"
|
"tlsEnable": "false"
|
||||||
},
|
},
|
||||||
"topicMappings": [
|
"topicMappings": [
|
||||||
|
{
|
||||||
|
"topics": [ "snmp" ],
|
||||||
|
"handler": "SNMP",
|
||||||
|
"id": "SNMP",
|
||||||
|
"config": {
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"topics": [ "dt1/ai/periodic/1" ],
|
"topics": [ "dt1/ai/periodic/1" ],
|
||||||
"handler": "DT1T",
|
"handler": "DT1T",
|
||||||
@ -44,6 +53,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"topics": [ "locative/event/#" ],
|
||||||
|
"handler": "Locative",
|
||||||
|
"id": "Locative",
|
||||||
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"topics": [ "IoT/MBGW3/Measurement" ],
|
"topics": [ "IoT/MBGW3/Measurement" ],
|
||||||
"handler": "MBGW3",
|
"handler": "MBGW3",
|
||||||
|
@ -4,8 +4,10 @@ from loguru import logger
|
|||||||
try:
|
try:
|
||||||
srcConn = psycopg2.connect(database="level_monitoring_berresheim")
|
srcConn = psycopg2.connect(database="level_monitoring_berresheim")
|
||||||
srcConn.autocommit = False
|
srcConn.autocommit = False
|
||||||
|
destConn = psycopg2.connect(database="udi-berresheim")
|
||||||
|
destConn.autocommit = False
|
||||||
|
|
||||||
with srcConn.cursor() as srcCur:
|
with srcConn.cursor() as srcCur, destConn.cursor() as destCur:
|
||||||
srcCur.execute("select time, application_name, raw_level, level, status, battery from measurement_t")
|
srcCur.execute("select time, application_name, raw_level, level, status, battery from measurement_t")
|
||||||
for srcObj in srcCur:
|
for srcObj in srcCur:
|
||||||
timestamp = srcObj[0]
|
timestamp = srcObj[0]
|
||||||
@ -20,10 +22,17 @@ try:
|
|||||||
destTime = timestamp
|
destTime = timestamp
|
||||||
destApplication = "de-hottis-level-monitoring"
|
destApplication = "de-hottis-level-monitoring"
|
||||||
destDevice = "eui-a84041a2c18341d6"
|
destDevice = "eui-a84041a2c18341d6"
|
||||||
destAttributes = '{"ApplicationId":"de-hottis-level-monitoring", "DeviceType":"dragino-ldds75", "Hint": "Migrated"}'
|
destAttributes = '{"ApplicationId":"de-hottis-level-monitoring", "DeviceType":"dragino-ldds75", "Status":"' + status + '","Hint": "Migrated"}'
|
||||||
destValues = '{"Battery":{"unit":"V","label":"Battery","value":' + str(battery) + ',"variable":"Voltage"}, "Distance":{"unit":mm","label":"Distance","variable":"Level","value":' + str(rawLevel) + '}, "CorrectedDistance":{"unit":"mm", "label":"CorrectedDistance", "variable":"Level","value":' + str(level) + '}}'
|
destValues = '{"Battery":{"unit":"V","label":"Battery","value":' + str(battery) + ',"variable":"Voltage"}, "Distance":{"unit":"mm","label":"Distance","variable":"Level","value":' + str(rawLevel) + '}, "CorrectedDistance":{"unit":"mm", "label":"CorrectedDistance", "variable":"Level","value":' + str(level) + '}}'
|
||||||
logger.info(f"{destTime=}, {destApplication=}, {destDevice=}, {destAttributes=}, {destValues=}")
|
logger.info(f"{destTime=}, {destApplication=}, {destDevice=}, {destAttributes=}, {destValues=}")
|
||||||
|
|
||||||
|
destCur.execute("insert into measurements (time, application, device, attributes, values) values(%s, %s, %s, %s, %s)",
|
||||||
|
(destTime, destApplication, destDevice, destAttributes, destValues))
|
||||||
|
destConn.commit()
|
||||||
finally:
|
finally:
|
||||||
if srcConn:
|
if srcConn:
|
||||||
srcConn.close()
|
srcConn.close()
|
||||||
|
if destConn:
|
||||||
|
destConn.close()
|
||||||
|
|
||||||
|
|
||||||
|
79
migration/migrate-pv.py
Normal file
79
migration/migrate-pv.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import psycopg2
|
||||||
|
from loguru import logger
|
||||||
|
import os
|
||||||
|
|
||||||
|
srcPgHost = os.environ["SRC_PGHOST"]
|
||||||
|
srcPgUser = os.environ["SRC_PGUSER"]
|
||||||
|
srcPgPassword = os.environ["SRC_PGPASSWORD"]
|
||||||
|
srcPgDatabase = os.environ["SRC_PGDATABASE"]
|
||||||
|
destPgHost = os.environ["DEST_PGHOST"]
|
||||||
|
destPgUser = os.environ["DEST_PGUSER"]
|
||||||
|
destPgPassword = os.environ["DEST_PGPASSWORD"]
|
||||||
|
destPgDatabase = os.environ["DEST_PGDATABASE"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
srcConn = psycopg2.connect(
|
||||||
|
host=srcPgHost,
|
||||||
|
dbname=srcPgDatabase,
|
||||||
|
user=srcPgUser,
|
||||||
|
password=srcPgPassword,
|
||||||
|
sslmode='require'
|
||||||
|
)
|
||||||
|
srcConn.autocommit = False
|
||||||
|
|
||||||
|
destConn = psycopg2.connect(
|
||||||
|
host=destPgHost,
|
||||||
|
dbname=destPgDatabase,
|
||||||
|
user=destPgUser,
|
||||||
|
password=destPgPassword,
|
||||||
|
sslmode='require'
|
||||||
|
)
|
||||||
|
destConn.autocommit = False
|
||||||
|
|
||||||
|
with srcConn.cursor() as srcCur, destConn.cursor() as destCur:
|
||||||
|
srcCur.execute("select time, deviceid, status, state, importenergyactive, importenergyreactive, exportenergyactive, exportenergyreactive, powerapparent, poweractive, powerreactive, powerdemandpositive, powerdemandreverse, factor, angle, voltage, current, powerdemand from pv_power_measurement_t order by time")
|
||||||
|
for srcObj in srcCur:
|
||||||
|
timestamp = srcObj[0]
|
||||||
|
deviceName = srcObj[1]
|
||||||
|
status = srcObj[2]
|
||||||
|
state = srcObj[3]
|
||||||
|
importenergyactive = srcObj[4]
|
||||||
|
importenergyreactive = srcObj[5]
|
||||||
|
exportenergyactive = srcObj[6]
|
||||||
|
exportenergyreactive = srcObj[7]
|
||||||
|
powerapparent = srcObj[8]
|
||||||
|
poweractive = srcObj[9]
|
||||||
|
powerreactive = srcObj[10]
|
||||||
|
powerdemandpositive = srcObj[11]
|
||||||
|
powerdemandreverse = srcObj[12]
|
||||||
|
factor = srcObj[13]
|
||||||
|
angle = srcObj[14]
|
||||||
|
voltage = srcObj[15]
|
||||||
|
current = srcObj[16]
|
||||||
|
powerdemand = srcObj[17]
|
||||||
|
|
||||||
|
|
||||||
|
logger.info(f"{timestamp=}, {deviceName=}")
|
||||||
|
|
||||||
|
destTime = timestamp
|
||||||
|
destApplication = "PV"
|
||||||
|
destDevice = "Powermeter"
|
||||||
|
destAttributes = f"{{\"ApplicationId\":\"PV\", \"Status\":\"{status}\",\"Hint\": \"Migrated\"}}"
|
||||||
|
destValues = f"{{\"Cnt\": {{\"unit\": \"\", \"label\": \"\", \"value\": \"-1\", \"variable\": \"Cnt\"}}, \"Angle\": {{\"unit\": \"degree\", \"label\": \"\", \"value\": \"{angle}\", \"variable\": \"Angle\"}}, \"State\": {{\"unit\": \"\", \"label\": \"\", \"value\": \"{state}\", \"variable\": \"State\"}}, \"Factor\": {{\"unit\": \"\", \"label\": \"\", \"value\": \"{factor}\", \"variable\": \"Factor\"}}, \"Current\": {{\"unit\": \"A\", \"label\": \"\", \"value\": \"{current}\", \"variable\": \"Current\"}}, \"Voltage\": {{\"unit\": \"V\", \"label\": \"\", \"value\": \"{voltage}\", \"variable\": \"Voltage\"}}, \"PowerActive\": {{\"unit\": \"W\", \"label\": \"\", \"value\": \"{poweractive}\", \"variable\": \"PowerActive\"}}, \"PowerApparent\": {{\"unit\": \"VA\", \"label\": \"\", \"value\": \"{powerapparent}\", \"variable\": \"PowerApparent\"}}, \"PowerReactive\": {{\"unit\": \"VA\", \"label\": \"\", \"value\": \"{powerreactive}\", \"variable\": \"PowerReactive\"}}, \"ExportEnergyActive\": {{\"unit\": \"Wh\", \"label\": \"\", \"value\": \"{exportenergyactive}\", \"variable\": \"ExportEnergyActive\"}}, \"ImportEnergyActive\": {{\"unit\": \"Wh\", \"label\": \"\", \"value\": \"{importenergyactive}\", \"variable\": \"ImportEnergyActive\"}}, \"PowerDemandReverse\": {{\"unit\": \"W\", \"label\": \"\", \"value\": \"{powerdemandreverse}\", \"variable\": \"PowerDemandReverse\"}}, \"PowerDemandPositive\": {{\"unit\": \"W\", \"label\": \"\", \"value\": \"{powerdemandpositive}\", \"variable\": \"PowerDemandPositive\"}}, \"ExportEnergyReactive\": {{\"unit\": \"VAh\", \"label\": \"\", \"value\": \"{exportenergyreactive}\", \"variable\": \"ExportEnergyReactive\"}}, \"ImportEnergyReactive\": {{\"unit\": \"VAh\", \"label\": \"\", \"value\": \"{importenergyreactive}\", \"variable\": \"ImportEnergyReactive\"}}}}"
|
||||||
|
logger.info(f"{destTime=}, {destApplication=}, {destDevice=}, {destAttributes=}, {destValues=}")
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
destCur.execute("insert into measurements (time, application, device, attributes, values) values(%s, %s, %s, %s, %s)",
|
||||||
|
(destTime, destApplication, destDevice, destAttributes, destValues))
|
||||||
|
destConn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
destConn.rollback()
|
||||||
|
logger.error(f"Error {e} when inserted time {destTime}")
|
||||||
|
finally:
|
||||||
|
if srcConn:
|
||||||
|
srcConn.close()
|
||||||
|
if destConn:
|
||||||
|
destConn.close()
|
||||||
|
|
||||||
|
|
78
migration/migrate-temperature.py
Normal file
78
migration/migrate-temperature.py
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import psycopg2
|
||||||
|
from loguru import logger
|
||||||
|
import os
|
||||||
|
|
||||||
|
srcPgHost = os.environ["SRC_PGHOST"]
|
||||||
|
srcPgUser = os.environ["SRC_PGUSER"]
|
||||||
|
srcPgPassword = os.environ["SRC_PGPASSWORD"]
|
||||||
|
srcPgDatabase = os.environ["SRC_PGDATABASE"]
|
||||||
|
destPgHost = os.environ["DEST_PGHOST"]
|
||||||
|
destPgUser = os.environ["DEST_PGUSER"]
|
||||||
|
destPgPassword = os.environ["DEST_PGPASSWORD"]
|
||||||
|
destPgDatabase = os.environ["DEST_PGDATABASE"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
srcConn = psycopg2.connect(
|
||||||
|
host=srcPgHost,
|
||||||
|
dbname=srcPgDatabase,
|
||||||
|
user=srcPgUser,
|
||||||
|
password=srcPgPassword,
|
||||||
|
sslmode='require'
|
||||||
|
)
|
||||||
|
srcConn.autocommit = False
|
||||||
|
|
||||||
|
destConn = psycopg2.connect(
|
||||||
|
host=destPgHost,
|
||||||
|
dbname=destPgDatabase,
|
||||||
|
user=destPgUser,
|
||||||
|
password=destPgPassword,
|
||||||
|
sslmode='require'
|
||||||
|
)
|
||||||
|
destConn.autocommit = False
|
||||||
|
|
||||||
|
with srcConn.cursor() as srcCur, destConn.cursor() as destCur:
|
||||||
|
srcCur.execute("select time, location, status, temperature, category from room_climate_measurement_t where category = 'heating' and time > '2023-12-19 05:20:00' order by time")
|
||||||
|
for srcObj in srcCur:
|
||||||
|
timestamp = srcObj[0]
|
||||||
|
location = srcObj[1]
|
||||||
|
status = srcObj[2]
|
||||||
|
temperature = srcObj[3]
|
||||||
|
category = srcObj[4]
|
||||||
|
|
||||||
|
logger.info(f"{timestamp=}, {location=}, {status=}, {temperature=}, {category=}")
|
||||||
|
|
||||||
|
destTime = timestamp
|
||||||
|
|
||||||
|
match category:
|
||||||
|
case 'heating':
|
||||||
|
destApplication = 'Temperature Heating'
|
||||||
|
case 'Outdoor':
|
||||||
|
destApplication = 'Temperature Wago'
|
||||||
|
case 'Device':
|
||||||
|
destApplication = 'Temperature Wago'
|
||||||
|
case 'Indoor':
|
||||||
|
destApplication = 'Temperature Multisensor' if location != 'Anna-Koeln-2' else 'Temperature Shelly Plus HT'
|
||||||
|
case 'Special':
|
||||||
|
destApplication = 'Temperature Multisensor'
|
||||||
|
|
||||||
|
destDevice = location
|
||||||
|
destAttributes = '{"ApplicationId":"temperature-imported", "Status":"' + status + '","Location":"' + location + '","Category":"' + category + '","Hint": "Migrated"}'
|
||||||
|
destValues = '{"Value": {"unit": "°C", "label": "", "value": "' + str(temperature) + '", "variable": ""}}'
|
||||||
|
|
||||||
|
logger.info(f"{destTime=}, {destApplication=}, {destDevice=}, {destAttributes=}, {destValues=}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
destCur.execute("insert into measurements (time, application, device, attributes, values) values(%s, %s, %s, %s, %s)",
|
||||||
|
(destTime, destApplication, destDevice, destAttributes, destValues))
|
||||||
|
destConn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
destConn.rollback()
|
||||||
|
logger.error(f"Error {e} when inserted time {destTime}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if srcConn:
|
||||||
|
srcConn.close()
|
||||||
|
|
||||||
|
if destConn:
|
||||||
|
destConn.close()
|
||||||
|
|
8
queries/berresheim.sql
Normal file
8
queries/berresheim.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
create or replace view level_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'CorrectedDistance'->>'value' as float) as level,
|
||||||
|
cast(values->'Battery'->>'value' as float) as battery,
|
||||||
|
attributes->>'Status' as status,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'de-hottis-level-monitoring';
|
@ -45,3 +45,53 @@ create or replace view temperature_v as
|
|||||||
from measurements
|
from measurements
|
||||||
where application in ('Temperature Multisensor', 'Temperature Shelly Plus HT');
|
where application in ('Temperature Multisensor', 'Temperature Shelly Plus HT');
|
||||||
|
|
||||||
|
create or replace view temperature2_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Value'->>'value' as float) as temperature,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Temperature Wago';
|
||||||
|
|
||||||
|
create or replace view humidity_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Value'->>'value' as float) as humidity,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application in ('Humidity Multisensor');
|
||||||
|
|
||||||
|
create or replace view soil_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Water'->>'value' as float) as water,
|
||||||
|
cast(values->'Conductance'->>'value' as float) as conductance,
|
||||||
|
cast(values->'Temperature'->>'value' as float) as temperature,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'de-hottis-app01' and attributes->>'DeviceType' = 'dragino-lse01';
|
||||||
|
|
||||||
|
create or replace view co2_v as
|
||||||
|
select time,
|
||||||
|
cast(m.values->'CO2concentration'->>'value' as float) as co2concentration,
|
||||||
|
cast(m.values->'Humidity'->>'value' as float) as humidity,
|
||||||
|
cast(m.values->'Temperature'->>'value' as float) as temperature,
|
||||||
|
m.device as device,
|
||||||
|
d.attributes->>'Label' as label
|
||||||
|
from measurements m, devices d
|
||||||
|
where m.application = 'de-hottis-app01' and
|
||||||
|
m.attributes->>'DeviceType' = 'hottis-scd30' and
|
||||||
|
m.device = d.label;
|
||||||
|
|
||||||
|
create or replace view locative_v as
|
||||||
|
select time,
|
||||||
|
device as person,
|
||||||
|
values->'Location'->>'value' as location,
|
||||||
|
values->'Trigger'->>'value' as direction
|
||||||
|
from measurements
|
||||||
|
where application = 'Locative';
|
||||||
|
|
||||||
|
create or replace view router_v as
|
||||||
|
select time,
|
||||||
|
device,
|
||||||
|
cast(values->'wan-in'->>'value' as int) as wanInOctetsPerSeconds,
|
||||||
|
cast(values->'wan-out'->>'value' as int) as wanOutOctetsPerSeconds
|
||||||
|
from measurements
|
||||||
|
where application = 'SNMP' and device = '172.16.3.1';
|
||||||
|
11
queries/old-daily-temperature-query.sql
Normal file
11
queries/old-daily-temperature-query.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
select
|
||||||
|
extract('day' from time)::varchar || '.' || extract('month' from time)::varchar || '.' || extract('year' from time)::varchar as day,
|
||||||
|
avg(temperature)::numeric(10,0) as temperature
|
||||||
|
from room_climate_measurement_t
|
||||||
|
where
|
||||||
|
category = 'Outdoor' and
|
||||||
|
location = 'Outdoor' and
|
||||||
|
extract('hour' from time) = 12 and
|
||||||
|
time::date = now()::date
|
||||||
|
group by day
|
||||||
|
|
73
queries/old-pv-yield-query.sql
Normal file
73
queries/old-pv-yield-query.sql
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
-- query
|
||||||
|
|
||||||
|
with
|
||||||
|
first_day_in_year as (
|
||||||
|
select
|
||||||
|
date_trunc('day', min(time)) as day
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('year', time) and now()
|
||||||
|
),
|
||||||
|
first_value_in_year as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between (select day from first_day_in_year) and (select day from first_day_in_year) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
first_day_in_month as (
|
||||||
|
select
|
||||||
|
date_trunc('day', min(time)) as day
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('month', now()) and now()
|
||||||
|
),
|
||||||
|
first_value_in_month as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between (select day from first_day_in_month) and (select day from first_day_in_month) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
first_value_in_day as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
first(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where time >= date_trunc('day', now())
|
||||||
|
group by interval
|
||||||
|
),
|
||||||
|
last_value as (
|
||||||
|
select
|
||||||
|
time_bucket('1 day', time) as interval,
|
||||||
|
last(exportenergyactive, time) as energy
|
||||||
|
from pv_power_measurement_t
|
||||||
|
where
|
||||||
|
time between date_trunc('day', now()) and date_trunc('day', now()) + interval '1 day' and
|
||||||
|
status = 'Ok'
|
||||||
|
group by interval
|
||||||
|
)
|
||||||
|
select
|
||||||
|
extract(year from (select day from first_day_in_year))::text as period_value,
|
||||||
|
'Year' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_year))::numeric, 2) as yield
|
||||||
|
union
|
||||||
|
select
|
||||||
|
to_char((select day from first_day_in_month), 'Month') as period_value,
|
||||||
|
'Month' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_month))::numeric, 2) as yield
|
||||||
|
union
|
||||||
|
select
|
||||||
|
now()::date::text as period_value,
|
||||||
|
'Day' as period_name,
|
||||||
|
round(((select energy from last_value) - (select energy from first_value_in_day))::numeric, 2) as yield;
|
||||||
|
|
||||||
|
-- output format
|
||||||
|
-- wn@atuin:~/Workspace/go-workspace/src/universal-data-ingest [main ≡ +0 ~1 -0 !]$ mosquitto_sub -h 172.23.1.102 -v -t IoT/PV/Yields
|
||||||
|
-- IoT/PV/Yields {"Month":"1.43","Year":"285.39","Day":"0.00"}
|
@ -3,13 +3,7 @@ create or replace view power_v as
|
|||||||
cast(values->'ActivePowerL1'->>'value' as float) as power_l1,
|
cast(values->'ActivePowerL1'->>'value' as float) as power_l1,
|
||||||
cast(values->'ActivePowerL2'->>'value' as float) as power_l2,
|
cast(values->'ActivePowerL2'->>'value' as float) as power_l2,
|
||||||
cast(values->'ActivePowerL3'->>'value' as float) as power_l3,
|
cast(values->'ActivePowerL3'->>'value' as float) as power_l3,
|
||||||
device
|
cast(values->'ActivePowerL123'->>'value' as float) as power_total,
|
||||||
from measurements
|
|
||||||
where application = 'com-passavant-geiger-poc' and
|
|
||||||
attributes->>'FPort' = '1';
|
|
||||||
|
|
||||||
create or replace view power_factor_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'PowerfactorL1'->>'value' as float) as factor_l1,
|
cast(values->'PowerfactorL1'->>'value' as float) as factor_l1,
|
||||||
cast(values->'PowerfactorL2'->>'value' as float) as factor_l2,
|
cast(values->'PowerfactorL2'->>'value' as float) as factor_l2,
|
||||||
cast(values->'PowerfactorL3'->>'value' as float) as factor_l3,
|
cast(values->'PowerfactorL3'->>'value' as float) as factor_l3,
|
||||||
|
@ -2,9 +2,13 @@ if [ "$1" = "" ]; then
|
|||||||
echo "set namespace as argument"
|
echo "set namespace as argument"
|
||||||
fi
|
fi
|
||||||
N=$1
|
N=$1
|
||||||
|
if [ "$2" = "" ]; then
|
||||||
|
echo "set instance as argument"
|
||||||
|
fi
|
||||||
|
I=$2
|
||||||
PGHOST=`kubectl get services traefik -n system -o jsonpath="{.status.loadBalancer.ingress[0].ip}"`
|
PGHOST=`kubectl get services traefik -n system -o jsonpath="{.status.loadBalancer.ingress[0].ip}"`
|
||||||
PGPASSWORD=`kubectl get secrets udi-db-cred -n $N -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
PGPASSWORD=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
||||||
PGUSER=`kubectl get secrets udi-db-cred -n $N -o jsonpath="{.data.PGUSER}" | base64 --decode`
|
PGUSER=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGUSER}" | base64 --decode`
|
||||||
PGSSLMODE=`kubectl get secrets udi-db-cred -n $N -o jsonpath="{.data.PGSSLMODE}" | base64 --decode`
|
PGSSLMODE=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGSSLMODE}" | base64 --decode`
|
||||||
PGDATABASE=`kubectl get secrets udi-db-cred -n $N -o jsonpath="{.data.PGDATABASE}" | base64 --decode`
|
PGDATABASE=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGDATABASE}" | base64 --decode`
|
||||||
export PGUSER PGHOST PGPASSWORD PGSSLMODE PGDATABASE
|
export PGUSER PGHOST PGPASSWORD PGSSLMODE PGDATABASE
|
||||||
|
@ -16,6 +16,8 @@ import "udi/handlers/mbgw3"
|
|||||||
import "udi/handlers/sver"
|
import "udi/handlers/sver"
|
||||||
import "udi/handlers/svej"
|
import "udi/handlers/svej"
|
||||||
import "udi/handlers/dt1t"
|
import "udi/handlers/dt1t"
|
||||||
|
import "udi/handlers/locative"
|
||||||
|
import "udi/handlers/snmp"
|
||||||
|
|
||||||
|
|
||||||
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||||
@ -44,6 +46,10 @@ func InitDispatcher() {
|
|||||||
factory = svej.New
|
factory = svej.New
|
||||||
case "DT1T":
|
case "DT1T":
|
||||||
factory = dt1t.New
|
factory = dt1t.New
|
||||||
|
case "Locative":
|
||||||
|
factory = locative.New
|
||||||
|
case "SNMP":
|
||||||
|
factory = snmp.New
|
||||||
default:
|
default:
|
||||||
factory = nil
|
factory = nil
|
||||||
log.Printf("No handler %s found, ignore mapping", mapping.Handler)
|
log.Printf("No handler %s found, ignore mapping", mapping.Handler)
|
||||||
|
@ -17,8 +17,8 @@ require (
|
|||||||
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
github.com/jackc/pgx/v5 v5.4.3 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
golang.org/x/crypto v0.14.0 // indirect
|
golang.org/x/crypto v0.19.0 // indirect
|
||||||
golang.org/x/net v0.10.0 // indirect
|
golang.org/x/net v0.20.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/text v0.13.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -28,12 +28,18 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
|
|||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||||
|
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
|
||||||
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
|
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||||
|
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
73
src/udi/handlers/locative/locative.go
Normal file
73
src/udi/handlers/locative/locative.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package locative
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
"log"
|
||||||
|
"encoding/json"
|
||||||
|
"udi/config"
|
||||||
|
"udi/handlers/handler"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type LocativeHandler struct {
|
||||||
|
handler.CommonHandler
|
||||||
|
dbh *database.DatabaseHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
type locativeEvent struct {
|
||||||
|
Trigger string `json:"trigger"`
|
||||||
|
Device string `json:"device"`
|
||||||
|
Location string `json:"location"`
|
||||||
|
Latitude string `json:"latitude"`
|
||||||
|
Longitude string `json:"longitude"`
|
||||||
|
Person string `json:"person"`
|
||||||
|
Timestamp string `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
|
t := &LocativeHandler {
|
||||||
|
}
|
||||||
|
t.Id = id
|
||||||
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (self *LocativeHandler) Handle(message handler.MessageT) {
|
||||||
|
log.Printf("Handler Locative %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
var locativeEvent locativeEvent
|
||||||
|
err := json.Unmarshal([]byte(message.Payload), &locativeEvent)
|
||||||
|
if err != nil {
|
||||||
|
self.Lost("Unable to parse payload into locativeEvent struct", err, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
variables := make(map[string]database.VariableType)
|
||||||
|
locativeEventStructValue := reflect.ValueOf(locativeEvent)
|
||||||
|
for i := 0; i < locativeEventStructValue.NumField(); i++ {
|
||||||
|
field := locativeEventStructValue.Type().Field(i)
|
||||||
|
fieldValue := locativeEventStructValue.Field(i)
|
||||||
|
v := database.VariableType {
|
||||||
|
Label: "",
|
||||||
|
Variable: field.Name,
|
||||||
|
Unit: "",
|
||||||
|
Value: fieldValue.Interface(),
|
||||||
|
}
|
||||||
|
variables[field.Name] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement := database.Measurement {
|
||||||
|
Time: time.Now(),
|
||||||
|
Application: "Locative",
|
||||||
|
Device: locativeEvent.Person,
|
||||||
|
Values: variables,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
|
self.S()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
75
src/udi/handlers/snmp/snmp.go
Normal file
75
src/udi/handlers/snmp/snmp.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package snmp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
"log"
|
||||||
|
"encoding/json"
|
||||||
|
"udi/config"
|
||||||
|
"udi/handlers/handler"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type SnmpHandler struct {
|
||||||
|
handler.CommonHandler
|
||||||
|
dbh *database.DatabaseHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
type endpoint_t struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Variable string `json:"variable"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type observation_t struct {
|
||||||
|
Device string `json:"device"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Variables map[string]endpoint_t `json:"variables"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
|
t := &SnmpHandler {
|
||||||
|
}
|
||||||
|
t.Id = id
|
||||||
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *SnmpHandler) Handle(message handler.MessageT) {
|
||||||
|
log.Printf("Handler SNMP %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
var observation observation_t
|
||||||
|
err := json.Unmarshal([]byte(message.Payload), &observation)
|
||||||
|
if err != nil {
|
||||||
|
self.Lost("Unable to parse payload into Observation struct", err, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var measurement database.Measurement
|
||||||
|
measurement.Time = time.Now()
|
||||||
|
|
||||||
|
measurement.Application = "SNMP"
|
||||||
|
measurement.Device = observation.Device
|
||||||
|
|
||||||
|
measurement.Attributes = map[string]interface{} {
|
||||||
|
"Label": observation.Label,
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
|
for k, v := range observation.Variables {
|
||||||
|
measurement.Values[k] = database.VariableType {
|
||||||
|
Label: v.Label,
|
||||||
|
Variable: v.Variable,
|
||||||
|
Unit: "",
|
||||||
|
Value: v.Value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Prepared measurement item: %s", measurement)
|
||||||
|
|
||||||
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
|
self.S()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -2,7 +2,7 @@ package draginoLdds75
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
// "log"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -26,7 +26,7 @@ type message struct {
|
|||||||
TempC_DS18B20 string `json:"TempC_DS18B20"`
|
TempC_DS18B20 string `json:"TempC_DS18B20"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error {
|
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
||||||
if fPort != 2 {
|
if fPort != 2 {
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
}
|
}
|
||||||
@ -55,11 +55,20 @@ func Parse(fPort int, decodedPayload []byte, variables *map[string]database.Vari
|
|||||||
Unit: "mm",
|
Unit: "mm",
|
||||||
Value: distance,
|
Value: distance,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if distance == 20 {
|
||||||
|
(*attributes)["Status"] = "invalid value"
|
||||||
|
} else if distance == 0 {
|
||||||
|
(*attributes)["Status"] = "no sensor detected"
|
||||||
|
} else {
|
||||||
|
(*attributes)["Status"] = "Ok"
|
||||||
|
}
|
||||||
|
|
||||||
groundLevelI, exists := device.Attributes["GroundLevel"]
|
groundLevelI, exists := device.Attributes["GroundLevel"]
|
||||||
groundLevelS, ok := groundLevelI.(string)
|
groundLevelS, ok := groundLevelI.(string)
|
||||||
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
||||||
if exists && err3 == nil && ok {
|
if exists && err3 == nil && ok {
|
||||||
log.Println("add corrected distance")
|
//log.Println("add corrected distance")
|
||||||
correctedDistance := groundLevel - distance
|
correctedDistance := groundLevel - distance
|
||||||
(*variables)["CorrectedDistance"] = database.VariableType {
|
(*variables)["CorrectedDistance"] = database.VariableType {
|
||||||
Label: "CorrectedDistance",
|
Label: "CorrectedDistance",
|
||||||
@ -67,11 +76,11 @@ func Parse(fPort int, decodedPayload []byte, variables *map[string]database.Vari
|
|||||||
Unit: "mm",
|
Unit: "mm",
|
||||||
Value: correctedDistance,
|
Value: correctedDistance,
|
||||||
}
|
}
|
||||||
} else {
|
} /* else {
|
||||||
log.Printf("no ground level: %s %s %s", exists, err3, ok)
|
log.Printf("no ground level: %s %s %s", exists, err3, ok)
|
||||||
log.Printf("Device: %s", device)
|
log.Printf("Device: %s", device)
|
||||||
log.Printf("Attributes: %s", device.Attributes)
|
log.Printf("Attributes: %s", device.Attributes)
|
||||||
}
|
} */
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ type message struct {
|
|||||||
Dis2 int `json:"dis2"`
|
Dis2 int `json:"dis2"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error {
|
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
||||||
if fPort != 2 {
|
if fPort != 2 {
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
}
|
}
|
||||||
@ -55,6 +55,15 @@ func Parse(fPort int, decodedPayload []byte, variables *map[string]database.Vari
|
|||||||
Unit: "mm",
|
Unit: "mm",
|
||||||
Value: distance2,
|
Value: distance2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if distance1 == 2 {
|
||||||
|
(*attributes)["Status"] = "invalid value"
|
||||||
|
} else if distance1 == 1 {
|
||||||
|
(*attributes)["Status"] = "no sensor detected"
|
||||||
|
} else {
|
||||||
|
(*attributes)["Status"] = "Ok"
|
||||||
|
}
|
||||||
|
|
||||||
groundLevelI, exists := device.Attributes["GroundLevel"]
|
groundLevelI, exists := device.Attributes["GroundLevel"]
|
||||||
groundLevelS, ok := groundLevelI.(string)
|
groundLevelS, ok := groundLevelI.(string)
|
||||||
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
||||||
|
@ -24,7 +24,7 @@ type message struct {
|
|||||||
Water_SOIL string `json:"water_SOIL"`
|
Water_SOIL string `json:"water_SOIL"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, device *database.Device) error {
|
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
||||||
if fPort != 2 {
|
if fPort != 2 {
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ type emuMessage1 struct {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, variables *map[string]database.VariableType, _ *database.Device) error {
|
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
||||||
//log.Printf("Parse input: %d, %s", fPort, decodedPayload)
|
//log.Printf("Parse input: %d, %s", fPort, decodedPayload)
|
||||||
switch fPort {
|
switch fPort {
|
||||||
case 1:
|
case 1:
|
||||||
|
61
src/udi/handlers/ttn/models/hottisScd30/hottisScd30.go
Normal file
61
src/udi/handlers/ttn/models/hottisScd30/hottisScd30.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package hottisScd30
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"log"
|
||||||
|
"fmt"
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
type hottisScd30Values struct {
|
||||||
|
Status uint8
|
||||||
|
CO2Conc int32
|
||||||
|
Temp int32
|
||||||
|
Hum int32
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error {
|
||||||
|
if fPort != 2 {
|
||||||
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := base64.StdEncoding.DecodeString(frmPayload)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to base64-decode payload: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var values hottisScd30Values
|
||||||
|
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to cast into struct: %v", err)
|
||||||
|
}
|
||||||
|
var co2concentration float32 = float32(values.CO2Conc) / 100;
|
||||||
|
var temperature float32 = float32(values.Temp) / 100;
|
||||||
|
var humidity float32 = float32(values.Hum) / 100;
|
||||||
|
// log.Printf("CO2: %f, Temp: %f, Hum: %f, Status: %d", co2concentration, temperature, humidity, values.Status)
|
||||||
|
|
||||||
|
(*variables)["CO2concentration"] = database.VariableType {
|
||||||
|
Label: "CO2concentration",
|
||||||
|
Variable: "Concentration",
|
||||||
|
Unit: "ppm",
|
||||||
|
Value: co2concentration,
|
||||||
|
}
|
||||||
|
(*variables)["Temperature"] = database.VariableType {
|
||||||
|
Label: "Temperature",
|
||||||
|
Variable: "Temperature",
|
||||||
|
Unit: "°C",
|
||||||
|
Value: temperature,
|
||||||
|
}
|
||||||
|
(*variables)["Humidity"] = database.VariableType {
|
||||||
|
Label: "Humidity",
|
||||||
|
Variable: "Humidity",
|
||||||
|
Unit: "%",
|
||||||
|
Value: humidity,
|
||||||
|
}
|
||||||
|
|
||||||
|
(*attributes)["Status"] = values.Status
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package rawPayloadPrinter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"fmt"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
||||||
|
if fPort != 2 {
|
||||||
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := base64.StdEncoding.DecodeString(frmPayload)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to base64-decode payload: %v", err)
|
||||||
|
}
|
||||||
|
hexString := hex.EncodeToString(bytes)
|
||||||
|
|
||||||
|
log.Printf("Payload: %s", hexString)
|
||||||
|
return nil
|
||||||
|
}
|
@ -2,7 +2,7 @@ package ttn
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
//"log"
|
||||||
"time"
|
"time"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
@ -11,6 +11,8 @@ import (
|
|||||||
"udi/handlers/ttn/models/draginoLdds75"
|
"udi/handlers/ttn/models/draginoLdds75"
|
||||||
"udi/handlers/ttn/models/draginoLmds200"
|
"udi/handlers/ttn/models/draginoLmds200"
|
||||||
"udi/handlers/ttn/models/draginoLse01"
|
"udi/handlers/ttn/models/draginoLse01"
|
||||||
|
"udi/handlers/ttn/models/rawPayloadPrinter"
|
||||||
|
"udi/handlers/ttn/models/hottisScd30"
|
||||||
"udi/database"
|
"udi/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -85,7 +87,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *TTNHandler) Handle(message handler.MessageT) {
|
func (self *TTNHandler) Handle(message handler.MessageT) {
|
||||||
log.Printf("Handler TTN %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
//log.Printf("Handler TTN %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||||
|
|
||||||
var measurement database.Measurement
|
var measurement database.Measurement
|
||||||
measurement.Time = time.Now()
|
measurement.Time = time.Now()
|
||||||
@ -96,7 +98,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
self.Lost("Error when unmarshaling message", err, message)
|
self.Lost("Error when unmarshaling message", err, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Parsed message: %s", uplinkMessage)
|
//log.Printf("Parsed message: %s", uplinkMessage)
|
||||||
|
|
||||||
var attributes attributes
|
var attributes attributes
|
||||||
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
|
attributes.DeviceId = uplinkMessage.EndDeviceIds.DeviceId
|
||||||
@ -106,11 +108,11 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
|
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
|
||||||
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
|
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
|
||||||
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
|
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
|
||||||
log.Printf("RXM: %s", rxm)
|
//log.Printf("RXM: %s", rxm)
|
||||||
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
|
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
|
||||||
attributes.Gateways = append(attributes.Gateways, g)
|
attributes.Gateways = append(attributes.Gateways, g)
|
||||||
}
|
}
|
||||||
log.Printf("Attributes: %s", attributes)
|
//log.Printf("Attributes: %s", attributes)
|
||||||
measurement.Attributes = map[string]interface{} {
|
measurement.Attributes = map[string]interface{} {
|
||||||
"DeviceId": attributes.DeviceId,
|
"DeviceId": attributes.DeviceId,
|
||||||
"ApplicationId": attributes.ApplicationId,
|
"ApplicationId": attributes.ApplicationId,
|
||||||
@ -121,7 +123,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
"ConsumedAirtime": attributes.ConsumedAirtime,
|
"ConsumedAirtime": attributes.ConsumedAirtime,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
|
//log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
|
||||||
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
|
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
self.Lost("Error when loading device", err2, message)
|
self.Lost("Error when loading device", err2, message)
|
||||||
@ -131,9 +133,9 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
measurement.Device = attributes.DeviceId
|
measurement.Device = attributes.DeviceId
|
||||||
measurement.Attributes["DeviceType"] = device.DeviceType.ModelIdentifier
|
measurement.Attributes["DeviceType"] = device.DeviceType.ModelIdentifier
|
||||||
|
|
||||||
log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
|
//log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
|
||||||
|
|
||||||
var parser func(int, []byte, *map[string]database.VariableType, *database.Device) error
|
var parser func(int, []byte, string, *map[string]database.VariableType, *map[string]interface{}, *database.Device) error
|
||||||
switch device.DeviceType.ModelIdentifier {
|
switch device.DeviceType.ModelIdentifier {
|
||||||
case "emu-prof-ii-lora-cfg1":
|
case "emu-prof-ii-lora-cfg1":
|
||||||
parser = emuProfIILoRaCfg1.Parse
|
parser = emuProfIILoRaCfg1.Parse
|
||||||
@ -143,18 +145,27 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
parser = draginoLmds200.Parse
|
parser = draginoLmds200.Parse
|
||||||
case "dragino-lse01":
|
case "dragino-lse01":
|
||||||
parser = draginoLse01.Parse
|
parser = draginoLse01.Parse
|
||||||
|
case "raw-payload-printer":
|
||||||
|
parser = rawPayloadPrinter.Parse
|
||||||
|
case "hottis-scd30":
|
||||||
|
parser = hottisScd30.Parse
|
||||||
default:
|
default:
|
||||||
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
|
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
err3 := parser(uplinkMessage.UplinkMessage.FPort, uplinkMessage.UplinkMessage.DecodedPayload.Payload, &(measurement.Values), device)
|
err3 := parser(uplinkMessage.UplinkMessage.FPort,
|
||||||
|
uplinkMessage.UplinkMessage.DecodedPayload.Payload,
|
||||||
|
uplinkMessage.UplinkMessage.FrmPayload,
|
||||||
|
&(measurement.Values),
|
||||||
|
&(measurement.Attributes),
|
||||||
|
device)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
self.Lost("Model parser failed", err3, message)
|
self.Lost("Model parser failed", err3, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Prepared measurement item: %s", measurement)
|
//log.Printf("Prepared measurement item: %s", measurement)
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user