updated routermon e upsmon, ora funzionano
This commit is contained in:
@@ -7,17 +7,23 @@ import logging
|
||||
import signal
|
||||
import json
|
||||
|
||||
from influxdb_client.client.write.point import Point
|
||||
from influxdb_client.client.influxdb_client import InfluxDBClient
|
||||
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS
|
||||
|
||||
# Get environment variables
|
||||
env = dict(os.environ)
|
||||
LOGGER: logging.Logger
|
||||
|
||||
class Runner:
|
||||
running: bool = True
|
||||
def __init__(self):
|
||||
signal.signal(signal.SIGINT, self.stop)
|
||||
signal.signal(signal.SIGTERM, self.stop)
|
||||
class SignalHandler:
|
||||
running: bool
|
||||
|
||||
def stop(self, _):
|
||||
def __init__(self):
|
||||
self.running: bool = True
|
||||
signal.signal(signal.SIGINT, self._handle_sigint)
|
||||
signal.signal(signal.SIGTERM, self._handle_sigint)
|
||||
|
||||
def _handle_sigint(self, signum, frame):
|
||||
self.running = False
|
||||
|
||||
def send(port: serial.Serial, d: str):
|
||||
@@ -47,9 +53,14 @@ def bruteforceCommands(port: serial.Serial):
|
||||
|
||||
def main():
|
||||
INTERVAL = int(env['INTERVAL'])
|
||||
run = Runner.running
|
||||
LOGGER.debug(json.dumps(env, indent=2))
|
||||
run: SignalHandler = SignalHandler()
|
||||
port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1)
|
||||
while run:
|
||||
write_client = InfluxDBClient(url=env['INFLUXDB_URL'],
|
||||
token=env['INFLUXDB_TOKEN'],
|
||||
org=env['INFLUXDB_ORG'])
|
||||
write_api = write_client.write_api(write_options=ASYNCHRONOUS)
|
||||
while run.running:
|
||||
try:
|
||||
send(port, "Q1")
|
||||
data = receive(port, "Q1").lstrip('(').split()
|
||||
@@ -66,9 +77,13 @@ def main():
|
||||
'onLine': True if str(data[7]).endswith('1') else False,
|
||||
}
|
||||
LOGGER.debug(f"UPS Status: \n{json.dumps(values, indent=2)}")
|
||||
if values['onBatt']:
|
||||
LOGGER.info(f"OnBattery\n{json.dumps(values,indent=2)}")
|
||||
p = Point('ups')
|
||||
for k,v in values.items():
|
||||
p.field(k,v)
|
||||
write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'], record=p)
|
||||
time.sleep(INTERVAL)
|
||||
except KeyboardInterrupt:
|
||||
run = False
|
||||
except Exception as e:
|
||||
print(f"Unexpected exception: [{e}]")
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user