From f110264fc7fc2510dcedb70081bc4ce9cf0b9605 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Wed, 22 Aug 2018 19:03:34 +0200 Subject: [PATCH] backup current state --- schalter.py | 58 +++++++++++++++++----------------------- sopel-bot/default.cfg | 11 ++++++++ sopel-bot/defaults.cfg | 2 -- spaceapi.json | 10 +++---- systemd/ircbot.service | 11 ++++++++ systemd/schalter.service | 13 +++++++++ www/index.html | 22 ++++++++------- 7 files changed, 78 insertions(+), 49 deletions(-) create mode 100644 sopel-bot/default.cfg delete mode 100644 sopel-bot/defaults.cfg diff --git a/schalter.py b/schalter.py index dca9137..c0c529a 100755 --- a/schalter.py +++ b/schalter.py @@ -1,49 +1,43 @@ #!/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 onec +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 -import systemd.journal +# for logging +import logging +from systemd import journal ######################################################## -#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 ") +#start init +journal.write("CCCHB space api switch v. 0.3 ") +currentState = bool(0) ################################################################################# -#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)) - icons= data.get("state").get('icon') - if (pos!= wert_des_schalters): - log.info("state changed to: ", wert_des_schalters) + if (pos != wert_des_schalters): + journal.write("state changed to: %s " %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) - return wert_des_schalters ################################################################################# @@ -51,8 +45,8 @@ 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=200) -log.info("init complete... starting json foo") +GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600) +journal.write("init complete... starting json foo") #read json file with open('spaceapi.json','r') as infile: data = json.load(infile) @@ -61,13 +55,11 @@ 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) -currentValue = bool(0) -try: +try: while True: - time.sleep(1) - currentValue = switched(currentValue) - - #include IRCBOT status here? - #do other stuff otherwise just don't do anything?! + time.sleep(5) + switched(currentState) + #currentValue = #include IRCBOT status here? + #do other stuff otherwise just don't do anything?! except KeyboardInterrupt: GPIO.cleanup() diff --git a/sopel-bot/default.cfg b/sopel-bot/default.cfg new file mode 100644 index 0000000..f8e2f3f --- /dev/null +++ b/sopel-bot/default.cfg @@ -0,0 +1,11 @@ +[core] +nick = ccchb-schalter +host = irc.hackint.org +use_ssl = true +verify_ssl = false +port = 6697 +owner = ccchb-schalter +channels = #ccchb +extra = /home/pi/Eingangsschalter/ircbot/ +enable = spaceapi + diff --git a/sopel-bot/defaults.cfg b/sopel-bot/defaults.cfg deleted file mode 100644 index 7d5fd13..0000000 --- a/sopel-bot/defaults.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[core] -verify_ssl = false diff --git a/spaceapi.json b/spaceapi.json index d443641..650e90a 100644 --- a/spaceapi.json +++ b/spaceapi.json @@ -9,12 +9,12 @@ "logo": "https://ccchb.de/logo/CCCHB-logo_256x256_bw.png", "space": "CCCHB", "state": { + "lastchange": 1518643002.5509002, + "open": false, "icon": { - "closed": "https://ccchb.de/w/images/9/9e/SpaceClosed.svg", - "open": "https://ccchb.de/w/images/9/95/SpaceOpen.svg" - }, - "lastchange": 1534869423.371439, - "open": false + "open": "https://files.ccchb.de/assets/SpaceOpen.svg", + "closed": "https://files.ccchb.de/assets/SpaceClosed.svg" + } }, "url": "https://ccchb.de/" } diff --git a/systemd/ircbot.service b/systemd/ircbot.service index e69de29..68c3e15 100644 --- a/systemd/ircbot.service +++ b/systemd/ircbot.service @@ -0,0 +1,11 @@ +[Unit] +Description=Schalter IRC Bot + +[Service] +ExecStart=/usr/bin/sopel +User=pi +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target diff --git a/systemd/schalter.service b/systemd/schalter.service index e69de29..1c95058 100644 --- a/systemd/schalter.service +++ b/systemd/schalter.service @@ -0,0 +1,13 @@ +[Unit] +Description=Eingangsschalter + +[Service] +ExecStart=/home/pi/Eingangsschalter/schalter.py +WorkingDirectory=/home/pi/Eingangsschalter +User=pi +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target + diff --git a/www/index.html b/www/index.html index 7787256..c235f96 100644 --- a/www/index.html +++ b/www/index.html @@ -1,6 +1,7 @@ + - - - CCCHB is open - + +
- +
- + + +