Mercurial > code > home > repos > homeauto
annotate service/powerEagle/reader.py @ 311:ec6451f15ae5
powereagle reader writes to influxdb
Ignore-this: cbd088f968244a7e869868ff82b079f0
author | drewp@bigasterisk.com |
---|---|
date | Fri, 16 Sep 2016 01:28:48 -0700 |
parents | 7a1e9ef4c8b2 |
children | 655a11cde0ab |
rev | line source |
---|---|
211 | 1 #!bin/python |
2 import json, logging, time | |
3 import sys | |
4 sys.path.append("/my/proj/homeauto/lib") | |
5 from logsetup import log | |
6 from twisted.internet.defer import inlineCallbacks | |
7 from twisted.internet import reactor | |
8 from cyclone.httpclient import fetch | |
311 | 9 from influxdb import InfluxDBClient |
211 | 10 |
11 from private_config import deviceIp, cloudId, installId, macId, periodSec | |
12 | |
13 auth = (cloudId + ':' + installId).encode('base64').strip() | |
311 | 14 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') |
211 | 15 |
16 class Poller(object): | |
17 def __init__(self, carbon): | |
18 self.carbon = carbon | |
212
a7dd996617ef
LoopingCall sets the interview between calls, but I want the period of calls
drewp@bigasterisk.com
parents:
211
diff
changeset
|
19 reactor.callLater(0, self.poll) |
211 | 20 |
21 @inlineCallbacks | |
22 def poll(self): | |
23 ret = None | |
212
a7dd996617ef
LoopingCall sets the interview between calls, but I want the period of calls
drewp@bigasterisk.com
parents:
211
diff
changeset
|
24 startTime = time.time() |
211 | 25 try: |
26 resp = yield fetch( | |
27 'http://{deviceIp}/cgi-bin/cgi_manager'.format(deviceIp=deviceIp), | |
28 method='POST', | |
29 headers={'Authorization': ['Basic %s' % auth]}, | |
30 postdata='''<LocalCommand> | |
31 <Name>get_usage_data</Name> | |
32 <MacId>0x{macId}</MacId> | |
33 </LocalCommand> | |
34 <LocalCommand> | |
35 <Name>get_price_blocks</Name> | |
36 <MacId>0x{macId}</MacId> | |
37 </LocalCommand>'''.format(macId=macId)) | |
38 ret = json.loads(resp.body) | |
39 if ret['demand_units'] != 'kW': | |
40 raise ValueError | |
41 if ret['summation_units'] != 'kWh': | |
42 raise ValueError | |
311 | 43 influx.write_points([ |
44 dict(measurement='housePowerW', | |
45 fields=dict(value=float(ret['demand']) * 1000), | |
46 tags=dict(house='berkeley'), | |
47 time=int(startTime)), | |
48 dict(measurement='housePowerSumDeliveredKwh', | |
49 fields=dict(value=float(ret['summation_delivered'])), | |
50 tags=dict(house='berkeley'), | |
51 time=int(startTime)), | |
52 ], time_precision='s') | |
211 | 53 except Exception as e: |
54 log.error("failed: %r", e) | |
55 log.error(repr(ret)) | |
56 | |
212
a7dd996617ef
LoopingCall sets the interview between calls, but I want the period of calls
drewp@bigasterisk.com
parents:
211
diff
changeset
|
57 now = time.time() |
216 | 58 goal = startTime + periodSec - .2 |
212
a7dd996617ef
LoopingCall sets the interview between calls, but I want the period of calls
drewp@bigasterisk.com
parents:
211
diff
changeset
|
59 reactor.callLater(max(1, goal - now), self.poll) |
211 | 60 |
61 | |
62 log.setLevel(logging.INFO) | |
311 | 63 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') |
64 | |
65 p = Poller(influx) | |
211 | 66 reactor.run() |