comparison service/frontDoorArduino/frontDoorArduino.py @ 860:db20fbc4ca10

frontdoorarduino can return an rdf graph Ignore-this: 4593075f2f7b5b3387b578b0a4fba207 darcs-hash:20130128074850-312f9-fc74143ec847fa02cb5ce3c9f577391abecabe0d
author drewp <drewp@bigasterisk.com>
date Sun, 27 Jan 2013 23:48:50 -0800
parents 0aafd40e4afc
children 0fa988159bb3
comparison
equal deleted inserted replaced
859:379cefec542e 860:db20fbc4ca10
15 15
16 import cyclone.web, json, traceback, os, sys 16 import cyclone.web, json, traceback, os, sys
17 from twisted.python import log 17 from twisted.python import log
18 from twisted.internet import reactor, task 18 from twisted.internet import reactor, task
19 from twisted.web.client import getPage 19 from twisted.web.client import getPage
20 from rdflib import Namespace, Literal, URIRef
20 21
21 sys.path.append("/my/proj/house/frontdoor") 22 sys.path.append("/my/proj/house/frontdoor")
22 from loggingserial import LoggingSerial 23 from loggingserial import LoggingSerial
23 24
24 sys.path.append("/my/proj/homeauto/lib") 25 sys.path.append("/my/proj/homeauto/lib")
25 from cycloneerr import PrettyErrorHandler 26 from cycloneerr import PrettyErrorHandler
27
28 sys.path.append("/my/site/magma")
29 from stategraph import StateGraph
30
31 DEV = Namespace("http://projects.bigasterisk.com/device/")
32 ROOM = Namespace("http://projects.bigasterisk.com/room/")
26 33
27 class Board(object): 34 class Board(object):
28 """ 35 """
29 arduino board actions, plus the last values we wrote to it 36 arduino board actions, plus the last values we wrote to it
30 """ 37 """
86 self.settings.board.ping() 93 self.settings.board.ping()
87 94
88 self.set_header("Content-Type", "application/xhtml+xml") 95 self.set_header("Content-Type", "application/xhtml+xml")
89 self.write(open("index.html").read()) 96 self.write(open("index.html").read())
90 97
98 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler):
99 def get(self):
100 g = StateGraph(ctx=DEV['frontDoorArduino'])
101
102 board = self.settings.board
103 g.add((DEV['frontDoorOpen'], ROOM['state'],
104 ROOM['open'] if board.getDoor() else ROOM['closed']))
105 g.add((DEV['frontYardLight'], ROOM['state'],
106 ROOM['on'] if board.getYardLight() else ROOM['off']))
107 g.add((DEV['frontDoorLcd'], ROOM['text'],
108 Literal(board.getLcd())))
109 g.add((DEV['frontDoorLcd'], ROOM['brightness'],
110 Literal(board.getLcdBrightness())))
111
112 # not temperature yet because it's slow and should be cached from
113 # the last poll
114
115 self.set_header('Content-type', 'application/x-trig')
116 self.write(g.asTrig())
117
91 class Lcd(PrettyErrorHandler, cyclone.web.RequestHandler): 118 class Lcd(PrettyErrorHandler, cyclone.web.RequestHandler):
92 def get(self): 119 def get(self):
93 self.set_header("Content-Type", "text/plain") 120 self.set_header("Content-Type", "text/plain")
94 self.write(self.settings.board.getLcd()) 121 self.write(self.settings.board.getLcd())
95 122
138 165
139 class Application(cyclone.web.Application): 166 class Application(cyclone.web.Application):
140 def __init__(self, board): 167 def __init__(self, board):
141 handlers = [ 168 handlers = [
142 (r"/", index), 169 (r"/", index),
170 (r"/graph", GraphResource),
143 (r'/lcd', Lcd), 171 (r'/lcd', Lcd),
144 (r'/door', Door), 172 (r'/door', Door),
145 (r'/temperature', Temperature), 173 (r'/temperature', Temperature),
146 (r'/lcd/backlight', Backlight), 174 (r'/lcd/backlight', Backlight),
147 (r'/yardLight', YardLight), 175 (r'/yardLight', YardLight),
157 self.boardName = boardName 185 self.boardName = boardName
158 self.last = None 186 self.last = None
159 187
160 def poll(self): 188 def poll(self):
161 try: 189 try:
190 # this should be fetching everything and pinging reasoning
191 # if anything is new
162 new = self.board.getDoor() 192 new = self.board.getDoor()
163 if new != self.last: 193 if new != self.last:
164 msg = json.dumps(dict(board=self.boardName, 194 msg = json.dumps(dict(board=self.boardName,
165 name="frontDoor", state=new)) 195 name="frontDoor", state=new))
166 getPage(self.postUrl, 196 getPage(self.postUrl,