New Config; foodoord for the basement/1st floor (_unten/_oben)

This commit is contained in:
Bandie 2021-01-19 18:47:19 +01:00
parent 116ff5eba0
commit 8bb7f10d84
Signed by: Bandie
GPG Key ID: 843D7FA93BA46312
3 changed files with 267 additions and 4 deletions

View File

@ -1,5 +1,4 @@
[door_firstlevel_old]
[doorstatus]
status_url =
[door_firstlevel]
status_url =
key =
secret =

167
foodoord_oben Executable file
View File

@ -0,0 +1,167 @@
#! /usr/bin/python
import os
import stat
import time
import pifacedigitalio
import requests
import signal
import sys
import grp
from ConfigParser import SafeConfigParser
#Read config
parser = SafeConfigParser()
parser.read('/etc/foodoord.conf')
doorapi = parser.get('doorstatus', 'status_url')
consumerkey = parser.get('doorstatus', 'key')
consumersecret = parser.get('doorstatus', 'secret')
#Definitions for output
LED_RED=6
LED_GREEN=7
RELAYS_LOCK=0
RELAYS_UNLOCK=1
#Definitions for input
DOOR_BELL=0
REED_RELAYS=1 #not implementet yet
#Definitions for LEDcolor
RED=1
GREEN=2
ORANGE=3
if __name__ == "__main__":
def doorbell(event):
if (STATUS):
pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(1.5)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
def close_button(event):
global STATUS
STATUS = False
write_state("closed")
update_api(True)
set_led(RED)
listener = pifacedigitalio.InputEventListener()
listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, doorbell, settle_time=10)
listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, close_button, settle_time=5)
listener.activate()
def signal_handler(signal, frame):
listener.deactivate()
os.remove("/var/run/foodoord.pipe")
write_state("closed")
update_api(True)
set_led(RED)
sys.exit(0)
def set_led(color):
if (color==RED):
pifacedigital.leds[LED_RED].turn_on()
pifacedigital.leds[LED_GREEN].turn_off()
elif (color==GREEN):
pifacedigital.leds[LED_GREEN].turn_on()
pifacedigital.leds[LED_RED].turn_off()
elif (color==ORANGE):
pifacedigital.leds[LED_RED].turn_on()
pifacedigital.leds[LED_GREEN].turn_on()
pifacedigital = pifacedigitalio.PiFaceDigital()
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
def write_state(state):
try:
handle = open("/tmp/door_state", "w")
handle.write(state)
handle.close()
except:
pass
def update_api(locked):
#if locked:
#
# try:
# os.system("/usr/bin/mosquitto_pub -h mqtt.chaospott.de -t foobar/aerie/licht -m 'zentral-aus'")
# except:
# pass
try:
os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"aerie\": " + str(locked).lower() + " }' '" + doorapi + "' ")
except:
pass
#Startsettings
STATUS = False
pifacedigital.leds[LED_RED].turn_on()
#Setting up FiFo to get sshd-output
try:
os.mkfifo("/var/run/foodoord.pipe")
os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
except OSError:
pass
with open("/var/run/foodoord.pipe", "r") as ssh_input:
while 1:
#Read sshd-output from pipe
Pipe = ssh_input.readline()[:-1]
if (Pipe == "close" and STATUS):
pifacedigital.relays[RELAYS_LOCK].toggle()
time.sleep(1)
pifacedigital.relays[RELAYS_LOCK].toggle()
STATUS = False
write_state("closed")
update_api(True)
try:
r = requests.get(url+'&door=aerie&locked=1', timeout=2)
#print (r.status_code)
r = requests.get(old_api+'&status=closed', timeout=2)
#print (r.status_code)
except:
pass
set_led(RED)
elif (Pipe == "open"):
pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(1.5)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
if (STATUS==False):
try:
r = requests.get(url+"&door=aerie&locked=0", timeout=2)
#print (r.status_code)
r = requests.get(old_api+'&status=open', timeout=2)
#print (r.status_code)
except:
pass
update_api(False)
write_state("open")
STATUS = True
set_led(GREEN)
time.sleep(0.1)

97
foodoord_unten Executable file
View File

@ -0,0 +1,97 @@
#! /usr/bin/python
import os
import stat
import time
import urllib2
import signal
import sys
import RPi.GPIO as gpio
import grp
from ConfigParser import SafeConfigParser
#Read config
parser = SafeConfigParser()
parser.read('/etc/foodoord.conf')
doorapi = parser.get('doorstatus', 'status_url')
consumerkey = parser.get('doorstatus', 'key')
consumersecret = parser.get('doorstatus', 'secret')
#Definitions for output
LED_RED=6
LED_GREEN=7
RELAYS_LOCK=0
RELAYS_UNLOCK=1
PIN_OPEN=24
PIN_CLOSE=27
#Definitions for input
DOOR_BELL=0
REED_RELAYS=1 #not implementet yet
#Definitions for LEDcolor
RED=1
GREEN=2
ORANGE=3
def write_state(state):
try:
handle = open("/tmp/door_state", "w")
handle.write(state)
handle.close()
except:
pass
def update_api(locked):
try:
os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"cellar\": " + str(locked).lower() + " }' '" + doorapi + "' ")
except:
pass
if __name__ == "__main__":
#Startsettings
STATUS=False
gpio.setmode(gpio.BCM)
gpio.setup(PIN_OPEN, gpio.OUT)
gpio.setup(PIN_CLOSE, gpio.OUT)
#Setting up FiFo to get sshd-output
try:
os.mkfifo("/var/run/foodoord.pipe")
os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
except OSError:
pass
with open("/var/run/foodoord.pipe", "r") as ssh_input:
while 1:
#Read sshd-output from pipe
Pipe = ssh_input.readline()[:-1]
if (Pipe == "close"):
gpio.output(PIN_CLOSE,1)
time.sleep(1)
gpio.output(PIN_CLOSE,0)
write_state("closed")
update_api(True)
elif (Pipe == "open"):
#Locking
gpio.output(PIN_OPEN,1)
time.sleep(1)
gpio.output(PIN_OPEN,0)
#Save State
write_state("open")
#Status Update
update_api(False)
time.sleep(0.2)