better logging and black
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
3ba9e6fab4
commit
157371ff59
54
rhf1s001.py
54
rhf1s001.py
|
@ -16,71 +16,82 @@ DST_INFLUX_USER = os.getenv("DST_INFLUX_USER")
|
||||||
DST_INFLUX_PASS = os.getenv("DST_INFLUX_PASS")
|
DST_INFLUX_PASS = os.getenv("DST_INFLUX_PASS")
|
||||||
DST_INFLUX_DB = os.getenv("DST_INFLUX_DB")
|
DST_INFLUX_DB = os.getenv("DST_INFLUX_DB")
|
||||||
|
|
||||||
|
|
||||||
def on_connect_ttn(client, userdata, flags, rc):
|
def on_connect_ttn(client, userdata, flags, rc):
|
||||||
logging.info("connected to ttn %s - %s", SRC_MQTT_HOST, str(rc))
|
logging.info("connected to ttn %s - %s", SRC_MQTT_HOST, str(rc))
|
||||||
client.subscribe("v3/+/devices/+/up")
|
client.subscribe("v3/+/devices/+/up")
|
||||||
|
|
||||||
|
|
||||||
def on_log(client, userdata, level, buf):
|
def on_log(client, userdata, level, buf):
|
||||||
logging_level = mqtt.LOGGING_LEVEL[level]
|
logging_level = mqtt.LOGGING_LEVEL[level]
|
||||||
logging.log(logging_level, buf)
|
logging.log(logging_level, buf)
|
||||||
logging.info("got a log message level %s: %s", level, str(buf))
|
logging.info("got a log message level %s: %s", level, str(buf))
|
||||||
|
|
||||||
|
|
||||||
# The callback for when a PUBLISH message is received from the server.
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
def on_message_ttn(client, userdata, msg):
|
def on_message_ttn(client, userdata, msg):
|
||||||
data = json.loads(msg.payload)
|
data = json.loads(msg.payload)
|
||||||
logging.info("new data received via gw %s", data["uplink_message"]["rx_metadata"][0]["gateway_ids"]["gateway_id"])
|
logging.info(
|
||||||
|
"new data received via gw %s",
|
||||||
|
data["uplink_message"]["rx_metadata"][0]["gateway_ids"]["gateway_id"],
|
||||||
|
)
|
||||||
|
|
||||||
# write to influxdb
|
# write to influxdb
|
||||||
logging.info("writing data to influxdb")
|
batt = data["uplink_message"]["decoded_payload"]["battery"]
|
||||||
influxdb.write_points(
|
hum = data["uplink_message"]["decoded_payload"]["hum"]
|
||||||
[
|
temp = data["uplink_message"]["decoded_payload"]["temp"]
|
||||||
|
logging.info(f"writing data to influxdb: batt={batt}, hum={hum}, temp={temp}")
|
||||||
|
point = [
|
||||||
{
|
{
|
||||||
"measurement": "risinghf",
|
"measurement": "risinghf",
|
||||||
"tags": {
|
"tags": {
|
||||||
"device": "rhf1s001",
|
"device": "rhf1s001",
|
||||||
},
|
},
|
||||||
"fields": {
|
"fields": {
|
||||||
"battery": data["uplink_message"]["decoded_payload"]["battery"],
|
"battery": batt,
|
||||||
"hum": data["uplink_message"]["decoded_payload"]["hum"],
|
"hum": hum,
|
||||||
"temp": data["uplink_message"]["decoded_payload"]["temp"],
|
"temp": temp,
|
||||||
"counter": data["uplink_message"]["f_cnt"],
|
"counter": data["uplink_message"]["f_cnt"],
|
||||||
"rssi": data["uplink_message"]["rx_metadata"][0]["rssi"],
|
"rssi": data["uplink_message"]["rx_metadata"][0]["rssi"],
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
)
|
influxdb.write_points(point)
|
||||||
|
|
||||||
logging.info("data processing done")
|
logging.info("data processing done")
|
||||||
|
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
logging.info("disconnecting from mqtt")
|
logging.info("disconnecting from mqtt")
|
||||||
client_ttn.disconnect()
|
client_ttn.disconnect()
|
||||||
client_ttn.loop_stop()
|
client_ttn.loop_stop()
|
||||||
|
|
||||||
|
|
||||||
def handleSIGTERM(signalNumber, frame):
|
def handleSIGTERM(signalNumber, frame):
|
||||||
logging.info("got SIGTERM")
|
logging.info("got SIGTERM")
|
||||||
shutdown()
|
shutdown()
|
||||||
return
|
return
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
signal.signal(signal.SIGTERM, handleSIGTERM)
|
signal.signal(signal.SIGTERM, handleSIGTERM)
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format='%(asctime)s - %(message)s',
|
format="%(asctime)s - %(message)s",
|
||||||
datefmt='%Y-%m-%d %H:%M:%S %Z'
|
datefmt="%Y-%m-%d %H:%M:%S %Z",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prepare InfluxDB
|
# Prepare InfluxDB
|
||||||
influxdb = InfluxDBClient(
|
influxdb = InfluxDBClient(
|
||||||
host=DST_INFLUX_HOST,
|
host=DST_INFLUX_HOST,
|
||||||
port=443,
|
port=443,
|
||||||
database=DST_INFLUX_DB,
|
database=DST_INFLUX_DB,
|
||||||
username=DST_INFLUX_USER,
|
username=DST_INFLUX_USER,
|
||||||
password=DST_INFLUX_PASS,
|
password=DST_INFLUX_PASS,
|
||||||
ssl=True,
|
ssl=True,
|
||||||
verify_ssl=True,
|
verify_ssl=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prepare MQTT
|
# Prepare MQTT
|
||||||
|
@ -89,7 +100,7 @@ if __name__ == '__main__':
|
||||||
client_ttn.on_connect = on_connect_ttn
|
client_ttn.on_connect = on_connect_ttn
|
||||||
client_ttn.on_message = on_message_ttn
|
client_ttn.on_message = on_message_ttn
|
||||||
client_ttn.on_log = on_log
|
client_ttn.on_log = on_log
|
||||||
client_ttn.username_pw_set(SRC_MQTT_USER,SRC_MQTT_PASS)
|
client_ttn.username_pw_set(SRC_MQTT_USER, SRC_MQTT_PASS)
|
||||||
client_ttn.tls_set()
|
client_ttn.tls_set()
|
||||||
client_ttn.connect(SRC_MQTT_HOST, 8883, 60)
|
client_ttn.connect(SRC_MQTT_HOST, 8883, 60)
|
||||||
|
|
||||||
|
@ -100,4 +111,3 @@ if __name__ == '__main__':
|
||||||
client_ttn.disconnect()
|
client_ttn.disconnect()
|
||||||
client_ttn.loop_stop()
|
client_ttn.loop_stop()
|
||||||
logging.info("tschuess")
|
logging.info("tschuess")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue