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