forked from DaveBerkeley/elster_meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pachube_post.py
executable file
·206 lines (153 loc) · 3.96 KB
/
pachube_post.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
import time
import datetime
import os
import sys
import optparse
import httplib
import json
#
# Post data to Cosm feed
class Cosm:
host = 'api.cosm.com'
agent = "python cosm 1.0"
def __init__(self, feed, key, https=True):
self.headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"X-ApiKey" : key,
"User-Agent" : self.agent,
}
self.params = "/v2/feeds/" + str(feed)
if https:
self.Connection = httplib.HTTPSConnection
else:
self.Connection = httplib.HTTPConnection
def put(self, info):
items = []
for key, value in info:
items.append( { "id" : key, "current_value" : value } )
data = {
"version" : "1.0.0",
"datastreams": items,
}
body = json.dumps(data)
http = self.Connection(self.host)
http.request("PUT", self.params, body, self.headers)
response = http.getresponse()
http.close()
return response.status, response.reason
#
#
def make_path():
pathfmt = "/usr/local/data/solar/%s/%02d/%02d.log"
now = datetime.datetime.now()
path = pathfmt % (now.year, now.month, now.day)
return path
def parse_line(text):
# read text in the form "HH:MM:SS wwwww"
hms, w = text.strip().split()
hms = hms.split(":")
h, m, s = [ int(x,10) for x in hms ]
secs = (h * 3600) + (m * 60) + s
watts = float(int(w, 10))
return secs, watts * 3600
#
# Moving Average Filter
class Filter:
def __init__(self, ntaps):
self.ntaps = ntaps
self.data = None
def filter(self, data):
if self.data is None:
self.data = [ data, ] * self.ntaps
self.data = self.data[1:] + [ data ]
return int(sum(self.data) / float(self.ntaps))
#
#
class NoData(Exception):
pass
def read_line(f, fn=None):
fx = fn
if fx is None:
fx = f.read
line = fx()
if not line:
raise NoData()
print `line`
t, w = parse_line(line)
return t, w
#
#
def put_data(cosm, p, today_kwh, kwh):
info = [
("0", p),
("1", today_kwh),
("2", kWh),
]
if opts.test:
print info
return
try:
cosm.put(info)
except Exception, ex:
print "Error", str(ex)
#
#
FEED = <your feed here>
API_KEY = '<your key here>'
p = optparse.OptionParser()
p.add_option("--test", "-t", dest="test", action="store_true")
opts, args = p.parse_args()
this_path = None
last_w = None
last_t = None
first_w = None
lpf = Filter(3)
cosm = Cosm(FEED, API_KEY)
while True:
try:
sys.stdout.flush()
time.sleep(10)
path = make_path()
if path != this_path:
if not os.path.exists(path):
continue
print "open path", path
f = open(path, "r")
# get the first line
try:
t, first_w = read_line(f, f.readline)
except Exception, ex:
print "Error", str(ex)
continue
f.seek(0, 2) # seek to end
this_path = path
try:
t, w = read_line(f)
except NoData, ex:
continue
except Exception, ex:
print "Error", str(ex)
continue
if w == last_w:
continue
if last_w is None:
last_w = w
last_t = t
continue
dt = t - last_t
if not dt:
continue
dw = w - last_w
power = dw / dt
last_t = t
last_w = w
p = lpf.filter(power)
kWh = w / (3600 * 1000.0)
today_kwh = (w - first_w) / (3600 * 1000.0)
print p, today_kwh, kWh
put_data(cosm, p, today_kwh, kWh)
except Exception, ex:
# track down that error!
print "Error", str(ex)
# FIN