Compare commits
No commits in common. "main" and "0.0.8" have entirely different histories.
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,8 +1,5 @@
|
|||||||
src/udi/udi
|
src/udi/udi
|
||||||
src/udi/main
|
|
||||||
src/udi/migrate_schema
|
src/udi/migrate_schema
|
||||||
tmp/
|
tmp/
|
||||||
ENVDB
|
ENVDB
|
||||||
ENVDB.cluster
|
ENVDB.cluster
|
||||||
deployment/secrets.txt
|
|
||||||
deployment/secrets
|
|
||||||
|
@ -1,55 +1,9 @@
|
|||||||
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:
|
||||||
|
from_secret: image_name
|
||||||
registry:
|
registry:
|
||||||
from_secret: container_registry
|
from_secret: container_registry
|
||||||
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG}
|
||||||
@ -62,18 +16,15 @@ steps:
|
|||||||
- event: [push, tag]
|
- event: [push, tag]
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
image: quay.io/wollud1969/woodpecker-helper:0.5.1
|
image: portainer/kubectl-shell:latest
|
||||||
environment:
|
secrets:
|
||||||
KUBE_CONFIG_CONTENT:
|
- source: kube_config
|
||||||
from_secret: kube_config
|
target: KUBE_CONFIG_CONTENT
|
||||||
ENCRYPTION_KEY:
|
- source: image_name
|
||||||
from_secret: encryption_key
|
target: IMAGE_NAME
|
||||||
MD5_CHECKSUM:
|
|
||||||
from_secret: secrets_checksum
|
|
||||||
commands:
|
commands:
|
||||||
- export IMAGE_TAG=$CI_COMMIT_TAG
|
|
||||||
- printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig
|
- printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig
|
||||||
- export KUBECONFIG=/tmp/kubeconfig
|
- export KUBECONFIG=/tmp/kubeconfig
|
||||||
- ./deployment/deploy.sh
|
- cat $CI_WORKSPACE/deployment/deploy-yml.tmpl | sed -e 's,%IMAGE%,'$IMAGE_NAME':'$CI_COMMIT_TAG',' | kubectl apply -f -
|
||||||
when:
|
when:
|
||||||
- event: tag
|
- event: tag
|
||||||
|
12
Dockerfile
12
Dockerfile
@ -1,8 +1,16 @@
|
|||||||
|
FROM golang:1.21-alpine 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 udi ./
|
COPY --from=builder /go/src/udi ./
|
||||||
|
|
||||||
ENTRYPOINT ["./udi"]
|
ENTRYPOINT ["./udi"]
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
if [ "$ENCRYPTION_KEY" = "" ]; then
|
|
||||||
echo "ENCRYPTION_KEY not set"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$MD5_CHECKSUM" = "" ]; then
|
|
||||||
echo "No checksum given"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
SECRETS_CIPHERTEXT_FILE=secrets.enc
|
|
||||||
SECRETS_PLAINTEXT_FILE=/tmp/secrets
|
|
||||||
TMP_FILE=`mktemp`
|
|
||||||
POD_NAME_SUFFIX=`date +%s`
|
|
||||||
|
|
||||||
cat $SECRETS_CIPHERTEXT_FILE | \
|
|
||||||
kubectl run openssl-$POD_NAME_SUFFIX \
|
|
||||||
--rm \
|
|
||||||
--image bitnami/debian-base-buildpack:latest \
|
|
||||||
--env KEY=$ENCRYPTION_KEY \
|
|
||||||
-i \
|
|
||||||
-q \
|
|
||||||
-- \
|
|
||||||
/bin/sh -c "openssl enc -aes-256-cbc -salt -pass env:KEY -a -d" > \
|
|
||||||
$TMP_FILE
|
|
||||||
|
|
||||||
if [ `uname` = "Darwin" ]; then
|
|
||||||
CALCULATED_CHECKSUM=`cat $TMP_FILE | md5`
|
|
||||||
elif [ `uname` = "Linux" ]; then
|
|
||||||
CALCULATED_CHECKSUM=`cat $TMP_FILE | md5sum - | awk '{print $1}'`
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$MD5_CHECKSUM" != "$CALCULATED_CHECKSUM" ]; then
|
|
||||||
echo "Invalid checksum"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
#cat $TMP_FILE
|
|
||||||
mv $TMP_FILE $SECRETS_PLAINTEXT_FILE
|
|
||||||
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolumeClaim
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
metadata:
|
||||||
name: %PRE%-udi-archive
|
name: udi-archive
|
||||||
|
namespace: udi
|
||||||
spec:
|
spec:
|
||||||
accessModes:
|
accessModes:
|
||||||
- ReadWriteOnce
|
- ReadWriteOnce
|
||||||
@ -13,11 +14,12 @@ spec:
|
|||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: %PRE%-udi
|
name: udi
|
||||||
|
namespace: udi
|
||||||
labels:
|
labels:
|
||||||
app: udi
|
app: udi
|
||||||
annotations:
|
annotations:
|
||||||
secret.reloader.stakater.com/reload: "%PRE%-udi-conf,%PRE%-udi-db-cred,%PRE%-mqtt-password"
|
secret.reloader.stakater.com/reload: "udi-conf,udi-db-cred"
|
||||||
spec:
|
spec:
|
||||||
replicas: 1
|
replicas: 1
|
||||||
selector:
|
selector:
|
||||||
@ -31,18 +33,20 @@ spec:
|
|||||||
containers:
|
containers:
|
||||||
- name: udi
|
- name: udi
|
||||||
image: %IMAGE%
|
image: %IMAGE%
|
||||||
|
env:
|
||||||
|
- name: UDI_CONF
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: udi-conf
|
||||||
|
key: UDI_CONF
|
||||||
envFrom:
|
envFrom:
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: %PRE%-udi-db-cred
|
name: udi-db-cred
|
||||||
- secretRef:
|
|
||||||
name: %PRE%-mqtt-password
|
|
||||||
- configMapRef:
|
|
||||||
name: %PRE%-udi-conf
|
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- mountPath: /archive
|
- mountPath: /archive
|
||||||
name: udi-archive
|
name: udi-archive
|
||||||
volumes:
|
volumes:
|
||||||
- name: udi-archive
|
- name: udi-archive
|
||||||
persistentVolumeClaim:
|
persistentVolumeClaim:
|
||||||
claimName: %PRE%-udi-archive
|
claimName: udi-archive
|
||||||
|
|
||||||
|
@ -1,92 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
if [ "$IMAGE_TAG" == "" ]; then
|
|
||||||
echo "Make sure IMAGE_TAG is set"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
IMAGE_NAME=$FORGE_NAME/$CI_REPO
|
|
||||||
|
|
||||||
CONFIG_FILE=config.json
|
|
||||||
|
|
||||||
|
|
||||||
DEPLOYMENT_DIR=$PWD/deployment
|
|
||||||
INSTANCES_DIR=$DEPLOYMENT_DIR/instances
|
|
||||||
|
|
||||||
pushd $DEPLOYMENT_DIR > /dev/null
|
|
||||||
./decrypt-secrets.sh || exit 1
|
|
||||||
. /tmp/secrets
|
|
||||||
rm /tmp/secrets
|
|
||||||
popd > /dev/null
|
|
||||||
|
|
||||||
for NAMESPACE_DIR in `find $INSTANCES_DIR -type d -mindepth 1 -maxdepth 1`; do
|
|
||||||
NAMESPACE=`basename $NAMESPACE_DIR`
|
|
||||||
echo "Namespace: $NAMESPACE"
|
|
||||||
|
|
||||||
kubectl create namespace $NAMESPACE \
|
|
||||||
--dry-run=client \
|
|
||||||
-o yaml | \
|
|
||||||
kubectl -f - apply
|
|
||||||
|
|
||||||
pushd $NAMESPACE_DIR > /dev/null
|
|
||||||
for INSTANCE_DIR in `find . -type d -mindepth 1 -maxdepth 1`; do
|
|
||||||
pushd $INSTANCE_DIR > /dev/null
|
|
||||||
INSTANCE=`basename $INSTANCE_DIR`
|
|
||||||
echo "Instance: $INSTANCE"
|
|
||||||
|
|
||||||
# set secret configuration from encrypted and decrypted file
|
|
||||||
VARIABLE_PREFIX=`echo "$NAMESPACE""_""$INSTANCE" | tr - _`
|
|
||||||
|
|
||||||
|
|
||||||
# set MQTT_PASSWORD as secret
|
|
||||||
MQTT_PASSWORD_VARIABLE=$VARIABLE_PREFIX"_MQTT_PASSWORD"
|
|
||||||
MQTT_PASSWORD="${!MQTT_PASSWORD_VARIABLE}"
|
|
||||||
# echo "MQTT_PASSWORD_VARIABLE: $MQTT_PASSWORD_VARIABLE"
|
|
||||||
# echo "MQTT_PASSWORD: $MQTT_PASSWORD"
|
|
||||||
kubectl create secret generic $INSTANCE-mqtt-password \
|
|
||||||
--from-literal=MQTT_PASSWORD="$MQTT_PASSWORD" \
|
|
||||||
--dry-run=client \
|
|
||||||
-o yaml \
|
|
||||||
--save-config | \
|
|
||||||
kubectl apply -f - -n $NAMESPACE
|
|
||||||
|
|
||||||
|
|
||||||
LOGIN_VARIABLE=$VARIABLE_PREFIX"_PGUSER"
|
|
||||||
NEW_UDI_DB_LOGIN="${!LOGIN_VARIABLE}"
|
|
||||||
PASSWORD_VARIABLE=$VARIABLE_PREFIX"_PGPASSWORD"
|
|
||||||
NEW_UDI_DB_PASSWORD="${!PASSWORD_VARIABLE}"
|
|
||||||
DATABASE_VARIABLE=$VARIABLE_PREFIX"_PGDATABASE"
|
|
||||||
NEW_UDI_DB_DATABASE="${!DATABASE_VARIABLE}"
|
|
||||||
NEW_UDI_DB_HOST=database.database1.svc.cluster.local
|
|
||||||
|
|
||||||
kubectl create secret generic $INSTANCE-udi-db-cred \
|
|
||||||
--dry-run=client \
|
|
||||||
-o yaml \
|
|
||||||
--save-config \
|
|
||||||
--from-literal=PGUSER="$NEW_UDI_DB_LOGIN" \
|
|
||||||
--from-literal=PGPASSWORD="$NEW_UDI_DB_PASSWORD" \
|
|
||||||
--from-literal=PGDATABASE="$NEW_UDI_DB_DATABASE" \
|
|
||||||
--from-literal=PGHOST="$NEW_UDI_DB_HOST" \
|
|
||||||
--from-literal=PGSSLMODE="require" | \
|
|
||||||
kubectl apply -f - -n $NAMESPACE
|
|
||||||
|
|
||||||
# set configuration as configMap
|
|
||||||
kubectl create configmap $INSTANCE-udi-conf \
|
|
||||||
--from-literal=UDI_CONF="`cat $CONFIG_FILE`" \
|
|
||||||
--dry-run=client \
|
|
||||||
-o yaml \
|
|
||||||
--save-config | \
|
|
||||||
kubectl apply -f - -n $NAMESPACE
|
|
||||||
|
|
||||||
# prepare k8s deployment statement
|
|
||||||
cat $DEPLOYMENT_DIR/deploy-yml.tmpl | \
|
|
||||||
sed -e 's,%IMAGE%,'$IMAGE_NAME':'$IMAGE_TAG','g | \
|
|
||||||
sed -e 's,%PRE%,'$INSTANCE','g | \
|
|
||||||
kubectl apply -f - -n $NAMESPACE
|
|
||||||
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
popd > /dev/null
|
|
||||||
done
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
ENCRYPTION_KEY=`openssl rand -hex 32`
|
|
||||||
echo $ENCRYPTION_KEY
|
|
||||||
|
|
||||||
SECRETS_PLAINTEXT_FILE=secrets.txt
|
|
||||||
SECRETS_CIPHERTEXT_FILE=secrets.enc
|
|
||||||
|
|
||||||
if [ `uname` = "Darwin" ]; then
|
|
||||||
cat $SECRETS_PLAINTEXT_FILE | md5
|
|
||||||
elif [ `uname` = "Linux" ]; then
|
|
||||||
cat $SECRETS_PLAINTEXT_FILE | md5sum - | awk '{print $1}'
|
|
||||||
fi
|
|
||||||
|
|
||||||
POD_NAME_SUFFIX=`date +%s`
|
|
||||||
|
|
||||||
cat $SECRETS_PLAINTEXT_FILE | \
|
|
||||||
kubectl run openssl-$POD_NAME_SUFFIX \
|
|
||||||
--rm \
|
|
||||||
--image bitnami/debian-base-buildpack:latest \
|
|
||||||
--env KEY=$ENCRYPTION_KEY \
|
|
||||||
-i \
|
|
||||||
-q \
|
|
||||||
-- \
|
|
||||||
/bin/sh -c "openssl enc -aes-256-cbc -salt -pass env:KEY -a" > \
|
|
||||||
$SECRETS_CIPHERTEXT_FILE
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
|
||||||
"username": "de-hottis-level-monitoring@ttn",
|
|
||||||
"password": "ENV",
|
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "v3/#" ],
|
|
||||||
"handler": "TTN",
|
|
||||||
"id": "TTN0",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "/archive"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
|
||||||
"username": "de-hottis-saerbeck-monitoring@ttn",
|
|
||||||
"password": "ENV",
|
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "v3/#" ],
|
|
||||||
"handler": "TTN",
|
|
||||||
"id": "TTN0",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "/archive"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
|
||||||
"username": "de-hottis-level-monitoring@ttn",
|
|
||||||
"password": "ENV",
|
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "v3/#" ],
|
|
||||||
"handler": "TTN",
|
|
||||||
"id": "TTN0",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "/archive"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
|
||||||
"username": "de-hottis-lora-test1@ttn",
|
|
||||||
"password": "ENV",
|
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "v3/#" ],
|
|
||||||
"handler": "TTN",
|
|
||||||
"id": "TTN0",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "/archive"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
|
||||||
"username": "de-hottis-app01@ttn",
|
|
||||||
"password": "ENV",
|
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "v3/#" ],
|
|
||||||
"handler": "TTN",
|
|
||||||
"id": "TTN0",
|
|
||||||
"config": {
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "/archive"
|
|
||||||
}
|
|
||||||
}
|
|
47
deployment/load-config.sh
Executable file
47
deployment/load-config.sh
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
FILE=$1
|
||||||
|
if [ "$FILE" = "" ]; then
|
||||||
|
echo "give config file to load as first argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
SECRET_NAME=$2
|
||||||
|
if [ "$SECRET_NAME" = "" ]; then
|
||||||
|
echo "give secret name to create/modify as second argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
NAMESPACE=$3
|
||||||
|
if [ "$NAMESPACE" = "" ]; then
|
||||||
|
echo "give namespace as third argument"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
kubectl create secret generic $SECRET_NAME \
|
||||||
|
--from-literal=UDI_CONF="`cat $FILE`" \
|
||||||
|
-n $NAMESPACE \
|
||||||
|
--dry-run=client \
|
||||||
|
-o yaml \
|
||||||
|
--save-config | \
|
||||||
|
kubectl apply -f -
|
||||||
|
|
||||||
|
. ~/Workspace/MyKubernetesEnv/ENVDB
|
||||||
|
DATABASE=udi
|
||||||
|
LOGIN=udi
|
||||||
|
PASSWORD=`openssl rand -base64 24`
|
||||||
|
psql <<EOF
|
||||||
|
ALTER USER $LOGIN WITH PASSWORD '$PASSWORD';
|
||||||
|
GRANT ALL PRIVILEGES ON DATABASE $DATABASE TO $LOGIN;
|
||||||
|
COMMIT;
|
||||||
|
EOF
|
||||||
|
|
||||||
|
kubectl create secret generic udi-db-cred \
|
||||||
|
--dry-run=client \
|
||||||
|
-o yaml \
|
||||||
|
--save-config \
|
||||||
|
--from-literal=PGUSER="$LOGIN" \
|
||||||
|
--from-literal=PGHOST="timescaledb.database.svc.cluster.local" \
|
||||||
|
--from-literal=PGPASSWORD="$PASSWORD" \
|
||||||
|
--from-literal=PGSSLMODE="require" \
|
||||||
|
--from-literal=PGDATABASE="$DATABASE" | \
|
||||||
|
kubectl apply -f - -n $NAMESPACE
|
||||||
|
|
@ -1,38 +0,0 @@
|
|||||||
U2FsdGVkX1+v6L4gc+CbYCZyo/UVN7QfmEntIBpk+GAHGf3d7m/4hfcYd39Eh2td
|
|
||||||
lXSmNdt1cdFw/UfZ1x1OlGm/fqLh/j/rWPgEc6BwEcDFDEXpTucTjUHNDonYNH8j
|
|
||||||
eDWeAGokfguqgQG16CBLHdeyocP0kTPJSrIKQgG1Mzzck/kfB1Z6Ggv4z5KEx2dy
|
|
||||||
2rrnm+BeFT1yITwoxa3iJeudcSQznNIqQa+Mx4fUsPV+yorahp4gs0PVVj9POnAT
|
|
||||||
yRhpQgkaq5oZNVcYrWS5+6mmhbzL5jIAa4wfzVep/69RcfBkV5Oj5JJGaQzH0T74
|
|
||||||
wg8dWz/scdi2kkCn0KroJPrsG/lAsFYhbX4kUJQeRUX1pWr/iwD0i8LRx+f2C82Y
|
|
||||||
HgpsnG6c5nPRy68TltgRgCRAIJj87rR/fATVowcpChfe9sXCwfLEZ5Q2hDK8eAPW
|
|
||||||
VS87axMkProyHJZe1GK0v9CAVWpXlxv6eAr8u2SftGA87Xu3ebQ4SjReXIcAb7M6
|
|
||||||
08UnxW4YcfH+usgU2GUuNlzRctAq334AfBWYQO51l/ELJAzaDi6Ht4Czr6R7Bsfh
|
|
||||||
M3ZcjcgqY7j7ywDFmKq/a8Q0Dsjm2sezNtrrRWusomgSKFEf8WncOdkcWOAiza4T
|
|
||||||
+Qubfr1SuZuWFF+migGtYM3X8YS+VpmMRIpJ1otibMELgjvldWGqHIK1uIThLq7F
|
|
||||||
MvQ0Nog6UNg79/8vrUoEUPPB5fQsXcNC5zcpVMrpJcGogBHhsXk1EPFcB75sx/65
|
|
||||||
bl2BZlCBacH9MNIBPh17dMC46EV1FNaLiO1N3/qJkxrkiG5wBDjDlnyMn/mYc/o1
|
|
||||||
olNuIO0nnn2x8ZU02lRo8RqcqOywseZeBhAzOj+899n5Qa/0YQAnb0Y9WAxqLft/
|
|
||||||
0C45HcK5Kgd3C6wqvVUqcQ/UMxQzv0y1cM8gbfpGjUvJ6gUj7vkW08D55A6gV8Lf
|
|
||||||
SrneWAP/1B1mmV16vHaXwoYpTpQwM7i7fHWBOpH7nq6E+0P3LHyon43dYo4P6KM7
|
|
||||||
He3R6phTFp36WI4ZCUQafTDZS196Ol2ZyEAonVwSOIEIyptXeoAmleolXC/eL84Z
|
|
||||||
bEbhld8g+ulrVSrBXFpCY3jBsqPVBYEpZaGYgevsrHPSbwWa/qQkTKnOO4+oz6Pe
|
|
||||||
9iJ1yJbSWfg6Gkr6iqE41Dp4VGXtwTDHHb9YMd56iWHAkxZLFIWdYUr8XfQS6j70
|
|
||||||
j5kV3jV/w5EHGYruBdtxAWc7YKq3pfqvh9R7dD/8JOFZhA140+zmOCWG4qdDhv+5
|
|
||||||
F9vlawudssa9ZHGi1jBFPCNW13LBhUdyCY3apKF4HHeeuA465uzxIqwtkJSigdun
|
|
||||||
vC9ooYZrJjYOnJSTJnKH0WSD0pPC6CIkge+Fxuksq6cst5Zcysw1xz5zs7UNeAP+
|
|
||||||
kLs1+8Kn2d1hJuzSWdWlj7xGratLEdA6pqcfBKvMYtY0kpPPDrxm+F1FZ7LyV+dc
|
|
||||||
G1vfI6aS2azrFrBNXSeOArJ/erGHIGhWxFY0c3bcGOjXwsLWRjQ03Kdj9ffj6UFL
|
|
||||||
4JJaI0I01RilAo+woaZhNmOHl1VxSsU1lDGF7IvW3t0qKLaSg/Rv3pQqdKyjq8I5
|
|
||||||
IxPlUEMdo1EDZZx4qLmYBM1tWhgMbn4nx6P2BS7obnPdaf3B0RPxI68Z49RYZKvR
|
|
||||||
/wTyr7oWCCRQDwCuVH8t/jUrSWspzEK7ApXHdh7T9JlNurFW7oxc8ylooQrAn3Gn
|
|
||||||
mru7X3cUeVtiosAklZ7w+JNxm44IRmDKNVDeAaat+q35EA8MRFGiuXEOeNw54tWH
|
|
||||||
zNkUyUJ79Ie7BkGrZFUFqkvfY3Q/xLaBGYDQe65S8/rerybL0YI7RmMiz4x7yq8L
|
|
||||||
GoIDwPsn0z/AFefoGTi0tAXZeC+EA62okK1kKR9qrh9gmD59uiMbFX1BHe3rWhgP
|
|
||||||
cCPScYeameXV3K6wwQpX8JTdptqMAH5cpEVoUZ/PZZpkaiCuWcMODVbqTpm4SRPt
|
|
||||||
Q9s5+6/g0TUUqz7Fwi0dlfnMZVuK0a1Uf/SBYR7f/UYVLfF5juTZ+IRJwQWwp6QX
|
|
||||||
CzfYms0W34/srtM72mQOpKTd0o3xuFyVbQtZPOpNghIjArQqwt34nEzXPYHqasDx
|
|
||||||
c/yIPdW+B/YVcFPdRV16Izqmjdlupv6pPjY/T6GdHczQsH9gD28HN9+Ka2Cvficf
|
|
||||||
evO7IXe0RuvodQ3tB4LmeWoJB10G7Sko2EEfpFTDXke9Ak/5cGrpdPMtbXCAIm1o
|
|
||||||
B5UhrqNuUYSWdo0mGttbSjFR7pyLujsxLNnp8teBi33QOUhrSId5+mOvtFDGiZKa
|
|
||||||
QCC+W+BIh6IFIwnxH4dDxjz3M65NXzqNV+6mXEFU77cX+oTF4BRe0R/L4nPoaBAN
|
|
||||||
smRxtqBItpVFUdsOVb6bXg==
|
|
@ -1,8 +0,0 @@
|
|||||||
create or replace view level_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'CorrectedDistance'->>'value' as float) as level,
|
|
||||||
cast(values->'Battery'->>'value' as float) as battery,
|
|
||||||
attributes->>'Status' as status,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application = 'de-hottis-level-monitoring';
|
|
@ -43,106 +43,5 @@ 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
|
|
||||||
select time,
|
|
||||||
cast(values->'Value'->>'value' as float) as temperature,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application = 'Temperature Wago';
|
|
||||||
|
|
||||||
create or replace view humidity_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'Value'->>'value' as float) as humidity,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application in ('Humidity Multisensor')
|
|
||||||
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
|
|
||||||
select time,
|
|
||||||
cast(values->'Water'->>'value' as float) as water,
|
|
||||||
cast(values->'Conductance'->>'value' as float) as conductance,
|
|
||||||
cast(values->'Temperature'->>'value' as float) as temperature,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application = 'de-hottis-app01' and attributes->>'DeviceType' = 'dragino-lse01';
|
|
||||||
|
|
||||||
create or replace view co2_v as
|
|
||||||
select time,
|
|
||||||
cast(m.values->'CO2concentration'->>'value' as float) as co2concentration,
|
|
||||||
cast(m.values->'Humidity'->>'value' as float) as humidity,
|
|
||||||
cast(m.values->'Temperature'->>'value' as float) as temperature,
|
|
||||||
cast(m.values->'Brightness'->>'value' as int) as brightness,
|
|
||||||
m.device as device,
|
|
||||||
d.attributes->>'Label' as label
|
|
||||||
from measurements m, devices d
|
|
||||||
where m.application = 'de-hottis-app01' and
|
|
||||||
m.attributes->>'DeviceType' = 'hottis-scd30' and
|
|
||||||
m.device = d.label;
|
|
||||||
|
|
||||||
create or replace view locative_v as
|
|
||||||
select time,
|
|
||||||
device as person,
|
|
||||||
values->'Location'->>'value' as location,
|
|
||||||
values->'Trigger'->>'value' as direction
|
|
||||||
from measurements
|
|
||||||
where application = 'Locative';
|
|
||||||
|
|
||||||
create or replace view router_v as
|
|
||||||
select time,
|
|
||||||
device,
|
|
||||||
cast(values->'wan-in'->>'value' as int) as wanInOctetsPerSeconds,
|
|
||||||
cast(values->'wan-out'->>'value' as int) as wanOutOctetsPerSeconds
|
|
||||||
from measurements
|
|
||||||
where application = 'SNMP' and device = '172.16.3.1';
|
|
||||||
|
|
||||||
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';
|
|
||||||
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
select
|
|
||||||
extract('day' from time)::varchar || '.' || extract('month' from time)::varchar || '.' || extract('year' from time)::varchar as day,
|
|
||||||
avg(temperature)::numeric(10,0) as temperature
|
|
||||||
from room_climate_measurement_t
|
|
||||||
where
|
|
||||||
category = 'Outdoor' and
|
|
||||||
location = 'Outdoor' and
|
|
||||||
extract('hour' from time) = 12 and
|
|
||||||
time::date = now()::date
|
|
||||||
group by day
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
|||||||
-- query
|
|
||||||
|
|
||||||
with
|
|
||||||
first_day_in_year as (
|
|
||||||
select
|
|
||||||
date_trunc('day', min(time)) as day
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where
|
|
||||||
time between date_trunc('year', time) and now()
|
|
||||||
),
|
|
||||||
first_value_in_year as (
|
|
||||||
select
|
|
||||||
time_bucket('1 day', time) as interval,
|
|
||||||
first(exportenergyactive, time) as energy
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where
|
|
||||||
time between (select day from first_day_in_year) and (select day from first_day_in_year) + interval '1 day' and
|
|
||||||
status = 'Ok'
|
|
||||||
group by interval
|
|
||||||
),
|
|
||||||
first_day_in_month as (
|
|
||||||
select
|
|
||||||
date_trunc('day', min(time)) as day
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where
|
|
||||||
time between date_trunc('month', now()) and now()
|
|
||||||
),
|
|
||||||
first_value_in_month as (
|
|
||||||
select
|
|
||||||
time_bucket('1 day', time) as interval,
|
|
||||||
first(exportenergyactive, time) as energy
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where
|
|
||||||
time between (select day from first_day_in_month) and (select day from first_day_in_month) + interval '1 day' and
|
|
||||||
status = 'Ok'
|
|
||||||
group by interval
|
|
||||||
),
|
|
||||||
first_value_in_day as (
|
|
||||||
select
|
|
||||||
time_bucket('1 day', time) as interval,
|
|
||||||
first(exportenergyactive, time) as energy
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where time >= date_trunc('day', now())
|
|
||||||
group by interval
|
|
||||||
),
|
|
||||||
last_value as (
|
|
||||||
select
|
|
||||||
time_bucket('1 day', time) as interval,
|
|
||||||
last(exportenergyactive, time) as energy
|
|
||||||
from pv_power_measurement_t
|
|
||||||
where
|
|
||||||
time between date_trunc('day', now()) and date_trunc('day', now()) + interval '1 day' and
|
|
||||||
status = 'Ok'
|
|
||||||
group by interval
|
|
||||||
)
|
|
||||||
select
|
|
||||||
extract(year from (select day from first_day_in_year))::text as period_value,
|
|
||||||
'Year' as period_name,
|
|
||||||
round(((select energy from last_value) - (select energy from first_value_in_year))::numeric, 2) as yield
|
|
||||||
union
|
|
||||||
select
|
|
||||||
to_char((select day from first_day_in_month), 'Month') as period_value,
|
|
||||||
'Month' as period_name,
|
|
||||||
round(((select energy from last_value) - (select energy from first_value_in_month))::numeric, 2) as yield
|
|
||||||
union
|
|
||||||
select
|
|
||||||
now()::date::text as period_value,
|
|
||||||
'Day' as period_name,
|
|
||||||
round(((select energy from last_value) - (select energy from first_value_in_day))::numeric, 2) as yield;
|
|
||||||
|
|
||||||
-- output format
|
|
||||||
-- wn@atuin:~/Workspace/go-workspace/src/universal-data-ingest [main ≡ +0 ~1 -0 !]$ mosquitto_sub -h 172.23.1.102 -v -t IoT/PV/Yields
|
|
||||||
-- IoT/PV/Yields {"Month":"1.43","Year":"285.39","Day":"0.00"}
|
|
@ -1,14 +0,0 @@
|
|||||||
create or replace view power_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'ActivePowerL1'->>'value' as float) as power_l1,
|
|
||||||
cast(values->'ActivePowerL2'->>'value' as float) as power_l2,
|
|
||||||
cast(values->'ActivePowerL3'->>'value' as float) as power_l3,
|
|
||||||
cast(values->'ActivePowerL123'->>'value' as float) as power_total,
|
|
||||||
cast(values->'PowerfactorL1'->>'value' as float) as factor_l1,
|
|
||||||
cast(values->'PowerfactorL2'->>'value' as float) as factor_l2,
|
|
||||||
cast(values->'PowerfactorL3'->>'value' as float) as factor_l3,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application = 'com-passavant-geiger-poc' and
|
|
||||||
attributes->>'FPort' = '1';
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
|||||||
create or replace view badesee_temperature_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'Temp_Red'->>'value' as float) as Temp1,
|
|
||||||
cast(values->'Temp_White'->>'value' as float) as Temp2,
|
|
||||||
cast(values->'Temp_Black'->>'value' as float) as Temp3,
|
|
||||||
cast(values->'Battery'->>'value' as float) as battery,
|
|
||||||
attributes->>'Status' as status,
|
|
||||||
device
|
|
||||||
from measurements
|
|
||||||
where application = 'de-hottis-saerbeck-monitoring' and
|
|
||||||
device = 'eui-a84041318187ec13';
|
|
||||||
|
|
||||||
create or replace view cubecell_threeway_temperature2_v as
|
|
||||||
select time,
|
|
||||||
cast(values->'Temperature2'->>'value' as float) as value,
|
|
||||||
values->'Temperature2'->>'label' as label
|
|
||||||
from measurements
|
|
||||||
where application = 'de-hottis-saerbeck-monitoring' and
|
|
||||||
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="udi-hottis"
|
PGUSER="udi"
|
||||||
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 default-udi-db-cred -n udi -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
PGPASSWORD=`kubectl get secrets 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
|
@ -1,14 +0,0 @@
|
|||||||
if [ "$1" = "" ]; then
|
|
||||||
echo "set namespace as argument"
|
|
||||||
fi
|
|
||||||
N=$1
|
|
||||||
if [ "$2" = "" ]; then
|
|
||||||
echo "set instance as argument"
|
|
||||||
fi
|
|
||||||
I=$2
|
|
||||||
PGHOST=`kubectl get services traefik -n system -o jsonpath="{.status.loadBalancer.ingress[0].ip}"`
|
|
||||||
PGPASSWORD=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGPASSWORD}" | base64 --decode`
|
|
||||||
PGUSER=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGUSER}" | base64 --decode`
|
|
||||||
PGSSLMODE=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGSSLMODE}" | base64 --decode`
|
|
||||||
PGDATABASE=`kubectl get secrets $I-udi-db-cred -n $N -o jsonpath="{.data.PGDATABASE}" | base64 --decode`
|
|
||||||
export PGUSER PGHOST PGPASSWORD PGSSLMODE PGDATABASE
|
|
@ -4,54 +4,6 @@
|
|||||||
"tlsEnable": "false"
|
"tlsEnable": "false"
|
||||||
},
|
},
|
||||||
"topicMappings": [
|
"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" ],
|
"topics": [ "IoT/PV/Values" ],
|
||||||
"handler": "PV",
|
"handler": "PV",
|
||||||
@ -62,16 +14,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"topics": [ "locative/event/#" ],
|
|
||||||
"handler": "Locative",
|
|
||||||
"id": "Locative",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"topics": [ "IoT/MBGW3/Measurement" ],
|
"topics": [ "IoT/MBGW3/Measurement" ],
|
||||||
"handler": "MBGW3",
|
"handler": "MBGW3",
|
||||||
@ -128,16 +70,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"topics": [ "zigbee2mqtt/+" ],
|
|
||||||
"handler": "Z2M",
|
|
||||||
"id": "Z2M",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
"topics": [ "shellyplusht/+/status/temperature:0" ],
|
||||||
"handler": "SVEJ",
|
"handler": "SVEJ",
|
105
src/udi/config-iot.json
Normal file
105
src/udi/config-iot.json
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
{
|
||||||
|
"mqtt": {
|
||||||
|
"broker": "mqtt://172.23.1.102:1883",
|
||||||
|
"tlsEnable": "false"
|
||||||
|
},
|
||||||
|
"topicMappings": [
|
||||||
|
{
|
||||||
|
"topics": [ "IoT/PV/Values" ],
|
||||||
|
"handler": "PV",
|
||||||
|
"id": "PV",
|
||||||
|
"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": [ "NR/Multisensor/+/Temperatur" ],
|
||||||
|
"handler": "SVEJ",
|
||||||
|
"id": "SVEJ0",
|
||||||
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
"application": "Temperature Multisensor",
|
||||||
|
"deviceSelector": "T:2",
|
||||||
|
"valueSelector": "J:$.CurrentTemperature",
|
||||||
|
"unitSelector": "C:°C"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"topics": [ "NR/Multisensor/+/Feuchte" ],
|
||||||
|
"handler": "SVEJ",
|
||||||
|
"id": "SVEJ1",
|
||||||
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"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:%"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"archiver": {
|
||||||
|
"dir": "./tmp/udi"
|
||||||
|
}
|
||||||
|
}
|
@ -1,15 +1,15 @@
|
|||||||
{
|
{
|
||||||
"mqtt": {
|
"mqtt": {
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
"broker": "mqtt://172.23.1.102:1883",
|
||||||
"username": "de-hottis-saerbeck-monitoring@ttn",
|
"tlsEnable": "false"
|
||||||
"tlsEnable": "true"
|
|
||||||
},
|
},
|
||||||
"topicMappings": [
|
"topicMappings": [
|
||||||
{
|
{
|
||||||
"topics": [ "v3/#" ],
|
"topics": [ "ttn/#" ],
|
||||||
"handler": "TTN",
|
"handler": "TTN",
|
||||||
"id": "TTN0",
|
"id": "TTN0",
|
||||||
"config": {
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
"mqtt": {
|
"mqtt": {
|
||||||
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
"broker": "ssl://eu1.cloud.thethings.network:8883",
|
||||||
"username": "com-passavant-geiger-poc@ttn",
|
"username": "com-passavant-geiger-poc@ttn",
|
||||||
|
"password": "ENV",
|
||||||
"tlsEnable": "true"
|
"tlsEnable": "true"
|
||||||
},
|
},
|
||||||
"topicMappings": [
|
"topicMappings": [
|
||||||
@ -10,12 +11,13 @@
|
|||||||
"handler": "TTN",
|
"handler": "TTN",
|
||||||
"id": "TTN0",
|
"id": "TTN0",
|
||||||
"config": {
|
"config": {
|
||||||
|
"databaseConnStr": "",
|
||||||
"attributes": {
|
"attributes": {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"archiver": {
|
"archiver": {
|
||||||
"dir": "/archive"
|
"dir": "./tmp/udi"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,35 +0,0 @@
|
|||||||
{
|
|
||||||
"mqtt": {
|
|
||||||
"broker": "mqtt://172.23.1.102:1883",
|
|
||||||
"tlsEnable": "false"
|
|
||||||
},
|
|
||||||
"topicMappings": [
|
|
||||||
{
|
|
||||||
"topics": [ "NR/Multisensor/+/Temperatur" ],
|
|
||||||
"handler": "SVEJ",
|
|
||||||
"id": "SVEJ0",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
"application": "Temperature Multisensor",
|
|
||||||
"deviceSelector": "T:2",
|
|
||||||
"valueSelector": "J:$.CurrentTemperature",
|
|
||||||
"unitSelector": "C:°C"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"topics": [ "zigbee2mqtt/+" ],
|
|
||||||
"handler": "Z2M",
|
|
||||||
"id": "Z2M",
|
|
||||||
"config": {
|
|
||||||
"databaseConnStr": "",
|
|
||||||
"attributes": {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"archiver": {
|
|
||||||
"dir": "./tmp/udi"
|
|
||||||
}
|
|
||||||
}
|
|
29
src/udi/config-ttn.json
Normal file
29
src/udi/config-ttn.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"mqtt": {
|
||||||
|
"broker": "mqtt://172.23.1.102:1883",
|
||||||
|
"tlsEnable": "false"
|
||||||
|
},
|
||||||
|
"topicMappings": [
|
||||||
|
{
|
||||||
|
"topics": [ "v3/de-hottis-level-monitoring@ttn/devices/+/up" ],
|
||||||
|
"handler": "TTN"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handlers": [
|
||||||
|
{
|
||||||
|
"name": "TTN",
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "IoT",
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"archiver": {
|
||||||
|
"dir": "./tmp/udi"
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ import "log"
|
|||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
type HandlerConfigT struct {
|
type HandlerConfigT struct {
|
||||||
|
DatabaseConnStr string `json:"databaseConnStr"`
|
||||||
Attributes map[string]string `json:"attributes"`
|
Attributes map[string]string `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ type ConfigT struct {
|
|||||||
Mqtt struct {
|
Mqtt struct {
|
||||||
Broker string `json:"broker"`
|
Broker string `json:"broker"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string
|
Password string `json:"password"`
|
||||||
TlsEnable string `json:"tlsEnable"`
|
TlsEnable string `json:"tlsEnable"`
|
||||||
} `json:"mqtt"`
|
} `json:"mqtt"`
|
||||||
TopicMappings []struct {
|
TopicMappings []struct {
|
||||||
@ -35,6 +36,8 @@ func LoadConfiguration() {
|
|||||||
log.Fatalf("Unable to parse configuration: %s", err)
|
log.Fatalf("Unable to parse configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if Config.Mqtt.Password == "ENV" {
|
||||||
Config.Mqtt.Password = os.Getenv("MQTT_PASSWORD")
|
Config.Mqtt.Password = os.Getenv("MQTT_PASSWORD")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
src/udi/config/example.json
Normal file
26
src/udi/config/example.json
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"mqtt": {
|
||||||
|
"broker": "172.23.1.102:1883",
|
||||||
|
"username": "",
|
||||||
|
"password": "",
|
||||||
|
"tlsEnable": "false"
|
||||||
|
},
|
||||||
|
"topicMappings": [
|
||||||
|
{
|
||||||
|
"topics": ["IoT/MBGW3/Measurement"],
|
||||||
|
"handler": "IoT"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"handlers": [
|
||||||
|
{
|
||||||
|
"name": "IoT",
|
||||||
|
"databaseConnStr": "",
|
||||||
|
"attributes": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"archiver": {
|
||||||
|
"dir": "/mnt/udi/archive"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,94 +0,0 @@
|
|||||||
package counter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
type statsTuple_t struct {
|
|
||||||
Successful int `json:"good"`
|
|
||||||
Failed int `json:"bad"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type stats_t struct {
|
|
||||||
Received statsTuple_t `json:"received"`
|
|
||||||
Archived statsTuple_t `json:"archived"`
|
|
||||||
Dispatched statsTuple_t `json:"dispatched"`
|
|
||||||
Handled map[string]statsTuple_t `json:"handled"`
|
|
||||||
Stored statsTuple_t `json:"stored"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var stats stats_t
|
|
||||||
|
|
||||||
func S(id string) {
|
|
||||||
switch id {
|
|
||||||
case "Received":
|
|
||||||
stats.Received.Successful = stats.Received.Successful + 1
|
|
||||||
case "Archived":
|
|
||||||
stats.Archived.Successful += 1
|
|
||||||
case "Dispatched":
|
|
||||||
stats.Dispatched.Successful += 1
|
|
||||||
case "Stored":
|
|
||||||
stats.Stored.Successful += 1
|
|
||||||
default:
|
|
||||||
log.Printf("Unknown stats id %s", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func F(id string) {
|
|
||||||
switch id {
|
|
||||||
case "Received":
|
|
||||||
stats.Received.Failed += 1
|
|
||||||
case "Archived":
|
|
||||||
stats.Archived.Failed += 1
|
|
||||||
case "Dispatched":
|
|
||||||
stats.Dispatched.Failed += 1
|
|
||||||
case "Stored":
|
|
||||||
stats.Stored.Failed += 1
|
|
||||||
default:
|
|
||||||
log.Printf("Unknown stats id %s", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SH(id string) {
|
|
||||||
if _, ok := stats.Handled[id]; ok {
|
|
||||||
tuple := stats.Handled[id]
|
|
||||||
tuple.Successful += 1
|
|
||||||
stats.Handled[id] = tuple
|
|
||||||
} else {
|
|
||||||
stats.Handled[id] = statsTuple_t { Successful:1, Failed:0, }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FH(id string) {
|
|
||||||
if _, ok := stats.Handled[id]; ok {
|
|
||||||
tuple := stats.Handled[id]
|
|
||||||
tuple.Failed += 1
|
|
||||||
stats.Handled[id] = tuple
|
|
||||||
} else {
|
|
||||||
stats.Handled[id] = statsTuple_t { Successful:0, Failed:1, }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitCounter() {
|
|
||||||
stats = stats_t {
|
|
||||||
Received: statsTuple_t {Successful:0,Failed:0,},
|
|
||||||
Archived: statsTuple_t {Successful:0,Failed:0,},
|
|
||||||
Dispatched: statsTuple_t {Successful:0,Failed:0,},
|
|
||||||
Stored: statsTuple_t {Successful:0,Failed:0,},
|
|
||||||
Handled: make(map[string]statsTuple_t),
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
sj, err := json.Marshal(stats)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Unable to marshal stats object: %s", err)
|
|
||||||
}
|
|
||||||
log.Println(string(sj))
|
|
||||||
time.Sleep(time.Second * 60)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
@ -9,7 +9,6 @@ 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 {
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
//"time"
|
//"time"
|
||||||
"fmt"
|
"fmt"
|
||||||
"udi/counter"
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@ -15,10 +14,9 @@ type DatabaseHandle struct {
|
|||||||
dbh *gorm.DB
|
dbh *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDatabaseHandle() *DatabaseHandle {
|
func NewDatabaseHandle(dsn string) *DatabaseHandle {
|
||||||
var db DatabaseHandle
|
var db DatabaseHandle
|
||||||
// inject the whole database configuration via the well-known PG* env variables
|
conn, err := gorm.Open(postgres.Open(dsn))
|
||||||
conn, err := gorm.Open(postgres.Open(""))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to open database connection: %s", err)
|
log.Printf("Unable to open database connection: %s", err)
|
||||||
db.initialized = false
|
db.initialized = false
|
||||||
@ -33,19 +31,16 @@ func NewDatabaseHandle() *DatabaseHandle {
|
|||||||
func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
func (self *DatabaseHandle) StoreMeasurement(measurement *Measurement) {
|
||||||
if ! self.initialized {
|
if ! self.initialized {
|
||||||
log.Printf("Database connection not initialized, can not store, measurement %s lost", measurement)
|
log.Printf("Database connection not initialized, can not store, measurement %s lost", measurement)
|
||||||
counter.F("Stored")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result := self.dbh.Create(measurement)
|
result := self.dbh.Create(measurement)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
log.Printf("Unable to insert, measurement %s lost, error: %s", measurement, result.Error)
|
log.Printf("Unable to insert, measurement %s lost, error: %s", measurement, result.Error)
|
||||||
counter.F("Stored")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Successfully stored measurement")
|
log.Println("Successfully stored measurement")
|
||||||
counter.S("Stored")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DatabaseHandle) GetDeviceByLabelAndApplication(applicationLabel string, deviceLabel string) (*Device, error) {
|
func (self *DatabaseHandle) GetDeviceByLabelAndApplication(applicationLabel string, deviceLabel string) (*Device, error) {
|
||||||
@ -70,27 +65,5 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ import "fmt"
|
|||||||
import "net/url"
|
import "net/url"
|
||||||
import "udi/mqtt"
|
import "udi/mqtt"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
import "udi/counter"
|
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
import "udi/handlers/ttn"
|
import "udi/handlers/ttn"
|
||||||
import "udi/handlers/iot"
|
import "udi/handlers/iot"
|
||||||
@ -15,17 +14,13 @@ import "udi/handlers/pv"
|
|||||||
import "udi/handlers/mbgw3"
|
import "udi/handlers/mbgw3"
|
||||||
import "udi/handlers/sver"
|
import "udi/handlers/sver"
|
||||||
import "udi/handlers/svej"
|
import "udi/handlers/svej"
|
||||||
import "udi/handlers/dt1t"
|
|
||||||
import "udi/handlers/locative"
|
|
||||||
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)
|
||||||
var archiverChannel chan handler.MessageT = make(chan handler.MessageT, 100)
|
var archiverChannel chan handler.MessageT = make(chan handler.MessageT, 100)
|
||||||
|
|
||||||
func InitDispatcher() {
|
func InitDispatcher() {
|
||||||
log.Printf("Dispatcher initializing")
|
log.Printf("Initializing dispatcher")
|
||||||
go archiver()
|
go archiver()
|
||||||
|
|
||||||
for _, mapping := range config.Config.TopicMappings {
|
for _, mapping := range config.Config.TopicMappings {
|
||||||
@ -34,47 +29,38 @@ func InitDispatcher() {
|
|||||||
var factory interface{}
|
var factory interface{}
|
||||||
switch mapping.Handler {
|
switch mapping.Handler {
|
||||||
case "TTN":
|
case "TTN":
|
||||||
factory = ttn.New
|
factory = ttn.NewTTNHandler
|
||||||
case "IoT":
|
case "IoT":
|
||||||
factory = iot.New
|
factory = iot.NewIoTHandler
|
||||||
case "PV":
|
case "PV":
|
||||||
factory = pv.New
|
factory = pv.NewPvHandler
|
||||||
case "MBGW3":
|
case "MBGW3":
|
||||||
factory = mbgw3.New
|
factory = mbgw3.NewMbgw3Handler
|
||||||
case "SVER":
|
case "SVER":
|
||||||
factory = sver.New
|
factory = sver.NewSverHandler
|
||||||
case "SVEJ":
|
case "SVEJ":
|
||||||
factory = svej.New
|
factory = svej.NewSvejHandler
|
||||||
case "DT1T":
|
|
||||||
factory = dt1t.New
|
|
||||||
case "Locative":
|
|
||||||
factory = locative.New
|
|
||||||
case "PREP":
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn, ok := factory.(func(string, config.HandlerConfigT) handler.Handler)
|
fn, ok := factory.(func(config.HandlerConfigT) handler.Handler)
|
||||||
if ! ok {
|
if ! ok {
|
||||||
log.Println("Typ Assertion failed")
|
log.Println("Typ Assertion failed")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
handler := fn(mapping.Id, mapping.Config)
|
handler := fn(mapping.Config)
|
||||||
handlerMap[mapping.Id] = handler
|
handlerMap[mapping.Id] = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.Printf("handlerMap: %s", handlerMap)
|
log.Printf("handlerMap: %s", handlerMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeMessage(filename string, item handler.MessageT) {
|
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 {
|
if err != nil {
|
||||||
log.Printf("Unable to open archiving file %s, message is not archived: %s", filename, err)
|
log.Printf("Unable to open archiving file %s, message is not archived: %s", filename, err)
|
||||||
counter.F("Archived")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
@ -82,11 +68,9 @@ func storeMessage(filename string, item handler.MessageT) {
|
|||||||
_, err = file.WriteString(string(archivingString) + "\n")
|
_, err = file.WriteString(string(archivingString) + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to write message, message is not archived: %s", err)
|
log.Printf("Unable to write message, message is not archived: %s", err)
|
||||||
counter.F("Archived")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Println("Successfully archived message")
|
log.Println("Successfully archived message")
|
||||||
counter.S("Archived")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func archiver() {
|
func archiver() {
|
||||||
@ -102,10 +86,9 @@ func archiver() {
|
|||||||
err := os.MkdirAll(currentArchivingDir, 0755)
|
err := os.MkdirAll(currentArchivingDir, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Unable to create archiving dir %s: %s", currentArchivingDir, err)
|
log.Printf("Unable to create archiving dir %s: %s", currentArchivingDir, err)
|
||||||
counter.F("Archived")
|
|
||||||
}
|
}
|
||||||
lastArchivingDir = currentArchivingDir
|
lastArchivingDir = currentArchivingDir
|
||||||
//log.Printf("Archiving dir %s created", currentArchivingDir)
|
log.Printf("Archiving dir %s created", currentArchivingDir)
|
||||||
}
|
}
|
||||||
archivingFilename := fmt.Sprintf("%s/%s", currentArchivingDir, url.PathEscape(message.Topic))
|
archivingFilename := fmt.Sprintf("%s/%s", currentArchivingDir, url.PathEscape(message.Topic))
|
||||||
storeMessage(archivingFilename, message)
|
storeMessage(archivingFilename, message)
|
||||||
@ -117,7 +100,7 @@ func InputDispatcher() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case mqttMessage := <- mqtt.InputChannel:
|
case mqttMessage := <- mqtt.InputChannel:
|
||||||
//log.Printf("Message arrived in inputDispatcher, topic: %s\n", mqttMessage.Topic)
|
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
|
archiverChannel <- message
|
||||||
handleMessage(message)
|
handleMessage(message)
|
||||||
@ -131,19 +114,16 @@ func handleMessage(message handler.MessageT) {
|
|||||||
for _, subscribedTopic := range mapping.Topics {
|
for _, subscribedTopic := range mapping.Topics {
|
||||||
// log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
// log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
||||||
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
||||||
//log.Printf("Handle message in handler %s", mapping.Id)
|
log.Printf("Handle message in handler %s", mapping.Id)
|
||||||
handler, exists := handlerMap[mapping.Id]
|
handler, exists := handlerMap[mapping.Id]
|
||||||
if exists {
|
if exists {
|
||||||
handler.Handle(message)
|
handler.Handle(message)
|
||||||
counter.S("Dispatched")
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Handler %s not found, message %s is lost", mapping.Id, message)
|
log.Printf("Handler %s not found, message %s is lost", mapping.Id, message)
|
||||||
counter.F("Dispatched")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("No match for topic %s, message %s is lost", message.Topic, message)
|
log.Printf("No match for topic %s, message %s is lost", message.Topic, message)
|
||||||
counter.F("Dispatched")
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,24 @@
|
|||||||
module udi
|
module udi
|
||||||
|
|
||||||
go 1.22.3
|
go 1.21.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/eclipse/paho.mqtt.golang v1.5.0
|
github.com/eclipse/paho.mqtt.golang v1.4.3
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.4.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.11
|
gorm.io/driver/postgres v1.5.4
|
||||||
gorm.io/gorm v1.25.12
|
gorm.io/gorm v1.25.5
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/websocket v1.5.3 // indirect
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
github.com/jackc/pgx/v5 v5.7.2 // indirect
|
github.com/jackc/pgx/v5 v5.4.3 // 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.32.0 // indirect
|
golang.org/x/crypto v0.14.0 // indirect
|
||||||
golang.org/x/net v0.34.0 // indirect
|
golang.org/x/net v0.10.0 // indirect
|
||||||
golang.org/x/sync v0.10.0 // indirect
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/text v0.21.0 // indirect
|
golang.org/x/text v0.13.0 // indirect
|
||||||
)
|
)
|
||||||
|
@ -11,10 +11,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
|
|||||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
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 h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
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.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
|
||||||
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
|
||||||
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 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
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 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
@ -28,19 +26,19 @@ 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.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
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=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
|
||||||
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
|
||||||
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
|
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
|
||||||
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
package dt1t
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"strconv"
|
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
|
||||||
"udi/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type Dt1tHandler struct {
|
|
||||||
handler.CommonHandler
|
|
||||||
ready bool
|
|
||||||
label string
|
|
||||||
dbh *database.DatabaseHandle
|
|
||||||
application string
|
|
||||||
device string
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
||||||
t := &Dt1tHandler {
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Attributes["Application"] == "" {
|
|
||||||
log.Println("Error: application not configured")
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
t.application = config.Attributes["Application"]
|
|
||||||
if config.Attributes["Device"] == "" {
|
|
||||||
log.Println("Error: device not configured")
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
t.device = config.Attributes["Device"]
|
|
||||||
t.Id = id
|
|
||||||
|
|
||||||
t.dbh = database.NewDatabaseHandle()
|
|
||||||
log.Printf("Handler DT1T %d initialized", id)
|
|
||||||
t.ready = true
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|
||||||
if ! self.ready {
|
|
||||||
self.Lost("Handler is not marked as ready", nil, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// log.Printf("Handler DT1T %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
|
||||||
|
|
||||||
temperature, err := strconv.Atoi(message.Payload)
|
|
||||||
if err != nil {
|
|
||||||
self.Lost("Invalid raw value", err, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if temperature & 0x8000 != 0{
|
|
||||||
temperature = ((temperature - 1) ^ 0xffff) * -1
|
|
||||||
}
|
|
||||||
temperatureF := float32(temperature) / 10.0
|
|
||||||
|
|
||||||
var measurement database.Measurement
|
|
||||||
measurement.Time = time.Now()
|
|
||||||
measurement.Application = self.application
|
|
||||||
measurement.Device = self.device
|
|
||||||
|
|
||||||
var variable database.VariableType
|
|
||||||
variable.Label = "Temperature"
|
|
||||||
variable.Variable = ""
|
|
||||||
variable.Unit = "°C"
|
|
||||||
variable.Value = fmt.Sprintf("%f", temperatureF)
|
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
|
||||||
measurement.Values["Value"] = variable
|
|
||||||
|
|
||||||
// log.Printf("Prepared measurement item: %s", measurement)
|
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
|
||||||
self.S()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,10 +1,6 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"udi/counter"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MessageT struct {
|
type MessageT struct {
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
@ -15,32 +11,5 @@ type MessageT struct {
|
|||||||
type Handler interface {
|
type Handler interface {
|
||||||
GetId() string
|
GetId() string
|
||||||
Handle(MessageT)
|
Handle(MessageT)
|
||||||
Lost(msg string, err error, message MessageT)
|
|
||||||
S()
|
|
||||||
F()
|
|
||||||
}
|
|
||||||
|
|
||||||
type CommonHandler struct {
|
|
||||||
Id string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *CommonHandler) S() {
|
|
||||||
counter.SH(self.Id)
|
|
||||||
}
|
|
||||||
func (self *CommonHandler) F() {
|
|
||||||
counter.FH(self.Id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *CommonHandler) GetId() string {
|
|
||||||
return self.Id
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *CommonHandler) Lost(msg string, err error, message MessageT) {
|
|
||||||
if err == nil {
|
|
||||||
log.Printf("Error: %s, message %s is lost", msg, message)
|
|
||||||
} else {
|
|
||||||
log.Printf("Error: %s (%s), message %s is lost", msg, err, message)
|
|
||||||
}
|
|
||||||
self.F()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,23 +1,29 @@
|
|||||||
package iot
|
package iot
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
import "udi/config"
|
import "fmt"
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type IoTHandler struct {
|
type IoTHandler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewIoTHandler() handler.Handler {
|
||||||
t := &IoTHandler {
|
t := &IoTHandler {
|
||||||
|
id: idSeq,
|
||||||
}
|
}
|
||||||
t.Id = id
|
idSeq += 1
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *IoTHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("IoT%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *IoTHandler) Handle(message handler.MessageT) {
|
func (self *IoTHandler) Handle(message handler.MessageT) {
|
||||||
log.Printf("Handler IoT %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
log.Printf("Handler IoT %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
package locative
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/config"
|
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type LocativeHandler struct {
|
|
||||||
handler.CommonHandler
|
|
||||||
dbh *database.DatabaseHandle
|
|
||||||
}
|
|
||||||
|
|
||||||
type locativeEvent struct {
|
|
||||||
Trigger string `json:"trigger"`
|
|
||||||
Device string `json:"device"`
|
|
||||||
Location string `json:"location"`
|
|
||||||
Latitude string `json:"latitude"`
|
|
||||||
Longitude string `json:"longitude"`
|
|
||||||
Person string `json:"person"`
|
|
||||||
Timestamp string `json:"timestamp"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
||||||
t := &LocativeHandler {
|
|
||||||
}
|
|
||||||
t.Id = id
|
|
||||||
t.dbh = database.NewDatabaseHandle()
|
|
||||||
log.Printf("Handler Locative %d initialized", id)
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func (self *LocativeHandler) Handle(message handler.MessageT) {
|
|
||||||
log.Printf("Handler Locative %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
|
||||||
|
|
||||||
var locativeEvent locativeEvent
|
|
||||||
err := json.Unmarshal([]byte(message.Payload), &locativeEvent)
|
|
||||||
if err != nil {
|
|
||||||
self.Lost("Unable to parse payload into locativeEvent struct", err, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
variables := make(map[string]database.VariableType)
|
|
||||||
locativeEventStructValue := reflect.ValueOf(locativeEvent)
|
|
||||||
for i := 0; i < locativeEventStructValue.NumField(); i++ {
|
|
||||||
field := locativeEventStructValue.Type().Field(i)
|
|
||||||
fieldValue := locativeEventStructValue.Field(i)
|
|
||||||
v := database.VariableType {
|
|
||||||
Label: "",
|
|
||||||
Variable: field.Name,
|
|
||||||
Unit: "",
|
|
||||||
Value: fieldValue.Interface(),
|
|
||||||
}
|
|
||||||
variables[field.Name] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
measurement := database.Measurement {
|
|
||||||
Time: time.Now(),
|
|
||||||
Application: "Locative",
|
|
||||||
Device: locativeEvent.Person,
|
|
||||||
Values: variables,
|
|
||||||
}
|
|
||||||
|
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
|
||||||
self.S()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
|||||||
package mbgw3
|
package mbgw3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
"log"
|
"log"
|
||||||
|
//"reflect"
|
||||||
|
"time"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"fmt"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
@ -11,8 +13,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type Mbgw3Handler struct {
|
type Mbgw3Handler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,22 +31,26 @@ type Observation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewMbgw3Handler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &Mbgw3Handler {
|
t := &Mbgw3Handler {
|
||||||
|
id: idSeq,
|
||||||
}
|
}
|
||||||
t.Id = id
|
idSeq += 1
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
log.Printf("Handler MBGW3 %d initialized", id)
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Mbgw3Handler) GetId() string {
|
||||||
|
return fmt.Sprintf("MBGW3%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
||||||
//log.Printf("Handler MBGW3 %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
// log.Printf("Handler MBGW3 %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
|
|
||||||
var observation Observation
|
var observation Observation
|
||||||
err := json.Unmarshal([]byte(message.Payload), &observation)
|
err := json.Unmarshal([]byte(message.Payload), &observation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.Lost("Unable to parse payload into Observation struct", err, message)
|
log.Printf("Unable to parse payload into Observation struct, message %s -> %s is lost, error ", message.Topic, message.Payload, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,10 +95,9 @@ func (self *Mbgw3Handler) Handle(message handler.MessageT) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.Printf("Prepared measurement item: %s", measurement)
|
// log.Printf("Prepared measurement item: %s", measurement)
|
||||||
|
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,79 +0,0 @@
|
|||||||
package prepared
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
"log"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/config"
|
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type PreparedHandler struct {
|
|
||||||
handler.CommonHandler
|
|
||||||
dbh *database.DatabaseHandle
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
Device string `json:"device"`
|
|
||||||
Label string `json:"label"`
|
|
||||||
Variables map[string]endpoint_t `json:"variables"`
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
||||||
t := &PreparedHandler {
|
|
||||||
}
|
|
||||||
t.Id = id
|
|
||||||
t.dbh = database.NewDatabaseHandle()
|
|
||||||
log.Printf("Handler Prepared %d initialized", id)
|
|
||||||
return t
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
self.Lost("Unable to parse payload into Observation struct", err, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var measurement database.Measurement
|
|
||||||
measurement.Time = time.Now()
|
|
||||||
|
|
||||||
measurement.Application = self.Id
|
|
||||||
measurement.Device = observation.Device
|
|
||||||
|
|
||||||
measurement.Attributes = map[string]interface{} {
|
|
||||||
"Label": observation.Label,
|
|
||||||
}
|
|
||||||
|
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
|
||||||
for k, v := range observation.Variables {
|
|
||||||
measurement.Values[k] = database.VariableType {
|
|
||||||
Label: v.Label,
|
|
||||||
Variable: v.Variable,
|
|
||||||
Unit: v.Unit,
|
|
||||||
Value: v.Value,
|
|
||||||
Status: v.Status,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Prepared measurement item: %s", measurement)
|
|
||||||
|
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
|
||||||
self.S()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
|||||||
package pv
|
package pv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
"log"
|
"fmt"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
@ -11,8 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type PvHandler struct {
|
type PvHandler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,15 +40,18 @@ type PvValue struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewPvHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &PvHandler {
|
t := &PvHandler {
|
||||||
|
id: idSeq,
|
||||||
}
|
}
|
||||||
t.Id = id
|
idSeq += 1
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
log.Printf("Handler PV %d initialized", id)
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *PvHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("PV%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *PvHandler) Handle(message handler.MessageT) {
|
func (self *PvHandler) Handle(message handler.MessageT) {
|
||||||
//log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
//log.Printf("Handler PV %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
@ -53,7 +59,7 @@ func (self *PvHandler) Handle(message handler.MessageT) {
|
|||||||
var pvValue PvValue
|
var pvValue PvValue
|
||||||
err := json.Unmarshal([]byte(message.Payload), &pvValue)
|
err := json.Unmarshal([]byte(message.Payload), &pvValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.Lost("Unable to parse payload into pvValue struct", err, message)
|
log.Printf("Unable to parse payload into pvValue struct, message %s -> %s is lost, error: %s", message.Topic, message.Payload, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +85,6 @@ func (self *PvHandler) Handle(message handler.MessageT) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,8 +13,10 @@ import (
|
|||||||
"udi/database"
|
"udi/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type SingleValueExtractorJsonpathHandler struct {
|
type SingleValueExtractorJsonpathHandler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
ready bool
|
ready bool
|
||||||
application string
|
application string
|
||||||
deviceSelector string
|
deviceSelector string
|
||||||
@ -34,10 +36,12 @@ C:ConstantValue
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewSvejHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SingleValueExtractorJsonpathHandler {
|
t := &SingleValueExtractorJsonpathHandler {
|
||||||
|
id: idSeq,
|
||||||
ready: false,
|
ready: false,
|
||||||
}
|
}
|
||||||
|
idSeq += 1
|
||||||
|
|
||||||
if config.Attributes["application"] == "" {
|
if config.Attributes["application"] == "" {
|
||||||
log.Println("Error: application not configured")
|
log.Println("Error: application not configured")
|
||||||
@ -73,25 +77,29 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
t.unitJsonpath = jp
|
t.unitJsonpath = jp
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Id = id
|
|
||||||
t.ready = true
|
t.ready = true
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
log.Printf("Handler SVEJ %d initialized", id)
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []string, jPayload interface{}, selector string, jp *jsonpath.Compiled) (string, error) {
|
func (self *SingleValueExtractorJsonpathHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("SVE%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lost(msg string, message handler.MessageT) {
|
||||||
|
log.Printf("Error: %s, message %s is lost", msg, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func 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)
|
||||||
@ -101,7 +109,6 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
|||||||
}
|
}
|
||||||
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])
|
||||||
@ -112,40 +119,37 @@ func (self *SingleValueExtractorJsonpathHandler) ExtractionHelper(subTopics []st
|
|||||||
|
|
||||||
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT) {
|
||||||
if ! self.ready {
|
if ! self.ready {
|
||||||
self.Lost("Handler is not marked as ready", nil, message)
|
log.Println("Handler is not marked as ready, message %s is lost", 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 {
|
||||||
self.Lost("Unable to unmarshal payload", err, message)
|
lost(fmt.Sprintf("Unable to unmarshal payload: %s", err), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
device, err1 := self.ExtractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
device, err1 := extractionHelper(subTopics, jPayload, self.deviceSelector, self.deviceJsonpath)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
self.Lost("Device extraction failed", err1, message)
|
lost(fmt.Sprintf("Device extraction failed with %s", err1), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("device: %s", device)
|
value, err2 := extractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
||||||
|
|
||||||
value, err2 := self.ExtractionHelper(subTopics, jPayload, self.valueSelector, self.valueJsonpath)
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
self.Lost("Value extraction failed", err2, message)
|
lost(fmt.Sprintf("Value extraction failed with %s", 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)
|
lost(fmt.Sprintf("Unit extraction failed with %s", err3), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +165,5 @@ func (self *SingleValueExtractorJsonpathHandler) Handle(message handler.MessageT
|
|||||||
|
|
||||||
log.Printf("Prepared measurement item: %s", measurement)
|
log.Printf("Prepared measurement item: %s", measurement)
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,22 +1,28 @@
|
|||||||
package sver
|
package sver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"regexp"
|
"regexp"
|
||||||
"log"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/oliveagle/jsonpath"
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
"udi/handlers/handler"
|
||||||
"udi/database"
|
"udi/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type SingleValueExtractorRegexHandler struct {
|
type SingleValueExtractorRegexHandler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
ready bool
|
ready bool
|
||||||
config localConfig
|
config localConfig
|
||||||
payloadRegex *regexp.Regexp
|
payloadRegex *regexp.Regexp
|
||||||
|
payloadJsonpath *jsonpath.Compiled
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,10 +44,12 @@ type localConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewSverHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SingleValueExtractorRegexHandler {
|
t := &SingleValueExtractorRegexHandler {
|
||||||
|
id: idSeq,
|
||||||
ready: false,
|
ready: false,
|
||||||
}
|
}
|
||||||
|
idSeq += 1
|
||||||
|
|
||||||
var localConfig localConfig
|
var localConfig localConfig
|
||||||
if config.Attributes["application"] == "" {
|
if config.Attributes["application"] == "" {
|
||||||
@ -56,6 +64,18 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
} else {
|
} else {
|
||||||
t.payloadRegex = nil
|
t.payloadRegex = nil
|
||||||
}
|
}
|
||||||
|
payloadJsonpath := config.Attributes["payloadJsonpath"]
|
||||||
|
if payloadJsonpath != "" {
|
||||||
|
j, err := jsonpath.Compile(payloadJsonpath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Unable to compile jsonpath %s", payloadJsonpath)
|
||||||
|
t.payloadJsonpath = nil
|
||||||
|
} else {
|
||||||
|
t.payloadJsonpath = j
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.payloadJsonpath = nil
|
||||||
|
}
|
||||||
|
|
||||||
if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL {
|
if config.Attributes["deviceFrom"] != TOPIC_SEL && config.Attributes["deviceFrom"] != PAYLOAD_SEL && config.Attributes["deviceFrom"] != CONSTANT_SEL {
|
||||||
log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
|
log.Printf("Error: invalid value %s for deviceFrom", config.Attributes["deviceFrom"])
|
||||||
@ -108,16 +128,22 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
|
|
||||||
t.config = localConfig
|
t.config = localConfig
|
||||||
|
|
||||||
t.Id = id
|
|
||||||
t.ready = true
|
t.ready = true
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
log.Printf("Handler SVER %d initialized", id)
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *SingleValueExtractorRegexHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("SVE%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lost(msg string, message handler.MessageT) {
|
||||||
|
log.Printf("Error: %s, message %s is lost", msg, message)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
||||||
if ! self.ready {
|
if ! self.ready {
|
||||||
self.Lost("Handler is not marked as ready", nil, message)
|
log.Println("Handler is not marked as ready, message %s is lost", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Printf("Handler SingleValueExtractor %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
//log.Printf("Handler SingleValueExtractor %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
@ -134,21 +160,31 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
payloadMatches = self.payloadRegex.FindStringSubmatch(message.Payload)
|
payloadMatches = self.payloadRegex.FindStringSubmatch(message.Payload)
|
||||||
//log.Printf("Matches: %s", strings.Join(payloadMatches, ", "))
|
//log.Printf("Matches: %s", strings.Join(payloadMatches, ", "))
|
||||||
}
|
}
|
||||||
|
if self.payloadJsonpath != nil {
|
||||||
|
var jsonData interface{}
|
||||||
|
json.Unmarshal([]byte(message.Payload), &jsonData)
|
||||||
|
p, err := self.payloadJsonpath.Lookup(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
lost(fmt.Sprintf("jsonpath error: %s", err), message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("XXXX: %s", reflect.TypeOf(p))
|
||||||
|
}
|
||||||
|
|
||||||
switch self.config.deviceFrom {
|
switch self.config.deviceFrom {
|
||||||
case TOPIC_SEL:
|
case TOPIC_SEL:
|
||||||
if self.config.devicePart >= len(subTopics) {
|
if self.config.devicePart >= len(subTopics) {
|
||||||
self.Lost("devicePart out of range", nil, message)
|
lost("devicePart out of range", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
measurement.Device = subTopics[self.config.devicePart]
|
measurement.Device = subTopics[self.config.devicePart]
|
||||||
case PAYLOAD_SEL:
|
case PAYLOAD_SEL:
|
||||||
if self.payloadRegex == nil {
|
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||||
self.Lost("no payloadRegex defined, devicePart can't be used", nil, message)
|
lost("no payloadRegex or payloadJsonpath defined, devicePart can't be used", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if self.config.devicePart >= len(payloadMatches) {
|
if self.config.devicePart >= len(payloadMatches) {
|
||||||
self.Lost("devicePart out of range", nil, message)
|
lost("devicePart out of range", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
measurement.Device = payloadMatches[self.config.devicePart]
|
measurement.Device = payloadMatches[self.config.devicePart]
|
||||||
@ -163,12 +199,12 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
|
|
||||||
switch self.config.valueFrom {
|
switch self.config.valueFrom {
|
||||||
case PAYLOAD_SEL:
|
case PAYLOAD_SEL:
|
||||||
if self.payloadRegex == nil {
|
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||||
self.Lost("no payloadRegex defined, valuePart can't be used", nil, message)
|
lost("no payloadRegex or payloadJsonpath defined, valuePart can't be used", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if self.config.valuePart >= len(payloadMatches) {
|
if self.config.valuePart >= len(payloadMatches) {
|
||||||
self.Lost("valuePart out of range", nil, message)
|
lost("valuePart out of range", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
variable.Value = payloadMatches[self.config.valuePart]
|
variable.Value = payloadMatches[self.config.valuePart]
|
||||||
@ -178,12 +214,12 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
|
|
||||||
switch self.config.unitFrom {
|
switch self.config.unitFrom {
|
||||||
case PAYLOAD_SEL:
|
case PAYLOAD_SEL:
|
||||||
if self.payloadRegex == nil {
|
if self.payloadRegex == nil && self.payloadJsonpath == nil {
|
||||||
self.Lost("no payloadRegex defined, unitPart can't be used", nil, message)
|
lost("no payloadRegex or payloadJsonpath defined, unitPart can't be used", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if self.config.unitPart >= len(payloadMatches) {
|
if self.config.unitPart >= len(payloadMatches) {
|
||||||
self.Lost("unitPart out of range", nil, message)
|
lost("unitPart out of range", message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
variable.Unit = payloadMatches[self.config.unitPart]
|
variable.Unit = payloadMatches[self.config.unitPart]
|
||||||
@ -195,6 +231,5 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
|
|
||||||
//log.Printf("Prepared measurement item: %s", measurement)
|
//log.Printf("Prepared measurement item: %s", measurement)
|
||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,89 +0,0 @@
|
|||||||
package draginoLdds75
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
// "log"
|
|
||||||
"strings"
|
|
||||||
"strconv"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
"decoded_payload": {
|
|
||||||
"Bat": 3.299,
|
|
||||||
"Distance": "352 mm",
|
|
||||||
"Interrupt_flag": 0,
|
|
||||||
"Sensor_flag": 1,
|
|
||||||
"TempC_DS18B20": "0.00"
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
type message struct {
|
|
||||||
Bat float32 `json:"Bat"`
|
|
||||||
Distance string `json:"Distance"`
|
|
||||||
Interrupt_flag int `json:"Interrupt_flag"`
|
|
||||||
Sensor_flag int `json:"Sensor_flag"`
|
|
||||||
TempC_DS18B20 string `json:"TempC_DS18B20"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
var message message
|
|
||||||
err := json.Unmarshal(decodedPayload, &message)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
|
||||||
}
|
|
||||||
distanceParts := strings.Split(message.Distance, " ")
|
|
||||||
if len(distanceParts) != 2 && distanceParts[1] != "mm" {
|
|
||||||
return fmt.Errorf("Invalid format for distance value: %s", message.Distance)
|
|
||||||
}
|
|
||||||
distance, err2 := strconv.Atoi(distanceParts[0])
|
|
||||||
if err2 != nil {
|
|
||||||
return fmt.Errorf("Distance value is no number: %s -> %s", message.Distance, err2)
|
|
||||||
}
|
|
||||||
(*variables)["Battery"] = database.VariableType {
|
|
||||||
Label: "Battery",
|
|
||||||
Variable: "Voltage",
|
|
||||||
Unit: "V",
|
|
||||||
Value: message.Bat,
|
|
||||||
}
|
|
||||||
(*variables)["Distance"] = database.VariableType {
|
|
||||||
Label: "Distance",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: distance,
|
|
||||||
}
|
|
||||||
|
|
||||||
if distance == 20 {
|
|
||||||
(*attributes)["Status"] = "invalid value"
|
|
||||||
} else if distance == 0 {
|
|
||||||
(*attributes)["Status"] = "no sensor detected"
|
|
||||||
} else {
|
|
||||||
(*attributes)["Status"] = "Ok"
|
|
||||||
}
|
|
||||||
|
|
||||||
groundLevelI, exists := device.Attributes["GroundLevel"]
|
|
||||||
groundLevelS, ok := groundLevelI.(string)
|
|
||||||
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
|
||||||
if exists && err3 == nil && ok {
|
|
||||||
//log.Println("add corrected distance")
|
|
||||||
correctedDistance := groundLevel - distance
|
|
||||||
(*variables)["CorrectedDistance"] = database.VariableType {
|
|
||||||
Label: "CorrectedDistance",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: correctedDistance,
|
|
||||||
}
|
|
||||||
} /* else {
|
|
||||||
log.Printf("no ground level: %s %s %s", exists, err3, ok)
|
|
||||||
log.Printf("Device: %s", device)
|
|
||||||
log.Printf("Attributes: %s", device.Attributes)
|
|
||||||
} */
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,83 +0,0 @@
|
|||||||
{
|
|
||||||
"end_device_ids": {
|
|
||||||
"device_id": "eui-a84041a14185f67f",
|
|
||||||
"application_ids": {
|
|
||||||
"application_id": "de-hottis-level-monitoring"
|
|
||||||
},
|
|
||||||
"dev_eui": "A84041A14185F67F",
|
|
||||||
"join_eui": "A840410000000101",
|
|
||||||
"dev_addr": "260B3987"
|
|
||||||
},
|
|
||||||
"correlation_ids": [
|
|
||||||
"gs:uplink:01HHP8EVC1N78FGTNBTNZYGCVY"
|
|
||||||
],
|
|
||||||
"received_at": "2023-12-15T08:11:04.142793605Z",
|
|
||||||
"uplink_message": {
|
|
||||||
"session_key_id": "AYZ/cI2YeDLCZr4urRDzCw==",
|
|
||||||
"f_port": 2,
|
|
||||||
"f_cnt": 27758,
|
|
||||||
"frm_payload": "DOMBYAAAAAE=",
|
|
||||||
"decoded_payload": {
|
|
||||||
"Bat": 3.299,
|
|
||||||
"Distance": "352 mm",
|
|
||||||
"Interrupt_flag": 0,
|
|
||||||
"Sensor_flag": 1,
|
|
||||||
"TempC_DS18B20": "0.00"
|
|
||||||
},
|
|
||||||
"rx_metadata": [
|
|
||||||
{
|
|
||||||
"gateway_ids": {
|
|
||||||
"gateway_id": "eui-00005813d35e3021",
|
|
||||||
"eui": "00005813D35E3021"
|
|
||||||
},
|
|
||||||
"timestamp": 3654294763,
|
|
||||||
"rssi": -85,
|
|
||||||
"channel_rssi": -85,
|
|
||||||
"snr": 8.8,
|
|
||||||
"location": {
|
|
||||||
"latitude": 52.17065267448476,
|
|
||||||
"longitude": 7.629437184774199,
|
|
||||||
"source": "SOURCE_REGISTRY"
|
|
||||||
},
|
|
||||||
"uplink_token": "CiIKIAoUZXVpLTAwMDA1ODEzZDM1ZTMwMjESCAAAWBPTXjAhEOu5wM4NGgwIl5TwqwYQ5Z/7vgMg+OvDp63eBA==",
|
|
||||||
"channel_index": 2,
|
|
||||||
"received_at": "2023-12-15T08:11:03.937349093Z"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"settings": {
|
|
||||||
"data_rate": {
|
|
||||||
"lora": {
|
|
||||||
"bandwidth": 125000,
|
|
||||||
"spreading_factor": 7,
|
|
||||||
"coding_rate": "4/5"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"frequency": "867500000",
|
|
||||||
"timestamp": 3654294763
|
|
||||||
},
|
|
||||||
"received_at": "2023-12-15T08:11:03.938112475Z",
|
|
||||||
"consumed_airtime": "0.056576s",
|
|
||||||
"locations": {
|
|
||||||
"user": {
|
|
||||||
"latitude": 52.1710648323742,
|
|
||||||
"longitude": 7.62751003482794,
|
|
||||||
"altitude": 37,
|
|
||||||
"source": "SOURCE_REGISTRY"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"version_ids": {
|
|
||||||
"brand_id": "dragino",
|
|
||||||
"model_id": "ldds75",
|
|
||||||
"hardware_version": "_unknown_hw_version_",
|
|
||||||
"firmware_version": "1.1.4",
|
|
||||||
"band_id": "EU_863_870"
|
|
||||||
},
|
|
||||||
"network_ids": {
|
|
||||||
"net_id": "000013",
|
|
||||||
"ns_id": "EC656E0000000181",
|
|
||||||
"tenant_id": "ttn",
|
|
||||||
"cluster_id": "eu1",
|
|
||||||
"cluster_address": "eu1.cloud.thethings.network"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,90 +0,0 @@
|
|||||||
package draginoLmds200
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
"decoded_payload": {
|
|
||||||
"Bat": 3.082,
|
|
||||||
"DALARM_count": 0,
|
|
||||||
"Distance_alarm": 0,
|
|
||||||
"Interrupt_alarm": 0,
|
|
||||||
"dis1": 105,
|
|
||||||
"dis2": 201
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
type message struct {
|
|
||||||
Bat float32 `json:"Bat"`
|
|
||||||
DALARM_count int `json:"DALARM_count"`
|
|
||||||
Distance_alarm int `json:"Distance_alarm"`
|
|
||||||
Interrupt_alarm int `json:"Interrupt_alarm"`
|
|
||||||
Dis1 int `json:"dis1"`
|
|
||||||
Dis2 int `json:"dis2"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
var message message
|
|
||||||
err := json.Unmarshal(decodedPayload, &message)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
|
||||||
}
|
|
||||||
(*variables)["Battery"] = database.VariableType {
|
|
||||||
Label: "Battery",
|
|
||||||
Variable: "Voltage",
|
|
||||||
Unit: "V",
|
|
||||||
Value: message.Bat,
|
|
||||||
}
|
|
||||||
distance1 := message.Dis1 * 10
|
|
||||||
(*variables)["Distance1"] = database.VariableType {
|
|
||||||
Label: "Distance1",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: distance1,
|
|
||||||
}
|
|
||||||
distance2 := message.Dis2 * 10
|
|
||||||
(*variables)["Distance2"] = database.VariableType {
|
|
||||||
Label: "Distance2",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: distance2,
|
|
||||||
}
|
|
||||||
|
|
||||||
if distance1 == 2 {
|
|
||||||
(*attributes)["Status"] = "invalid value"
|
|
||||||
} else if distance1 == 1 {
|
|
||||||
(*attributes)["Status"] = "no sensor detected"
|
|
||||||
} else {
|
|
||||||
(*attributes)["Status"] = "Ok"
|
|
||||||
}
|
|
||||||
|
|
||||||
groundLevelI, exists := device.Attributes["GroundLevel"]
|
|
||||||
groundLevelS, ok := groundLevelI.(string)
|
|
||||||
groundLevel, err3 := strconv.Atoi(groundLevelS)
|
|
||||||
if exists && err3 == nil && ok {
|
|
||||||
correctedDistance1 := groundLevel - distance1
|
|
||||||
(*variables)["CorrectedDistance1"] = database.VariableType {
|
|
||||||
Label: "CorrectedDistance1",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: correctedDistance1,
|
|
||||||
}
|
|
||||||
correctedDistance2 := groundLevel - distance2
|
|
||||||
(*variables)["CorrectedDistance2"] = database.VariableType {
|
|
||||||
Label: "CorrectedDistance2",
|
|
||||||
Variable: "Level",
|
|
||||||
Unit: "mm",
|
|
||||||
Value: correctedDistance2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,63 +0,0 @@
|
|||||||
package draginoLse01
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
{
|
|
||||||
"Bat":3.211,
|
|
||||||
"TempC_DS18B20":"0.0",
|
|
||||||
"conduct_SOIL":32,
|
|
||||||
"temp_SOIL":"7.56",
|
|
||||||
"water_SOIL":"25.92"
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
type message struct {
|
|
||||||
Bat float32 `json:"Bat"`
|
|
||||||
TempC_DS18B20 string `json:"TempC_DS18B20"`
|
|
||||||
Conduct_SOIL int `json:"conduct_SOIL"`
|
|
||||||
Temp_SOIL string `json:"temp_SOIL"`
|
|
||||||
Water_SOIL string `json:"water_SOIL"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
var message message
|
|
||||||
err := json.Unmarshal(decodedPayload, &message)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
|
||||||
}
|
|
||||||
(*variables)["Battery"] = database.VariableType {
|
|
||||||
Label: "Battery",
|
|
||||||
Variable: "Voltage",
|
|
||||||
Unit: "V",
|
|
||||||
Value: message.Bat,
|
|
||||||
}
|
|
||||||
(*variables)["Conductance"] = database.VariableType {
|
|
||||||
Label: "Conductance",
|
|
||||||
Variable: "Conductance",
|
|
||||||
Unit: "uS/cm",
|
|
||||||
Value: message.Conduct_SOIL,
|
|
||||||
}
|
|
||||||
(*variables)["Temperature"] = database.VariableType {
|
|
||||||
Label: "Temperature",
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: message.Temp_SOIL,
|
|
||||||
}
|
|
||||||
(*variables)["Water"] = database.VariableType {
|
|
||||||
Label: "Water",
|
|
||||||
Variable: "Water",
|
|
||||||
Unit: "%",
|
|
||||||
Value: message.Water_SOIL,
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
@ -1,74 +0,0 @@
|
|||||||
package draginoLsn50
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"encoding/json"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
"decoded_payload": {
|
|
||||||
"ALARM_status": "FALSE",
|
|
||||||
"BatV": 3.659,
|
|
||||||
"Temp_Black": 3276.7,
|
|
||||||
"Temp_Red": 22.6,
|
|
||||||
"Temp_White": 3276.7,
|
|
||||||
"Work_mode": "DS18B20"
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
type message struct {
|
|
||||||
ALARM_status string `json:"ALARM_status"`
|
|
||||||
Bat float32 `json:"BatV"`
|
|
||||||
Work_mode string `json:"Work_mode"`
|
|
||||||
Temp_Black float32 `json:"Temp_Black"`
|
|
||||||
Temp_Red float32 `json:"Temp_Red"`
|
|
||||||
Temp_White float32 `json:"Temp_White"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
var message message
|
|
||||||
err := json.Unmarshal(decodedPayload, &message)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
|
||||||
}
|
|
||||||
(*variables)["Battery"] = database.VariableType {
|
|
||||||
Label: "Battery",
|
|
||||||
Variable: "Voltage",
|
|
||||||
Unit: "V",
|
|
||||||
Value: message.Bat,
|
|
||||||
}
|
|
||||||
(*variables)["Alarm"] = database.VariableType {
|
|
||||||
Label: "Alarm",
|
|
||||||
Variable: "Alarm",
|
|
||||||
Unit: "",
|
|
||||||
Value: message.ALARM_status,
|
|
||||||
}
|
|
||||||
(*variables)["Temp_Red"] = database.VariableType {
|
|
||||||
Label: "Temp_Red",
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: message.Temp_Red,
|
|
||||||
}
|
|
||||||
(*variables)["Temp_Black"] = database.VariableType {
|
|
||||||
Label: "Temp_Black",
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: message.Temp_Black,
|
|
||||||
}
|
|
||||||
(*variables)["Temp_White"] = database.VariableType {
|
|
||||||
Label: "Temp_White",
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: message.Temp_White,
|
|
||||||
}
|
|
||||||
|
|
||||||
(*attributes)["Status"] = "Ok"
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -177,81 +177,83 @@ type emuMessage1 struct {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Parse(fPort int, decodedPayload []byte, _ string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
func Parse(fPort int, decodedPayload []byte) (map[string]database.VariableType, error) {
|
||||||
//log.Printf("Parse input: %d, %s", fPort, decodedPayload)
|
//log.Printf("Parse input: %d, %s", fPort, decodedPayload)
|
||||||
switch fPort {
|
switch fPort {
|
||||||
case 1:
|
case 1:
|
||||||
var emuMessage1 emuMessage1
|
var emuMessage1 emuMessage1
|
||||||
err := json.Unmarshal(decodedPayload, &emuMessage1)
|
err := json.Unmarshal(decodedPayload, &emuMessage1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
return nil, fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
||||||
}
|
}
|
||||||
(*variables)["ActivePowerL1"] = database.VariableType {
|
variables := make(map[string]database.VariableType)
|
||||||
|
variables["ActivePowerL1"] = database.VariableType {
|
||||||
Variable: "ActivePowerL1",
|
Variable: "ActivePowerL1",
|
||||||
Unit: emuMessage1.ActivePowerL1.Unit,
|
Unit: emuMessage1.ActivePowerL1.Unit,
|
||||||
Value: emuMessage1.ActivePowerL1.Value,
|
Value: emuMessage1.ActivePowerL1.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ActivePowerL2"] = database.VariableType {
|
variables["ActivePowerL2"] = database.VariableType {
|
||||||
Variable: "ActivePowerL2",
|
Variable: "ActivePowerL2",
|
||||||
Unit: emuMessage1.ActivePowerL2.Unit,
|
Unit: emuMessage1.ActivePowerL2.Unit,
|
||||||
Value: emuMessage1.ActivePowerL2.Value,
|
Value: emuMessage1.ActivePowerL2.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ActivePowerL3"] = database.VariableType {
|
variables["ActivePowerL3"] = database.VariableType {
|
||||||
Variable: "ActivePowerL3",
|
Variable: "ActivePowerL3",
|
||||||
Unit: emuMessage1.ActivePowerL3.Unit,
|
Unit: emuMessage1.ActivePowerL3.Unit,
|
||||||
Value: emuMessage1.ActivePowerL3.Value,
|
Value: emuMessage1.ActivePowerL3.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ActivePowerL123"] = database.VariableType {
|
variables["ActivePowerL123"] = database.VariableType {
|
||||||
Variable: "ActivePowerL123",
|
Variable: "ActivePowerL123",
|
||||||
Unit: emuMessage1.ActivePowerL123.Unit,
|
Unit: emuMessage1.ActivePowerL123.Unit,
|
||||||
Value: emuMessage1.ActivePowerL123.Value,
|
Value: emuMessage1.ActivePowerL123.Value,
|
||||||
}
|
}
|
||||||
(*variables)["PowerfactorL1"] = database.VariableType {
|
variables["PowerfactorL1"] = database.VariableType {
|
||||||
Variable: "PowerfactorL1",
|
Variable: "PowerfactorL1",
|
||||||
Unit: emuMessage1.PowerfactorL1.Unit,
|
Unit: emuMessage1.PowerfactorL1.Unit,
|
||||||
Value: emuMessage1.PowerfactorL1.Value,
|
Value: emuMessage1.PowerfactorL1.Value,
|
||||||
}
|
}
|
||||||
(*variables)["PowerfactorL2"] = database.VariableType {
|
variables["PowerfactorL2"] = database.VariableType {
|
||||||
Variable: "PowerfactorL2",
|
Variable: "PowerfactorL2",
|
||||||
Unit: emuMessage1.PowerfactorL2.Unit,
|
Unit: emuMessage1.PowerfactorL2.Unit,
|
||||||
Value: emuMessage1.PowerfactorL2.Value,
|
Value: emuMessage1.PowerfactorL2.Value,
|
||||||
}
|
}
|
||||||
(*variables)["PowerfactorL3"] = database.VariableType {
|
variables["PowerfactorL3"] = database.VariableType {
|
||||||
Variable: "PowerfactorL3",
|
Variable: "PowerfactorL3",
|
||||||
Unit: emuMessage1.PowerfactorL3.Unit,
|
Unit: emuMessage1.PowerfactorL3.Unit,
|
||||||
Value: emuMessage1.PowerfactorL3.Value,
|
Value: emuMessage1.PowerfactorL3.Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return variables, nil
|
||||||
case 2:
|
case 2:
|
||||||
var emuMessage2 emuMessage2
|
var emuMessage2 emuMessage2
|
||||||
err := json.Unmarshal(decodedPayload, &emuMessage2)
|
err := json.Unmarshal(decodedPayload, &emuMessage2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
return nil, fmt.Errorf("Unable to parse payload, fPort %d, error %s", fPort, err)
|
||||||
}
|
}
|
||||||
(*variables)["ActiveEnergyExport"] = database.VariableType {
|
variables := make(map[string]database.VariableType)
|
||||||
|
variables["ActiveEnergyExport"] = database.VariableType {
|
||||||
Variable: "ActiveEnergyExport",
|
Variable: "ActiveEnergyExport",
|
||||||
Unit: emuMessage2.ActiveEnergyExport.Unit,
|
Unit: emuMessage2.ActiveEnergyExport.Unit,
|
||||||
Value: emuMessage2.ActiveEnergyExport.Value,
|
Value: emuMessage2.ActiveEnergyExport.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ActiveEnergyImport"] = database.VariableType {
|
variables["ActiveEnergyImport"] = database.VariableType {
|
||||||
Variable: "ActiveEnergyImport",
|
Variable: "ActiveEnergyImport",
|
||||||
Unit: emuMessage2.ActiveEnergyImport.Unit,
|
Unit: emuMessage2.ActiveEnergyImport.Unit,
|
||||||
Value: emuMessage2.ActiveEnergyImport.Value,
|
Value: emuMessage2.ActiveEnergyImport.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ReactiveEnergyExport"] = database.VariableType {
|
variables["ReactiveEnergyExport"] = database.VariableType {
|
||||||
Variable: "ReactiveEnergyExport",
|
Variable: "ReactiveEnergyExport",
|
||||||
Unit: emuMessage2.ReactiveEnergyExport.Unit,
|
Unit: emuMessage2.ReactiveEnergyExport.Unit,
|
||||||
Value: emuMessage2.ReactiveEnergyExport.Value,
|
Value: emuMessage2.ReactiveEnergyExport.Value,
|
||||||
}
|
}
|
||||||
(*variables)["ReactiveEnergyImport"] = database.VariableType {
|
variables["ReactiveEnergyImport"] = database.VariableType {
|
||||||
Variable: "ReactiveEnergyImport",
|
Variable: "ReactiveEnergyImport",
|
||||||
Unit: emuMessage2.ReactiveEnergyImport.Unit,
|
Unit: emuMessage2.ReactiveEnergyImport.Unit,
|
||||||
Value: emuMessage2.ReactiveEnergyImport.Value,
|
Value: emuMessage2.ReactiveEnergyImport.Value,
|
||||||
}
|
}
|
||||||
return nil
|
return variables, nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
return nil, fmt.Errorf("Unexpected fPort %d", fPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package hottisScd30
|
|
||||||
|
|
||||||
import (
|
|
||||||
//"log"
|
|
||||||
"fmt"
|
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/binary"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
type hottisScd30Values struct {
|
|
||||||
Status uint8
|
|
||||||
CO2Conc int32
|
|
||||||
Temp int32
|
|
||||||
Hum int32
|
|
||||||
Bri int32
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, _ *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := base64.StdEncoding.DecodeString(frmPayload)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to base64-decode payload: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var values hottisScd30Values
|
|
||||||
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to cast into struct: %v", err)
|
|
||||||
}
|
|
||||||
var co2concentration float32 = float32(values.CO2Conc) / 100;
|
|
||||||
var temperature float32 = float32(values.Temp) / 100;
|
|
||||||
var humidity float32 = float32(values.Hum) / 100;
|
|
||||||
|
|
||||||
// log.Printf("CO2: %f, Temp: %f, Hum: %f, Status: %d", co2concentration, temperature, humidity, values.Status)
|
|
||||||
|
|
||||||
(*variables)["CO2concentration"] = database.VariableType {
|
|
||||||
Label: "CO2concentration",
|
|
||||||
Variable: "Concentration",
|
|
||||||
Unit: "ppm",
|
|
||||||
Value: co2concentration,
|
|
||||||
}
|
|
||||||
(*variables)["Temperature"] = database.VariableType {
|
|
||||||
Label: "Temperature",
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: temperature,
|
|
||||||
}
|
|
||||||
(*variables)["Humidity"] = database.VariableType {
|
|
||||||
Label: "Humidity",
|
|
||||||
Variable: "Humidity",
|
|
||||||
Unit: "%",
|
|
||||||
Value: humidity,
|
|
||||||
}
|
|
||||||
(*variables)["Brightness"] = database.VariableType {
|
|
||||||
Label: "Brightness",
|
|
||||||
Variable: "Brightness",
|
|
||||||
Unit: "",
|
|
||||||
Value: values.Bri,
|
|
||||||
}
|
|
||||||
|
|
||||||
(*attributes)["Status"] = values.Status
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
package hottisThreeWayThermometer
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"fmt"
|
|
||||||
"bytes"
|
|
||||||
"strconv"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/binary"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
type hottisThreeWayThermometerValues struct {
|
|
||||||
Status uint8
|
|
||||||
Battery uint16
|
|
||||||
SensorAddress1 uint64
|
|
||||||
Value1 int32
|
|
||||||
SensorAddress2 uint64
|
|
||||||
Value2 int32
|
|
||||||
SensorAddress3 uint64
|
|
||||||
Value3 int32
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSensorName(sensorsMap *map[string]interface{}, sensorAddress uint64) string {
|
|
||||||
key := strconv.FormatUint(sensorAddress, 10)
|
|
||||||
if sensorName, exists := (*sensorsMap)[key].(string); exists {
|
|
||||||
return sensorName
|
|
||||||
}
|
|
||||||
return "Sensor" + key
|
|
||||||
}
|
|
||||||
|
|
||||||
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, attributes *map[string]interface{}, device *database.Device) error {
|
|
||||||
deviceAttrs := (*device).Attributes
|
|
||||||
sensorsMap := deviceAttrs["Sensors"].(map[string]interface{})
|
|
||||||
|
|
||||||
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 hottisThreeWayThermometerValues
|
|
||||||
err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &values)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to cast into struct: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var battery float32 = float32(values.Battery) / 1000.0
|
|
||||||
var value1 float32 = float32(values.Value1) / 128.0
|
|
||||||
var value2 float32 = float32(values.Value2) / 128.0
|
|
||||||
var value3 float32 = float32(values.Value3) / 128.0
|
|
||||||
|
|
||||||
//log.Printf("Status: %d, Battery: %d", values.Status, values.Battery);
|
|
||||||
log.Printf("Status: %d", values.Status);
|
|
||||||
log.Printf("Battery: %d, %f", values.Battery, battery);
|
|
||||||
log.Printf("Sensor1: Addr: %d, Value: %f", values.SensorAddress1, value1);
|
|
||||||
log.Printf("Sensor2: Addr: %d, Value: %f", values.SensorAddress2, value2);
|
|
||||||
log.Printf("Sensor3: Addr: %d, Value: %f", values.SensorAddress3, value3);
|
|
||||||
|
|
||||||
(*variables)["Battery"] = database.VariableType {
|
|
||||||
Label: "Battery",
|
|
||||||
Variable: "Voltage",
|
|
||||||
Unit: "V",
|
|
||||||
Value: battery,
|
|
||||||
}
|
|
||||||
(*variables)["Temperature1"] = database.VariableType {
|
|
||||||
Label: getSensorName(&sensorsMap, values.SensorAddress1),
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: value1,
|
|
||||||
}
|
|
||||||
(*variables)["Temperature2"] = database.VariableType {
|
|
||||||
Label: getSensorName(&sensorsMap, values.SensorAddress2),
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: value2,
|
|
||||||
}
|
|
||||||
(*variables)["Temperature3"] = database.VariableType {
|
|
||||||
Label: getSensorName(&sensorsMap, values.SensorAddress3),
|
|
||||||
Variable: "Temperature",
|
|
||||||
Unit: "°C",
|
|
||||||
Value: value3,
|
|
||||||
}
|
|
||||||
|
|
||||||
(*attributes)["Status"] = values.Status
|
|
||||||
(*attributes)["SensorAddress1"] = values.SensorAddress1
|
|
||||||
(*attributes)["SensorAddress2"] = values.SensorAddress2
|
|
||||||
(*attributes)["SensorAddress3"] = values.SensorAddress3
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package rawPayloadPrinter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"fmt"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/hex"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
func Parse(fPort int, _ []byte, frmPayload string, variables *map[string]database.VariableType, _ *map[string]interface{}, _ *database.Device) error {
|
|
||||||
if fPort != 2 {
|
|
||||||
return fmt.Errorf("Unexpected fPort %d", fPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes, err := base64.StdEncoding.DecodeString(frmPayload)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to base64-decode payload: %v", err)
|
|
||||||
}
|
|
||||||
hexString := hex.EncodeToString(bytes)
|
|
||||||
|
|
||||||
log.Printf("Payload: %s", hexString)
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,27 +1,18 @@
|
|||||||
package ttn
|
package ttn
|
||||||
|
|
||||||
import (
|
import "log"
|
||||||
"fmt"
|
import "fmt"
|
||||||
"log"
|
import "time"
|
||||||
"time"
|
import "encoding/json"
|
||||||
"encoding/json"
|
import "udi/config"
|
||||||
"udi/config"
|
import "udi/handlers/handler"
|
||||||
"udi/handlers/handler"
|
import "udi/handlers/ttn/models/emuProfIILoRaCfg1"
|
||||||
"udi/handlers/ttn/models/emuProfIILoRaCfg1"
|
import "udi/database"
|
||||||
"udi/handlers/ttn/models/draginoLdds75"
|
|
||||||
"udi/handlers/ttn/models/draginoLmds200"
|
|
||||||
"udi/handlers/ttn/models/draginoLse01"
|
|
||||||
"udi/handlers/ttn/models/draginoLsn50"
|
|
||||||
"udi/handlers/ttn/models/rawPayloadPrinter"
|
|
||||||
"udi/handlers/ttn/models/hottisScd30"
|
|
||||||
"udi/handlers/ttn/models/hottisGy21"
|
|
||||||
"udi/handlers/ttn/models/hottisThreeWayThermometer"
|
|
||||||
"udi/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
type TTNHandler struct {
|
type TTNHandler struct {
|
||||||
handler.CommonHandler
|
id int
|
||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,17 +72,25 @@ func (self *DecodedPayloaderHolder) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func NewTTNHandler(config config.HandlerConfigT) handler.Handler {
|
||||||
t := &TTNHandler {
|
t := &TTNHandler {
|
||||||
|
id: idSeq,
|
||||||
}
|
}
|
||||||
t.Id = id
|
idSeq += 1
|
||||||
t.dbh = database.NewDatabaseHandle()
|
t.dbh = database.NewDatabaseHandle(config.DatabaseConnStr)
|
||||||
log.Printf("Handler TTN %d initialized", id)
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *TTNHandler) GetId() string {
|
||||||
|
return fmt.Sprintf("TTN%d", self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lost(msg string, message handler.MessageT) {
|
||||||
|
log.Printf("Error: %s, message %s is lost", msg, message)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *TTNHandler) Handle(message handler.MessageT) {
|
func (self *TTNHandler) Handle(message handler.MessageT) {
|
||||||
//log.Printf("Handler TTN %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
// log.Printf("Handler TTN %d processing %s -> %s", self.id, message.Topic, message.Payload)
|
||||||
|
|
||||||
var measurement database.Measurement
|
var measurement database.Measurement
|
||||||
measurement.Time = time.Now()
|
measurement.Time = time.Now()
|
||||||
@ -99,7 +98,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
var uplinkMessage uplinkMessage
|
var uplinkMessage uplinkMessage
|
||||||
err := json.Unmarshal([]byte(message.Payload), &uplinkMessage)
|
err := json.Unmarshal([]byte(message.Payload), &uplinkMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.Lost("Error when unmarshaling message", err, message)
|
lost(fmt.Sprintf("Error when unmarshaling message: %s, ", err), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Printf("Parsed message: %s", uplinkMessage)
|
//log.Printf("Parsed message: %s", uplinkMessage)
|
||||||
@ -112,7 +111,7 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
|
attributes.FrmPayload = uplinkMessage.UplinkMessage.FrmPayload
|
||||||
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
|
attributes.ConsumedAirtime = uplinkMessage.UplinkMessage.ConsumedAirtime
|
||||||
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
|
for _, rxm := range uplinkMessage.UplinkMessage.RxMetadata {
|
||||||
//log.Printf("RXM: %s", rxm)
|
log.Printf("RXM: %s", rxm)
|
||||||
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
|
g := gatewayAttributes { GatewayId: rxm.GatewayIds.GatewayId, Rssi: rxm.Rssi, Snr: rxm.Snr }
|
||||||
attributes.Gateways = append(attributes.Gateways, g)
|
attributes.Gateways = append(attributes.Gateways, g)
|
||||||
}
|
}
|
||||||
@ -130,54 +129,31 @@ func (self *TTNHandler) Handle(message handler.MessageT) {
|
|||||||
//log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
|
//log.Printf("ApplicationId: %s, DeviceId: %s", attributes.ApplicationId, attributes.DeviceId)
|
||||||
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
|
device, err2 := self.dbh.GetDeviceByLabelAndApplication(attributes.ApplicationId, attributes.DeviceId)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
self.Lost("Error when loading device", err2, message)
|
lost(fmt.Sprintf("Error when loading device: %s, ", err2), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
measurement.Application = attributes.ApplicationId
|
measurement.Application = attributes.ApplicationId
|
||||||
measurement.Device = attributes.DeviceId
|
measurement.Device = attributes.DeviceId
|
||||||
measurement.Attributes["DeviceType"] = device.DeviceType.ModelIdentifier
|
|
||||||
|
|
||||||
//log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
|
//log.Printf("DeviceLabel: %s, DeviceType: %s", device.Label, device.DeviceType.ModelIdentifier)
|
||||||
|
|
||||||
var parser func(int, []byte, string, *map[string]database.VariableType, *map[string]interface{}, *database.Device) error
|
var parser func(int, []byte) (map[string]database.VariableType, error)
|
||||||
switch device.DeviceType.ModelIdentifier {
|
switch device.DeviceType.ModelIdentifier {
|
||||||
case "emu-prof-ii-lora-cfg1":
|
case "emu-prof-ii-lora-cfg1":
|
||||||
parser = emuProfIILoRaCfg1.Parse
|
parser = emuProfIILoRaCfg1.Parse
|
||||||
case "dragino-ldds75":
|
|
||||||
parser = draginoLdds75.Parse
|
|
||||||
case "dragino-lmds200":
|
|
||||||
parser = draginoLmds200.Parse
|
|
||||||
case "dragino-lse01":
|
|
||||||
parser = draginoLse01.Parse
|
|
||||||
case "dragino-lsn50":
|
|
||||||
parser = draginoLsn50.Parse
|
|
||||||
case "raw-payload-printer":
|
|
||||||
parser = rawPayloadPrinter.Parse
|
|
||||||
case "hottis-scd30":
|
|
||||||
parser = hottisScd30.Parse
|
|
||||||
case "hottis-gy21":
|
|
||||||
parser = hottisGy21.Parse
|
|
||||||
case "hottis-threeway-thermometer":
|
|
||||||
parser = hottisThreeWayThermometer.Parse
|
|
||||||
default:
|
default:
|
||||||
self.Lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), nil, message)
|
lost(fmt.Sprintf("No parser found for %s", device.DeviceType.ModelIdentifier), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
variables, err3 := parser(uplinkMessage.UplinkMessage.FPort, uplinkMessage.UplinkMessage.DecodedPayload.Payload)
|
||||||
err3 := parser(uplinkMessage.UplinkMessage.FPort,
|
|
||||||
uplinkMessage.UplinkMessage.DecodedPayload.Payload,
|
|
||||||
uplinkMessage.UplinkMessage.FrmPayload,
|
|
||||||
&(measurement.Values),
|
|
||||||
&(measurement.Attributes),
|
|
||||||
device)
|
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
self.Lost("Model parser failed", err3, message)
|
lost(fmt.Sprintf("Model parser failed: %s", err3), message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//log.Printf("Prepared measurement item: %s", measurement)
|
measurement.Values = variables
|
||||||
|
log.Printf("Prepared measurement item: %s", measurement)
|
||||||
self.dbh.StoreMeasurement(&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,113 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -5,7 +5,6 @@ import "os"
|
|||||||
import "os/signal"
|
import "os/signal"
|
||||||
import "udi/mqtt"
|
import "udi/mqtt"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
import "udi/counter"
|
|
||||||
import "udi/dispatcher"
|
import "udi/dispatcher"
|
||||||
|
|
||||||
|
|
||||||
@ -24,8 +23,6 @@ func main() {
|
|||||||
mqtt.StartMqttClient()
|
mqtt.StartMqttClient()
|
||||||
defer mqtt.StopMqttClient()
|
defer mqtt.StopMqttClient()
|
||||||
|
|
||||||
counter.InitCounter()
|
|
||||||
|
|
||||||
log.Println("UDI running")
|
log.Println("UDI running")
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
|
@ -7,7 +7,6 @@ import MQTT "github.com/eclipse/paho.mqtt.golang"
|
|||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
import "crypto/tls"
|
import "crypto/tls"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
import "udi/counter"
|
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Topic string
|
Topic string
|
||||||
@ -27,12 +26,10 @@ func onMessageReceived(client MQTT.Client, message MQTT.Message) {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case InputChannel <- m:
|
case InputChannel <- m:
|
||||||
counter.S("Received")
|
|
||||||
{}
|
{}
|
||||||
//log.Println("Message sent to channel")
|
//log.Println("Message sent to channel")
|
||||||
default:
|
default:
|
||||||
log.Println("Channel full, message lost")
|
log.Println("Channel full, message lost")
|
||||||
counter.F("Received")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +54,7 @@ func onConnect(client MQTT.Client) {
|
|||||||
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
||||||
log.Fatalf("Unable to subscribe to topic %s, error %s", topic, token.Error())
|
log.Fatalf("Unable to subscribe to topic %s, error %s", topic, token.Error())
|
||||||
}
|
}
|
||||||
log.Printf("Topic %s subscribed", topic)
|
log.Printf("Successfully subscribed to topic %s", topic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,19 +101,19 @@ func StartMqttClient() {
|
|||||||
|
|
||||||
enableTls := config.Config.Mqtt.TlsEnable
|
enableTls := config.Config.Mqtt.TlsEnable
|
||||||
if enableTls == "true" {
|
if enableTls == "true" {
|
||||||
//log.Println("Enabling TLS connection")
|
log.Println("Enabling TLS connection")
|
||||||
tlsConfig := &tls.Config {
|
tlsConfig := &tls.Config {
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
opts.SetTLSConfig(tlsConfig)
|
opts.SetTLSConfig(tlsConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Broker connecting")
|
log.Println("Trying to connect to broker")
|
||||||
mqttClient = MQTT.NewClient(opts)
|
mqttClient = MQTT.NewClient(opts)
|
||||||
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
||||||
log.Fatalf("Unable to connect to broker %s, error %s", broker, token.Error())
|
log.Fatalf("Unable to connect to broker %s, error %s", broker, token.Error())
|
||||||
}
|
}
|
||||||
//log.Printf("Successfully connected to broker %s", broker)
|
log.Printf("Successfully connected to broker %s", broker)
|
||||||
|
|
||||||
go outputDispatcher(mqttClient)
|
go outputDispatcher(mqttClient)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user