annotate service/bluetooth/bluetoothService.py @ 1147:ef494fe0499f

forgot devices_shared.py Ignore-this: 210e7777d9d4d11f148bb7e63f5de65a darcs-hash:8dd34afc9d00fe796f94da254519e22ca1fa7d03
author drewp <drewp@bigasterisk.com>
date Wed, 04 Apr 2018 14:58:27 -0700
parents f045826df601
children
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:
821
f045826df601 some action filter change
drewp <drewp@bigasterisk.com>
parents: 815
diff changeset
74 if (msg.get('name', '') and
f045826df601 some action filter change
drewp <drewp@bigasterisk.com>
parents: 815
diff changeset
75 msg['name'] not in ['THINKPAD_T43'] and
f045826df601 some action filter change
drewp <drewp@bigasterisk.com>
parents: 815
diff changeset
76 msg['action'] == 'arrive'):
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
77 hub.post("visitorNet", payload=js) # sans datetime
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
78 msg['created'] = datetime.datetime.now(tz.gettz('UTC'))
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
79 mongo.insert(msg, safe=True)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
80
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
81 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
82 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
83
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
84 class Poller(object):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
85 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
86 self.lastAddrs = set() # addresses
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
87 self.currentGraph = Graph()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
88 self.lastPollTime = 0
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
89
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
90 def poll(self):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
91 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
92 devs = deferToThread(getNearbyDevices)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
93
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
94 devs.addCallback(self.compare)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
95 devs.addErrback(log.error)
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
96 return devs
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
97
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
98 def compare(self, addrs):
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
99 self.lastPollTime = time.time()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
100
809
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 self.currentGraph = newGraph
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
114
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
115 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
116 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
117 "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
118 "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
119 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
120 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
121 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
122 mongoInsert(doc)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
123
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
124 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
125 def get(self):
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
126 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
127 if age > self.settings.config['period'] + 30:
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
128 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
129
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 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
131 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
132 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
133 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
134 )))
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
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 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
137 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
138 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
139 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
140 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
141 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
142 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
143 "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
144 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
145 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
146 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
147 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
148 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
149 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
150
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 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
152 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
153 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
154 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
155
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
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 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
158 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
159 self.write(open(fn).read())
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
160
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
161 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
162 config = {
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
163 "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
164 }
bebb8f7c5a3e move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents: 807
diff changeset
165 log.setLevel(logging.INFO)
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
166 poller = Poller()
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
167 reactor.listenTCP(9077, cyclone.web.Application([
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
168 (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
169 (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
170 (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
171 # 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
172 ], 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
173 task.LoopingCall(poller.poll).start(config['period'])
807
4713bb87e34e moved from proj/room
drewp <drewp@bigasterisk.com>
parents:
diff changeset
174 reactor.run()