comparison service/wifi/wifi.py @ 1461:517cbb905d4c

remote suspend, wifi group arg Ignore-this: 76cd196b934298df35125bd180ede9c6 darcs-hash:1893e27489c5537950d377dfde761497f906df6c
author drewp <drewp@bigasterisk.com>
date Sat, 23 Nov 2019 12:02:32 -0800
parents 42f4fed9cd25
children 32d134dbfb1e
comparison
equal deleted inserted replaced
1459:6c6b14c39cd2 1461:517cbb905d4c
37 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): 37 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
38 def get(self): 38 def get(self):
39 age = time.time() - self.settings.poller.lastPollTime 39 age = time.time() - self.settings.poller.lastPollTime
40 if age > 10: 40 if age > 10:
41 raise ValueError("poll data is stale. age=%s" % age) 41 raise ValueError("poll data is stale. age=%s" % age)
42 42
43 self.set_header("Content-Type", "text/html") 43 self.set_header("Content-Type", "text/html")
44 self.write(open("index.html").read()) 44 self.write(open("index.html").read())
45 45
46 def whenConnected(mongo, macThatIsNowConnected): 46 def whenConnected(mongo, macThatIsNowConnected):
47 lastArrive = None 47 lastArrive = None
57 57
58 return lastArrive['created'] 58 return lastArrive['created']
59 59
60 def connectedAgoString(conn): 60 def connectedAgoString(conn):
61 return ago.human(conn.astimezone(tz.tzutc()).replace(tzinfo=None)) 61 return ago.human(conn.astimezone(tz.tzutc()).replace(tzinfo=None))
62 62
63 class Table(PrettyErrorHandler, cyclone.web.RequestHandler): 63 class Table(PrettyErrorHandler, cyclone.web.RequestHandler):
64 def get(self): 64 def get(self):
65 def rowDict(row): 65 def rowDict(row):
66 row['cls'] = "signal" if row.get('connected') else "nosignal" 66 row['cls'] = "signal" if row.get('connected') else "nosignal"
67 if 'name' not in row: 67 if 'name' not in row:
105 def assertCurrent(self): 105 def assertCurrent(self):
106 dt = time.time() - self.lastPollTime 106 dt = time.time() - self.lastPollTime
107 assert dt < 10, "last poll was %s sec ago" % dt 107 assert dt < 10, "last poll was %s sec ago" % dt
108 108
109 @inlineCallbacks 109 @inlineCallbacks
110 def poll(self): 110 def poll(self):
111 try: 111 try:
112 newAddrs = yield self.wifi.getPresentMacAddrs() 112 newAddrs = yield self.wifi.getPresentMacAddrs()
113 self.onNodes(newAddrs) 113 self.onNodes(newAddrs)
114 except Exception as e: 114 except Exception as e:
115 log.error("poll error: %r\n%s", e, traceback.format_exc()) 115 log.error("poll error: %r\n%s", e, traceback.format_exc())
144 'measurement': 'presence', 144 'measurement': 'presence',
145 'tags': {'sensor': 'wifi', 'address': address,}, 145 'tags': {'sensor': 'wifi', 'address': address,},
146 'fields': {'value': value}, 146 'fields': {'value': value},
147 'time': now, 147 'time': now,
148 } 148 }
149 149
150 def computeActions(self, newWithSignal): 150 def computeActions(self, newWithSignal):
151 actions = [] 151 actions = []
152 152
153 def makeAction(addr: SeenNode, act: str): 153 def makeAction(addr: SeenNode, act: str):
154 d = dict(sensor="wifi", 154 d = dict(sensor="wifi",
157 if act == 'arrive': 157 if act == 'arrive':
158 # this won't cover the possible case that you get on 158 # this won't cover the possible case that you get on
159 # wifi but don't have an ip yet. We'll record an 159 # wifi but don't have an ip yet. We'll record an
160 # action with no ip and then never record your ip. 160 # action with no ip and then never record your ip.
161 d['ip'] = addr.ip 161 d['ip'] = addr.ip
162 return d 162 return d
163 163
164 for addr in newWithSignal: 164 for addr in newWithSignal:
165 if addr.mac not in [r.mac for r in self.lastWithSignal]: 165 if addr.mac not in [r.mac for r in self.lastWithSignal]:
166 actions.append(makeAction(addr, 'arrive')) 166 actions.append(makeAction(addr, 'arrive'))
167 167
210 g.add((dev.uri, ROOM['connectedAgo'], 210 g.add((dev.uri, ROOM['connectedAgo'],
211 Literal(connectedAgoString(conn)), ctx)) 211 Literal(connectedAgoString(conn)), ctx))
212 g.add((dev.uri, ROOM['connected'], Literal(conn), ctx)) 212 g.add((dev.uri, ROOM['connected'], Literal(conn), ctx))
213 masterGraph.setToGraph(g) 213 masterGraph.setToGraph(g)
214 214
215 class RemoteSuspend(PrettyErrorHandler, cyclone.web.RequestHandler):
216 def post(self):
217 # windows is running shutter (https://www.den4b.com/products/shutter)
218 fetch('http://DESKTOP-GOU4AC4:8011/action', postdata={'id': 'Sleep'})
219
215 220
216 if __name__ == '__main__': 221 if __name__ == '__main__':
217 args = docopt.docopt(''' 222 args = docopt.docopt('''
218 Usage: 223 Usage:
219 wifi.py [options] 224 wifi.py [options]
232 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor'] 237 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor']
233 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') 238 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main')
234 239
235 config = ConjunctiveGraph() 240 config = ConjunctiveGraph()
236 config.parse(open('private_config.n3'), format='n3') 241 config.parse(open('private_config.n3'), format='n3')
237 242
238 masterGraph = PatchableGraph() 243 masterGraph = PatchableGraph()
239 wifi = Wifi(config) 244 wifi = Wifi(config)
240 poller = Poller(wifi, mongo) 245 poller = Poller(wifi, mongo)
241 task.LoopingCall(poller.poll).start(1/float(args['--poll'])) 246 task.LoopingCall(poller.poll).start(1/float(args['--poll']))
242 247
243 reactor.listenTCP( 248 reactor.listenTCP(
244 int(args['--port']), 249 int(args['--port']),
245 cyclone.web.Application( 250 cyclone.web.Application(
246 [ 251 [
247 (r"/", Index), 252 (r"/", Index),
253 (r"/(wifi\.js)", cyclone.web.StaticFileHandler, {"path": '.'}),
248 (r'/json', Json), 254 (r'/json', Json),
249 (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}), 255 (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}),
250 (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), 256 (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}),
251 (r'/table', Table), 257 (r'/table', Table),
258 (r'/remoteSuspend', RemoteSuspend),
252 #(r'/activity', Activity), 259 #(r'/activity', Activity),
253 ], 260 ],
254 wifi=wifi, 261 wifi=wifi,
255 poller=poller, 262 poller=poller,
256 mongo=mongo)) 263 mongo=mongo))