-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
221 lines (172 loc) · 5.79 KB
/
Copy pathserver.py
File metadata and controls
221 lines (172 loc) · 5.79 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from flask import Flask, request
import datetime
import os
import paho.mqtt.client as mqtt
import threading
from pprint import pprint, pformat
import json
app = Flask(__name__)
MQTT_AVAIL_TOPIC = 'eaglepower/availability'
MQTT_DEMAND_TOPIC = 'eaglepower/demand/value'
MQTT_SUM_TOPIC = 'eaglepower/summation/value'
MQTT_PRICE_TOPIC = 'eaglepower/price/value'
client = None
client_connected = False
class AbstractDataBlock(object):
key = None
@classmethod
def fromObject(cls, o):
raise NotImplementedError()
def post(self):
raise NotImplementedError()
class DemandDataBlock(AbstractDataBlock):
KEY = 'InstantaneousDemand'
def __init__(self, demand):
# demand in kW.
super(DemandDataBlock, self).__init__()
self.demand = demand
@classmethod
def fromObject(cls, o):
"""
{"componentId": "all",
"data": {"demand": 0.717, "units": "kW"},
"dataType": "InstantaneousDemand",
"subdeviceGuid": "00078100008fa8bc",
"timestamp": "1605672440000"}
:param o:
:return:
"""
demand = o['data']['demand']
units = o['data']['units']
if units == 'kW':
value = float(demand)
elif units == 'W':
value = float(demand * 1000)
else:
raise Exception('Unknown unit for demand block. \n{}'.format(pformat(o)))
return cls(demand=value)
def post(self):
if client is None or client_connected == False:
return
client.publish(MQTT_DEMAND_TOPIC, self.demand)
class SummationDataBlock(AbstractDataBlock):
KEY = 'CurrentSummation'
def __init__(self, summation):
# float value representing kWh
super(SummationDataBlock, self).__init__()
self.summation = summation
@classmethod
def fromObject(cls, o):
"""
{"componentId": "all",
"data": {"summationDelivered": 123298.985,
"summationReceived": 0.0,
"units": "kWh"},
"dataType": "CurrentSummation",
"subdeviceGuid": "00078100008fa8bc",
"timestamp": "1605672461000"}
:param o:
:return:
"""
summation = float(o['data']['summationDelivered'])
units = o['data']['units']
if units != 'kWh':
raise Exception('Unknown unit for demand block. \n{}'.format(pformat(o)))
return cls(summation=summation)
def post(self):
if client is None or client_connected == False:
return
client.publish(MQTT_SUM_TOPIC, self.summation)
class PriceDataBlock(AbstractDataBlock):
KEY = 'Price'
def __init__(self, price):
# float price in dollars.
super(PriceDataBlock, self).__init__()
self.price = price
@classmethod
def fromObject(cls, o):
"""
{"componentId": "all",
"data": {"PriceCurrency": "USD",
"PriceDuration": 65535,
"PriceRateLabel": "Block 1",
"PriceStartTime": 1605672472,
"PriceTier": 0,
"PriceTrailingDigits": 4,
"price": 0.0935,
"units": "min"},
"dataType": "Price",
"subdeviceGuid": "00078100008fa8bc",
"timestamp": "1605672472000"}
:param o:
:return:
"""
price = float(o['data']['price'])
return cls(price=price)
def post(self):
if client is None or client_connected == False:
return
client.publish(MQTT_PRICE_TOPIC, self.price)
def processPostData(postData):
# The time from the sender clock for the data received.
timestamp = float(postData['timestamp']) / 1000
# ts = datetime.datetime.fromtimestamp(timestamp)
body = postData.get('body')
if not body:
return
DATABLOCKS = (
DemandDataBlock,
SummationDataBlock,
PriceDataBlock,
)
DATABLOCKS_MAP = {x.KEY:x for x in DATABLOCKS}
for b in body:
dt = b.get('dataType')
if dt is None:
continue
dataCls = DATABLOCKS_MAP.get(dt)
if dataCls is None:
continue
o = dataCls.fromObject(b)
o.post()
@app.route('/data', methods=['POST'])
def data():
data = request.get_json()
processPostData(data)
return ''
def iterMockData(path):
with open(path, 'r') as f:
c = f.read()
for block in c.split('\n\n'):
block = block.strip()
if block:
d = json.loads(block)
yield d
def connectMqtt():
global client
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
global client_connected
print("Connected with result code " + str(rc))
if rc == 0:
client.publish(MQTT_AVAIL_TOPIC, 'online')
client_connected = True
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client(client_id='EaglePower')
client.will_set(MQTT_AVAIL_TOPIC, 'offline')
client.on_connect = on_connect
client.on_message = on_message
client.connect(os.environ['MQTT_HOST'], int(os.environ['MQTT_PORT']), 60)
client.loop_start()
# client.loop_forever()
return client
def main():
connectMqtt()
# for d in iterMockData('dumps.txt'):
# processPostData(d)
# # return
app.run(host='0.0.0.0', port=44202)
if __name__ == '__main__':
main()