From 5d1dbfba31d3d83311de9c55ea0462086829d905 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Wed, 22 Aug 2018 22:39:18 +0200 Subject: [PATCH] fixes for pyropeter --- install.sh | 10 ++++-- schalter.py | 61 +++++++++++++++++++---------------- sopel-bot/modules/spaceapi.py | 22 +++++++------ systemd/ircbot.service | 5 +-- systemd/schalter.service | 4 +-- 5 files changed, 59 insertions(+), 43 deletions(-) mode change 100644 => 100755 install.sh diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 index 48bb5b4..2a21b07 --- a/install.sh +++ b/install.sh @@ -1,4 +1,4 @@ -#!/usr/bin/bash +#!/bin/sh INSTALL_PATH=$PWD # install dependencies @@ -12,10 +12,14 @@ chown pi:pi /var/www/html/spaceapi.json cp $INSTALL_PATH/www/index.html /var/www/html/index.html # install ircbot -ln -s $INSTALL_PATH/sopel-bot /home/pi/.sopel +ln -s $INSTALL_PATH/sopel-bot/modules /home/pi/.sopel +if [ ! -f /home/pi/.sopel/default.cfg ]; then + cp $INSTALL_PATH/sopel-bot/default.cfg /home/pi/.sopel/ +fi #install services -cp $INSTALL_PATH/systemd/{ircbot,schalter}.service /etc/systemd/system +cp $INSTALL_PATH/systemd/schalter.service /etc/systemd/system +cp $INSTALL_PATH/systemd/ircbot.service /etc/systemd/system systemctl daemon-reload systemctl enable ircbot schalter systemctl start ircbot schalter diff --git a/schalter.py b/schalter.py index c0c529a..0a1bbf6 100755 --- a/schalter.py +++ b/schalter.py @@ -1,43 +1,49 @@ #!/usr/bin/env python3 ######################################################################### -# Spaceapi Implementation for a 63A switch -# (using 5 V 150 mA Raspberry Pi GPIO pin) +# Spaceapi Implementation for a 63A switch +# (using 5 V 150 mA Raspberry Pi GPIO pin) +# # ######################################################################### -# to make sure that everything does not happen at onec -import time -import datetime +# to make sure that everything does not happen at once +import time +import datetime # json and raspi imports import json -# for using GPIO pins of the raspi +# for using GPIO pins of the raspi import RPi.GPIO as GPIO -# for logging -import logging -from systemd import journal +# for logging +import logging +import systemd.journal ######################################################## -#start init -journal.write("CCCHB space api switch v. 0.3 ") -currentState = bool(0) +#start init +log = logging.getLogger("SpaceAPI Schalter") #create a logger +log_fmt = logging.Formatter("%(levelname)s %(message)s") #define logging format +log_ch = JournalHandler() # create logging Handler +log_ch.setFormatter(log_fmt) +log.addHandler(log_ch) + +log.setLevel(logging.INFO) +log.info("CCCHB space api switch v. 0.3 ") ################################################################################# -#function definitons: +#function definitons: ################################################################################# # will read the GPIO header of the pi and write the current state in the www spaceapi json file def switched(pos): - global currentState wert_des_schalters = not bool(GPIO.input(pin_number)) - if (pos != wert_des_schalters): - journal.write("state changed to: %s " %wert_des_schalters) + icons = data.get("state").get('icon') + if pos != wert_des_schalters: + log.info("state changed to: ", wert_des_schalters) chn_time = datetime.datetime.now().isoformat() - icons= data.get("state").get('icon') data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}}) - currentState= wert_des_schalters with open('/var/www/html/spaceapi.json', 'w') as outfile: - json.dump(data, outfile,sort_keys=True) + json.dump(data, outfile, sort_keys=True) + return wert_des_schalters ################################################################################# @@ -45,21 +51,22 @@ def switched(pos): pin_number = 18; #set GPIO Pin number GPIO.setmode(GPIO.BCM) GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600) -journal.write("init complete... starting json foo") +GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200) +log.info("init complete... starting json foo") #read json file with open('spaceapi.json','r') as infile: data = json.load(infile) -json.JSONDecoder(data) #decode json to python (may not be neccecary.) #run switched once to get current switch status and change if needed. #switched(wert_des_schalters) -try: +currentValue = False +try: while True: - time.sleep(5) - switched(currentState) - #currentValue = #include IRCBOT status here? - #do other stuff otherwise just don't do anything?! + time.sleep(1) + currentValue = switched(currentValue) + + #include IRCBOT status here? + #do other stuff otherwise just don't do anything?! except KeyboardInterrupt: GPIO.cleanup() diff --git a/sopel-bot/modules/spaceapi.py b/sopel-bot/modules/spaceapi.py index 0c8844a..1b6fb18 100644 --- a/sopel-bot/modules/spaceapi.py +++ b/sopel-bot/modules/spaceapi.py @@ -3,7 +3,6 @@ import json import sopel.module SPACEAPI = "/var/www/html/spaceapi.json" -CHANNEL = "#ccchb" PLACE = "ccchb" cache = { @@ -19,20 +18,26 @@ def status_msg(status): status_text = "closed" if status: status_text = "open" - return PLACE + ' is ' + status_text + return '{} is {}'.format(PLACE, status_text) def change_status(bot, status): status_text = "closed" if status: status_text = "open" - bot.say(PLACE +' changed to '+ status_text, CHANNEL) + for ch in bot.channels: + bot.notice('{} changed to {}'.format(PLACE, status_text), ch) topic = status_msg(status) - channel = bot.channels[CHANNEL] - if channel.topic != None: - topic = channel.topic.replace(status_msg(not status), topic) - bot.write(('TOPIC', CHANNEL + ' :' + topic)) + for ch in bot.channels: + channel = bot.channels[ch] + topic_new = topic + topic_cur = status_msg(not status) + if channel.topic != None: + topic_new = channel.topic.replace(topic_cur, topic_new) + topic_cur = channel.topic + if topic_new != topic_cur: + bot.write(('TOPIC', ch), topic_new) def check_status(bot, human=False): data = get_spaceapi() @@ -40,8 +45,7 @@ def check_status(bot, human=False): if status != cache["open"]: cache["open"] = status - if CHANNEL in bot.channels: - change_status(bot, status) + change_status(bot, status) if human: bot.reply(status_msg(status)) diff --git a/systemd/ircbot.service b/systemd/ircbot.service index 68c3e15..2b4dab7 100644 --- a/systemd/ircbot.service +++ b/systemd/ircbot.service @@ -1,11 +1,12 @@ [Unit] Description=Schalter IRC Bot -[Service] +[Service] ExecStart=/usr/bin/sopel User=pi +Group=pi Restart=always -RestartSec=3 +RestartSec=300 [Install] WantedBy=multi-user.target diff --git a/systemd/schalter.service b/systemd/schalter.service index 1c95058..f403848 100644 --- a/systemd/schalter.service +++ b/systemd/schalter.service @@ -5,9 +5,9 @@ Description=Eingangsschalter ExecStart=/home/pi/Eingangsschalter/schalter.py WorkingDirectory=/home/pi/Eingangsschalter User=pi +Group=pi Restart=always -RestartSec=3 +RestartSec=30 [Install] WantedBy=multi-user.target -