Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use Noisebridge API, don't directly call gate.

  • Loading branch information...
commit 52f269eeabf382394edb05d474116713f112c2ef 1 parent 7e10a5f
Danny O'Brien authored

Showing 1 changed file with 24 additions and 18 deletions. Show diff stats Hide diff stats

  1. +24 18 baron.py
42 baron.py
@@ -20,22 +20,28 @@
20 20 serial_path = ''
21 21 keypad = 0
22 22
23   -def chat_with_gate(message): #stolen from NB api
24   - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
25   - port = 30012
26   - try:
27   - s.connect(('minotaur.noise', port))
28   - except socket.error:
29   - return "Failed: Could not connect"
30   - data = message
31   - s.sendall(data)
32   - s.shutdown(1)
33   - s.settimeout(5)
  23 +
  24 +import urllib,urllib2, json
  25 +gate_endpoint = 'http://api.noisebridge.net/gate/'
  26 +open_command = {'open' : 1 }
  27 +def open_gate(endpoint = gate_endpoint, command = open_command):
  28 + results = None
34 29 try:
35   - buf = s.recv(2048)
36   - except socket.timeout:
37   - buf = "Failed: No response"
38   - return buf
  30 + results = urllib2.urlopen(endpoint,
  31 + urllib.urlencode(command)).read()
  32 + return json.loads(results)
  33 + except urllib2.HTTPError, e:
  34 + return { 'error' : True,
  35 + 'message': "HTTP Error %d when calling api.noisebridge.net/gate/ : %s"
  36 + % (e.code, e.read()) }
  37 + except urllib2.URLError, e:
  38 + return { 'error' : True,
  39 + 'message': "Could not reach api.noisebridge.net/gate/ data is %d"
  40 + % e.args }
  41 + except ValueError:
  42 + return { 'error' : True,
  43 + 'message' : 'Could not decode JSON from api.noisebridge.net/gate/ %r'
  44 + % results }
39 45
40 46 def door_loop():
41 47 global codes, codes_path, serial_path, keypad
@@ -53,12 +59,12 @@ def door_loop():
53 59 else: # they hit #, *, or we timed out
54 60 break
55 61 if digits in codes:
56   - gate_message = chat_with_gate("OPEN!")
57   - if 'Acknowledged' in gate_message:
  62 + gate_status = open_gate()
  63 + if gate_status.get('open', False):
58 64 log.write("success, gate opening\n")
59 65 keypad.write('GH') #green led, happy sound
60 66 else:
61   - log.write("error with the gate: " + gate_message + "\n")
  67 + log.write("error with the gate: " + gate_status.open('message', 'No message received from gate') + "\n")
62 68 keypad.write('SR') #sad sound, red led
63 69 time.sleep(0.2)
64 70 keypad.write('QSR') #quiet, sad sound, red led

0 comments on commit 52f269e

Please sign in to comment.
Something went wrong with that request. Please try again.