98 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#! /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)
 | 
						|
 |