Compare commits
39 Commits
0.0.72
...
influxdb-m
| Author | SHA1 | Date | |
|---|---|---|---|
|
6ef80f8438
|
|||
|
47116904fc
|
|||
|
5b46ecb0b1
|
|||
|
a1ea1b230e
|
|||
|
97679561d8
|
|||
|
a78c6952f0
|
|||
|
3b69e1e2af
|
|||
|
2dfca8d70a
|
|||
|
0352b720cd
|
|||
|
95984157e8
|
|||
| 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
|
@@ -1,32 +1,73 @@
|
||||
when:
|
||||
- event: tag
|
||||
|
||||
|
||||
steps:
|
||||
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 ../..
|
||||
|
||||
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"
|
||||
|
||||
dockerize:
|
||||
image: plugins/kaniko
|
||||
settings:
|
||||
repo: ${FORGE_NAME}/${CI_REPO}
|
||||
registry:
|
||||
from_secret: container_registry
|
||||
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
||||
from_secret: local_registry
|
||||
tags: latest,${CI_COMMIT_TAG}
|
||||
username:
|
||||
from_secret: container_registry_username
|
||||
from_secret: local_username
|
||||
password:
|
||||
from_secret: container_registry_password
|
||||
from_secret: local_password
|
||||
dockerfile: Dockerfile
|
||||
when:
|
||||
- event: [push, tag]
|
||||
|
||||
deploy:
|
||||
image: portainer/kubectl-shell:latest
|
||||
secrets:
|
||||
- source: kube_config
|
||||
target: KUBE_CONFIG_CONTENT
|
||||
- source: encryption_key
|
||||
target: ENCRYPTION_KEY
|
||||
- source: secrets_checksum
|
||||
target: MD5_CHECKSUM
|
||||
image: quay.io/wollud1969/k8s-admin-helper:0.4.1
|
||||
environment:
|
||||
KUBE_CONFIG_CONTENT:
|
||||
from_secret: kube_config
|
||||
GPG_PASSPHRASE:
|
||||
from_secret: gpg_passphrase
|
||||
commands:
|
||||
- export IMAGE_TAG=$CI_COMMIT_TAG
|
||||
- printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig
|
||||
- export KUBECONFIG=/tmp/kubeconfig
|
||||
- ./deployment/deploy.sh
|
||||
when:
|
||||
- event: tag
|
||||
|
||||
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
|
||||
|
||||
ENV UDI_CONF ""
|
||||
|
||||
COPY --from=builder /go/src/udi ./
|
||||
COPY udi ./
|
||||
|
||||
ENTRYPOINT ["./udi"]
|
||||
|
||||
|
||||
@@ -4,6 +4,11 @@ if [ "$IMAGE_TAG" == "" ]; then
|
||||
echo "Make sure IMAGE_TAG is set"
|
||||
exit 1
|
||||
fi
|
||||
if [ "$GPG_PASSPHRASE" == "" ]; then
|
||||
echo "Make sure GPG_PASSPHRASE is set"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
|
||||
IMAGE_NAME=$FORGE_NAME/$CI_REPO
|
||||
@@ -15,7 +20,10 @@ DEPLOYMENT_DIR=$PWD/deployment
|
||||
INSTANCES_DIR=$DEPLOYMENT_DIR/instances
|
||||
|
||||
pushd $DEPLOYMENT_DIR > /dev/null
|
||||
./decrypt-secrets.sh || exit 1
|
||||
# ./decrypt-secrets.sh || exit 1
|
||||
# . /tmp/secrets
|
||||
|
||||
gpg --decrypt --yes --batch --passphrase "$GPG_PASSPHRASE" --homedir /tmp/.gnupg -o /tmp/secrets secrets.asc
|
||||
. /tmp/secrets
|
||||
rm /tmp/secrets
|
||||
popd > /dev/null
|
||||
|
||||
@@ -6,13 +6,22 @@
|
||||
"topicMappings": [
|
||||
{
|
||||
"topics": [ "snmp" ],
|
||||
"handler": "SNMP",
|
||||
"handler": "PREP",
|
||||
"id": "SNMP",
|
||||
"config": {
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "tsm" ],
|
||||
"handler": "PREP",
|
||||
"id": "TSM",
|
||||
"config": {
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "dt1/ai/periodic/1" ],
|
||||
"handler": "DT1T",
|
||||
@@ -53,6 +62,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "IoT/Car/Values" ],
|
||||
"handler": "Car",
|
||||
"id": "Car",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "locative/event/#" ],
|
||||
"handler": "Locative",
|
||||
|
||||
20
deployment/secrets.asc
Normal file
20
deployment/secrets.asc
Normal file
@@ -0,0 +1,20 @@
|
||||
-----BEGIN PGP MESSAGE-----
|
||||
|
||||
jA0ECQMIetkwUzO5q5z+0ukB9IS9Z6rRbXzGHS4dOfwLMbyx2auDu7PYaAgHLHkI
|
||||
25aul+Q2RMUc1WtWgtyIPyVrq84ctjx4AmnXU3Gbsqte6QJIk0RX8w886gTI+NeF
|
||||
RJahlDNeL9LLltQInkFPDXgLC0u/8a+az0aVZUnfAHVhcYkkgQ0JZasc0VQjLqHV
|
||||
dQ3sHmGj1sUC712AujE1f+T5J9CImBdK4r4sZeFq0c3km7hIpOJNkg8LX+0fj9Un
|
||||
/LPPg0Qjq15dnb44UB6g+gsCUCJqTylGAMH4HLvsgRLbs4T4Za0O9lUKZ7UzuvlZ
|
||||
MnbJLIyJt2zxBVQ6NeuchvTmb8Ppkrk2qrO2mXC4hB0YyGtmT3Dy178pNdy7mYR6
|
||||
zLPx7wO+x4MOJOY8uzclHLucAfBhLTgTFCrje+268e99bXuFhAXOA2zqYSyhBI5M
|
||||
XnHUr14XsTACy8CwZRoIpvy9Roe0N3pJq/kdkx/zJ3ieBUWazR5HYFtBeixcELHa
|
||||
EIH9vZCqGi4NfatsZGYyGofVQhV9TnV5yyAruBDsdb0JIYIz1rznzW4megwR6O0K
|
||||
I7W4cTtzT6XdUZQGyWHZO+FSEQ3zoFxLPWeVWj4MJSj0qvd/G2Z07tAw8MJueGRK
|
||||
DsQJOwhrZVH79zuQE+zF+TR3K7A9O3pq0C618gXzXYnVIRoxYqAGo5RyPGlswxRS
|
||||
3sAf8spYQ3iSvo2whpRJ1c7ytmGKPbA3+4YXmJLemCCxiJK3yNJya6xg/hVTeJlL
|
||||
w76+IPGx+VwfURNt+JDf3Xfq2pnppDjW59a69FMraiWCovSl6cUGHr9X+mgQ+C39
|
||||
7OUhKLHQP4s+Yeak5St8/MKKG57bC1k4j07sh9pCXiAPPST3gVhSDmAGPGUDdISd
|
||||
ieOw/ofwzATB+qgEyhPWcRcR/RU0fYwz2q0AxdI6brLgxgGPO6gvQiulY0+MKtuZ
|
||||
kYUnBJiOQ4PBqnPDhtUjGvjT8YAbadWj+pH44bCGd7Q0Kg==
|
||||
=KCiQ
|
||||
-----END PGP MESSAGE-----
|
||||
@@ -1,38 +0,0 @@
|
||||
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
|
||||
smRxtqBItpVFUdsOVb6bXg==
|
||||
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
|
||||
@@ -129,3 +129,54 @@ create or replace view lora_sht21_v as
|
||||
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';
|
||||
|
||||
|
||||
-- Status string `unit:"" json:"status"`
|
||||
-- Timestamp string `unit:"" json:"timestamp"`
|
||||
-- VoltageL1 float32 `unit:"V" json:"voltageL1"`
|
||||
-- VoltageL2 float32 `unit:"V" json:"voltageL2"`
|
||||
-- VoltageL3 float32 `unit:"V" json:"voltageL3"`
|
||||
-- CurrentL1 float32 `unit:"A" json:"currentL1"`
|
||||
-- CurrentL2 float32 `unit:"A" json:"currentL2"`
|
||||
-- CurrentL3 float32 `unit:"A" json:"currentL3"`
|
||||
-- PowerL1 float32 `unit:"W" json:"powerL1"`
|
||||
-- PowerL2 float32 `unit:"W" json:"powerL2"`
|
||||
-- PowerL3 float32 `unit:"W" json:"powerL3"`
|
||||
-- TotalImportEnergy float32 `unit:"Wh" json:"totalImportEnergy"`
|
||||
-- TotalExportEnergy float32 `unit:"Wh" json:"totalExportEnergy"`
|
||||
-- Cnt int `unit:"" json:"cnt"`
|
||||
|
||||
|
||||
create or replace view car_values_v as
|
||||
select time,
|
||||
cast(values->'VoltageL1'->>'value' as float) as voltage_l1,
|
||||
cast(values->'VoltageL2'->>'value' as float) as voltage_l2,
|
||||
cast(values->'VoltageL3'->>'value' as float) as voltage_l3,
|
||||
cast(values->'CurrentL1'->>'value' as float) as current_l1,
|
||||
cast(values->'CurrentL2'->>'value' as float) as current_l2,
|
||||
cast(values->'CurrentL3'->>'value' as float) as current_l3,
|
||||
cast(values->'PowerL1'->>'value' as float) as power_l1,
|
||||
cast(values->'PowerL2'->>'value' as float) as power_l2,
|
||||
cast(values->'PowerL3'->>'value' as float) as power_l3,
|
||||
cast(values->'TotalImportEnergy'->>'value' as float) as total_import_energy,
|
||||
cast(values->'TotalExportEnergy'->>'value' as float) as total_export_energy,
|
||||
values->'Status'->>'value' as status,
|
||||
device
|
||||
from measurements
|
||||
where application = 'Car';
|
||||
@@ -41,3 +41,4 @@ create or replace view cubecell_threeway_battery_v as
|
||||
from measurements
|
||||
where application = 'de-hottis-saerbeck-monitoring' and
|
||||
device = 'eui-70b3d57ed0068fa4';
|
||||
|
||||
|
||||
145
src/udi/config-test-2.json
Normal file
145
src/udi/config-test-2.json
Normal file
@@ -0,0 +1,145 @@
|
||||
{
|
||||
"mqtt": {
|
||||
"broker": "mqtt://172.23.1.102:1883",
|
||||
"tlsEnable": "false"
|
||||
},
|
||||
"topicMappings": [
|
||||
{
|
||||
"topics": [ "snmp" ],
|
||||
"handler": "PREP",
|
||||
"id": "SNMP",
|
||||
"config": {
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "tsm" ],
|
||||
"handler": "PREP",
|
||||
"id": "TSM",
|
||||
"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/PV/Values" ],
|
||||
"handler": "PV",
|
||||
"id": "PV",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "IoT/Car/Values" ],
|
||||
"handler": "Car",
|
||||
"id": "Car",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "IoT/MBGW3/Measurement" ],
|
||||
"handler": "MBGW3",
|
||||
"id": "MBGW3",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"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": [ "zigbee2mqtt/+" ],
|
||||
"handler": "Z2M",
|
||||
"id": "Z2M",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"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:%"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"archiver": {
|
||||
"dir": "./tmp/udi"
|
||||
}
|
||||
}
|
||||
@@ -5,23 +5,19 @@
|
||||
},
|
||||
"topicMappings": [
|
||||
{
|
||||
"topics": [ "NR/Multisensor/+/Temperatur" ],
|
||||
"handler": "SVEJ",
|
||||
"id": "SVEJ0",
|
||||
"topics": [ "IoT/PV/Values" ],
|
||||
"handler": "PV",
|
||||
"id": "PV",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
"application": "Temperature Multisensor",
|
||||
"deviceSelector": "T:2",
|
||||
"valueSelector": "J:$.CurrentTemperature",
|
||||
"unitSelector": "C:°C"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"topics": [ "zigbee2mqtt/+" ],
|
||||
"handler": "Z2M",
|
||||
"id": "Z2M",
|
||||
"topics": [ "IoT/Car/Values" ],
|
||||
"handler": "Car",
|
||||
"id": "Car",
|
||||
"config": {
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
|
||||
@@ -1,46 +1,38 @@
|
||||
package database
|
||||
|
||||
import "time"
|
||||
import "gorm.io/gorm"
|
||||
|
||||
|
||||
type VariableType struct {
|
||||
Label string `json:"label"`
|
||||
Variable string `json:"variable"`
|
||||
Unit string `json:"unit"`
|
||||
Value interface{} `json:"value,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type Measurement struct {
|
||||
Time time.Time `gorm:"not null;primary_key"`
|
||||
Application string `gorm:"not null"`
|
||||
Time time.Time
|
||||
Application string
|
||||
Device string
|
||||
Attributes map[string]interface{} `gorm:"serializer:json;type:jsonb"`
|
||||
Values map[string]VariableType `gorm:"serializer:json;type:jsonb"`
|
||||
Attributes map[string]interface{}
|
||||
Values map[string]VariableType
|
||||
}
|
||||
|
||||
// Simplified structures for backward compatibility
|
||||
type DeviceType struct {
|
||||
Label string
|
||||
ModelIdentifier string
|
||||
Attributes map[string]interface{}
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
gorm.Model
|
||||
Label string `gorm:"not null"`
|
||||
Attributes map[string]interface{} `gorm:"serializer:json;type:jsonb"`
|
||||
}
|
||||
|
||||
|
||||
type DeviceType struct {
|
||||
gorm.Model
|
||||
Label string `gorm:"not null"`
|
||||
ModelIdentifier string
|
||||
Attributes map[string]interface{} `gorm:"serializer:json;type:jsonb"`
|
||||
Label string
|
||||
Attributes map[string]interface{}
|
||||
}
|
||||
|
||||
type Device struct {
|
||||
gorm.Model
|
||||
Label string `gorm:"not null;uniqueIndex:idx_label_application_id"`
|
||||
ApplicationID int `gorm:"not null;uniqueIndex:idx_label_application_id"`
|
||||
Label string
|
||||
Application Application
|
||||
DeviceTypeID int `gorm:"not null"`
|
||||
DeviceType DeviceType
|
||||
Attributes map[string]interface{} `gorm:"serializer:json;type:jsonb"`
|
||||
Attributes map[string]interface{}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,45 +1,144 @@
|
||||
package database
|
||||
|
||||
|
||||
import (
|
||||
"log"
|
||||
//"time"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"udi/counter"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
|
||||
influxdb "github.com/influxdata/influxdb1-client/v2"
|
||||
)
|
||||
|
||||
type DatabaseHandle struct {
|
||||
initialized bool
|
||||
dbh *gorm.DB
|
||||
client influxdb.Client
|
||||
database string
|
||||
}
|
||||
|
||||
func NewDatabaseHandle() *DatabaseHandle {
|
||||
var db DatabaseHandle
|
||||
// inject the whole database configuration via the well-known PG* env variables
|
||||
conn, err := gorm.Open(postgres.Open(""))
|
||||
if err != nil {
|
||||
log.Printf("Unable to open database connection: %s", err)
|
||||
db.initialized = false
|
||||
} else {
|
||||
db.dbh = conn
|
||||
db.initialized = true
|
||||
log.Println("Database connection opened")
|
||||
|
||||
// Read configuration from environment variables
|
||||
influxURL := os.Getenv("INFLUXDB_URL")
|
||||
if influxURL == "" {
|
||||
influxURL = "http://localhost:8086"
|
||||
}
|
||||
|
||||
influxDB := os.Getenv("INFLUXDB_DATABASE")
|
||||
if influxDB == "" {
|
||||
influxDB = "udi"
|
||||
}
|
||||
|
||||
username := os.Getenv("INFLUXDB_USER")
|
||||
password := os.Getenv("INFLUXDB_PASSWORD")
|
||||
|
||||
// Create InfluxDB client
|
||||
client, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
||||
Addr: influxURL,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Unable to create InfluxDB client: %s", err)
|
||||
db.initialized = false
|
||||
return &db
|
||||
}
|
||||
|
||||
// Test connection
|
||||
_, _, err = client.Ping(0)
|
||||
if err != nil {
|
||||
log.Printf("Unable to ping InfluxDB: %s", err)
|
||||
db.initialized = false
|
||||
client.Close()
|
||||
return &db
|
||||
}
|
||||
|
||||
db.client = client
|
||||
db.database = influxDB
|
||||
db.initialized = true
|
||||
log.Printf("InfluxDB connection opened (URL: %s, Database: %s)", influxURL, influxDB)
|
||||
|
||||
return &db
|
||||
}
|
||||
|
||||
func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
||||
if ! self.initialized {
|
||||
log.Printf("Database connection not initialized, can not store, measurement %s lost", measurement)
|
||||
if !self.initialized {
|
||||
log.Printf("Database connection not initialized, can not store, measurement %v lost", measurement)
|
||||
counter.F("Stored")
|
||||
return
|
||||
}
|
||||
|
||||
result := self.dbh.Create(measurement)
|
||||
if result.Error != nil {
|
||||
log.Printf("Unable to insert, measurement %s lost, error: %s", measurement, result.Error)
|
||||
// Create batch points
|
||||
bp, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
|
||||
Database: self.database,
|
||||
Precision: "s",
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Unable to create batch points: %s", err)
|
||||
counter.F("Stored")
|
||||
return
|
||||
}
|
||||
|
||||
// Build tags
|
||||
tags := make(map[string]string)
|
||||
if measurement.Device != "" {
|
||||
tags["device"] = measurement.Device
|
||||
}
|
||||
|
||||
// Build fields from Values
|
||||
fields := make(map[string]interface{})
|
||||
for key, varType := range measurement.Values {
|
||||
// Store the value with the variable name as field key
|
||||
fields[key] = varType.Value
|
||||
|
||||
// Optionally store metadata as separate fields
|
||||
if varType.Unit != "" {
|
||||
fields[key+"_unit"] = varType.Unit
|
||||
}
|
||||
// This is already the column name, so we can skip it
|
||||
//if varType.Variable != "" {
|
||||
// fields[key+"_variable"] = varType.Variable
|
||||
//}
|
||||
if varType.Status != "" {
|
||||
fields[key+"_status"] = varType.Status
|
||||
}
|
||||
}
|
||||
// Add attributes as fields
|
||||
for key, value := range measurement.Attributes {
|
||||
if strValue, ok := value.(string); ok {
|
||||
fields[key] = strValue
|
||||
} else {
|
||||
fields[key] = fmt.Sprintf("%v", value)
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure we have at least one field
|
||||
if len(fields) == 0 {
|
||||
log.Printf("No fields to store in measurement, skipping")
|
||||
counter.F("Stored")
|
||||
return
|
||||
}
|
||||
|
||||
// Create point
|
||||
pt, err := influxdb.NewPoint(
|
||||
measurement.Application,
|
||||
tags,
|
||||
fields,
|
||||
measurement.Time,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Unable to create point: %s", err)
|
||||
counter.F("Stored")
|
||||
return
|
||||
}
|
||||
|
||||
bp.AddPoint(pt)
|
||||
|
||||
// Write batch
|
||||
err = self.client.Write(bp)
|
||||
if err != nil {
|
||||
log.Printf("Unable to write to InfluxDB, measurement lost, error: %s", err)
|
||||
counter.F("Stored")
|
||||
return
|
||||
}
|
||||
@@ -48,49 +147,9 @@ func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
||||
counter.S("Stored")
|
||||
}
|
||||
|
||||
func (self *DatabaseHandle) GetDeviceByLabelAndApplication(applicationLabel string, deviceLabel string) (*Device, error) {
|
||||
if ! self.initialized {
|
||||
err := fmt.Errorf("Database connection not initialized")
|
||||
return nil, err
|
||||
func (self *DatabaseHandle) Close() {
|
||||
if self.initialized && self.client != nil {
|
||||
self.client.Close()
|
||||
log.Println("InfluxDB connection closed")
|
||||
}
|
||||
|
||||
var device Device
|
||||
result := self.dbh.
|
||||
Preload("Application").
|
||||
Preload("DeviceType").
|
||||
Joins("JOIN applications ON devices.application_id = applications.id").
|
||||
Where("devices.label = ? AND applications.label = ?", deviceLabel, applicationLabel).
|
||||
First(&device)
|
||||
|
||||
if result.Error != nil {
|
||||
err := fmt.Errorf("Query failed: %s", result.Error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"log"
|
||||
//"time"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Migrate() {
|
||||
dsn := ""
|
||||
db, err := gorm.Open(postgres.Open(dsn))
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to open database connection: %s", err)
|
||||
}
|
||||
|
||||
db.AutoMigrate(&Application{})
|
||||
log.Println("Application created")
|
||||
|
||||
db.AutoMigrate(&DeviceType{})
|
||||
log.Println("DeviceType created")
|
||||
|
||||
db.AutoMigrate(&Device{})
|
||||
log.Println("Device created")
|
||||
|
||||
db.AutoMigrate(&Measurement{})
|
||||
log.Println("Measurement created")
|
||||
|
||||
log.Println("Remember to call create_hypertable on measurements, sowhat I can't do that for you.")
|
||||
|
||||
/*
|
||||
m := Measurement {
|
||||
Time: time.Now(),
|
||||
Application: "app",
|
||||
Attributes: nil,
|
||||
Values: []SensorType {
|
||||
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 1.0 },
|
||||
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 3.0 },
|
||||
},
|
||||
}
|
||||
db.Create(&m)
|
||||
|
||||
m = Measurement {
|
||||
Time: time.Now(),
|
||||
Application: "app",
|
||||
Attributes: nil,
|
||||
Values: []SensorType {
|
||||
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 10.0 },
|
||||
{ Variable: "Temperature", Unit: "Degree Celsius", Value: 30.0 },
|
||||
},
|
||||
}
|
||||
db.Create(&m)
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
create extension if not exists timescaledb;
|
||||
|
||||
create table application_t (
|
||||
id serial not null primary key,
|
||||
label text not null unique,
|
||||
attributes jsonb
|
||||
);
|
||||
|
||||
create table sensor_type_t (
|
||||
id serial not null primary key,
|
||||
label text not null unique,
|
||||
variable text not null,
|
||||
unit text not null,
|
||||
converter text not null,
|
||||
attributes jsonb
|
||||
);
|
||||
|
||||
create table sensor_t (
|
||||
id serial not null primary key,
|
||||
label text not null,
|
||||
application int references application_t(id),
|
||||
sensor_type int references sensor_type_t(id),
|
||||
attributes jsonb,
|
||||
unique (label, application)
|
||||
);
|
||||
|
||||
create table measurement_t (
|
||||
time timestamp without time zone not null,
|
||||
application text not null,
|
||||
sensor_type text not null,
|
||||
sensor text not null,
|
||||
variable text not null,
|
||||
unit text not null,
|
||||
value double precision not null,
|
||||
attributes jsonb
|
||||
);
|
||||
|
||||
select create_hypertable('measurement_t', 'time');
|
||||
|
||||
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
package dispatcher
|
||||
|
||||
import "log"
|
||||
import "time"
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "net/url"
|
||||
import "udi/mqtt"
|
||||
import "udi/config"
|
||||
import "udi/counter"
|
||||
import "udi/handlers/handler"
|
||||
import "udi/handlers/ttn"
|
||||
import "udi/handlers/iot"
|
||||
import "udi/handlers/pv"
|
||||
import "udi/handlers/mbgw3"
|
||||
import "udi/handlers/sver"
|
||||
import "udi/handlers/svej"
|
||||
import "udi/handlers/dt1t"
|
||||
import "udi/handlers/locative"
|
||||
import "udi/handlers/snmp"
|
||||
import "udi/handlers/z2m"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/counter"
|
||||
"udi/handlers/car"
|
||||
"udi/handlers/dt1t"
|
||||
"udi/handlers/handler"
|
||||
"udi/handlers/iot"
|
||||
"udi/handlers/locative"
|
||||
"udi/handlers/mbgw3"
|
||||
"udi/handlers/prepared"
|
||||
"udi/handlers/pv"
|
||||
"udi/handlers/svej"
|
||||
"udi/handlers/sver"
|
||||
|
||||
// "udi/handlers/ttn"
|
||||
"udi/handlers/z2m"
|
||||
"udi/mqtt"
|
||||
)
|
||||
|
||||
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||
var archiverChannel chan handler.MessageT = make(chan handler.MessageT, 100)
|
||||
@@ -33,8 +36,8 @@ func InitDispatcher() {
|
||||
|
||||
var factory interface{}
|
||||
switch mapping.Handler {
|
||||
case "TTN":
|
||||
factory = ttn.New
|
||||
// case "TTN":
|
||||
// factory = ttn.New
|
||||
case "IoT":
|
||||
factory = iot.New
|
||||
case "PV":
|
||||
@@ -49,17 +52,19 @@ func InitDispatcher() {
|
||||
factory = dt1t.New
|
||||
case "Locative":
|
||||
factory = locative.New
|
||||
case "SNMP":
|
||||
factory = snmp.New
|
||||
case "PREP":
|
||||
factory = prepared.New
|
||||
case "Z2M":
|
||||
factory = z2m.New
|
||||
case "Car":
|
||||
factory = car.New
|
||||
default:
|
||||
factory = nil
|
||||
log.Printf("No handler %s found, ignore mapping", mapping.Handler)
|
||||
}
|
||||
|
||||
fn, ok := factory.(func(string, config.HandlerConfigT) handler.Handler)
|
||||
if ! ok {
|
||||
if !ok {
|
||||
log.Println("Typ Assertion failed")
|
||||
break
|
||||
}
|
||||
@@ -71,7 +76,7 @@ func InitDispatcher() {
|
||||
}
|
||||
|
||||
func storeMessage(filename string, item handler.MessageT) {
|
||||
file, err := os.OpenFile(filename, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
||||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Printf("Unable to open archiving file %s, message is not archived: %s", filename, err)
|
||||
counter.F("Archived")
|
||||
@@ -95,7 +100,7 @@ func archiver() {
|
||||
|
||||
for {
|
||||
select {
|
||||
case message := <- archiverChannel:
|
||||
case message := <-archiverChannel:
|
||||
currentDateStr := message.Timestamp.Format("2006/01/02/15")
|
||||
currentArchivingDir := archivingRootDir + "/" + currentDateStr
|
||||
if currentArchivingDir != lastArchivingDir {
|
||||
@@ -116,9 +121,9 @@ func archiver() {
|
||||
func InputDispatcher() {
|
||||
for {
|
||||
select {
|
||||
case mqttMessage := <- mqtt.InputChannel:
|
||||
case mqttMessage := <-mqtt.InputChannel:
|
||||
//log.Printf("Message arrived in inputDispatcher, topic: %s\n", mqttMessage.Topic)
|
||||
message := handler.MessageT { time.Now(), mqttMessage.Topic, string(mqttMessage.Payload) }
|
||||
message := handler.MessageT{time.Now(), mqttMessage.Topic, string(mqttMessage.Payload)}
|
||||
archiverChannel <- message
|
||||
handleMessage(message)
|
||||
}
|
||||
|
||||
@@ -3,23 +3,14 @@ module udi
|
||||
go 1.22.3
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.3
|
||||
github.com/google/uuid v1.4.0
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
|
||||
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
|
||||
gorm.io/driver/postgres v1.5.9
|
||||
gorm.io/gorm v1.25.11
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/pgx/v5 v5.5.5 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
golang.org/x/crypto v0.25.0 // indirect
|
||||
golang.org/x/net v0.27.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
golang.org/x/net v0.34.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,46 +1,14 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
|
||||
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
|
||||
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
|
||||
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c h1:qSHzRbhzK8RdXOsAdfDgO49TtqC1oZ+acxPrkfTxcCs=
|
||||
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
|
||||
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI=
|
||||
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
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/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
|
||||
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
|
||||
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
||||
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
|
||||
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
|
||||
94
src/udi/handlers/car/car.go
Normal file
94
src/udi/handlers/car/car.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package car
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"reflect"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/database"
|
||||
"udi/handlers/handler"
|
||||
)
|
||||
|
||||
type CarHandler struct {
|
||||
handler.CommonHandler
|
||||
dbh *database.DatabaseHandle
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
"status": "Ok",
|
||||
"timestamp": "2025-12-15T13:11:15.648243",
|
||||
"voltageL1": 228.68,
|
||||
"voltageL2": 227.69,
|
||||
"voltageL3": 228.53,
|
||||
"currentL1": 0.0,
|
||||
"currentL2": 0.0,
|
||||
"currentL3": 0.0,
|
||||
"powerL1": 0.0,
|
||||
"powerL2": 0.0,
|
||||
"powerL3": 0.0,
|
||||
"totalImportEnergy": 0.0,
|
||||
"totalExportEnergy": 0.0,
|
||||
"cnt": 399300}
|
||||
*/
|
||||
|
||||
type CarValue struct {
|
||||
Status string `unit:"" json:"status"`
|
||||
Timestamp string `unit:"" json:"timestamp"`
|
||||
VoltageL1 float32 `unit:"V" json:"voltageL1"`
|
||||
VoltageL2 float32 `unit:"V" json:"voltageL2"`
|
||||
VoltageL3 float32 `unit:"V" json:"voltageL3"`
|
||||
CurrentL1 float32 `unit:"A" json:"currentL1"`
|
||||
CurrentL2 float32 `unit:"A" json:"currentL2"`
|
||||
CurrentL3 float32 `unit:"A" json:"currentL3"`
|
||||
PowerL1 float32 `unit:"W" json:"powerL1"`
|
||||
PowerL2 float32 `unit:"W" json:"powerL2"`
|
||||
PowerL3 float32 `unit:"W" json:"powerL3"`
|
||||
TotalImportEnergy float32 `unit:"Wh" json:"totalImportEnergy"`
|
||||
TotalExportEnergy float32 `unit:"Wh" json:"totalExportEnergy"`
|
||||
Cnt int `unit:"" json:"cnt"`
|
||||
}
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &CarHandler{}
|
||||
t.Id = id
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
log.Printf("Handler Car %d initialized", id)
|
||||
return t
|
||||
}
|
||||
|
||||
func (self *CarHandler) Handle(message handler.MessageT) {
|
||||
//log.Printf("Handler Car %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||
|
||||
var carValue CarValue
|
||||
err := json.Unmarshal([]byte(message.Payload), &carValue)
|
||||
if err != nil {
|
||||
self.Lost("Unable to parse payload into carValue struct", err, message)
|
||||
return
|
||||
}
|
||||
|
||||
variables := make(map[string]database.VariableType)
|
||||
carValueStructValue := reflect.ValueOf(carValue)
|
||||
for i := 0; i < carValueStructValue.NumField(); i++ {
|
||||
field := carValueStructValue.Type().Field(i)
|
||||
fieldValue := carValueStructValue.Field(i)
|
||||
v := database.VariableType{
|
||||
Label: "",
|
||||
Variable: field.Name,
|
||||
Unit: field.Tag.Get("unit"),
|
||||
Value: fieldValue.Interface(),
|
||||
}
|
||||
variables[field.Name] = v
|
||||
}
|
||||
|
||||
measurement := database.Measurement{
|
||||
Time: time.Now(),
|
||||
Application: "Car",
|
||||
Device: "Powermeter",
|
||||
Values: variables,
|
||||
}
|
||||
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
}
|
||||
@@ -74,12 +74,22 @@ func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
||||
|
||||
measurement.Values = make(map[string]database.VariableType)
|
||||
unitMap := map[string]string { "Energy": "Wh", "Power": "W", "Voltage": "V", "Current": "A", "Volume": "m3" }
|
||||
keyCount := make(map[string]int)
|
||||
|
||||
for k, v := range observation.Values {
|
||||
unit, exists := unitMap[k]
|
||||
if ! exists {
|
||||
unit = "Unmapped Unit"
|
||||
}
|
||||
measurement.Values[k] = database.VariableType {
|
||||
|
||||
// Check if key already exists and create unique key if needed
|
||||
keyCount[k]++
|
||||
uniqueKey := k
|
||||
if keyCount[k] > 1 {
|
||||
uniqueKey = k + strconv.Itoa(keyCount[k])
|
||||
}
|
||||
|
||||
measurement.Values[uniqueKey] = database.VariableType {
|
||||
Label: "",
|
||||
Variable: k,
|
||||
Unit: unit,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package snmp
|
||||
package prepared
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
|
||||
type SnmpHandler struct {
|
||||
type PreparedHandler struct {
|
||||
handler.CommonHandler
|
||||
dbh *database.DatabaseHandle
|
||||
}
|
||||
@@ -19,6 +19,8 @@ type endpoint_t struct {
|
||||
Label string `json:"label"`
|
||||
Variable string `json:"variable"`
|
||||
Value string `json:"value"`
|
||||
Unit string `json:"unit"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type observation_t struct {
|
||||
@@ -29,16 +31,16 @@ type observation_t struct {
|
||||
|
||||
|
||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||
t := &SnmpHandler {
|
||||
t := &PreparedHandler {
|
||||
}
|
||||
t.Id = id
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
log.Printf("Handler SNMP %d initialized", id)
|
||||
log.Printf("Handler Prepared %d initialized", id)
|
||||
return t
|
||||
}
|
||||
|
||||
func (self *SnmpHandler) Handle(message handler.MessageT) {
|
||||
// log.Printf("Handler SNMP %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||
func (self *PreparedHandler) Handle(message handler.MessageT) {
|
||||
log.Printf("Handler Prepared %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
||||
|
||||
var observation observation_t
|
||||
err := json.Unmarshal([]byte(message.Payload), &observation)
|
||||
@@ -50,7 +52,7 @@ func (self *SnmpHandler) Handle(message handler.MessageT) {
|
||||
var measurement database.Measurement
|
||||
measurement.Time = time.Now()
|
||||
|
||||
measurement.Application = "SNMP"
|
||||
measurement.Application = self.Id
|
||||
measurement.Device = observation.Device
|
||||
|
||||
measurement.Attributes = map[string]interface{} {
|
||||
@@ -62,12 +64,13 @@ func (self *SnmpHandler) Handle(message handler.MessageT) {
|
||||
measurement.Values[k] = database.VariableType {
|
||||
Label: v.Label,
|
||||
Variable: v.Variable,
|
||||
Unit: "",
|
||||
Unit: v.Unit,
|
||||
Value: v.Value,
|
||||
Status: v.Status,
|
||||
}
|
||||
}
|
||||
|
||||
// log.Printf("Prepared measurement item: %s", measurement)
|
||||
log.Printf("Prepared measurement item: %s", measurement)
|
||||
|
||||
self.dbh.StoreMeasurement(&measurement)
|
||||
self.S()
|
||||
@@ -1,15 +0,0 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -1,58 +1,23 @@
|
||||
package z2m
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
"strings"
|
||||
"reflect"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"udi/config"
|
||||
"udi/handlers/handler"
|
||||
"udi/database"
|
||||
"udi/handlers/z2m/models/wsdcgq11lm"
|
||||
"udi/handlers/z2m/models/wsdcgq01lm"
|
||||
"udi/handlers/z2m/models/gs361ah04"
|
||||
"udi/handlers/handler"
|
||||
)
|
||||
|
||||
|
||||
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 := &Z2MHandler{}
|
||||
t.Id = id
|
||||
t.dbh = database.NewDatabaseHandle()
|
||||
log.Printf("Handler Z2M %d initialized", id)
|
||||
@@ -65,49 +30,51 @@ func (self *Z2MHandler) Handle(message handler.MessageT) {
|
||||
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)
|
||||
measurement.Device = deviceId
|
||||
|
||||
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)
|
||||
// Parse JSON direkt in eine map
|
||||
var jsonData map[string]interface{}
|
||||
err := json.Unmarshal([]byte(message.Payload), &jsonData)
|
||||
if err != nil {
|
||||
self.Lost("Failed to parse JSON payload", err, 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)
|
||||
measurement.Values = make(map[string]database.VariableType)
|
||||
|
||||
// Extract device info for application naming
|
||||
if deviceData, ok := jsonData["device"]; ok {
|
||||
if deviceMap, ok := deviceData.(map[string]any); ok {
|
||||
manufacturerId, hasManufacturer := deviceMap["manufacturerID"]
|
||||
model, hasModel := deviceMap["model"]
|
||||
|
||||
if !hasManufacturer || !hasModel {
|
||||
self.Lost("Missing manufacturerID or model in device data", fmt.Errorf("manufacturerID: %v, model: %v", hasManufacturer, hasModel), handler.MessageT{})
|
||||
return
|
||||
}
|
||||
|
||||
measurement.Application = "z2m_" + fmt.Sprintf("%v", manufacturerId) + "_" + model.(string)
|
||||
}
|
||||
delete(jsonData, "device")
|
||||
}
|
||||
|
||||
// Konvertiere die restlichen Elemente in VariableType-Map
|
||||
for key, value := range jsonData {
|
||||
measurement.Values[key] = database.VariableType{
|
||||
Label: key,
|
||||
Variable: "",
|
||||
Unit: "",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
import "udi/database"
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
log.SetPrefix("UDI Migrate Schema: ")
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||
|
||||
log.Println("Starting")
|
||||
|
||||
database.Migrate()
|
||||
|
||||
log.Println("Done")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user