@@ 190-303 (lines=114) @@ | ||
187 | resp.status = falcon.HTTP_200 |
|
188 | _ = id_ |
|
189 | ||
190 | @staticmethod |
|
191 | def on_get(req, resp, id_): |
|
192 | if 'API-KEY' not in req.headers or \ |
|
193 | not isinstance(req.headers['API-KEY'], str) or \ |
|
194 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
195 | access_control(req) |
|
196 | else: |
|
197 | api_key_control(req) |
|
198 | if not id_.isdigit() or int(id_) <= 0: |
|
199 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
200 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
201 | ||
202 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
203 | cursor = cnx.cursor() |
|
204 | ||
205 | query = (" SELECT id, name, uuid " |
|
206 | " FROM tbl_meters ") |
|
207 | cursor.execute(query) |
|
208 | rows_meters = cursor.fetchall() |
|
209 | ||
210 | meter_dict = dict() |
|
211 | if rows_meters is not None and len(rows_meters) > 0: |
|
212 | for row in rows_meters: |
|
213 | meter_dict[row[2]] = {"type": 'meter', |
|
214 | "id": row[0], |
|
215 | "name": row[1], |
|
216 | "uuid": row[2]} |
|
217 | ||
218 | query = (" SELECT id, name, uuid " |
|
219 | " FROM tbl_offline_meters ") |
|
220 | cursor.execute(query) |
|
221 | rows_offline_meters = cursor.fetchall() |
|
222 | ||
223 | offline_meter_dict = dict() |
|
224 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
225 | for row in rows_offline_meters: |
|
226 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
227 | "id": row[0], |
|
228 | "name": row[1], |
|
229 | "uuid": row[2]} |
|
230 | ||
231 | query = (" SELECT id, name, uuid " |
|
232 | " FROM tbl_virtual_meters ") |
|
233 | cursor.execute(query) |
|
234 | rows_virtual_meters = cursor.fetchall() |
|
235 | ||
236 | virtual_meter_dict = dict() |
|
237 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
238 | for row in rows_virtual_meters: |
|
239 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
240 | "id": row[0], |
|
241 | "name": row[1], |
|
242 | "uuid": row[2]} |
|
243 | ||
244 | query = (" SELECT id, energy_flow_diagram_id, name " |
|
245 | " FROM tbl_energy_flow_diagrams_nodes") |
|
246 | cursor.execute(query) |
|
247 | rows_nodes = cursor.fetchall() |
|
248 | ||
249 | node_dict = dict() |
|
250 | node_list_dict = dict() |
|
251 | if rows_nodes is not None and len(rows_nodes) > 0: |
|
252 | for row in rows_nodes: |
|
253 | node_dict[row[0]] = row[2] |
|
254 | if node_list_dict.get(row[1]) is None: |
|
255 | node_list_dict[row[1]] = list() |
|
256 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
|
257 | ||
258 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
|
259 | " FROM tbl_energy_flow_diagrams_links") |
|
260 | cursor.execute(query) |
|
261 | rows_links = cursor.fetchall() |
|
262 | ||
263 | link_list_dict = dict() |
|
264 | if rows_links is not None and len(rows_links) > 0: |
|
265 | for row in rows_links: |
|
266 | # find meter by uuid |
|
267 | meter = meter_dict.get(row[4], None) |
|
268 | if meter is None: |
|
269 | meter = virtual_meter_dict.get(row[4], None) |
|
270 | if meter is None: |
|
271 | meter = offline_meter_dict.get(row[4], None) |
|
272 | ||
273 | if link_list_dict.get(row[1]) is None: |
|
274 | link_list_dict[row[1]] = list() |
|
275 | link_list_dict[row[1]].append({"id": row[0], |
|
276 | "source_node": { |
|
277 | "id": row[2], |
|
278 | "name": node_dict.get(row[2])}, |
|
279 | "target_node": { |
|
280 | "id": row[3], |
|
281 | "name": node_dict.get(row[3])}, |
|
282 | "meter": meter}) |
|
283 | ||
284 | query = (" SELECT id, name, uuid " |
|
285 | " FROM tbl_energy_flow_diagrams " |
|
286 | " WHERE id = %s ") |
|
287 | cursor.execute(query, (id_,)) |
|
288 | row = cursor.fetchone() |
|
289 | cursor.close() |
|
290 | cnx.close() |
|
291 | ||
292 | if row is None: |
|
293 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
294 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
295 | else: |
|
296 | meta_result = {"id": row[0], |
|
297 | "name": row[1], |
|
298 | "uuid": row[2], |
|
299 | "nodes": node_list_dict.get(row[0], None), |
|
300 | "links": link_list_dict.get(row[0], None), |
|
301 | } |
|
302 | ||
303 | resp.text = json.dumps(meta_result) |
|
304 | ||
305 | @staticmethod |
|
306 | @user_logger |
|
@@ 1286-1399 (lines=114) @@ | ||
1283 | resp.status = falcon.HTTP_200 |
|
1284 | _ = id_ |
|
1285 | ||
1286 | @staticmethod |
|
1287 | def on_get(req, resp, id_): |
|
1288 | if 'API-KEY' not in req.headers or \ |
|
1289 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1290 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1291 | access_control(req) |
|
1292 | else: |
|
1293 | api_key_control(req) |
|
1294 | if not id_.isdigit() or int(id_) <= 0: |
|
1295 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1296 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
1297 | ||
1298 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1299 | cursor = cnx.cursor() |
|
1300 | ||
1301 | query = (" SELECT id, name, uuid " |
|
1302 | " FROM tbl_meters ") |
|
1303 | cursor.execute(query) |
|
1304 | rows_meters = cursor.fetchall() |
|
1305 | ||
1306 | meter_dict = dict() |
|
1307 | if rows_meters is not None and len(rows_meters) > 0: |
|
1308 | for row in rows_meters: |
|
1309 | meter_dict[row[2]] = {"type": 'meter', |
|
1310 | "id": row[0], |
|
1311 | "name": row[1], |
|
1312 | "uuid": row[2]} |
|
1313 | ||
1314 | query = (" SELECT id, name, uuid " |
|
1315 | " FROM tbl_offline_meters ") |
|
1316 | cursor.execute(query) |
|
1317 | rows_offline_meters = cursor.fetchall() |
|
1318 | ||
1319 | offline_meter_dict = dict() |
|
1320 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
1321 | for row in rows_offline_meters: |
|
1322 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
1323 | "id": row[0], |
|
1324 | "name": row[1], |
|
1325 | "uuid": row[2]} |
|
1326 | ||
1327 | query = (" SELECT id, name, uuid " |
|
1328 | " FROM tbl_virtual_meters ") |
|
1329 | cursor.execute(query) |
|
1330 | rows_virtual_meters = cursor.fetchall() |
|
1331 | ||
1332 | virtual_meter_dict = dict() |
|
1333 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1334 | for row in rows_virtual_meters: |
|
1335 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1336 | "id": row[0], |
|
1337 | "name": row[1], |
|
1338 | "uuid": row[2]} |
|
1339 | ||
1340 | query = (" SELECT id, energy_flow_diagram_id, name " |
|
1341 | " FROM tbl_energy_flow_diagrams_nodes") |
|
1342 | cursor.execute(query) |
|
1343 | rows_nodes = cursor.fetchall() |
|
1344 | ||
1345 | node_dict = dict() |
|
1346 | node_list_dict = dict() |
|
1347 | if rows_nodes is not None and len(rows_nodes) > 0: |
|
1348 | for row in rows_nodes: |
|
1349 | node_dict[row[0]] = row[2] |
|
1350 | if node_list_dict.get(row[1]) is None: |
|
1351 | node_list_dict[row[1]] = list() |
|
1352 | node_list_dict[row[1]].append({"id": row[0], "name": row[2]}) |
|
1353 | ||
1354 | query = (" SELECT id, energy_flow_diagram_id, source_node_id, target_node_id, meter_uuid " |
|
1355 | " FROM tbl_energy_flow_diagrams_links") |
|
1356 | cursor.execute(query) |
|
1357 | rows_links = cursor.fetchall() |
|
1358 | ||
1359 | link_list_dict = dict() |
|
1360 | if rows_links is not None and len(rows_links) > 0: |
|
1361 | for row in rows_links: |
|
1362 | # find meter by uuid |
|
1363 | meter = meter_dict.get(row[4], None) |
|
1364 | if meter is None: |
|
1365 | meter = virtual_meter_dict.get(row[4], None) |
|
1366 | if meter is None: |
|
1367 | meter = offline_meter_dict.get(row[4], None) |
|
1368 | ||
1369 | if link_list_dict.get(row[1]) is None: |
|
1370 | link_list_dict[row[1]] = list() |
|
1371 | link_list_dict[row[1]].append({"id": row[0], |
|
1372 | "source_node": { |
|
1373 | "id": row[2], |
|
1374 | "name": node_dict.get(row[2])}, |
|
1375 | "target_node": { |
|
1376 | "id": row[3], |
|
1377 | "name": node_dict.get(row[3])}, |
|
1378 | "meter": meter}) |
|
1379 | ||
1380 | query = (" SELECT id, name, uuid " |
|
1381 | " FROM tbl_energy_flow_diagrams " |
|
1382 | " WHERE id = %s ") |
|
1383 | cursor.execute(query, (id_,)) |
|
1384 | row = cursor.fetchone() |
|
1385 | cursor.close() |
|
1386 | cnx.close() |
|
1387 | ||
1388 | if row is None: |
|
1389 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1390 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
1391 | else: |
|
1392 | meta_result = {"id": row[0], |
|
1393 | "name": row[1], |
|
1394 | "uuid": row[2], |
|
1395 | "nodes": node_list_dict.get(row[0], None), |
|
1396 | "links": link_list_dict.get(row[0], None), |
|
1397 | } |
|
1398 | ||
1399 | resp.text = json.dumps(meta_result) |
|
1400 | ||
1401 | ||
1402 | class EnergyFlowDiagramImport: |