Mercurial > code > home > repos > homeauto
annotate service/tomatoWifi/tomatoWifi.py @ 980:2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
Ignore-this: ecc5280b15d66020412f82ad84862074
darcs-hash:20150504002120-312f9-85bcb342f5bdae78d7b6d9083929944d4a467001
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 03 May 2015 17:21:20 -0700 |
parents | 2489d111f4f1 |
children | fc184ea1416d |
rev | line source |
---|---|
805 | 1 #!/usr/bin/python |
2 """ | |
3 scrape the tomato router status pages to see who's connected to the | |
4 wifi access points. Includes leases that aren't currently connected. | |
5 | |
6 Returns: | |
7 json listing (for magma page) | |
8 rdf graph (for reasoning) | |
9 activity stream, when we start saving history | |
10 | |
11 Todo: this should be the one polling and writing to mongo, not entrancemusic | |
967 | 12 |
805 | 13 """ |
14 from __future__ import division | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
15 import sys, cyclone.web, json, traceback, time, pystache, datetime, logging |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
16 import web.utils |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
17 from cyclone.httpclient import fetch |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
18 sys.path.append("/home/drewp/projects/photo/lib/python2.7/site-packages") |
805 | 19 from dateutil import tz |
20 from twisted.internet import reactor, task | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
21 from twisted.internet.defer import inlineCallbacks |
805 | 22 |
23 from pymongo import Connection, DESCENDING | |
24 from rdflib import Namespace, Literal, URIRef | |
25 sys.path.append("/my/site/magma") | |
26 from stategraph import StateGraph | |
27 from wifi import Wifi | |
28 | |
29 sys.path.append("/my/proj/homeauto/lib") | |
30 from cycloneerr import PrettyErrorHandler | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
31 from logsetup import log |
805 | 32 |
841
ddc2cdbfde50
rdflib and jsonlib api updates
drewp <drewp@bigasterisk.com>
parents:
806
diff
changeset
|
33 |
805 | 34 DEV = Namespace("http://projects.bigasterisk.com/device/") |
35 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
36 reasoning = "http://bang:9071/" |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
37 |
805 | 38 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): |
39 def get(self): | |
40 | |
41 age = time.time() - self.settings.poller.lastPollTime | |
42 if age > 10: | |
43 raise ValueError("poll data is stale. age=%s" % age) | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
44 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
45 self.set_header("Content-Type", "text/html") |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
46 self.write(open("index.html").read()) |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
47 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
48 def whenConnected(mongo, macThatIsNowConnected): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
49 lastArrive = None |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
50 for ev in mongo.find({'address': macThatIsNowConnected.upper()}, |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
51 sort=[('created', -1)], |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
52 max_scan=100000): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
53 if ev['action'] == 'arrive': |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
54 lastArrive = ev |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
55 if ev['action'] == 'leave': |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
56 break |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
57 if lastArrive is None: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
58 raise ValueError("no past arrivals") |
805 | 59 |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
60 return lastArrive['created'] |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
61 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
62 def connectedAgoString(conn): |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
63 return web.utils.datestr( |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
64 conn.astimezone(tz.tzutc()).replace(tzinfo=None)) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
65 |
805 | 66 class Table(PrettyErrorHandler, cyclone.web.RequestHandler): |
67 def get(self): | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
68 def rowDict(row): |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
69 row['cls'] = "signal" if row.get('connected') else "nosignal" |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
70 if 'name' not in row: |
879
691aa1333073
handle wifi users with no clientHostname
drewp <drewp@bigasterisk.com>
parents:
867
diff
changeset
|
71 row['name'] = row.get('clientHostname', '-') |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
72 if 'signal' not in row: |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
73 row['signal'] = 'yes' if row.get('connected') else 'no' |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
74 |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
75 try: |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
76 conn = whenConnected(self.settings.mongo, row.get('mac', '??')) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
77 row['connectedAgo'] = connectedAgoString(conn) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
78 except ValueError: |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
79 row['connectedAgo'] = 'yes' if row.get('connected') else '' |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
80 row['router'] = row.get('ssid', '') |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
81 return row |
805 | 82 |
83 self.set_header("Content-Type", "application/xhtml+xml") | |
84 self.write(pystache.render( | |
85 open("table.mustache").read(), | |
86 dict( | |
87 rows=sorted(map(rowDict, self.settings.poller.lastAddrs), | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
88 key=lambda a: (not a.get('connected'), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
89 a.get('name')))))) |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
90 |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
91 |
805 | 92 class Json(PrettyErrorHandler, cyclone.web.RequestHandler): |
93 def get(self): | |
94 self.set_header("Content-Type", "application/json") | |
95 age = time.time() - self.settings.poller.lastPollTime | |
96 if age > 10: | |
97 raise ValueError("poll data is stale. age=%s" % age) | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
98 self.write(json.dumps({"wifi" : self.settings.poller.lastAddrs, |
805 | 99 "dataAge" : age})) |
100 | |
101 class GraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): | |
102 def get(self): | |
103 g = StateGraph(ctx=DEV['wifi']) | |
104 | |
105 # someday i may also record specific AP and their strength, | |
106 # for positioning. But many users just want to know that the | |
107 # device is connected to some bigasterisk AP. | |
108 aps = URIRef("http://bigasterisk.com/wifiAccessPoints") | |
109 age = time.time() - self.settings.poller.lastPollTime | |
110 if age > 10: | |
111 raise ValueError("poll data is stale. age=%s" % age) | |
112 | |
113 for dev in self.settings.poller.lastAddrs: | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
114 if not dev.get('connected'): |
805 | 115 continue |
966
cce0107a78b4
scan dhcpd.leases to get more info about networked devices
drewp <drewp@bigasterisk.com>
parents:
879
diff
changeset
|
116 uri = URIRef("http://bigasterisk.com/mac/%s" % dev['mac'].lower()) |
cce0107a78b4
scan dhcpd.leases to get more info about networked devices
drewp <drewp@bigasterisk.com>
parents:
879
diff
changeset
|
117 g.add((uri, ROOM['macAddress'], Literal(dev['mac'].lower()))) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
118 |
805 | 119 g.add((uri, ROOM['connected'], aps)) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
120 if 'clientHostname' in dev: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
121 g.add((uri, ROOM['wifiNetworkName'], Literal(dev['clientHostname']))) |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
122 if 'name' in dev: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
123 g.add((uri, ROOM['deviceName'], Literal(dev['name']))) |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
124 if 'signal' in dev: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
125 g.add((uri, ROOM['signalStrength'], Literal(dev['signal']))) |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
126 try: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
127 conn = whenConnected(self.settings.mongo, dev['mac']) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
128 except ValueError: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
129 pass |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
130 else: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
131 g.add((uri, ROOM['connectedAgo'], |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
132 Literal(connectedAgoString(conn)))) |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
133 g.add((uri, ROOM['connected'], Literal(conn))) |
805 | 134 |
135 self.set_header('Content-type', 'application/x-trig') | |
136 self.write(g.asTrig()) | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
137 |
805 | 138 class Poller(object): |
139 def __init__(self, wifi, mongo): | |
140 self.wifi = wifi | |
141 self.mongo = mongo | |
142 self.lastAddrs = [] | |
143 self.lastWithSignal = [] | |
144 self.lastPollTime = 0 | |
145 | |
146 def assertCurrent(self): | |
147 dt = time.time() - self.lastPollTime | |
148 assert dt < 10, "last poll was %s sec ago" % dt | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
149 |
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
150 @inlineCallbacks |
805 | 151 def poll(self): |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
152 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
153 connectedField = 'connected' |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
154 |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
155 # UVA mode: |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
156 addDhcpData = lambda *args: None |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
157 |
805 | 158 try: |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
159 newAddrs = yield self.wifi.getPresentMacAddrs() |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
160 addDhcpData(newAddrs) |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
161 |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
162 newWithSignal = [a for a in newAddrs if a.get('connected')] |
805 | 163 |
164 actions = self.computeActions(newWithSignal) | |
165 for action in actions: | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
166 log.info("action: %s", action) |
805 | 167 action['created'] = datetime.datetime.now(tz.gettz('UTC')) |
168 mongo.save(action) | |
169 try: | |
170 self.doEntranceMusic(action) | |
171 except Exception, e: | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
172 log.error("entrancemusic error: %r", e) |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
173 |
805 | 174 self.lastWithSignal = newWithSignal |
855 | 175 if actions: # this doesn't currently include signal strength changes |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
176 fetch(reasoning + "immediateUpdate", |
857
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
177 timeout=2, |
1168b0cd90af
fix fetch calls in tomatowifi
drewp <drewp@bigasterisk.com>
parents:
856
diff
changeset
|
178 headers={'user-agent': ['tomatoWifi']}).addErrback(log.warn) |
805 | 179 self.lastAddrs = newAddrs |
180 self.lastPollTime = time.time() | |
181 except Exception, e: | |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
182 log.error("poll error: %r\n%s", e, traceback.format_exc()) |
805 | 183 |
184 def computeActions(self, newWithSignal): | |
185 actions = [] | |
186 | |
187 def makeAction(addr, act): | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
188 d = dict(sensor="wifi", |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
189 address=addr.get('mac').upper(), # mongo data is legacy uppercase |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
190 name=addr.get('name'), |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
191 networkName=addr.get('clientHostname'), |
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
192 action=act) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
193 if act == 'arrive' and 'ip' in addr: |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
194 # this won't cover the possible case that you get on |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
195 # wifi but don't have an ip yet. We'll record an |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
196 # action with no ip and then never record your ip. |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
197 d['ip'] = addr['ip'] |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
198 return d |
805 | 199 |
200 for addr in newWithSignal: | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
201 if addr['mac'] not in [r['mac'] for r in self.lastWithSignal]: |
805 | 202 actions.append(makeAction(addr, 'arrive')) |
203 | |
204 for addr in self.lastWithSignal: | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
205 if addr['mac'] not in [r['mac'] for r in newWithSignal]: |
805 | 206 actions.append(makeAction(addr, 'leave')) |
207 | |
208 return actions | |
209 | |
210 | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
211 # these need to move out to their own service |
805 | 212 def doEntranceMusic(self, action): |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
213 import restkit, jsonlib |
805 | 214 dt = self.deltaSinceLastArrive(action['name']) |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
215 log.debug("dt=%s", dt) |
805 | 216 if dt > datetime.timedelta(hours=1): |
217 hub = restkit.Resource( | |
218 # PSHB not working yet; "http://bang:9030/" | |
219 "http://slash:9049/" | |
220 ) | |
221 action = action.copy() | |
222 del action['created'] | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
223 del action['_id'] |
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
224 log.info("post to %s", hub) |
805 | 225 hub.post("visitorNet", payload=jsonlib.dumps(action)) |
226 | |
227 def deltaSinceLastArrive(self, name): | |
228 results = list(self.mongo.find({'name' : name}).sort('created', | |
229 DESCENDING).limit(1)) | |
230 if not results: | |
231 return datetime.timedelta.max | |
232 now = datetime.datetime.now(tz.gettz('UTC')) | |
233 last = results[0]['created'].replace(tzinfo=tz.gettz('UTC')) | |
234 return now - last | |
856
4b386fc51325
rewrite tomatowifi from restkit to cyclone httpclient
drewp <drewp@bigasterisk.com>
parents:
855
diff
changeset
|
235 |
805 | 236 |
237 if __name__ == '__main__': | |
238 config = { | |
239 'servePort' : 9070, | |
240 'pollFrequency' : 1/5, | |
241 } | |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
242 from twisted.python import log as twlog |
980
2f1cb8b5950a
rewrites for better graph export, removal of dhcp reader
drewp <drewp@bigasterisk.com>
parents:
967
diff
changeset
|
243 #twlog.startLogging(sys.stdout) |
806
85e50a597244
entrancemusic improve logging
drewp <drewp@bigasterisk.com>
parents:
805
diff
changeset
|
244 #log.setLevel(10) |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
245 #log.setLevel(logging.DEBUG) |
805 | 246 |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
247 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor'] |
805 | 248 |
249 wifi = Wifi() | |
250 poller = Poller(wifi, mongo) | |
251 task.LoopingCall(poller.poll).start(1/config['pollFrequency']) | |
252 | |
867
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
253 reactor.listenTCP(config['servePort'], |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
254 cyclone.web.Application( |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
255 [ |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
256 (r"/", Index), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
257 (r'/json', Json), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
258 (r'/graph', GraphHandler), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
259 (r'/table', Table), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
260 #(r'/activity', Activity), |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
261 ], |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
262 wifi=wifi, |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
263 poller=poller, |
d9bc9a82dcca
redo wifi scraper to work with zyxel router report page too. add last connected time (from mongo) to web table
drewp <drewp@bigasterisk.com>
parents:
857
diff
changeset
|
264 mongo=mongo)) |
805 | 265 reactor.run() |