annotate service/bluetooth/bluetoothService.py @ 815:7b0d282b292d

prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT Ignore-this: ad7b99b1259dc1501179a70faca5454c darcs-hash:20110816072302-312f9-6af13fa96f1d70c2065bf5fec84da7574fee88a5.gz
author drewp <drewp@bigasterisk.com>
date Tue, 16 Aug 2011 00:23:02 -0700
parents bebb8f7c5a3e
children 3a429f6cc9dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 #!/usr/bin/python
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 """
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 watch for bluetooth devices
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 this discoverer finds me if my treo has its screen on only, so I
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 have to wake up my own treo for a few seconds.
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 I can use 'hcitool cc <addr> && hcitool rssi <addr>' to wake it up and
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 get its signal strength, but that pattern crashes my treo easily. I
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 still don't have an access that wakes up the treo and then doesn't
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 crash it. Maybe I could pretend to be a headset or something.
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 depends on ubuntu package: python-bluez
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 """
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 from __future__ import absolute_import
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
18 import logging, time, datetime, restkit, jsonlib, sys, socket
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
19 import cyclone.web, pystache
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
20 from dateutil.tz import tzutc, tzlocal
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
21 from bluetooth import discover_devices, lookup_name
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
22 from twisted.internet import reactor, task
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
23 from twisted.internet.threads import deferToThread
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 from rdflib.Graph import Graph
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
25 from rdflib import Literal, Namespace, RDFS, URIRef
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 from pymongo import Connection
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 from dateutil import tz
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29 sys.path.append("/my/proj/homeauto/lib")
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 from cycloneerr import PrettyErrorHandler
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 from logsetup import log
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
33 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor']
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 ROOM = Namespace("http://projects.bigasterisk.com/room/")
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
37 # the mongodb serves as a much bigger cache, but I am expecting that
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
38 # 1) i won't fill memory with too many names; 2) this process will see
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
39 # each new device before it leaves, so I'll have the leaving name in
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
40 # my cache
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
41 nameCache = {} # addr : name
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
42
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
43 def lookupPastName(addr):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
44 row = mongo.find_one({"address" : addr,
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
45 'name' : {'$exists' : True}},
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
46 sort=[("created",-1)])
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
47 if row is None:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
48 return None
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
49 return row['name']
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
50
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
51 def getNearbyDevices():
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
52 addrs = discover_devices()
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
54 for a in addrs:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
55 if a not in nameCache:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
56 n = lookup_name(a) or lookupPastName(a)
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
57 if n is not None:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
58 nameCache[a] = n
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
59
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
60 log.debug("discover found %r", addrs)
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
61 return addrs
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
63 hub = restkit.Resource(
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
64 # PSHB not working yet; "http://bang:9030/"
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
65 "http://slash:9049/"
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
66 )
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
67
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
68 def mongoInsert(msg):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
69 try:
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
70 js = jsonlib.dumps(msg)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
71 except UnicodeDecodeError:
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
72 pass
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
73 else:
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
74 if msg.get('name', '') and msg['name'] not in ['THINKPAD_T43']:
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
75 hub.post("visitorNet", payload=js) # sans datetime
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
76 msg['created'] = datetime.datetime.now(tz.gettz('UTC'))
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 mongo.insert(msg, safe=True)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
79 def deviceUri(addr):
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
80 return URIRef("http://bigasterisk.com/bluetooth/%s" % addr)
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
81
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
82 class Poller(object):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
83 def __init__(self):
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
84 self.lastAddrs = set() # addresses
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 self.currentGraph = Graph()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
86 self.lastPollTime = 0
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 def poll(self):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89 log.debug("get devices")
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
90 devs = deferToThread(getNearbyDevices)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
92 devs.addCallback(self.compare)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93 devs.addErrback(log.error)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 return devs
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
96 def compare(self, addrs):
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97 self.lastPollTime = time.time()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
98
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
99 newGraph = Graph()
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
100 addrs = set(addrs)
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
101 for addr in addrs.difference(self.lastAddrs):
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
102 self.recordAction('arrive', addr)
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
103 for addr in self.lastAddrs.difference(addrs):
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
104 self.recordAction('leave', addr)
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
105 for addr in addrs:
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
106 uri = deviceUri(addr)
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
107 newGraph.add((ROOM['bluetooth'], ROOM['senses'], uri))
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
108 if addr in nameCache:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
109 newGraph.add((uri, RDFS.label, Literal(nameCache[addr])))
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
110 self.lastAddrs = addrs
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
111 self.currentGraph = newGraph
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
112
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
113 def recordAction(self, action, addr):
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
114 doc = {"sensor" : "bluetooth",
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
115 "address" : addr,
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
116 "action" : action}
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
117 if addr in nameCache:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
118 doc["name"] = nameCache[addr]
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
119 log.info("action: %s", doc)
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
120 mongoInsert(doc)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
121
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
122 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123 def get(self):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 age = time.time() - self.settings.poller.lastPollTime
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
125 if age > self.settings.config['period'] + 30:
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 raise ValueError("poll data is stale. age=%s" % age)
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
127
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
128 self.set_header("Content-Type", "application/xhtml+xml")
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
129 self.write(pystache.render(
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
130 open("index.xhtml").read(),
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
131 dict(host=socket.gethostname(),
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
132 )))
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
133
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
134 class Recent(PrettyErrorHandler, cyclone.web.RequestHandler):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
135 def get(self):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
136 name = {} # addr : name
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
137 events = []
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
138 hours = float(self.get_argument("hours", default="3"))
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
139 t1 = datetime.datetime.now(tzutc()) - datetime.timedelta(seconds=60*60*hours)
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
140 for row in mongo.find({"sensor":"bluetooth",
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
141 "created":{"$gt":t1}}, sort=[("created", 1)]):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
142 if 'name' in row:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
143 name[row['address']] = row['name']
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
144 row['t'] = int(row['created'].astimezone(tzlocal()).strftime("%s"))
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
145 del row['created']
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
146 del row['_id']
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
147 events.append(row)
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
148
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
149 for r in events:
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
150 r['name'] = name.get(r['address'], r['address'])
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
151 self.set_header("Content-Type", "application/json")
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
152 self.write(jsonlib.dumps({"events" : events}))
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
153
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
154
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
155 class Static(PrettyErrorHandler, cyclone.web.RequestHandler):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
156 def get(self, fn):
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
157 self.write(open(fn).read())
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
158
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
159 if __name__ == '__main__':
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
160 config = {
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
161 "period" : 60,
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
162 }
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
163 log.setLevel(logging.INFO)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
164 poller = Poller()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
165 reactor.listenTCP(9077, cyclone.web.Application([
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 (r'/', Index),
815
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
167 (r'/recent', Recent),
7b0d282b292d prettier viewer of recent BT activity. don't announce BT with no names. don't announce leaving BT
drewp <drewp@bigasterisk.com>
parents: 809
diff changeset
168 (r'/(underscore-min.js|pretty.js)', Static),
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
169 # graph, json, table, ...
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
170 ], poller=poller, config=config))
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
171 task.LoopingCall(poller.poll).start(config['period'])
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
172 reactor.run()