Skip to content

Instantly share code, notes, and snippets.

@adamancini
Created July 12, 2012 14:42
Show Gist options
  • Save adamancini/3098543 to your computer and use it in GitHub Desktop.
Save adamancini/3098543 to your computer and use it in GitHub Desktop.
usermain
import logging
from Snap import snap
log = logging.getLogger('SimplyHomeBaseStation')
import json
import urllib2
import sys
basestationId = 2 # to be specified in config.yaml?
url = "http://dev.simplyhomeclient.com/events.json" # to be specified in config.yaml?
appDict = {'Content-Type': 'application/json'}
def makeDataReq(data, macaddress):
dataDict = {'base_station_id': basestationId, 'data': data, 'data_type': 1, 'device_id': macaddress}
dataDict = json.dumps(dataDict)
return urllib2.Request(url, dataDict, appDict)
def stripMac(macaddress):
return macaddress.replace("\\x",":")[1:]
def door_event(data, macaddress):
log.info(data)
macaddress = stripMac(macaddress)
log.info("Got something from: %s", macaddress)
req = makeDataReq(data, macaddress)
try:
f = urllib2.urlopen(req)
response = f.read()
log.info(response)
except Exception, e:
raise e
finally:
f.close()
def heartbeat(battery, macaddress):
macaddress = stripMac(macaddress)
log.info('Received heartbeat from %s', macaddress)
def main():
global server, snapCom
snapCom = snap.Snap(funcs={'door_event' : door_event, 'heartbeat' : heartbeat})
snapCom.open_serial(1, "/dev/ttyS1")
snapCom.accept_tcp()
if __name__ == "__main__":
logging.basicConfig(level=logging.ERROR)
log = logging.getLogger('simplyhome')
hdlr = logging.FileHandler('/var/tmp/simplyhome.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
log.addHandler(hdlr)
log.setLevel(logging.DEBUG)
log.info("Starting up\n")
main()
print "Initialized... \n"
snapCom.loop()
print "Listenting... \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment