comparison service/garageArduino/garageArduino.py @ 5:4c44c80a6a72

move garage door opener from parport to arduino Ignore-this: c7d21f40841af0e64b26dd4991e441ae
author drewp@bigasterisk.com
date Sun, 14 Aug 2011 21:46:57 -0700
parents be855a111619
children 33e6678d76ab
comparison
equal deleted inserted replaced
4:be855a111619 5:4c44c80a6a72
3 talks to frontdoordriver.pde on an arduino 3 talks to frontdoordriver.pde on an arduino
4 """ 4 """
5 5
6 from __future__ import division 6 from __future__ import division
7 7
8 import cyclone.web, json, traceback, os, sys, time 8 import cyclone.web, json, traceback, os, sys, time, logging
9 from twisted.python import log 9 from twisted.internet import reactor, task, defer
10 from twisted.internet import reactor, task
11 from twisted.web.client import getPage 10 from twisted.web.client import getPage
12 sys.path.append("/my/proj/house/frontdoor") 11 sys.path.append("/my/proj/house/frontdoor")
13 from loggingserial import LoggingSerial 12 from loggingserial import LoggingSerial
13 sys.path.append("/my/proj/homeauto/lib")
14 from cycloneerr import PrettyErrorHandler
15 from logsetup import log
14 sys.path.append("../../../room") 16 sys.path.append("../../../room")
15 from carbondata import CarbonClient 17 from carbondata import CarbonClient
16 sys.path.append("/my/site/magma") 18 sys.path.append("/my/site/magma")
17 from stategraph import StateGraph 19 from stategraph import StateGraph
18 from rdflib import Namespace, RDF, Literal 20 from rdflib import Namespace, RDF, Literal
19 sys.path.append("/my/proj/homeauto/lib") 21
20 from cycloneerr import PrettyErrorHandler
21 22
22 ROOM = Namespace("http://projects.bigasterisk.com/room/") 23 ROOM = Namespace("http://projects.bigasterisk.com/room/")
23 DEV = Namespace("http://projects.bigasterisk.com/device/") 24 DEV = Namespace("http://projects.bigasterisk.com/device/")
24 25
25 class ArduinoGarage(object): 26 class ArduinoGarage(object):
43 44
44 def setThreshold(self, t): 45 def setThreshold(self, t):
45 """set 10-bit threshold""" 46 """set 10-bit threshold"""
46 self.ser.write("\x60\x03"+chr(max(1 << 2, t) >> 2)) 47 self.ser.write("\x60\x03"+chr(max(1 << 2, t) >> 2))
47 return self.ser.readJson()['threshold'] 48 return self.ser.readJson()['threshold']
49
50 def setGarage(self, level):
51 """set garage door opener pin"""
52 self.ser.write("\x60\x04"+chr(int(bool(level))))
53 return self.ser.readJson()['garage']
48 54
49 55
50 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): 56 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
51 def get(self): 57 def get(self):
52 """ 58 """
110 self.write(json.dumps({"threshold" : thr})) 116 self.write(json.dumps({"threshold" : thr}))
111 117
112 def put(self): 118 def put(self):
113 pass 119 pass
114 120
121 class GarageDoorOpen(PrettyErrorHandler, cyclone.web.RequestHandler):
122 def post(self):
123 self.set_header("Content-Type", "text/plain")
124 self.settings.arduino.setGarage(True)
125 self.write("pin high, waiting..\n")
126 self.flush()
127 d = defer.Deferred()
128 def finish():
129 self.settings.arduino.setGarage(False)
130 self.write("pin low. Done")
131 d.callback(None)
132 reactor.callLater(1.5, finish) # this time depends on the LP circuit
133 return d
115 134
116 class Application(cyclone.web.Application): 135 class Application(cyclone.web.Application):
117 def __init__(self, ard, poller): 136 def __init__(self, ard, poller):
118 handlers = [ 137 handlers = [
119 (r"/", Index), 138 (r"/", Index),
120 (r"/graph", GraphPage), 139 (r"/graph", GraphPage),
121 (r"/frontDoorMotion", FrontDoorMotion), 140 (r"/frontDoorMotion", FrontDoorMotion),
122 (r'/housePower', HousePower), 141 (r'/housePower', HousePower),
123 (r'/housePower/raw', HousePowerRaw), 142 (r'/housePower/raw', HousePowerRaw),
124 (r'/housePower/threshold', HousePowerThreshold), 143 (r'/housePower/threshold', HousePowerThreshold),
144 (r'/garageDoorOpen', GarageDoorOpen),
125 ] 145 ]
126 settings = {"arduino" : ard, "poller" : poller} 146 settings = {"arduino" : ard, "poller" : poller}
127 cyclone.web.Application.__init__(self, handlers, **settings) 147 cyclone.web.Application.__init__(self, handlers, **settings)
128 148
129 149
218 'servePort' : 9050, 238 'servePort' : 9050,
219 'pollFrequency' : 5, 239 'pollFrequency' : 5,
220 'boardName' : 'garage', # gets sent with updates 240 'boardName' : 'garage', # gets sent with updates
221 } 241 }
222 242
223 #log.startLogging(sys.stdout) 243 #from twisted.python import log as twlog
244 #twlog.startLogging(sys.stdout)
245
246 log.setLevel(logging.DEBUG)
224 247
225 ard = ArduinoGarage() 248 ard = ArduinoGarage()
226 249
227 period = 1/config['pollFrequency'] 250 period = 1/config['pollFrequency']
228 p = Poller(ard, period) 251 p = Poller(ard, period)