-
Notifications
You must be signed in to change notification settings - Fork 0
/
digital.py
71 lines (60 loc) · 1.96 KB
/
digital.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
import time
import re
import httplib
import pygame
import ConfigParser
import json
import subprocess
import RPi.GPIO as GPIO
from datetime import datetime
config = ConfigParser.ConfigParser()
config.read('alarm.cfg')
#Open port for communication
pygame.init();
pygame.init();
pygame.mixer.music.load(config.get('Settings', 'doorChime'));
#GPIO.setmode(GPIO.BOARD);
GPIO.setmode(GPIO.BCM)
def getState(gpioNum):
response = str(GPIO.input(gpioNum));
return response;
def alert(gpioNum, newstate):
conn = httplib.HTTPConnection(config.get('Settings', 'httpHost'));
conn.request("GET", config.get('Settings', 'httpURI') + "/doorPort/" + str(gpioNum) + "/eventType/" + newstate);
r1 = conn.getresponse();
jsonDecoded = json.loads(r1.read());
return jsonDecoded;
def monitorStateChange(gpioNum, newstate):
start = datetime.now();
now = datetime.now();
delta = now - start;
monitorTime = 100000;
time.sleep(.1);
while (delta.microseconds < monitorTime) :
tmpState = getState(gpioNum);
if (tmpState != newstate):
now = datetime.now();
delta = now - start;
print "bailed at " + str(delta.microseconds) + " port: " + str(gpioNum) + " started: " + start.isoformat(' ');
return False;
now = datetime.now();
delta = now - start;
return True;
states = eval(config.get('Settings', 'sensors'));
shellcmd = config.get('Settings', 'shell');
for (i, state) in enumerate(states):
GPIO.setup(state[0], GPIO.IN);
while True:
for (i, state) in enumerate(states):
newstate = getState(state[0]);
if (newstate != state[1]):
if (monitorStateChange(state[0], newstate)):
print "State Changed to: " + newstate + " Port: " + str(state[0]) + " at: " + datetime.now().isoformat(' ');
if (newstate == '0'):
pygame.mixer.music.play()
responce = alert( state[0], newstate);
# args = ['/home/lart/alarm/event.sh', str(responce['eventID']), responce['armedStatus'], newstate]
# p = subprocess.Popen(args)
states[i][1] = newstate;
# time.sleep(.1);