From 1943c52ed41a9cd3b3a6a81f68eb7a2e9c7fb65e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thibaut=20VAR=C3=88NE?= Date: 2022年1月23日 18:34:15 +0100 Subject: [PATCH] Add tools/ticprocess.py A simple script matching my current needs :) --- README.md | 2 +- tools/ticprocess.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 tools/ticprocess.py diff --git a/README.md b/README.md index 4ee029e..042f56a 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ stdbuf -oL ./tic2json -2 -dr < /dev/ttyS0 | while read line; do echo "$line" | n Will only log `EAST`, `IRMS1`, `URMS1`, `SINSTS`, `SMAXSN` and `UMOY1`, tagged with `PRM` (the meter's ID), at the timestamp provided by the meter. Note: 'socket_listener' expects _exactly_ 1 JSON object per UDP packet (decoding of data is done on a per-packet basis), -hence the need to send each line individually with `nc`. +hence the need to send each line individually with `nc`. Another alternative is to use the script provided in `tools/ticprocess.py`. ## Embedded applications diff --git a/tools/ticprocess.py b/tools/ticprocess.py new file mode 100755 index 0000000..1c139bc --- /dev/null +++ b/tools/ticprocess.py @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +## +## ticprocess.py +## Basic processing of tic2json dictionary output +## +## (C) 2022 Thibaut VARENE +## License: GPLv2 - http://www.gnu.org/licenses/gpl-2.0.html +## + +# Lit la sortie dictionnaire de tic2json, et: +# - envoie la trame JSON brute via UDP, +# - vérifie la puissance apparente soutirée actuelle et publie via MQTT un statut de délestage suivant que la valeur VA_THRESH est dépassée ou non +# Permet par exemple de piloter une demande de délestage via MQTT tout en enregistrant les données avec telegraf +# Exemple d'utilisation: stdbuf -oL tic2json -d | ticprocess.py + +import sys +import socket +import json +import paho.mqtt.publish as publish + + +# Configuration variables +UDP_IP = "grafana" # adresse oÃ1 envoyer le paquet UDP +UDP_PORT = 8094 # port UDP +MQTT_BROKER = "hap-acl" # adresse du broker MQTT +MQTT_TOPIC = "energy/delest" # topic MQTT +MQTT_SKIP = 10 # nombre de trames à ignorer entre chaque publication MQTT +ETIQ_POWER = "SINSTS" # en mono: TICv1: "PAPP", TICv2: "SINSTS" +VA_THRESH = 8900 # valeur limite de la puissance apparente (en VA) + + +def over_vatresh(ticjsonline): + state = None + try: + tic = json.loads(ticjsonline) + s = tic.get(ETIQ_POWER) + v = tic.get("_tvalide") + + if v and s: + if s.get("data")> VA_THRESH: + state = True + else: + state = False + except: + pass + return state + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +skip = 0 + +for ticjsonline in sys.stdin: + sock.sendto(bytes(ticjsonline, "utf-8"), (UDP_IP, UDP_PORT)) + delest = over_vatresh(ticjsonline) + if not skip: + publish.single(MQTT_TOPIC, delest, hostname=MQTT_BROKER) + skip = MQTT_SKIP + skip -= 1 -- 2.39.5

AltStyle によって変換されたページ (->オリジナル) /