Improved logging | TODO: debug

This commit is contained in:
Torsten Kurbad 2017-09-07 17:11:56 +02:00
parent 48de4faf90
commit 75c8a13170

View File

@ -1,13 +1,17 @@
#!/usr/bin/python3
#
# Depends on pyyaml!
# Depends on:
# - python-systemd
# - pyyaml
import logging
import shlex
import subprocess
import sys
import yaml
from serial import Serial
from systemd.journal import JournalHandler
from threading import Timer
from time import sleep
@ -189,24 +193,35 @@ class ConvertUPS(Serial):
if __name__ == "__main__":
log = logging.getLogger('pyupsmon')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
try:
ups = ConvertUPS()
except:
log.error('Error: Could not open UPS serial connection.')
log.error('\tCheck the config file!')
print('Error: Could not open UPS serial connection.')
print('\tCheck the config file!')
sys.exit(1)
if not ups.upsInit():
log.error('PyUPSMon successfully started.')
print('Error: Could not initialize UPS.')
sys.exit(1)
log.info('PyUPSMon successfully started.')
while True:
(toggled, onBatt) = ups.powerToggled()
if toggled:
if onBatt:
log.critical('UPS AC power loss. Starting shutdown timer.')
print('UPS AC power loss. Starting shutdown timer.')
ups.startShutdownTimer()
if not onBatt:
log.critical('UPS AC power restored. Cancelling shutdown timer.')
print('UPS AC power restored. Cancelling shutdown timer.')
ups.cancelShutdownTimer()
sleep(ups.interval)