-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheagle200sim.py
275 lines (213 loc) · 8.44 KB
/
eagle200sim.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# SPDX-License-Identifier: GPL-2.0-only
from flask import Flask, abort, jsonify, request
from flask_httpauth import HTTPBasicAuth
import xml.etree.ElementTree as etree
class Eagle200Sim(object):
auth = {"0077dd": "6e61a3a94882eef9"}
def process_request(self, data):
xml = etree.fromstring(data)
command_name = xml.findtext("Name")
if command_name == "device_list":
return self._device_list()
elif command_name == "device_details":
details = xml.find("DeviceDetails")
address = details.findtext("HardwareAddress")
return self._device_details(address)
elif command_name == "device_query":
details = xml.find("DeviceDetails")
address = details.findtext("HardwareAddress")
components = xml.find("Components")
all = components.find("All")
if all is not None:
if all.text == "Y":
return self._device_query(address)
else:
component = components.find("Component")
name = component.findtext("Name")
variables = component.find("Variables")
variable = variables.find("Variable")
value = variable.findtext("Name")
return self._device_query(address, name, value)
abort(400, "Fuction not implemented")
def _device_list(self):
"""
Returns the device list
Example return:
<DeviceList>
<Device>
<HardwareAddress>0x000781000081fd0b</HardwareAddress>
<Manufacturer>generic</Manufacturer>
<ModelId>electric_meter</ModelId>
<Protocol>Zigbee</Protocol>
<LastContact>0x5989f8f5</LastContact>
<ConnectionStatus>Connected</ConnectionStatus>
<NetworkAddress>0x0000</NetworkAddress>
</Device>
</DeviceList>
"""
map = {
"HardwareAddress": "0x000781000081fd0b",
"Manufacturer": "generic",
"ModelId": "electric_meter",
"Protocol": "Zigbee",
"LastContact": "0x5989f8f5",
"ConnectionStatus": "Connected",
"NetworkAddress": "0x0000",
}
root = etree.Element("DeviceList")
device = etree.SubElement(root, "Device")
for key in map:
etree.SubElement(device, key).text = map[key]
# print(etree.tostring(root).decode())
return etree.tostring(root)
def _device_details(self, address):
"""
Returns the device details for a given hardware address
address:str the hardware address of the device
Example return:
<Device>
<DeviceDetails>
<Name>Power Meter</Name>
<HardwareAddress>0x000781000081fd0b</HardwareAddress>
<Protocol>Zigbee</Protocol>
<Manufacturer>Generic</Manufacturer>
<ModelId>electric_meter</ModelId>
</DeviceDetails>
<Components>
<Component>
<Name>Main</Name>
<FixedId>0</FixedId>
<Variables>
<Variable>zigbee:InstantaneousDemand</Variable>
<Variable>zigbee:Multiplier</Variable>
<Variable>zigbee:Divisor</Variable>
<Variable>zigbee:CurrentSummationDelivered</Variable>
<Variable>zigbee:Price</Variable>
<Variable>zigbee:RateLabel</Variable>
<Variable>zigbee:Message</Variable>
</Variables>
</Component>
</Components>
</Device>
"""
details_map = {
"Name": "Power Meter",
"HardwareAddress": address,
"Protocol": "Zigbee",
"Manufacturer": "Generic",
"ModelId": "electric_meter",
}
root = etree.Element("Device")
device_details = etree.SubElement(root, "DeviceDetails")
for key in details_map:
etree.SubElement(device_details, key).text = details_map[key]
components = etree.SubElement(root, "Components")
component = etree.SubElement(components, "Component")
component_map = {"Name": "Main", "FixedId": "0"}
for key in component_map:
etree.SubElement(component, key).text = component_map[key]
variables = etree.SubElement(component, "Variables")
variable_list = [
"zigbee:InstantaneousDemand",
"zigbee:Multiplier",
"zigbee:Divisor",
"zigbee:CurrentSummationDelivered",
"zigbee:Price",
"zigbee:RateLabel",
"zigbee:Message",
]
for item in variable_list:
etree.SubElement(variables, "Variable").text = item
# print(etree.tostring(root).decode())
return etree.tostring(root)
def _device_query(self, address, name=None, value=None):
"""
Returns the device query for a given hardware address, name, and variable
address:str the hardware address of the device to query
name:str the name of the component to query
value:str the variable to query
Example return:
<Device>
<DeviceDetails>
<Name>Power Meter</Name>
<HardwareAddress>0x000781000081fd0b</HardwareAddress>
<Protocol>Zigbee</Protocol>
<Manufacturer>Generic</Manufacturer>
<ModelId>electric_meter</ModelId>
<LastContact>0x59a0b67c</LastContact>
<ConnectionStatus>Connected</ConnectionStatus>
</DeviceDetails>
<Components>
<Component>
<Name>Main</Name>
<FixedId>0</FixedId>
<Variables>
<Variable>
<Name>zigbee:InstantaneousDemand</Name>
<Value>21.499 kW</Value>
</Variable>
</Variables>
</Component>
</Components>
</Device>
"""
details_map = {
"Name": "Power Meter",
"HardwareAddress": address,
"Protocol": "Zigbee",
"Manufacturer": "Generic",
"ModelId": "electric_meter",
"LastContact": "0x59a0b67c",
"ConnectionStatus": "Connected",
}
root = etree.Element("Device")
device_details = etree.SubElement(root, "DeviceDetails")
for key in details_map:
etree.SubElement(device_details, key).text = details_map[key]
components = etree.SubElement(root, "Components")
component = etree.SubElement(components, "Component")
component_map = {"Name": "Main", "FixedId": "0"}
if name is not None and name != component_map["Name"]:
abort(400, "name '%s' doesn't exist" % name)
for key in component_map:
etree.SubElement(component, key).text = component_map[key]
variables = etree.SubElement(component, "Variables")
variable_map = {
"zigbee:InstantaneousDemand": "21.499 kW",
"zigbee:Multiplier": "",
"zigbee:Divisor": "",
"zigbee:CurrentSummationDelivered": "1000 kWh",
"zigbee:Price": "0.1 CAD",
"zigbee:RateLabel": "CAD",
"zigbee:Message": "Hello, World!",
}
if value is not None and value not in variable_map:
abort(400, "Variable '%s' doesn't exist" % value)
if value is None:
for key in variable_map:
variable = etree.Element("Variable")
etree.SubElement(variable, "Name").text = key
etree.SubElement(variable, "Value").text = variable_map[key]
variables.append(variable)
else:
variable = etree.SubElement(variables, "Variable")
etree.SubElement(variable, "Name").text = value
etree.SubElement(variable, "Value").text = variable_map[value]
# print(etree.tostring(root).decode())
return etree.tostring(root)
app = Flask(__name__)
auth = HTTPBasicAuth()
eagle = Eagle200Sim()
@auth.get_password
def get_password(username):
return eagle.auth[username]
@app.route("/cgi-bin/post_manager", methods=["POST"])
@auth.login_required
def process_request():
if request.content_type != "text/xml":
abort(415, "Content-type must be 'text/xml'")
return eagle.process_request(request.data)
def create_app():
return app
if __name__ == "__main__":
app.run(debug=True)