Mercurial > code > home > repos > homeauto
comparison service/tomatoWifi/tomatoWifi.py @ 1114:e69599f47cf0
wifi write to influxdb
Ignore-this: 3d5b0194b9db14e7e261a0852d24e42
darcs-hash:b0774c8cb9b49e99cab8f551a0c6e830d968432f
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 16 Sep 2016 01:26:54 -0700 |
parents | 3a9f20f881d1 |
children | 70954bd426be |
comparison
equal
deleted
inserted
replaced
1113:0a864b90a0b4 | 1114:e69599f47cf0 |
---|---|
18 sys.path.append("/home/drewp/projects/photo/lib/python2.7/site-packages") | 18 sys.path.append("/home/drewp/projects/photo/lib/python2.7/site-packages") |
19 from dateutil import tz | 19 from dateutil import tz |
20 from twisted.internet import reactor, task | 20 from twisted.internet import reactor, task |
21 from twisted.internet.defer import inlineCallbacks | 21 from twisted.internet.defer import inlineCallbacks |
22 import docopt | 22 import docopt |
23 | 23 from influxdb import InfluxDBClient |
24 from pymongo import Connection, DESCENDING | 24 from pymongo import Connection, DESCENDING |
25 from rdflib import Namespace, Literal, URIRef | 25 from rdflib import Namespace, Literal, URIRef |
26 sys.path.append("/my/site/magma") | 26 sys.path.append("/my/site/magma") |
27 from stategraph import StateGraph | 27 from stategraph import StateGraph |
28 from wifi import Wifi | 28 from wifi import Wifi |
150 | 150 |
151 @inlineCallbacks | 151 @inlineCallbacks |
152 def poll(self): | 152 def poll(self): |
153 | 153 |
154 connectedField = 'connected' | 154 connectedField = 'connected' |
155 now = int(time.time()) | |
155 | 156 |
156 # UVA mode: | 157 # UVA mode: |
157 addDhcpData = lambda *args: None | 158 addDhcpData = lambda *args: None |
158 | 159 |
159 try: | 160 try: |
161 addDhcpData(newAddrs) | 162 addDhcpData(newAddrs) |
162 | 163 |
163 newWithSignal = [a for a in newAddrs if a.get('connected')] | 164 newWithSignal = [a for a in newAddrs if a.get('connected')] |
164 | 165 |
165 actions = self.computeActions(newWithSignal) | 166 actions = self.computeActions(newWithSignal) |
167 points = [] | |
166 for action in actions: | 168 for action in actions: |
167 log.info("action: %s", action) | 169 log.info("action: %s", action) |
168 action['created'] = datetime.datetime.now(tz.gettz('UTC')) | 170 action['created'] = datetime.datetime.now(tz.gettz('UTC')) |
169 mongo.save(action) | 171 mongo.save(action) |
172 points.append( | |
173 self.influxPoint(now, action['address'].lower(), | |
174 1 if action['action'] == 'arrive' else 0)) | |
170 try: | 175 try: |
171 self.doEntranceMusic(action) | 176 self.doEntranceMusic(action) |
172 except Exception, e: | 177 except Exception, e: |
173 log.error("entrancemusic error: %r", e) | 178 log.error("entrancemusic error: %r", e) |
174 | 179 |
180 if now // 3600 > self.lastPollTime // 3600: | |
181 log.info('hourly writes') | |
182 for addr in newWithSignal: | |
183 points.append(self.influxPoint(now, addr['mac'].lower(), 1)) | |
184 | |
185 influx.write_points(points, time_precision='s') | |
175 self.lastWithSignal = newWithSignal | 186 self.lastWithSignal = newWithSignal |
176 if actions: # this doesn't currently include signal strength changes | 187 if actions: # this doesn't currently include signal strength changes |
177 fetch(reasoning + "immediateUpdate", | 188 fetch(reasoning + "immediateUpdate", |
178 method='PUT', | 189 method='PUT', |
179 timeout=2, | 190 timeout=2, |
180 headers={'user-agent': ['tomatoWifi']}).addErrback(log.warn) | 191 headers={'user-agent': ['tomatoWifi']}).addErrback(log.warn) |
181 self.lastAddrs = newAddrs | 192 self.lastAddrs = newAddrs |
182 self.lastPollTime = time.time() | 193 self.lastPollTime = now |
183 except Exception, e: | 194 except Exception, e: |
184 log.error("poll error: %r\n%s", e, traceback.format_exc()) | 195 log.error("poll error: %r\n%s", e, traceback.format_exc()) |
185 | 196 |
197 def influxPoint(self, now, address, value): | |
198 return { | |
199 'measurement': 'presence', | |
200 'tags': {'sensor': 'wifi', 'address': address,}, | |
201 'fields': {'value': value}, | |
202 'time': now, | |
203 } | |
204 | |
186 def computeActions(self, newWithSignal): | 205 def computeActions(self, newWithSignal): |
187 actions = [] | 206 actions = [] |
188 | 207 |
189 def makeAction(addr, act): | 208 def makeAction(addr, act): |
190 d = dict(sensor="wifi", | 209 d = dict(sensor="wifi", |
251 twlog.startLogging(sys.stdout) | 270 twlog.startLogging(sys.stdout) |
252 log.setLevel(10) | 271 log.setLevel(10) |
253 log.setLevel(logging.DEBUG) | 272 log.setLevel(logging.DEBUG) |
254 | 273 |
255 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor'] | 274 mongo = Connection('bang', 27017, tz_aware=True)['visitor']['visitor'] |
275 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') | |
256 | 276 |
257 wifi = Wifi() | 277 wifi = Wifi() |
258 poller = Poller(wifi, mongo) | 278 poller = Poller(wifi, mongo) |
259 task.LoopingCall(poller.poll).start(1/float(args['--poll'])) | 279 task.LoopingCall(poller.poll).start(1/float(args['--poll'])) |
260 | 280 |