Skip to content

Commit

Permalink
Use Noisebridge API, don't directly call gate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny O'Brien authored and Danny O'Brien committed Apr 28, 2012
1 parent 7e10a5f commit 52f269e
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions baron.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@
serial_path = '' serial_path = ''
keypad = 0 keypad = 0


def chat_with_gate(message): #stolen from NB api
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) import urllib,urllib2, json
port = 30012 gate_endpoint = 'http://api.noisebridge.net/gate/'
try: open_command = {'open' : 1 }
s.connect(('minotaur.noise', port)) def open_gate(endpoint = gate_endpoint, command = open_command):
except socket.error: results = None
return "Failed: Could not connect"
data = message
s.sendall(data)
s.shutdown(1)
s.settimeout(5)
try: try:
buf = s.recv(2048) results = urllib2.urlopen(endpoint,
except socket.timeout: urllib.urlencode(command)).read()
buf = "Failed: No response" return json.loads(results)
return buf except urllib2.HTTPError, e:
return { 'error' : True,
'message': "HTTP Error %d when calling api.noisebridge.net/gate/ : %s"
% (e.code, e.read()) }
except urllib2.URLError, e:
return { 'error' : True,
'message': "Could not reach api.noisebridge.net/gate/ data is %d"
% e.args }
except ValueError:
return { 'error' : True,
'message' : 'Could not decode JSON from api.noisebridge.net/gate/ %r'
% results }


def door_loop(): def door_loop():
global codes, codes_path, serial_path, keypad global codes, codes_path, serial_path, keypad
Expand All @@ -53,12 +59,12 @@ def door_loop():
else: # they hit #, *, or we timed out else: # they hit #, *, or we timed out
break break
if digits in codes: if digits in codes:
gate_message = chat_with_gate("OPEN!") gate_status = open_gate()
if 'Acknowledged' in gate_message: if gate_status.get('open', False):
log.write("success, gate opening\n") log.write("success, gate opening\n")
keypad.write('GH') #green led, happy sound keypad.write('GH') #green led, happy sound
else: else:
log.write("error with the gate: " + gate_message + "\n") log.write("error with the gate: " + gate_status.open('message', 'No message received from gate') + "\n")
keypad.write('SR') #sad sound, red led keypad.write('SR') #sad sound, red led
time.sleep(0.2) time.sleep(0.2)
keypad.write('QSR') #quiet, sad sound, red led keypad.write('QSR') #quiet, sad sound, red led
Expand Down

0 comments on commit 52f269e

Please sign in to comment.