comparison service/wallscreen/wallscreen.py @ 866:a99b4d5afb83

use websockets for temperature update Ignore-this: 558ad53cf5b4b0c013041db555fbb458 darcs-hash:20130210214135-312f9-f69d37d4b9bce7fa6ec0e9c9889b2de90fd26a8f
author drewp <drewp@bigasterisk.com>
date Sun, 10 Feb 2013 13:41:35 -0800
parents db3e0510ab49
children 7f7911accb26
comparison
equal deleted inserted replaced
865:db3e0510ab49 866:a99b4d5afb83
2 for raspberry pi screen. 2 for raspberry pi screen.
3 B2G_HOMESCREEN=http://10.1.0.1:9102 b2g/b2g --screen=700x480 3 B2G_HOMESCREEN=http://10.1.0.1:9102 b2g/b2g --screen=700x480
4 and then fix the window with this: 4 and then fix the window with this:
5 echo "window.resizeTo(702,480)" | nc localhost 9999 5 echo "window.resizeTo(702,480)" | nc localhost 9999
6 """ 6 """
7 import json, sys 7 import json, sys, time
8 from dateutil.parser import parse 8 from dateutil.parser import parse
9 from twisted.internet import reactor 9 from twisted.internet import reactor, task
10 from twisted.internet.defer import inlineCallbacks 10 from twisted.internet.defer import inlineCallbacks
11 import cyclone.web, cyclone.httpclient, cyclone.websocket 11 import cyclone.web, cyclone.httpclient, cyclone.websocket
12 from rdflib import Graph, URIRef, Namespace, Literal, RDF 12 from rdflib import Graph, URIRef, Namespace, Literal, RDF
13 13
14 sys.path.append("../../lib") 14 sys.path.append("../../lib")
70 d['timeEvents'].sort(key=lambda ev: ev['start']) 70 d['timeEvents'].sort(key=lambda ev: ev['start'])
71 events.append(d) 71 events.append(d)
72 72
73 self.write(json.dumps({'tasks':out, 'events' : events})) 73 self.write(json.dumps({'tasks':out, 'events' : events}))
74 74
75 class Thermostat(PrettyErrorHandler, cyclone.web.RequestHandler): 75 @inlineCallbacks
76 @inlineCallbacks 76 def pushThermostat():
77 def get(self): 77 f = json.loads((yield cyclone.httpclient.fetch("http://bang:10001/requestedTemperature")).body)
78 self.write((yield cyclone.httpclient.fetch("http://bang:10001/requestedTemperature")).body) 78 [c.sendMessage(f) for c in liveClients]
79 79
80 80 class RefreshTemperature(PrettyErrorHandler, cyclone.web.RequestHandler):
81 def post(self):
82 return pushThermostat()
83
84 liveClients = set()
85
86 class Live(cyclone.websocket.WebSocketHandler):
87 def connectionMade(self, *args, **kwargs):
88 log.info("websocket opened")
89 liveClients.add(self)
90
91 def connectionLost(self, reason):
92 log.info("websocket closed")
93 liveClients.remove(self)
94
95 def messageReceived(self, message):
96 log.info("got message %s" % message)
97 self.sendMessage(message)
98
81 if __name__ == '__main__': 99 if __name__ == '__main__':
82 from twisted.python import log as twlog 100 from twisted.python import log as twlog
83 #twlog.startLogging(sys.stdout) 101 #twlog.startLogging(sys.stdout)
84 102
103 task.LoopingCall(pushThermostat).start(1)
104
85 port = 9102 105 port = 9102
86 reactor.listenTCP(port, cyclone.web.Application(handlers=[ 106 reactor.listenTCP(port, cyclone.web.Application(handlers=[
87 (r'/content', Content), 107 (r'/content', Content),
88 (r'/thermostat', Thermostat), 108 (r'/live', Live),
109 (r'/refreshTemperature', RefreshTemperature),
89 (r'/(.*)', cyclone.web.StaticFileHandler, 110 (r'/(.*)', cyclone.web.StaticFileHandler,
90 {"path" : ".", # security hole- serves this dir too 111 {"path" : ".", # security hole- serves this dir too
91 "default_filename" : "index.html"}), 112 "default_filename" : "index.html"}),
92 ])) 113 ]))
93 log.info("serving on %s" % port) 114 log.info("serving on %s" % port)