Initial commit
This commit is contained in:
commit
3204bf9a3f
10
relay_rpc.service
Normal file
10
relay_rpc.service
Normal file
@ -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
|
58
relay_rpc/main.py
Normal file
58
relay_rpc/main.py
Normal file
@ -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())
|
43
setyp.py
Normal file
43
setyp.py
Normal file
@ -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})
|
Loading…
Reference in New Issue
Block a user