Mercurial > code > home > repos > homeauto
comparison service/powerEagle/reader.py @ 416:655a11cde0ab
add request timeout. don't send NaN to influxdb. crash on failures.
Ignore-this: 7c5303376024a46e3cdc6aaf70cf4da8
author | drewp@bigasterisk.com |
---|---|
date | Tue, 26 Mar 2019 09:12:30 -0700 |
parents | ec6451f15ae5 |
children | 71aa55cd8433 |
comparison
equal
deleted
inserted
replaced
415:91b4a04a33e7 | 416:655a11cde0ab |
---|---|
1 #!bin/python | 1 #!bin/python |
2 import json, logging, time | 2 import json, logging, time, os |
3 import sys | 3 import sys |
4 sys.path.append("/my/proj/homeauto/lib") | 4 sys.path.append("/my/proj/homeauto/lib") |
5 from logsetup import log | 5 from logsetup import log |
6 from twisted.internet.defer import inlineCallbacks | 6 from twisted.internet.defer import inlineCallbacks |
7 from twisted.internet import reactor | 7 from twisted.internet import reactor |
32 <MacId>0x{macId}</MacId> | 32 <MacId>0x{macId}</MacId> |
33 </LocalCommand> | 33 </LocalCommand> |
34 <LocalCommand> | 34 <LocalCommand> |
35 <Name>get_price_blocks</Name> | 35 <Name>get_price_blocks</Name> |
36 <MacId>0x{macId}</MacId> | 36 <MacId>0x{macId}</MacId> |
37 </LocalCommand>'''.format(macId=macId)) | 37 </LocalCommand>'''.format(macId=macId), |
38 timeout=10) | |
38 ret = json.loads(resp.body) | 39 ret = json.loads(resp.body) |
39 if ret['demand_units'] != 'kW': | 40 if ret['demand_units'] != 'kW': |
40 raise ValueError | 41 raise ValueError |
41 if ret['summation_units'] != 'kWh': | 42 if ret['summation_units'] != 'kWh': |
42 raise ValueError | 43 raise ValueError |
43 influx.write_points([ | 44 pts = [ |
44 dict(measurement='housePowerW', | 45 dict(measurement='housePowerW', |
45 fields=dict(value=float(ret['demand']) * 1000), | 46 fields=dict(value=float(ret['demand']) * 1000), |
46 tags=dict(house='berkeley'), | 47 tags=dict(house='berkeley'), |
47 time=int(startTime)), | 48 time=int(startTime))] |
48 dict(measurement='housePowerSumDeliveredKwh', | 49 sd = float(ret['summation_delivered']) |
49 fields=dict(value=float(ret['summation_delivered'])), | 50 if sd > 0: # Sometimes nan |
51 pts.append(dict(measurement='housePowerSumDeliveredKwh', | |
52 fields=dict(value=float()), | |
50 tags=dict(house='berkeley'), | 53 tags=dict(house='berkeley'), |
51 time=int(startTime)), | 54 time=int(startTime))) |
52 ], time_precision='s') | 55 |
56 influx.write_points(pts, time_precision='s') | |
53 except Exception as e: | 57 except Exception as e: |
54 log.error("failed: %r", e) | 58 log.error("failed: %r", e) |
55 log.error(repr(ret)) | 59 log.error(repr(ret)) |
60 os.abort() | |
56 | 61 |
57 now = time.time() | 62 now = time.time() |
58 goal = startTime + periodSec - .2 | 63 goal = startTime + periodSec - .2 |
59 reactor.callLater(max(1, goal - now), self.poll) | 64 reactor.callLater(max(1, goal - now), self.poll) |
60 | 65 |