schalter.py update from backup

This commit is contained in:
Martin/Geno 2018-08-23 15:20:49 +02:00
parent 3c123c1fcd
commit ec05aa9b0d
No known key found for this signature in database
GPG key ID: 9D7D3C6BFF600C6A

View file

@ -15,19 +15,13 @@ import json
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
# for logging # for logging
import logging import logging
import systemd.journal from systemd import journal
######################################################## ########################################################
#start init #start init
log = logging.getLogger("SpaceAPI Schalter") #create a logger journal.write("CCCHB space api switch v. 0.3")
log_fmt = logging.Formatter("%(levelname)s %(message)s") #define logging format currentState = False
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:
@ -35,15 +29,16 @@ log.info("CCCHB space api switch v. 0.3 ")
# will read the GPIO header of the pi and write the current state in the www spaceapi json file # will read the GPIO header of the pi and write the current state in the www spaceapi json file
def switched(pos): def switched(pos):
global currentState
wert_des_schalters = not bool(GPIO.input(pin_number)) wert_des_schalters = not bool(GPIO.input(pin_number))
icons = data.get("state").get('icon')
if pos != wert_des_schalters: if pos != wert_des_schalters:
log.info("state changed to: ", wert_des_schalters) journal.write("state changed to: %s " %wert_des_schalters)
chn_time = datetime.datetime.now().isoformat() chn_time = datetime.datetime.now().isoformat()
icons = data.get("state").get('icon')
data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}}) 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: 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
################################################################################# #################################################################################
@ -51,8 +46,8 @@ def switched(pos):
pin_number = 18; #set GPIO Pin number pin_number = 18; #set GPIO Pin number
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200) GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600)
log.info("init complete... starting json foo") journal.write("init complete... starting json foo")
#read json file #read json file
with open('spaceapi.json','r') as infile: with open('spaceapi.json','r') as infile:
data = json.load(infile) data = json.load(infile)
@ -60,13 +55,10 @@ with open('spaceapi.json','r') as infile:
#run switched once to get current switch status and change if needed. #run switched once to get current switch status and change if needed.
#switched(wert_des_schalters) #switched(wert_des_schalters)
currentValue = False
try: try:
while True: while True:
time.sleep(1) time.sleep(5)
currentValue = switched(currentValue) switched(currentState)
#include IRCBOT status here?
#do other stuff otherwise just don't do anything?! #do other stuff otherwise just don't do anything?!
except KeyboardInterrupt: except KeyboardInterrupt:
GPIO.cleanup() GPIO.cleanup()