|
@@ -5,6 +5,10 @@
|
|
|
# written by Helmut Pozimski <helmut@pozimski.eu> 2013-2014,
|
|
|
# licensed under the 3-clause BSD license
|
|
|
|
|
|
+""" Main script for stdd, puts all the modules together, starts the daemon and
|
|
|
+takes care of sending the right values to the display.
|
|
|
+"""
|
|
|
+
|
|
|
import datetime
|
|
|
import time
|
|
|
import sys
|
|
@@ -16,158 +20,146 @@ from optparse import OptionParser
|
|
|
|
|
|
import stddlib.daemon
|
|
|
import stddlib.configuration
|
|
|
-from adafruit_7segment.Adafruit_7Segment import SevenSegment
|
|
|
-
|
|
|
-
|
|
|
-"""Create the OptionParser object, define and parse all options and
|
|
|
-parameters, only 3 are currently defined:
|
|
|
+from adafruit_7segment.sevensegment import SevenSegment
|
|
|
|
|
|
- * --daemon (shall stdd be run as daemon)
|
|
|
- * --user (user to change to)
|
|
|
- * --group (group to change to)
|
|
|
- * --config (path to configuration file)
|
|
|
|
|
|
-"""
|
|
|
-parser = OptionParser(prog="stdd", version="%prog 0.9.2", add_help_option=True)
|
|
|
-parser.add_option("-d", "--daemon", action="store_true", dest="daemon",
|
|
|
+PARSER = OptionParser(prog="stdd", version="%prog 0.9.2", add_help_option=True)
|
|
|
+PARSER.add_option("-d", "--daemon", action="store_true", dest="daemon",
|
|
|
help="run stdd as daemon")
|
|
|
-parser.add_option("-u", "--user", dest="user", help="define an unprivileged \
|
|
|
+PARSER.add_option("-u", "--user", dest="user", help="define an unprivileged \
|
|
|
user to run the daemon")
|
|
|
-parser.add_option("-g", "--group", dest="group", help="define an unprivileged\
|
|
|
+PARSER.add_option("-g", "--group", dest="group", help="define an unprivileged\
|
|
|
group to run the daemon")
|
|
|
-parser.add_option("-c", "--config", dest="config", help="define an\
|
|
|
+PARSER.add_option("-c", "--config", dest="config", help="define an\
|
|
|
alternative path to the configuration file")
|
|
|
-(options, arguments) = parser.parse_args()
|
|
|
+(OPTIONS, ARGUMENTS) = PARSER.parse_args()
|
|
|
|
|
|
""" define a sighandler to properly catch signals """
|
|
|
|
|
|
|
|
|
def sighandler(signum, frame):
|
|
|
+ """ Catches signals and ensures a clean exit. """
|
|
|
if signum == 2:
|
|
|
- logger.info("received SIGINT, stopping daemon")
|
|
|
+ LOGGER.info("received SIGINT, stopping daemon")
|
|
|
elif signum == 15:
|
|
|
- logger.info("received SIGTERM, stopping daemon")
|
|
|
- display.writeDigit(0, 0)
|
|
|
- display.writeDigit(1, 0)
|
|
|
- display.writeDigit(3, 0)
|
|
|
- display.writeDigit(4, 0)
|
|
|
- if options.daemon is True:
|
|
|
- if daemon.Stop() is True:
|
|
|
+ LOGGER.info("received SIGTERM, stopping daemon")
|
|
|
+ DISPLAY.write_digit(0, 0)
|
|
|
+ DISPLAY.write_digit(1, 0)
|
|
|
+ DISPLAY.write_digit(3, 0)
|
|
|
+ DISPLAY.write_digit(4, 0)
|
|
|
+ if OPTIONS.daemon is True:
|
|
|
+ if DAEMON.stop() is True:
|
|
|
sys.exit(0)
|
|
|
else:
|
|
|
- logger.error("stopping daemon failed, PID file was not deleted!")
|
|
|
+ LOGGER.error("stopping daemon failed, PID file was not deleted!")
|
|
|
sys.exit(1)
|
|
|
sys.exit(0)
|
|
|
|
|
|
-""" create the configuration object according to the given parameters and
|
|
|
-read the file itself
|
|
|
-
|
|
|
-"""
|
|
|
+# create the configuration object according to the given parameters and
|
|
|
+# read the file itself
|
|
|
|
|
|
signal.signal(signal.SIGTERM, sighandler)
|
|
|
signal.signal(signal.SIGINT, sighandler)
|
|
|
|
|
|
-config = stddlib.configuration.Conf()
|
|
|
-if options.config is not None:
|
|
|
- config.Read(options.config)
|
|
|
+CONFIG = stddlib.configuration.Conf()
|
|
|
+if OPTIONS.config is not None:
|
|
|
+ CONFIG.read(OPTIONS.config)
|
|
|
else:
|
|
|
- config.Read()
|
|
|
-config.Analyze()
|
|
|
+ CONFIG.read()
|
|
|
+CONFIG.analyze()
|
|
|
|
|
|
|
|
|
-""" create a logger to log errors according to configuration """
|
|
|
+# create a logger to log errors according to configuration
|
|
|
|
|
|
-logger = logging.getLogger("stdd")
|
|
|
+LOGGER = logging.getLogger("stdd")
|
|
|
|
|
|
-if config.syslog_level == "debug":
|
|
|
- logger.setLevel(logging.DEBUG)
|
|
|
-elif config.syslog_level == "error":
|
|
|
- logger.setLevel(logging.ERROR)
|
|
|
+if CONFIG.syslog_level == "debug":
|
|
|
+ LOGGER.setLevel(logging.DEBUG)
|
|
|
+elif CONFIG.syslog_level == "error":
|
|
|
+ LOGGER.setLevel(logging.ERROR)
|
|
|
else:
|
|
|
- logger.setLevel(logging.INFO)
|
|
|
+ LOGGER.setLevel(logging.INFO)
|
|
|
|
|
|
-if config.syslog_facility == "daemon":
|
|
|
- syslog_handler = logging.handlers.SysLogHandler(
|
|
|
+if CONFIG.syslog_facility == "daemon":
|
|
|
+ SYSLOG_HANDLER = logging.handlers.SysLogHandler(
|
|
|
"/dev/log",
|
|
|
facility=logging.handlers.SysLogHandler.LOG_DAEMON)
|
|
|
else:
|
|
|
- syslog_handler = logging.handlers.SysLogHandler("/dev/log")
|
|
|
+ SYSLOG_HANDLER = logging.handlers.SysLogHandler("/dev/log")
|
|
|
|
|
|
-console_handler = logging.StreamHandler()
|
|
|
+CONSOLE_HANDLER = logging.StreamHandler()
|
|
|
|
|
|
-formatter = logging.Formatter("%(name)s[" + str(os.getpid()) +
|
|
|
+FORMATTER = logging.Formatter("%(name)s[" + str(os.getpid()) +
|
|
|
"]: %(message)s")
|
|
|
|
|
|
-syslog_handler.setFormatter(formatter)
|
|
|
-console_handler.setFormatter(formatter)
|
|
|
+SYSLOG_HANDLER.setFormatter(FORMATTER)
|
|
|
+CONSOLE_HANDLER.setFormatter(FORMATTER)
|
|
|
|
|
|
-if options.daemon is True:
|
|
|
- logger.addHandler(syslog_handler)
|
|
|
+if OPTIONS.daemon is True:
|
|
|
+ LOGGER.addHandler(SYSLOG_HANDLER)
|
|
|
else:
|
|
|
- logger.addHandler(console_handler)
|
|
|
+ LOGGER.addHandler(CONSOLE_HANDLER)
|
|
|
|
|
|
-if options.daemon is True:
|
|
|
+if OPTIONS.daemon is True:
|
|
|
if os.access("/run", os.F_OK & os.W_OK) is True:
|
|
|
- daemon = stddlib.daemon.Daemon("/run/stdd", "stdd.pid")
|
|
|
+ DAEMON = stddlib.daemon.Daemon("/run/stdd", "stdd.pid")
|
|
|
else:
|
|
|
- daemon = stddlib.daemon.Daemon("/var/run/stdd", "stdd.pid")
|
|
|
- daemon.Daemonize()
|
|
|
- daemon.Start()
|
|
|
- logger.info("daemon started")
|
|
|
- cmdline = ""
|
|
|
- cmdcounter = 0
|
|
|
+ DAEMON = stddlib.daemon.Daemon("/var/run/stdd", "stdd.pid")
|
|
|
+ DAEMON.daemonize()
|
|
|
+ DAEMON.start()
|
|
|
+ LOGGER.info("daemon started")
|
|
|
+ CMDLINE = ""
|
|
|
+ CMDCOUNTER = 0
|
|
|
for element in sys.argv:
|
|
|
- if cmdcounter > 0:
|
|
|
- cmdline = cmdline + " " + element
|
|
|
+ if CMDCOUNTER > 0:
|
|
|
+ CMDLINE = CMDLINE + " " + element
|
|
|
else:
|
|
|
- cmdline = cmdline + element
|
|
|
- cmdcounter += 1
|
|
|
- daemon.SetName("stdd", cmdline)
|
|
|
- if options.user is not None and options.group is not None:
|
|
|
- daemon.DropPriv(options.user, options.group)
|
|
|
- logger.debug("dropped privileges, now running as " + options.user +
|
|
|
- " and group" + options.group)
|
|
|
-
|
|
|
-"""Initialize the display object"""
|
|
|
-display = SevenSegment(config.hw_address)
|
|
|
-logger.debug("opened hardware address")
|
|
|
-
|
|
|
-"""Set the brightness according to the configuration"""
|
|
|
-if datetime.datetime.now().time() > config.set_brightness_high and \
|
|
|
- datetime.datetime.now().time() < config.set_brightness_low:
|
|
|
- logger.debug("setting display brightness high")
|
|
|
- display.setBrightness(config.brightness_high)
|
|
|
+ CMDLINE = CMDLINE + element
|
|
|
+ CMDCOUNTER += 1
|
|
|
+ DAEMON.set_name("stdd", CMDLINE)
|
|
|
+ if OPTIONS.user is not None and OPTIONS.group is not None:
|
|
|
+ DAEMON.drop_privileges(OPTIONS.user, OPTIONS.group)
|
|
|
+ LOGGER.debug("dropped privileges, now running as " + OPTIONS.user +
|
|
|
+ " and group" + OPTIONS.group)
|
|
|
+
|
|
|
+# Initialize the display object
|
|
|
+DISPLAY = SevenSegment(CONFIG.hw_address)
|
|
|
+LOGGER.debug("opened hardware address")
|
|
|
+
|
|
|
+# Set the brightness according to the configuration
|
|
|
+if CONFIG.set_brightness_high < datetime.datetime.now().time()\
|
|
|
+ < CONFIG.set_brightness_low:
|
|
|
+ LOGGER.debug("setting display brightness high")
|
|
|
+ DISPLAY.set_brightness(CONFIG.brightness_high)
|
|
|
else:
|
|
|
- logger.debug("setting display brightness low")
|
|
|
- display.setBrightness(config.brightness_low)
|
|
|
-
|
|
|
-"""Define the main loop"""
|
|
|
+ LOGGER.debug("setting display brightness low")
|
|
|
+ DISPLAY.set_brightness(CONFIG.brightness_low)
|
|
|
|
|
|
+# Define the main loop
|
|
|
|
|
|
def main():
|
|
|
+ """ Main loop of the daemon. """
|
|
|
minute_written = 61
|
|
|
while True:
|
|
|
date_now = datetime.datetime.now()
|
|
|
- logger.debug("got datetime: " + str(date_now))
|
|
|
+ LOGGER.debug("got datetime: " + str(date_now))
|
|
|
minute = date_now.minute
|
|
|
hour = date_now.hour
|
|
|
- if config.blink_colon is True:
|
|
|
- logger.debug("blinking middle colon")
|
|
|
- display.setColon(date_now.second % 2)
|
|
|
+ if CONFIG.blink_colon is True:
|
|
|
+ LOGGER.debug("blinking middle colon")
|
|
|
+ DISPLAY.set_colon(date_now.second % 2)
|
|
|
else:
|
|
|
- display.setColon(True)
|
|
|
- """set the display brightness high or low when the point in time
|
|
|
- defined is reached.
|
|
|
-
|
|
|
- """
|
|
|
- if config.set_brightness_low.hour == hour:
|
|
|
- if config.set_brightness_low.minute == minute:
|
|
|
- logger.debug("setting display brightness low")
|
|
|
- display.setBrightness(config.brightness_low)
|
|
|
- if config.set_brightness_high.hour == hour:
|
|
|
- if config.set_brightness_high.minute == minute:
|
|
|
- logger.debug("setting display brightness high")
|
|
|
- display.setBrightness(config.brightness_high)
|
|
|
+ DISPLAY.set_colon(True)
|
|
|
+ # set the display brightness high or low when the point in time
|
|
|
+ # defined is reached.
|
|
|
+ if CONFIG.set_brightness_low.hour == hour:
|
|
|
+ if CONFIG.set_brightness_low.minute == minute:
|
|
|
+ LOGGER.debug("setting display brightness low")
|
|
|
+ DISPLAY.set_brightness(CONFIG.brightness_low)
|
|
|
+ if CONFIG.set_brightness_high.hour == hour:
|
|
|
+ if CONFIG.set_brightness_high.minute == minute:
|
|
|
+ LOGGER.debug("setting display brightness high")
|
|
|
+ DISPLAY.set_brightness(CONFIG.brightness_high)
|
|
|
if minute_written != minute:
|
|
|
try:
|
|
|
position2 = str(hour)[1]
|
|
@@ -184,11 +176,11 @@ def main():
|
|
|
position3 = "0"
|
|
|
else:
|
|
|
position3 = str(minute)[0]
|
|
|
- logger.debug("writing time to display")
|
|
|
- display.writeDigit(0, int(position1))
|
|
|
- display.writeDigit(1, int(position2))
|
|
|
- display.writeDigit(3, int(position3))
|
|
|
- display.writeDigit(4, int(position4))
|
|
|
+ LOGGER.debug("writing time to display")
|
|
|
+ DISPLAY.write_digit(0, int(position1))
|
|
|
+ DISPLAY.write_digit(1, int(position2))
|
|
|
+ DISPLAY.write_digit(3, int(position3))
|
|
|
+ DISPLAY.write_digit(4, int(position4))
|
|
|
minute_written = minute
|
|
|
time.sleep(1)
|
|
|
|