-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplotwatt.py
More file actions
67 lines (49 loc) · 2.05 KB
/
plotwatt.py
File metadata and controls
67 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pw.plotwattapi as pw
import traceback
class dblogger:
def __init__(self, config, log):
self.log = log
self.config = config
self.log.info("Connecting to plotwatt using house_id: {} api_key: {}".format(
config.get('db','house_id'),
config.get('db', 'api_key')))
self.pw = pw.Plotwatt(config.get('db','house_id'),
config.get('db', 'api_key'))
self.meters = self.pw.list_meters()
self.batchsize = config.getint('db', 'batchsize')
if len(self.meters) == 0:
self.log.info("Creating new meter.")
self.meters.extend(pw.create_meters(1))
self.log.info("Using meter: {}".format(
self.meters[0]))
self.cache = []
def close(self):
pass
def logit(self, data):
self.cache.append (data)
if len (self.cache) >= self.batchsize:
self.log.info ("Pushing {} readings...".format(len(self.cache)))
self.log.debug ("{}, {}".format(
[x['PowerSum']/1000.0 for x in self.cache],
[x['time'] for x in self.cache]))
self.pw.push_readings(
self.meters[0],
[x['PowerSum']/1000.0 for x in self.cache],
[x['time'] for x in self.cache])
self.cache = []
#sql = """INSERT INTO wattnode (Address, EnergyA, EnergyB, PowerA,
#PowerB, VoltA, VoltB, Freq, PowerFactorA, PowerFactorB) VALUES
#(%d, %f, %f, %f,
#%f, %f, %f, %f, %f, %f)"""%\
# (data['address'], data['EnergyA'], data['EnergyB'], data['PowerA'],
# data['PowerB'], data['VoltA'], data['VoltB'],
# data['Freq'], data['PowerFactorA'], data['PowerFactorB'])
if __name__ == "__main__":
HOUSE_ID = 11672
API_KEY = "MDM0OTAyMzEwZTM0"
p = pw.Plotwatt(HOUSE_ID, API_KEY)
meters = p.list_meters()
if len (meters) == 0:
print 'creating meter'
meters.extend(p.create_meters(1))
print 'meters: ', meters