1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #!/usr/bin/python3
# -*- coding: utf8 -*-
PIDFILE = "/tmp/extractorFan.pid"
CACHEFILE = "/tmp/extractorFan.cache"
import os
import argparse
import signal
import sys
pid = str(os.getpid())
def quit(signal, frame):
if os.path.isfile(PIDFILE):
os.remove(PIDFILE)
sys.exit(0)
signal.signal(signal.SIGINT, quit)
arguments = argparse.ArgumentParser()
arguments.add_argument("-d", "--debug", action="store_true", help="Debug mode")
arguments.add_argument("-r", "--regulation", nargs=1, help="Regulation: ON|OFF")
arguments.add_argument("-s", "--start", action="store_true", help="Start extractor fan")
arguments.add_argument("-S", "--stop", action="store_true", help="Stop extractor fan")
arguments.add_argument("-t", "--temp", nargs=1, type=int, help="Set temparture")
args = arguments.parse_args()
def debug(message):
if args.debug == True:
sys.stdout.write(message + "\n")
debug(pid)
if args.start:
if os.path.isfile(PIDFILE):
try:
with open(PIDFILE, 'r') as f:
oldpid = int(f.read())
os.kill(oldpid, signal.SIGQUIT)
os.remove(PIDFILE)
except:
msg = "Can't kill pid\n"
arguments.exit(4, message = msg)
debug("Start ventil")
elif args.stop:
if os.path.isfile(PIDFILE):
try:
with open(PIDFILE, 'r') as f:
oldpid = int(f.read())
os.kill(oldpid, signal.SIGQUIT)
os.remove(PIDFILE)
except:
msg = "Can't kill pid\n"
arguments.exit(4, message = msg)
debug("Stop ventil")
elif args.temp:
debug("temp %s" % args.temp)
elif args.regulation:
if args.regulation == ['on']:
if os.path.isfile(PIDFILE):
msg = "Now regulation on\n"
arguments.exit(3, message = msg)
try:
with open(PIDFILE, 'w') as f:
f.write(pid)
except:
msg = "Can't create pid\n"
arguments.exit(5, message = msg)
while True:
debug("Regulate" % args.regulation)
elif args.regulation == ['off']:
if os.path.isfile(PIDFILE):
try:
with open(PIDFILE, 'r') as f:
oldpid = int(f.read())
os.kill(oldpid, signal.SIGQUIT)
os.remove(PIDFILE)
except:
msg = "Can't kill pid\n"
arguments.exit(4, message = msg)
debug("%s" % args.regulation)
else:
arguments.error("For regulation choose ON or OFF\n")
else:
debug(arguments.parse_args(['-h']))
debug("Bye")
# vim: ft=python ts=8 et sw=4 sts=4 |
Partager