commit 3204bf9a3f0148c1c4d12962e9e84774181de5cf Author: Thomas Klaehn Date: Sat May 21 14:20:54 2022 +0200 Initial commit diff --git a/relay_rpc.service b/relay_rpc.service new file mode 100644 index 0000000..4009365 --- /dev/null +++ b/relay_rpc.service @@ -0,0 +1,10 @@ +[Unit] +Description=Relay rpc service +After=multi-user.target + +[Service] +Type=idle +ExecStart=/usr/local/bin/relay_rpc + +[Install] +WantedBy=multi-user.target diff --git a/relay_rpc/main.py b/relay_rpc/main.py new file mode 100644 index 0000000..688f4fa --- /dev/null +++ b/relay_rpc/main.py @@ -0,0 +1,58 @@ +"""Entry point""" +#!/usr/bin/env python + +import logging +import sys + +from xmlrpc.server import SimpleXMLRPCServer + +import RPi.GPIO as GPIO + +LOG_LEVEL = logging.ERROR +LOG_FILE = "/var/log/relay_rpc.log" +LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" + +HOST = "0.0.0.0" +PORT = 64001 + +GPIOS = [26, 20, 21] + + +def switch_relay(relay: int, state: bool): + """Switch relay""" + pin = int(GPIOS[relay - 1]) + log = logging.getLogger() + log.info("switching relay %s (pin %s) %s", relay, pin, state) + GPIO.output(pin, not state) + + +def init_relays(): + """Init relays""" + GPIO.setwarnings(False) + GPIO.setmode(GPIO.BCM) + for pin in GPIOS: + GPIO.setup(pin, GPIO.OUT) + GPIO.output(pin, True) + + +def main(): + """Entry point""" + logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL, filename=LOG_FILE) + # logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL) + + log = logging.getLogger() + + init_relays() + + server = SimpleXMLRPCServer((HOST, PORT), allow_none=True) + + server.register_function(switch_relay, 'switch_relay') + + log.info('Control-c to quit') + server.serve_forever() + log.info("Shutting down...") + log.info("...done. Exiting...") + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setyp.py b/setyp.py new file mode 100644 index 0000000..5671aee --- /dev/null +++ b/setyp.py @@ -0,0 +1,43 @@ + +import os +import shutil +import stat +from setuptools import setup +from setuptools.command.install import install + +NAME = 'relay_rpc' +VERSION = '1' +AUTHOR = 'Thomas Klaehn' +EMAIL = 'tkl@blackfinn.de' +PACKAGES = [NAME] +REQUIRES = ['RPi.GPIO'] + +SERVICEDIR = "/lib/systemd/system" +DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, NAME +".service") + +LOGFILE = "/var/log/" + NAME + ".log" + +ENTRY_POINTS = { + 'console_scripts': [ + 'relay_rpc = relay_rpc.main:main' + ] +} + +class Install(install): + def run(self): + install.run(self) + os.makedirs(SERVICEDIR, exist_ok=True) + shutil.copyfile(NAME + '.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT)) + os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) + + try: + open(LOGFILE, 'r') + except FileNotFoundError: + os.makedirs(os.path.dirname(LOGFILE), exist_ok=True) + open(LOGFILE, 'x') + os.chmod(LOGFILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH) + + +setup(name=NAME, version=VERSION, long_description=__doc__, author=AUTHOR, author_email=EMAIL, + packages=PACKAGES, zip_safe=False, install_requires=REQUIRES, entry_points=ENTRY_POINTS, + cmdclass={'install': Install})