Merge pull request #40 from DeanCording/main

Starlink-MQTT Systemd support
This commit is contained in:
sparky8512 2022-03-02 14:14:04 -08:00 committed by GitHub
commit b61f5a4789
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 3 deletions

View file

@ -15,9 +15,12 @@ Where *id_value* is the *id* value from the dish status information.
""" """
import logging import logging
import os
import signal
import sys import sys
import time import time
import json import json
import math
try: try:
@ -32,6 +35,14 @@ import dish_common
HOST_DEFAULT = "localhost" HOST_DEFAULT = "localhost"
class Terminated(Exception):
pass
def handle_sigterm(signum, frame):
# Turn SIGTERM into an exception so main loop can clean up
raise Terminated
def parse_args(): def parse_args():
parser = dish_common.create_arg_parser(output_description="publish it to a MQTT broker", parser = dish_common.create_arg_parser(output_description="publish it to a MQTT broker",
@ -72,6 +83,29 @@ def parse_args():
else: else:
parser.epilog += "\nSSL support options not available due to missing ssl module" parser.epilog += "\nSSL support options not available due to missing ssl module"
env_map = (
("MQTT_HOST", "hostname"),
("MQTT_PORT", "port"),
("MQTT_USERNAME", "username"),
("MQTT_PASSWORD", "password"),
("MQTT_SSL", "tls"),
)
env_defaults = {}
for var, opt in env_map:
# check both set and not empty string
val = os.environ.get(var)
if val:
if var == "MQTT_SSL":
if val == "insecure":
env_defaults[opt] = False
elif val == "secure":
env_defaults[opt] = True
else:
env_defaults["ssl_ca_cert"] = val
else:
env_defaults[opt] = val
parser.set_defaults(**env_defaults)
opts = dish_common.run_arg_parser(parser, need_id=True) opts = dish_common.run_arg_parser(parser, need_id=True)
if opts.username is None and opts.password is not None: if opts.username is None and opts.password is not None:
@ -102,7 +136,9 @@ def loop_body(opts, gstate):
if not "dish_{0}".format(category) in data: if not "dish_{0}".format(category) in data:
data["dish_{0}".format(category)] = {} data["dish_{0}".format(category)] = {}
data["dish_{0}".format(category)].update({key: val}) # Skip NaN values that occur on startup because they can upset Javascript JSON parsers
if not ((type(val) == float) and math.isnan(val)):
data["dish_{0}".format(category)].update({key: val})
def cb_add_sequence(key, val, category, _): def cb_add_sequence(key, val, category, _):
@ -145,6 +181,8 @@ def main():
gstate = dish_common.GlobalState(target=opts.target) gstate = dish_common.GlobalState(target=opts.target)
signal.signal(signal.SIGTERM, handle_sigterm)
try: try:
next_loop = time.monotonic() next_loop = time.monotonic()
while True: while True:
@ -155,10 +193,11 @@ def main():
time.sleep(next_loop - now) time.sleep(next_loop - now)
else: else:
break break
except Terminated:
pass
finally: finally:
gstate.shutdown() gstate.shutdown()
sys.exit(rc)
sys.exit(rc)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -0,0 +1,12 @@
[Unit]
Description=Starlink GRPC to MQTT exporter
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/starlink-grpc-tools/
Environment=MQTT_HOST=localhost MQTT_PORT=1883 MQTT_USERNAME=<changeme> MQTT_PASSWORD=<changeme> MQTT_SSL=false
ExecStart=/opt/starlink-grpc-tools/venv/bin/python3 dish_grpc_mqtt.py -t 10 status alert_detail
[Install]
WantedBy=multi-user.target