Compare commits

..

No commits in common. "72b2276603f2e0b05fff27dafaed1c3296014ca0" and "897c3a442a27eeb59c1e33f5940168b19027e077" have entirely different histories.

3 changed files with 200 additions and 41 deletions

View File

@ -58,26 +58,18 @@ do
outfile="${dest}/authorized_keys.${appendix}" outfile="${dest}/authorized_keys.${appendix}"
cat ${temp_outfile} |envsubst > ${outfile} cat ${temp_outfile} |envsubst > ${outfile}
# Oben # Oben
if [ "$(hostname)" = "foodoor" ]; then install -d -o ${appendix} -g nogroup -m 0700 /var/lib/foodoor/${appendix}/.ssh
install -d -o ${appendix} -g nogroup -m 0700 /var/lib/foodoor/${appendix}/.ssh install -b -S .last -o ${appendix} -g nogroup -m 0600 ${outfile} /var/lib/foodoor/${appendix}/.ssh/authorized_keys
install -b -S .last -o ${appendix} -g nogroup -m 0600 ${outfile} /var/lib/foodoor/${appendix}/.ssh/authorized_keys
fi
# Unten # Unten
if [ "$(hostname)" = "kellertuer" ]; then #if [ "${action}" = "open" ]; then
if [ "${action}" = "open" ]; then # owner="unlock"
owner="unlock" #elif [ "${action}" = "close" ]; then
elif [ "${action}" = "close" ]; then # owner="lock"
owner="lock" #fi
fi
install -d -o ${owner} -g nogroup -m 0700 /var/lib/foodoor/${action}/.ssh
install -b -S .last -o ${owner} -g nogroup -m 0600 ${outfile} /var/lib/foodoor/${action}/.ssh/authorized_keys
if [ "${appendix}" = "door" ]; then
install -d -o ${appendix} -g nogroup -m 0700 /var/lib/foodoor/${appendix}/.ssh
install -b -S .last -o ${appendix} -g nogroup -m 0600 ${outfile} /var/lib/foodoor/${appendix}/.ssh/authorized_keys
fi
fi
#install -d -o ${owner} -g nogroup -m 0700 /var/lib/foodoor/${action}/.ssh
#install -b -S .last -o ${owner} -g nogroup -m 0600 ${outfile} /var/lib/foodoor/${action}/.ssh/authorized_keys
done done

140
foodoord Executable file
View File

@ -0,0 +1,140 @@
#!/usr/bin/env python2
import os
import stat
import time
import pifacedigitalio
import urllib2
import signal
import sys
import grp
from ConfigParser import SafeConfigParser
#Read config
parser = SafeConfigParser()
parser.read('/etc/foodoord.conf')
url = parser.get('door_firstlevel', 'status_url')
old_api = parser.get('door_firstlevel_old', 'status_url')
#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(2)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
#print 'got doorbell'
def close_button(event):
global STATUS
STATUS = False
try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2)
except:
pass
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")
try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2)
except:
pass
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)
#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
try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2)
except:
pass
set_led(RED)
elif (Pipe == "open"):
pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(2)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
if (STATUS==False):
try:
urllib2.urlopen(url+'&door=aerie&locked=0', timeout=2)
urllib2.urlopen(old_api+'&status=open', timeout=2)
except:
pass
STATUS = True
set_led(GREEN)
time.sleep(0.1)

View File

@ -1,10 +1,10 @@
#!/usr/bin/env python2 #! /usr/bin/python
import os import os
import stat import stat
import time import time
import pifacedigitalio import pifacedigitalio
import urllib2 import requests
import signal import signal
import sys import sys
import grp import grp
@ -14,9 +14,9 @@ from ConfigParser import SafeConfigParser
parser = SafeConfigParser() parser = SafeConfigParser()
parser.read('/etc/foodoord.conf') parser.read('/etc/foodoord.conf')
url = parser.get('door_firstlevel', 'status_url') doorapi = parser.get('doorstatus', 'status_url')
old_api = parser.get('door_firstlevel_old', 'status_url') consumerkey = parser.get('doorstatus', 'key')
consumersecret = parser.get('doorstatus', 'secret')
#Definitions for output #Definitions for output
LED_RED=6 LED_RED=6
@ -40,18 +40,15 @@ if __name__ == "__main__":
def doorbell(event): def doorbell(event):
if (STATUS): if (STATUS):
pifacedigital.relays[RELAYS_UNLOCK].toggle() pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(2) time.sleep(1.5)
pifacedigital.relays[RELAYS_UNLOCK].toggle() pifacedigital.relays[RELAYS_UNLOCK].toggle()
#print 'got doorbell'
def close_button(event): def close_button(event):
global STATUS global STATUS
STATUS = False STATUS = False
try: write_state("closed")
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2) update_api(True)
except:
pass
set_led(RED) set_led(RED)
@ -65,11 +62,8 @@ if __name__ == "__main__":
listener.deactivate() listener.deactivate()
os.remove("/var/run/foodoord.pipe") os.remove("/var/run/foodoord.pipe")
try: write_state("closed")
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2) update_api(True)
urllib2.urlopen(old_api+'&status=closed', timeout=2)
except:
pass
set_led(RED) set_led(RED)
sys.exit(0) sys.exit(0)
@ -89,6 +83,28 @@ if __name__ == "__main__":
pifacedigital = pifacedigitalio.PiFaceDigital() pifacedigital = pifacedigitalio.PiFaceDigital()
signal.signal(signal.SIGTERM, signal_handler) 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 #Startsettings
STATUS = False STATUS = False
@ -113,9 +129,15 @@ if __name__ == "__main__":
pifacedigital.relays[RELAYS_LOCK].toggle() pifacedigital.relays[RELAYS_LOCK].toggle()
STATUS = False STATUS = False
write_state("closed")
update_api(True)
try: try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2) r = requests.get(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2) #print (r.status_code)
r = requests.get(old_api+'&status=closed', timeout=2)
#print (r.status_code)
except: except:
pass pass
@ -123,18 +145,23 @@ if __name__ == "__main__":
elif (Pipe == "open"): elif (Pipe == "open"):
pifacedigital.relays[RELAYS_UNLOCK].toggle() pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(2) time.sleep(1.5)
pifacedigital.relays[RELAYS_UNLOCK].toggle() pifacedigital.relays[RELAYS_UNLOCK].toggle()
if (STATUS==False): if (STATUS==False):
try: try:
urllib2.urlopen(url+'&door=aerie&locked=0', timeout=2) r = requests.get(url+"&door=aerie&locked=0", timeout=2)
urllib2.urlopen(old_api+'&status=open', timeout=2) #print (r.status_code)
r = requests.get(old_api+'&status=open', timeout=2)
#print (r.status_code)
except: except:
pass pass
update_api(False)
write_state("open")
STATUS = True STATUS = True
set_led(GREEN) set_led(GREEN)
time.sleep(0.1) time.sleep(0.1)