2018-08-21 18:57:27 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#########################################################################
|
2018-08-22 15:39:18 -05:00
|
|
|
# Spaceapi Implementation for a 63A switch
|
|
|
|
# (using 5 V 150 mA Raspberry Pi GPIO pin)
|
|
|
|
#
|
2018-08-21 18:57:27 -05:00
|
|
|
#
|
|
|
|
#########################################################################
|
|
|
|
|
2018-08-22 15:39:18 -05:00
|
|
|
# to make sure that everything does not happen at once
|
|
|
|
import time
|
|
|
|
import datetime
|
2018-08-21 18:57:27 -05:00
|
|
|
# json and raspi imports
|
|
|
|
import json
|
2018-08-22 15:39:18 -05:00
|
|
|
# for using GPIO pins of the raspi
|
2018-08-21 18:57:27 -05:00
|
|
|
import RPi.GPIO as GPIO
|
2018-08-22 15:39:18 -05:00
|
|
|
# for logging
|
|
|
|
import logging
|
|
|
|
import systemd.journal
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
########################################################
|
|
|
|
|
2018-08-22 15:39:18 -05:00
|
|
|
#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 ")
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
#################################################################################
|
2018-08-22 15:39:18 -05:00
|
|
|
#function definitons:
|
2018-08-21 18:57:27 -05:00
|
|
|
#################################################################################
|
|
|
|
|
|
|
|
# will read the GPIO header of the pi and write the current state in the www spaceapi json file
|
|
|
|
def switched(pos):
|
|
|
|
wert_des_schalters = not bool(GPIO.input(pin_number))
|
2018-08-22 15:39:18 -05:00
|
|
|
icons = data.get("state").get('icon')
|
|
|
|
if pos != wert_des_schalters:
|
|
|
|
log.info("state changed to: ", wert_des_schalters)
|
2018-08-21 18:57:27 -05:00
|
|
|
chn_time = datetime.datetime.now().isoformat()
|
|
|
|
data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}})
|
|
|
|
with open('/var/www/html/spaceapi.json', 'w') as outfile:
|
2018-08-22 15:39:18 -05:00
|
|
|
json.dump(data, outfile, sort_keys=True)
|
|
|
|
return wert_des_schalters
|
2018-08-21 18:57:27 -05:00
|
|
|
|
|
|
|
#################################################################################
|
|
|
|
|
|
|
|
#init GPIO pin on raspi
|
|
|
|
pin_number = 18; #set GPIO Pin number
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
2018-08-22 15:39:18 -05:00
|
|
|
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200)
|
|
|
|
log.info("init complete... starting json foo")
|
2018-08-21 18:57:27 -05:00
|
|
|
#read json file
|
|
|
|
with open('spaceapi.json','r') as infile:
|
|
|
|
data = json.load(infile)
|
|
|
|
|
|
|
|
|
|
|
|
#run switched once to get current switch status and change if needed.
|
|
|
|
#switched(wert_des_schalters)
|
2018-08-22 15:39:18 -05:00
|
|
|
currentValue = False
|
|
|
|
try:
|
2018-08-21 18:57:27 -05:00
|
|
|
while True:
|
2018-08-22 15:39:18 -05:00
|
|
|
time.sleep(1)
|
|
|
|
currentValue = switched(currentValue)
|
|
|
|
|
|
|
|
#include IRCBOT status here?
|
|
|
|
#do other stuff otherwise just don't do anything?!
|
2018-08-21 18:57:27 -05:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
GPIO.cleanup()
|