Compare commits
62 Commits
Author | SHA1 | Date | |
---|---|---|---|
61509c0000 | |||
3c09c04066 | |||
af739c7148 | |||
33ff176c79 | |||
134e3706cc | |||
3a56309b9f | |||
084645f002
|
|||
9815371199
|
|||
4debe45592
|
|||
71773968c9
|
|||
574e2886f5
|
|||
e25693fb84
|
|||
ff49d285dc
|
|||
8c3977162b
|
|||
b99b47ca40
|
|||
c40805b4cb
|
|||
a2eb38b414
|
|||
64cf45e22f
|
|||
9310a86687
|
|||
f4b404e2b1
|
|||
29148a13f4
|
|||
0356e9dcee
|
|||
a5b981357d
|
|||
57bbc6135e
|
|||
d704f7ba5e
|
|||
d0567a48f1
|
|||
ee22996433
|
|||
4130befdbf
|
|||
77c5df0697
|
|||
d4ee4c49de
|
|||
ae938d10b9 | |||
799ef9e00b | |||
311e732841 | |||
51e482e94e
|
|||
a1b98d3438
|
|||
166c414af1
|
|||
cedb1dfa5a
|
|||
a21fae4f8a
|
|||
1b6ac5d762
|
|||
95831d5e47
|
|||
c3dce9faab
|
|||
c332373691
|
|||
418b44289d
|
|||
23962cdebc
|
|||
b9e639e0a2
|
|||
1870089942
|
|||
f8bcfe4d25
|
|||
aa3f784a41
|
|||
57c635b1e0
|
|||
c5150b1b4f
|
|||
abc0ad0825
|
|||
b6a7447cb3
|
|||
58d86101f3
|
|||
81e067e672
|
|||
45e4cd793b
|
|||
830596f211
|
|||
a55f80b7d9
|
|||
3d68aa0e61
|
|||
f2f16c811a
|
|||
6f9327fdd6
|
|||
1d1942a4d3
|
|||
d6e7fa3949
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
src/udi/udi
|
src/udi/udi
|
||||||
|
src/udi/main
|
||||||
src/udi/migrate_schema
|
src/udi/migrate_schema
|
||||||
tmp/
|
tmp/
|
||||||
ENVDB
|
ENVDB
|
||||||
|
@ -1,5 +1,52 @@
|
|||||||
steps:
|
steps:
|
||||||
build:
|
build:
|
||||||
|
image: golang:1.22.5-alpine3.20
|
||||||
|
commands:
|
||||||
|
- GOPATH=/woodpecker/go
|
||||||
|
- cd src/udi
|
||||||
|
- go mod tidy
|
||||||
|
- go build -a -installsuffix nocgo -o udi main.go
|
||||||
|
- cp udi ../..
|
||||||
|
when:
|
||||||
|
- event: [push, tag]
|
||||||
|
|
||||||
|
scan:
|
||||||
|
image: quay.io/wollud1969/woodpecker-helper:0.5.1
|
||||||
|
environment:
|
||||||
|
TRIVY_TOKEN:
|
||||||
|
from_secret: trivy_token
|
||||||
|
TRIVY_URL:
|
||||||
|
from_secret: trivy_url
|
||||||
|
DTRACK_API_KEY:
|
||||||
|
from_secret: dtrack_api_key
|
||||||
|
DTRACK_API_URL:
|
||||||
|
from_secret: dtrack_api_url
|
||||||
|
commands:
|
||||||
|
- export GOPATH=/woodpecker/go # the export is required, otherwise trivy will not consider the variable
|
||||||
|
- HOME=/home/`id -nu`
|
||||||
|
- TAG="${CI_COMMIT_TAG:-$CI_COMMIT_SHA}"
|
||||||
|
- |
|
||||||
|
trivy fs \
|
||||||
|
--server $TRIVY_URL \
|
||||||
|
--token $TRIVY_TOKEN \
|
||||||
|
--format cyclonedx \
|
||||||
|
--scanners license \
|
||||||
|
--output /tmp/sbom.xml \
|
||||||
|
.
|
||||||
|
- cat /tmp/sbom.xml
|
||||||
|
- |
|
||||||
|
curl -X "POST" \
|
||||||
|
-H "Content-Type: multipart/form-data" \
|
||||||
|
-H "X-Api-Key: $DTRACK_API_KEY" \
|
||||||
|
-F "autoCreate=true" \
|
||||||
|
-F "projectName=$CI_REPO" \
|
||||||
|
-F "projectVersion=$TAG" \
|
||||||
|
-F "bom=@/tmp/sbom.xml"\
|
||||||
|
"$DTRACK_API_URL/api/v1/bom"
|
||||||
|
when:
|
||||||
|
- event: [push, tag]
|
||||||
|
|
||||||
|
dockerize:
|
||||||
image: plugins/kaniko
|
image: plugins/kaniko
|
||||||
settings:
|
settings:
|
||||||
repo: ${FORGE_NAME}/${CI_REPO}
|
repo: ${FORGE_NAME}/${CI_REPO}
|
||||||
@ -14,22 +61,15 @@ 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: quay.io/wollud1969/woodpecker-helper:0.5.1
|
||||||
secrets:
|
environment:
|
||||||
- source: kube_config
|
KUBE_CONFIG_CONTENT:
|
||||||
target: KUBE_CONFIG_CONTENT
|
from_secret: kube_config
|
||||||
- source: encryption_key
|
ENCRYPTION_KEY:
|
||||||
target: ENCRYPTION_KEY
|
from_secret: encryption_key
|
||||||
- source: secrets_checksum
|
MD5_CHECKSUM:
|
||||||
target: MD5_CHECKSUM
|
from_secret: secrets_checksum
|
||||||
commands:
|
commands:
|
||||||
- export IMAGE_TAG=$CI_COMMIT_TAG
|
- export IMAGE_TAG=$CI_COMMIT_TAG
|
||||||
- printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig
|
- printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig
|
||||||
|
11
Dockerfile
11
Dockerfile
@ -1,15 +1,8 @@
|
|||||||
FROM golang:1.22.5-alpine3.20 as builder
|
|
||||||
|
|
||||||
RUN mkdir -p /go/src
|
|
||||||
COPY ./src/ /go/src
|
|
||||||
WORKDIR /go/src/udi
|
|
||||||
RUN go build -a -installsuffix nocgo -o udi main.go
|
|
||||||
|
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
ENV UDI_CONF ""
|
ENV UDI_CONF ""
|
||||||
|
|
||||||
COPY --from=builder /go/src/udi ./
|
COPY udi ./
|
||||||
|
|
||||||
ENTRYPOINT ["./udi"]
|
ENTRYPOINT ["./udi"]
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ if [ "$MD5_CHECKSUM" = "" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
SECRETS_CIPHERTEXT_FILE=secrets.enc
|
SECRETS_CIPHERTEXT_FILE=secrets.enc
|
||||||
SECRETS_PLAINTEXT_FILE=/tmp/secrets
|
SECRETS_PLAINTEXT_FILE=/tmp/secrets
|
||||||
TMP_FILE=`mktemp`
|
TMP_FILE=`mktemp`
|
||||||
@ -37,7 +38,7 @@ if [ "$MD5_CHECKSUM" != "$CALCULATED_CHECKSUM" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# cat $TMP_FILE
|
#cat $TMP_FILE
|
||||||
mv $TMP_FILE $SECRETS_PLAINTEXT_FILE
|
mv $TMP_FILE $SECRETS_PLAINTEXT_FILE
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ for NAMESPACE_DIR in `find $INSTANCES_DIR -type d -mindepth 1 -maxdepth 1`; do
|
|||||||
NEW_UDI_DB_PASSWORD="${!PASSWORD_VARIABLE}"
|
NEW_UDI_DB_PASSWORD="${!PASSWORD_VARIABLE}"
|
||||||
DATABASE_VARIABLE=$VARIABLE_PREFIX"_PGDATABASE"
|
DATABASE_VARIABLE=$VARIABLE_PREFIX"_PGDATABASE"
|
||||||
NEW_UDI_DB_DATABASE="${!DATABASE_VARIABLE}"
|
NEW_UDI_DB_DATABASE="${!DATABASE_VARIABLE}"
|
||||||
NEW_UDI_DB_HOST=timescaledb.database.svc.cluster.local
|
NEW_UDI_DB_HOST=database.database1.svc.cluster.local
|
||||||
|
|
||||||
kubectl create secret generic $INSTANCE-udi-db-cred \
|
kubectl create secret generic $INSTANCE-udi-db-cred \
|
||||||
--dry-run=client \
|
--dry-run=client \
|
||||||
|
@ -6,13 +6,22 @@
|
|||||||
"topicMappings": [
|
"topicMappings": [
|
||||||
{
|
{
|
||||||
"topics": [ "snmp" ],
|
"topics": [ "snmp" ],
|
||||||
"handler": "SNMP",
|
"handler": "PREP",
|
||||||
"id": "SNMP",
|
"id": "SNMP",
|
||||||
"config": {
|
"config": {
|
||||||
"attributes": {
|
"attributes": {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"topics": [ "tsm" ],
|
||||||
|
"handler": "PREP",
|
||||||
|
"id": "TSM",
|
||||||
|
"config": {
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"topics": [ "dt1/ai/periodic/1" ],
|
"topics": [ "dt1/ai/periodic/1" ],
|
||||||
"handler": "DT1T",
|
"handler": "DT1T",
|
||||||
@ -119,6 +128,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"topics": [ "zigbee2mqtt/+" ],
|
||||||
|
"handler": "Z2M",
|
||||||
|
"id": "Z2M",
|
||||||
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
||||||
"handler": "SVEJ",
|
"handler": "SVEJ",
|
||||||
|
1
migration/.gitignore
vendored
1
migration/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
.venv
|
|
@ -1,38 +0,0 @@
|
|||||||
import psycopg2
|
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
try:
|
|
||||||
srcConn = psycopg2.connect(database="level_monitoring_berresheim")
|
|
||||||
srcConn.autocommit = False
|
|
||||||
destConn = psycopg2.connect(database="udi-berresheim")
|
|
||||||
destConn.autocommit = False
|
|
||||||
|
|
||||||
with srcConn.cursor() as srcCur, destConn.cursor() as destCur:
|
|
||||||
srcCur.execute("select time, application_name, raw_level, level, status, battery from measurement_t")
|
|
||||||
for srcObj in srcCur:
|
|
||||||
timestamp = srcObj[0]
|
|
||||||
deviceName = srcObj[1]
|
|
||||||
rawLevel = srcObj[2]
|
|
||||||
level = srcObj[3]
|
|
||||||
status = srcObj[4]
|
|
||||||
battery = srcObj[5]
|
|
||||||
|
|
||||||
logger.info(f"{timestamp=}, {deviceName=}, {rawLevel=}, {level=}, {status=}, {battery=}")
|
|
||||||
|
|
||||||
destTime = timestamp
|
|
||||||
destApplication = "de-hottis-level-monitoring"
|
|
||||||
destDevice = "eui-a84041a2c18341d6"
|
|
||||||
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) + '}}'
|
|
||||||
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:
|
|
||||||
if srcConn:
|
|
||||||
srcConn.close()
|
|
||||||
if destConn:
|
|
||||||
destConn.close()
|
|
||||||
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
|||||||
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()
|
|
||||||
|
|
||||||
|
|
@ -1,78 +0,0 @@
|
|||||||
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()
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
|||||||
loguru==0.7.2
|
|
||||||
psycopg2==2.9.9
|
|
@ -43,7 +43,22 @@ create or replace view temperature_v as
|
|||||||
cast(values->'Value'->>'value' as float) as temperature,
|
cast(values->'Value'->>'value' as float) as temperature,
|
||||||
device
|
device
|
||||||
from measurements
|
from measurements
|
||||||
where application in ('Temperature Multisensor', 'Temperature Shelly Plus HT');
|
where application in ('Temperature Multisensor', 'Temperature Shelly Plus HT')
|
||||||
|
union
|
||||||
|
select time,
|
||||||
|
cast(values->'Temperature'->>'value' as float) as temperature,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Zigbee2MQTT Hottis Eupenstr.' and
|
||||||
|
attributes->>'DeviceModel' in ('WSDCGQ11LM', 'WSDCGQ01LM');
|
||||||
|
|
||||||
|
create or replace view voltage_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Voltage'->>'value' as float) / 1000 as voltage,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Zigbee2MQTT Hottis Eupenstr.' and
|
||||||
|
attributes->>'DeviceModel' in ('WSDCGQ11LM', 'WSDCGQ01LM');
|
||||||
|
|
||||||
create or replace view temperature2_v as
|
create or replace view temperature2_v as
|
||||||
select time,
|
select time,
|
||||||
@ -57,7 +72,14 @@ create or replace view humidity_v as
|
|||||||
cast(values->'Value'->>'value' as float) as humidity,
|
cast(values->'Value'->>'value' as float) as humidity,
|
||||||
device
|
device
|
||||||
from measurements
|
from measurements
|
||||||
where application in ('Humidity Multisensor');
|
where application in ('Humidity Multisensor')
|
||||||
|
union
|
||||||
|
select time,
|
||||||
|
cast(values->'Humidity'->>'value' as float) as temperature,
|
||||||
|
device
|
||||||
|
from measurements
|
||||||
|
where application = 'Zigbee2MQTT Hottis Eupenstr.' and
|
||||||
|
attributes->>'DeviceModel' in ('WSDCGQ11LM', 'WSDCGQ01LM');
|
||||||
|
|
||||||
create or replace view soil_v as
|
create or replace view soil_v as
|
||||||
select time,
|
select time,
|
||||||
@ -96,3 +118,31 @@ create or replace view router_v as
|
|||||||
cast(values->'wan-out'->>'value' as int) as wanOutOctetsPerSeconds
|
cast(values->'wan-out'->>'value' as int) as wanOutOctetsPerSeconds
|
||||||
from measurements
|
from measurements
|
||||||
where application = 'SNMP' and device = '172.16.3.1';
|
where application = 'SNMP' and device = '172.16.3.1';
|
||||||
|
|
||||||
|
create or replace view lora_sht21_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Humidity'->>'value' as float) as humidity,
|
||||||
|
cast(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-gy21' and
|
||||||
|
m.device = d.label;
|
||||||
|
|
||||||
|
create or replace view ntp_server_snmp_v as
|
||||||
|
select time,
|
||||||
|
device,
|
||||||
|
cast(values->'load1'->>'value' as float) as laLoad1,
|
||||||
|
cast(values->'lan-in'->>'value' as int) as lanInOctetsPerSeconds,
|
||||||
|
cast(values->'lan-out'->>'value' as int) as lanOutOctetsPerSeconds
|
||||||
|
from measurements
|
||||||
|
where application = 'SNMP' and device = '172.16.13.10';
|
||||||
|
|
||||||
|
create or replace view ntp_server_variables_v as
|
||||||
|
select time,
|
||||||
|
device,
|
||||||
|
cast(values->'rootdisp'->>'value' as float) as rootdisp
|
||||||
|
from measurements
|
||||||
|
where application = 'TSM' and device = '172.16.13.10';
|
||||||
|
|
||||||
|
@ -10,14 +10,35 @@ create or replace view badesee_temperature_v as
|
|||||||
where application = 'de-hottis-saerbeck-monitoring' and
|
where application = 'de-hottis-saerbeck-monitoring' and
|
||||||
device = 'eui-a84041318187ec13';
|
device = 'eui-a84041318187ec13';
|
||||||
|
|
||||||
create or replace view cubecell_threeway_temperature_v as
|
create or replace view cubecell_threeway_temperature2_v as
|
||||||
select time,
|
select time,
|
||||||
cast(values->'Temperature1'->>'value' as float) as Temp1,
|
cast(values->'Temperature2'->>'value' as float) as value,
|
||||||
cast(values->'Temperature2'->>'value' as float) as Temp2,
|
values->'Temperature2'->>'label' as label
|
||||||
cast(values->'Temperature3'->>'value' as float) as Temp3,
|
|
||||||
device
|
|
||||||
from measurements
|
from measurements
|
||||||
where application = 'de-hottis-saerbeck-monitoring' and
|
where application = 'de-hottis-saerbeck-monitoring' and
|
||||||
device = 'eui-70b3d57ed0068fa4';
|
device = 'eui-70b3d57ed0068fa4';
|
||||||
|
|
||||||
|
create or replace view cubecell_threeway_temperature1_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Temperature1'->>'value' as float) as value,
|
||||||
|
values->'Temperature1'->>'label' as label
|
||||||
|
from measurements
|
||||||
|
where application = 'de-hottis-saerbeck-monitoring' and
|
||||||
|
device = 'eui-70b3d57ed0068fa4';
|
||||||
|
|
||||||
|
create or replace view cubecell_threeway_temperature3_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Temperature3'->>'value' as float) as value,
|
||||||
|
values->'Temperature3'->>'label' as label
|
||||||
|
from measurements
|
||||||
|
where application = 'de-hottis-saerbeck-monitoring' and
|
||||||
|
device = 'eui-70b3d57ed0068fa4';
|
||||||
|
|
||||||
|
create or replace view cubecell_threeway_battery_v as
|
||||||
|
select time,
|
||||||
|
cast(values->'Battery'->>'value' as float) as value,
|
||||||
|
values->'Battery'->>'label' as label
|
||||||
|
from measurements
|
||||||
|
where application = 'de-hottis-saerbeck-monitoring' and
|
||||||
|
device = 'eui-70b3d57ed0068fa4';
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
PGUSER="uditest"
|
PGUSER="udi-hottis"
|
||||||
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 uditest-db-cred -n udi-test -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
PGPASSWORD=`kubectl get secrets default-udi-db-cred -n udi -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
||||||
PGSSLMODE=require
|
PGSSLMODE=require
|
||||||
PGDATABASE="uditest"
|
PGDATABASE="uditest"
|
||||||
export PGUSER PGHOST PGPASSWORD PGSSLMODE PGDATABASE
|
export PGUSER PGHOST PGPASSWORD PGSSLMODE PGDATABASE
|
||||||
|
@ -4,72 +4,6 @@
|
|||||||
"tlsEnable": "false"
|
"tlsEnable": "false"
|
||||||
},
|
},
|
||||||
"topicMappings": [
|
"topicMappings": [
|
||||||
{
|
|
||||||
"topics": [ "IoT/PV/Values" ],
|
|
||||||
"handler": "PV",
|
|
||||||
"id": "PV",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "IoT/MBGW3/Measurement" ],
|
|
||||||
"handler": "MBGW3",
|
|
||||||
"id": "MBGW3",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "dt1/ai/periodic/1" ],
|
|
||||||
"handler": "DT1T",
|
|
||||||
"id": "DT1T.0",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
"Application": "Temperature Wago",
|
|
||||||
"Device": "Freezer",
|
|
||||||
"HardLow": "-273",
|
|
||||||
"SoftLow": "-50",
|
|
||||||
"SoftHigh": "20",
|
|
||||||
"HardHigh": "100"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "dt1/ai/periodic/3" ],
|
|
||||||
"handler": "DT1T",
|
|
||||||
"id": "DT1T.1",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
"Application": "Temperature Wago",
|
|
||||||
"Device": "Outdoor",
|
|
||||||
"HardLow": "-273",
|
|
||||||
"SoftLow": "-60",
|
|
||||||
"SoftHigh": "60",
|
|
||||||
"HardHigh": "100"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "IoT/OneWireGW/Bus 1/#" ],
|
|
||||||
"handler": "SVER",
|
|
||||||
"id": "SVER0",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
"application": "Temperature Heating",
|
|
||||||
"payloadRegex": "(\\d+(\\.\\d+)?)\\s*([^0-9\\s]\\S*)",
|
|
||||||
"deviceFrom": "topic",
|
|
||||||
"devicePart": "3",
|
|
||||||
"valueFrom": "payload",
|
|
||||||
"valuePart": "1",
|
|
||||||
"unitFrom": "payload",
|
|
||||||
"unitPart": "3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"topics": [ "NR/Multisensor/+/Temperatur" ],
|
"topics": [ "NR/Multisensor/+/Temperatur" ],
|
||||||
"handler": "SVEJ",
|
"handler": "SVEJ",
|
||||||
@ -85,44 +19,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"topics": [ "NR/Multisensor/+/Feuchte" ],
|
"topics": [ "zigbee2mqtt/+" ],
|
||||||
"handler": "SVEJ",
|
"handler": "Z2M",
|
||||||
"id": "SVEJ1",
|
"id": "Z2M",
|
||||||
"config": {
|
"config": {
|
||||||
"databaseConnStr": "",
|
"databaseConnStr": "",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
"application": "Humidity Multisensor",
|
|
||||||
"deviceSelector": "T:2",
|
|
||||||
"valueSelector": "J:$.CurrentRelativeHumidity",
|
|
||||||
"unitSelector": "C:%"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
|
||||||
"handler": "SVEJ",
|
|
||||||
"id": "SVEJ2",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
"application": "Temperature Shelly Plus HT",
|
|
||||||
"deviceSelector": "T:1",
|
|
||||||
"valueSelector": "J:$.tC",
|
|
||||||
"unitSelector": "C:°C"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "shellyplusht/+/status/humidity:0" ],
|
|
||||||
"handler": "SVEJ",
|
|
||||||
"id": "SVE4",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
"application": "Humidity Shelly Plus HT",
|
|
||||||
"deviceSelector": "T:1",
|
|
||||||
"valueSelector": "J:$.rh",
|
|
||||||
"unitSelector": "C:%"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ type VariableType struct {
|
|||||||
Variable string `json:"variable"`
|
Variable string `json:"variable"`
|
||||||
Unit string `json:"unit"`
|
Unit string `json:"unit"`
|
||||||
Value interface{} `json:"value,omitempty"`
|
Value interface{} `json:"value,omitempty"`
|
||||||
|
Status string `json:"status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Measurement struct {
|
type Measurement struct {
|
||||||
|
@ -25,7 +25,7 @@ func NewDatabaseHandle() *DatabaseHandle {
|
|||||||
} else {
|
} else {
|
||||||
db.dbh = conn
|
db.dbh = conn
|
||||||
db.initialized = true
|
db.initialized = true
|
||||||
//log.Println("Database connection opened")
|
log.Println("Database connection opened")
|
||||||
}
|
}
|
||||||
return &db
|
return &db
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.Println("Successfully stored measurement")
|
log.Println("Successfully stored measurement")
|
||||||
counter.S("Stored")
|
counter.S("Stored")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,5 +70,27 @@ func (self *DatabaseHandle) GetDeviceByLabelAndApplication(applicationLabel stri
|
|||||||
return &device, nil
|
return &device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *DatabaseHandle) GetDeviceByLabel(deviceLabel string) (*Device, error) {
|
||||||
|
if ! self.initialized {
|
||||||
|
err := fmt.Errorf("Database connection not initialized")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var device Device
|
||||||
|
result := self.dbh.
|
||||||
|
Preload("Application").
|
||||||
|
Preload("DeviceType").
|
||||||
|
Where("devices.label = ?", deviceLabel).
|
||||||
|
First(&device)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
err := fmt.Errorf("Query failed: %s", result.Error)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ 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/locative"
|
||||||
import "udi/handlers/snmp"
|
import "udi/handlers/prepared"
|
||||||
|
import "udi/handlers/z2m"
|
||||||
|
|
||||||
|
|
||||||
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||||
@ -48,8 +49,10 @@ func InitDispatcher() {
|
|||||||
factory = dt1t.New
|
factory = dt1t.New
|
||||||
case "Locative":
|
case "Locative":
|
||||||
factory = locative.New
|
factory = locative.New
|
||||||
case "SNMP":
|
case "PREP":
|
||||||
factory = snmp.New
|
factory = prepared.New
|
||||||
|
case "Z2M":
|
||||||
|
factory = z2m.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)
|
||||||
|
@ -3,23 +3,23 @@ module udi
|
|||||||
go 1.22.3
|
go 1.22.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/eclipse/paho.mqtt.golang v1.4.3
|
github.com/eclipse/paho.mqtt.golang v1.5.0
|
||||||
github.com/google/uuid v1.4.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
|
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
|
||||||
gorm.io/driver/postgres v1.5.9
|
gorm.io/driver/postgres v1.5.11
|
||||||
gorm.io/gorm v1.25.11
|
gorm.io/gorm v1.25.12
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/websocket v1.5.0 // indirect
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
github.com/jackc/pgx/v5 v5.5.5 // indirect
|
github.com/jackc/pgx/v5 v5.7.2 // indirect
|
||||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
github.com/jackc/puddle/v2 v2.2.1 // 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.25.0 // indirect
|
golang.org/x/crypto v0.32.0 // indirect
|
||||||
golang.org/x/net v0.27.0 // indirect
|
golang.org/x/net v0.34.0 // indirect
|
||||||
golang.org/x/sync v0.7.0 // indirect
|
golang.org/x/sync v0.10.0 // indirect
|
||||||
golang.org/x/text v0.16.0 // indirect
|
golang.org/x/text v0.21.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -28,14 +28,14 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
|||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
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.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
|
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||||
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
|
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
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=
|
||||||
|
@ -38,6 +38,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.Id = id
|
t.Id = id
|
||||||
|
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler DT1T %d initialized", id)
|
||||||
t.ready = true
|
t.ready = true
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler Locative %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package mbgw3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
@ -31,6 +32,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler MBGW3 %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package snmp
|
package prepared
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type SnmpHandler struct {
|
type PreparedHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
@ -19,6 +19,8 @@ type endpoint_t struct {
|
|||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Variable string `json:"variable"`
|
Variable string `json:"variable"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
|
Unit string `json:"unit"`
|
||||||
|
Status string `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type observation_t struct {
|
type observation_t struct {
|
||||||
@ -29,15 +31,16 @@ type observation_t struct {
|
|||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SnmpHandler {
|
t := &PreparedHandler {
|
||||||
}
|
}
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler Prepared %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SnmpHandler) Handle(message handler.MessageT) {
|
func (self *PreparedHandler) Handle(message handler.MessageT) {
|
||||||
log.Printf("Handler SNMP %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
log.Printf("Handler Prepared %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||||
|
|
||||||
var observation observation_t
|
var observation observation_t
|
||||||
err := json.Unmarshal([]byte(message.Payload), &observation)
|
err := json.Unmarshal([]byte(message.Payload), &observation)
|
||||||
@ -49,7 +52,7 @@ func (self *SnmpHandler) Handle(message handler.MessageT) {
|
|||||||
var measurement database.Measurement
|
var measurement database.Measurement
|
||||||
measurement.Time = time.Now()
|
measurement.Time = time.Now()
|
||||||
|
|
||||||
measurement.Application = "SNMP"
|
measurement.Application = self.Id
|
||||||
measurement.Device = observation.Device
|
measurement.Device = observation.Device
|
||||||
|
|
||||||
measurement.Attributes = map[string]interface{} {
|
measurement.Attributes = map[string]interface{} {
|
||||||
@ -61,8 +64,9 @@ func (self *SnmpHandler) Handle(message handler.MessageT) {
|
|||||||
measurement.Values[k] = database.VariableType {
|
measurement.Values[k] = database.VariableType {
|
||||||
Label: v.Label,
|
Label: v.Label,
|
||||||
Variable: v.Variable,
|
Variable: v.Variable,
|
||||||
Unit: "",
|
Unit: v.Unit,
|
||||||
Value: v.Value,
|
Value: v.Value,
|
||||||
|
Status: v.Status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package pv
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
"log"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
@ -41,6 +42,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler PV %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,19 +76,22 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.Id = id
|
t.Id = id
|
||||||
t.ready = true
|
t.ready = true
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler SVEJ %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (string, error) {
|
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (string, error) {
|
||||||
var res string
|
var res string
|
||||||
switch selector[:2] {
|
switch selector[:2] {
|
||||||
case "J:":
|
case "J:":
|
||||||
|
// extract using jsonpath from payload
|
||||||
r, e := jp.Lookup(jPayload)
|
r, e := jp.Lookup(jPayload)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
return "", fmt.Errorf("jp.Lookup failed with %s", e)
|
||||||
}
|
}
|
||||||
res = fmt.Sprint(r)
|
res = fmt.Sprint(r)
|
||||||
case "T:":
|
case "T:":
|
||||||
|
// T: extract from topic
|
||||||
i, e := strconv.Atoi(selector[2:])
|
i, e := strconv.Atoi(selector[2:])
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return "", fmt.Errorf("Atoi failed with %s", e)
|
return "", fmt.Errorf("Atoi failed with %s", e)
|
||||||
@ -98,6 +101,7 @@ func extractionHelper(subTopics []string, jPayload interface{}, selector string,
|
|||||||
}
|
}
|
||||||
res = subTopics[i]
|
res = subTopics[i]
|
||||||
case "C:":
|
case "C:":
|
||||||
|
// use constant value
|
||||||
res = selector[2:]
|
res = selector[2:]
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("Invalid selector: %s", selector[:2])
|
return "", fmt.Errorf("Invalid selector: %s", selector[:2])
|
||||||
@ -111,14 +115,14 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
self.Lost("Handler is not marked as ready", nil, message)
|
self.Lost("Handler is not marked as ready", nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Printf("Handler SingleValueExtractorJsonpath %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
log.Printf("Handler SingleValueExtractorJsonpath %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()
|
||||||
measurement.Application = self.application
|
measurement.Application = self.application
|
||||||
|
|
||||||
subTopics := strings.Split(message.Topic, "/")
|
subTopics := strings.Split(message.Topic, "/")
|
||||||
//log.Printf("Subtopics: %s", strings.Join(subTopics, ", "))
|
log.Printf("Subtopics: %s", strings.Join(subTopics, ", "))
|
||||||
var jPayload interface{}
|
var jPayload interface{}
|
||||||
err := json.Unmarshal([]byte(message.Payload), &jPayload)
|
err := json.Unmarshal([]byte(message.Payload), &jPayload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -126,17 +130,20 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
device, err1 := extractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
device, err1 := self.ExtractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
self.Lost("Device extraction failed", err1, message)
|
self.Lost("Device extraction failed", err1, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
value, err2 := extractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
log.Printf("device: %s", device)
|
||||||
|
|
||||||
|
value, err2 := self.ExtractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
self.Lost("Value extraction failed", err2, message)
|
self.Lost("Value extraction failed", err2, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
unit, err3 := extractionHelper(subTopics, jPayload, self.unitSelector, self.unitJsonpath)
|
|
||||||
|
unit, err3 := self.ExtractionHelper(subTopics, jPayload, self.unitSelector, self.unitJsonpath)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
self.Lost("Unit extraction failed", err3, message)
|
self.Lost("Unit extraction failed", err3, message)
|
||||||
return
|
return
|
||||||
@ -152,7 +159,7 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
measurement.Values["Value"] = variable
|
measurement.Values["Value"] = variable
|
||||||
|
|
||||||
//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()
|
||||||
}
|
}
|
||||||
|
@ -111,6 +111,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.Id = id
|
t.Id = id
|
||||||
t.ready = true
|
t.ready = true
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler SVER %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
55
src/udi/handlers/ttn/models/hottisGy21/hottisGY21.go
Normal file
55
src/udi/handlers/ttn/models/hottisGy21/hottisGY21.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package hottisGy21
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"log"
|
||||||
|
"fmt"
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"udi/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
type hottisGy21Values struct {
|
||||||
|
Connected uint8
|
||||||
|
Status uint8
|
||||||
|
RawHumidity uint16
|
||||||
|
RawTemperature uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 hottisGy21Values
|
||||||
|
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to cast into struct: %v", err)
|
||||||
|
}
|
||||||
|
var temperature float32 = -46.85 + 175.72 * (float32(values.RawTemperature) / 65536.0)
|
||||||
|
var humidity float32 = -6 + 125 * (float32(values.RawHumidity) / 65536.0);
|
||||||
|
|
||||||
|
// log.Printf("CO2: %f, Temp: %f, Hum: %f, Status: %d", co2concentration, temperature, humidity, values.Status)
|
||||||
|
|
||||||
|
(*variables)["Humidity"] = database.VariableType {
|
||||||
|
Label: "Humidity",
|
||||||
|
Variable: "Humidity",
|
||||||
|
Unit: "%",
|
||||||
|
Value: humidity,
|
||||||
|
}
|
||||||
|
(*variables)["Temperature"] = database.VariableType {
|
||||||
|
Label: "Temperature",
|
||||||
|
Variable: "Temperature",
|
||||||
|
Unit: "°C",
|
||||||
|
Value: temperature,
|
||||||
|
}
|
||||||
|
|
||||||
|
(*attributes)["Status"] = values.Status
|
||||||
|
return nil
|
||||||
|
}
|
@ -7,7 +7,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
|
||||||
"udi/database"
|
"udi/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,24 +21,17 @@ type hottisThreeWayThermometerValues struct {
|
|||||||
Value3 int32
|
Value3 int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSensorName(sensorsMap *map[string]string, sensorAddress uint64) string {
|
func getSensorName(sensorsMap *map[string]interface{}, sensorAddress uint64) string {
|
||||||
key := strconv.FormatUint(sensorAddress, 10)
|
key := strconv.FormatUint(sensorAddress, 10)
|
||||||
if sensorName, exists := (*sensorsMap)[key]; exists {
|
if sensorName, exists := (*sensorsMap)[key].(string); exists {
|
||||||
return sensorName
|
return sensorName
|
||||||
}
|
}
|
||||||
return "Sensor" + key
|
return "Sensor" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error {
|
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
||||||
sensorsMap := make(map[string]string)
|
deviceAttrs := (*device).Attributes
|
||||||
sensorsJSON, ok := (*attributes)["Sensors"].(string)
|
sensorsMap := deviceAttrs["Sensors"].(map[string]interface{})
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Unable to load sensor map from attributes")
|
|
||||||
}
|
|
||||||
errJ := json.Unmarshal([]byte(sensorsJSON), &sensorsMap)
|
|
||||||
if errJ != nil {
|
|
||||||
return fmt.Errorf("Unable to parse sensor map: %v", errJ)
|
|
||||||
}
|
|
||||||
|
|
||||||
if fPort != 2 {
|
if fPort != 2 {
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
return fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
|
@ -2,7 +2,7 @@ package ttn
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
//"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"udi/handlers/ttn/models/draginoLsn50"
|
"udi/handlers/ttn/models/draginoLsn50"
|
||||||
"udi/handlers/ttn/models/rawPayloadPrinter"
|
"udi/handlers/ttn/models/rawPayloadPrinter"
|
||||||
"udi/handlers/ttn/models/hottisScd30"
|
"udi/handlers/ttn/models/hottisScd30"
|
||||||
|
"udi/handlers/ttn/models/hottisGy21"
|
||||||
"udi/handlers/ttn/models/hottisThreeWayThermometer"
|
"udi/handlers/ttn/models/hottisThreeWayThermometer"
|
||||||
"udi/database"
|
"udi/database"
|
||||||
)
|
)
|
||||||
@ -85,6 +86,7 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
}
|
}
|
||||||
t.Id = id
|
t.Id = id
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler TTN %d initialized", id)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +155,8 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
parser = rawPayloadPrinter.Parse
|
parser = rawPayloadPrinter.Parse
|
||||||
case "hottis-scd30":
|
case "hottis-scd30":
|
||||||
parser = hottisScd30.Parse
|
parser = hottisScd30.Parse
|
||||||
|
case "hottis-gy21":
|
||||||
|
parser = hottisGy21.Parse
|
||||||
case "hottis-threeway-thermometer":
|
case "hottis-threeway-thermometer":
|
||||||
parser = hottisThreeWayThermometer.Parse
|
parser = hottisThreeWayThermometer.Parse
|
||||||
default:
|
default:
|
||||||
|
15
src/udi/handlers/z2m/models/gs361ah04/gs361ah04.go
Normal file
15
src/udi/handlers/z2m/models/gs361ah04/gs361ah04.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package gs361ah04
|
||||||
|
|
||||||
|
type Observation struct {
|
||||||
|
LinkQuality uint8 `unit:"" json:"linkquality"`
|
||||||
|
Battery uint8 `unit:"%" json:"battery"`
|
||||||
|
AwayMode string `unit:"" json:"away_mode"`
|
||||||
|
ChildLock string `unit:"" json:"child_lock"`
|
||||||
|
CurrentHeatingSetpoint float32 `unit:"°C" json:"current_heating_setpoint"`
|
||||||
|
LocalTemperature float32 `unit:"°C" json:"local_temperature"`
|
||||||
|
Preset string `unit:"" json:"preset"`
|
||||||
|
SystemMode string `unit:"" json:"system_mode"`
|
||||||
|
ValveDetection string `unit:"" json:"valve_detection"`
|
||||||
|
WindowDetection string `unit:"" json:"window_detection"`
|
||||||
|
}
|
||||||
|
|
11
src/udi/handlers/z2m/models/wsdcgq01lm/wsdcgq01lm.go
Normal file
11
src/udi/handlers/z2m/models/wsdcgq01lm/wsdcgq01lm.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package wsdcgq01lm
|
||||||
|
|
||||||
|
type Observation struct {
|
||||||
|
LinkQuality uint8 `unit:"" json:"linkquality"`
|
||||||
|
Battery uint8 `unit:"%" json:"battery"`
|
||||||
|
Humidity float32 `unit:"%H" json:"humidity"`
|
||||||
|
Pressure float32 `unit:"mbar" json:"pressure"`
|
||||||
|
Temperature float32 `unit:"°C" json:"temperature"`
|
||||||
|
Voltage uint16 `unit:"mV" json:"voltage"`
|
||||||
|
}
|
||||||
|
|
10
src/udi/handlers/z2m/models/wsdcgq11lm/wsdcgq11lm.go
Normal file
10
src/udi/handlers/z2m/models/wsdcgq11lm/wsdcgq11lm.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package wsdcgq11lm
|
||||||
|
|
||||||
|
type Observation struct {
|
||||||
|
LinkQuality uint8 `unit:"" json:"linkquality"`
|
||||||
|
Battery uint8 `unit:"%" json:"battery"`
|
||||||
|
Humidity float32 `unit:"%H" json:"humidity"`
|
||||||
|
Temperature float32 `unit:"°C" json:"temperature"`
|
||||||
|
Voltage uint16 `unit:"mV" json:"voltage"`
|
||||||
|
}
|
||||||
|
|
113
src/udi/handlers/z2m/z2m.go
Normal file
113
src/udi/handlers/z2m/z2m.go
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package z2m
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
"strings"
|
||||||
|
"reflect"
|
||||||
|
"encoding/json"
|
||||||
|
"udi/config"
|
||||||
|
"udi/handlers/handler"
|
||||||
|
"udi/database"
|
||||||
|
"udi/handlers/z2m/models/wsdcgq11lm"
|
||||||
|
"udi/handlers/z2m/models/wsdcgq01lm"
|
||||||
|
"udi/handlers/z2m/models/gs361ah04"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type Z2MHandler struct {
|
||||||
|
handler.CommonHandler
|
||||||
|
dbh *database.DatabaseHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse(T any, payload string, variables *map[string]database.VariableType) error {
|
||||||
|
observationType := reflect.TypeOf(T)
|
||||||
|
observation := reflect.New(observationType).Interface()
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(payload), observation)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to parse payload into Observation struct: %v, %s", err, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
observationValue := reflect.ValueOf(observation).Elem()
|
||||||
|
|
||||||
|
for i := 0; i < observationType.NumField(); i++ {
|
||||||
|
field := observationType.Field(i)
|
||||||
|
name := field.Name
|
||||||
|
unit := field.Tag.Get("unit")
|
||||||
|
value := observationValue.Field(i).Interface()
|
||||||
|
|
||||||
|
(*variables)[name] = database.VariableType {
|
||||||
|
Label: name,
|
||||||
|
Variable: "y",
|
||||||
|
Unit: unit,
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
|
t := &Z2MHandler {
|
||||||
|
}
|
||||||
|
t.Id = id
|
||||||
|
t.dbh = database.NewDatabaseHandle()
|
||||||
|
log.Printf("Handler Z2M %d initialized", id)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Z2MHandler) Handle(message handler.MessageT) {
|
||||||
|
log.Printf("Handler Z2M %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||||
|
|
||||||
|
var measurement database.Measurement
|
||||||
|
measurement.Time = time.Now()
|
||||||
|
|
||||||
|
|
||||||
|
subTopics := strings.Split(message.Topic, "/")
|
||||||
|
deviceId := subTopics[1]
|
||||||
|
log.Printf("DeviceId: %s", deviceId)
|
||||||
|
device, err1 := self.dbh.GetDeviceByLabel(deviceId)
|
||||||
|
if err1 != nil {
|
||||||
|
self.Lost("Error when loading device", err1, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Device: %s", device)
|
||||||
|
|
||||||
|
measurement.Application = device.Application.Label
|
||||||
|
measurement.Device = device.Attributes["Label"].(string)
|
||||||
|
|
||||||
|
var T any
|
||||||
|
switch device.DeviceType.ModelIdentifier {
|
||||||
|
case "WSDCGQ11LM":
|
||||||
|
T = wsdcgq11lm.Observation{}
|
||||||
|
case "WSDCGQ01LM":
|
||||||
|
T = wsdcgq01lm.Observation{}
|
||||||
|
case "GS361A-H04":
|
||||||
|
T = gs361ah04.Observation{}
|
||||||
|
default:
|
||||||
|
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
|
measurement.Attributes = make(map[string]interface{})
|
||||||
|
err3 := parse(T,
|
||||||
|
message.Payload,
|
||||||
|
&(measurement.Values))
|
||||||
|
if err3 != nil {
|
||||||
|
self.Lost("Model parser failed", err3, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement.Attributes["Status"] = "ok"
|
||||||
|
measurement.Attributes["DeviceId"] = deviceId
|
||||||
|
measurement.Attributes["DeviceModel"] = device.DeviceType.ModelIdentifier
|
||||||
|
log.Printf("Prepared measurement item: %s", measurement)
|
||||||
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
|
self.S()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user