@@ 1116-1508 (lines=393) @@ | ||
1113 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
|
1114 | ||
1115 | ||
1116 | class CombinedEquipmentParameterItem: |
|
1117 | def __init__(self): |
|
1118 | """"Initializes CombinedEquipmentParameterItem""" |
|
1119 | pass |
|
1120 | ||
1121 | @staticmethod |
|
1122 | def on_options(req, resp, id_, pid): |
|
1123 | _=req |
|
1124 | resp.status = falcon.HTTP_200 |
|
1125 | _=id_ |
|
1126 | @staticmethod |
|
1127 | def on_get(req, resp, id_, pid): |
|
1128 | if 'API-KEY' not in req.headers or \ |
|
1129 | not isinstance(req.headers['API-KEY'], str) or \ |
|
1130 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
1131 | access_control(req) |
|
1132 | else: |
|
1133 | api_key_control(req) |
|
1134 | if not id_.isdigit() or int(id_) <= 0: |
|
1135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1136 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1137 | ||
1138 | if not pid.isdigit() or int(pid) <= 0: |
|
1139 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1140 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
1141 | ||
1142 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1143 | cursor = cnx.cursor() |
|
1144 | ||
1145 | query = (" SELECT id, name " |
|
1146 | " FROM tbl_points ") |
|
1147 | cursor.execute(query) |
|
1148 | rows_points = cursor.fetchall() |
|
1149 | ||
1150 | point_dict = dict() |
|
1151 | if rows_points is not None and len(rows_points) > 0: |
|
1152 | for row in rows_points: |
|
1153 | point_dict[row[0]] = {"id": row[0], |
|
1154 | "name": row[1]} |
|
1155 | ||
1156 | query = (" SELECT id, name, uuid " |
|
1157 | " FROM tbl_meters ") |
|
1158 | cursor.execute(query) |
|
1159 | rows_meters = cursor.fetchall() |
|
1160 | ||
1161 | meter_dict = dict() |
|
1162 | if rows_meters is not None and len(rows_meters) > 0: |
|
1163 | for row in rows_meters: |
|
1164 | meter_dict[row[2]] = {"type": 'meter', |
|
1165 | "id": row[0], |
|
1166 | "name": row[1], |
|
1167 | "uuid": row[2]} |
|
1168 | ||
1169 | query = (" SELECT id, name, uuid " |
|
1170 | " FROM tbl_offline_meters ") |
|
1171 | cursor.execute(query) |
|
1172 | rows_offline_meters = cursor.fetchall() |
|
1173 | ||
1174 | offline_meter_dict = dict() |
|
1175 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
1176 | for row in rows_offline_meters: |
|
1177 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
1178 | "id": row[0], |
|
1179 | "name": row[1], |
|
1180 | "uuid": row[2]} |
|
1181 | ||
1182 | query = (" SELECT id, name, uuid " |
|
1183 | " FROM tbl_virtual_meters ") |
|
1184 | cursor.execute(query) |
|
1185 | rows_virtual_meters = cursor.fetchall() |
|
1186 | ||
1187 | virtual_meter_dict = dict() |
|
1188 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1189 | for row in rows_virtual_meters: |
|
1190 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1191 | "id": row[0], |
|
1192 | "name": row[1], |
|
1193 | "uuid": row[2]} |
|
1194 | ||
1195 | query = (" SELECT id, name, parameter_type, " |
|
1196 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
1197 | " FROM tbl_combined_equipments_parameters " |
|
1198 | " WHERE combined_equipment_id = %s AND id = %s ") |
|
1199 | cursor.execute(query, (id_, pid)) |
|
1200 | row = cursor.fetchone() |
|
1201 | cursor.close() |
|
1202 | cnx.close() |
|
1203 | ||
1204 | if row is None: |
|
1205 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1206 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1207 | else: |
|
1208 | constant = None |
|
1209 | point = None |
|
1210 | numerator_meter = None |
|
1211 | denominator_meter = None |
|
1212 | if row[2] == 'point': |
|
1213 | point = point_dict.get(row[4], None) |
|
1214 | constant = None |
|
1215 | numerator_meter = None |
|
1216 | denominator_meter = None |
|
1217 | elif row[2] == 'constant': |
|
1218 | constant = row[3] |
|
1219 | point = None |
|
1220 | numerator_meter = None |
|
1221 | denominator_meter = None |
|
1222 | elif row[2] == 'fraction': |
|
1223 | constant = None |
|
1224 | point = None |
|
1225 | # find numerator meter by uuid |
|
1226 | numerator_meter = meter_dict.get(row[5], None) |
|
1227 | if numerator_meter is None: |
|
1228 | numerator_meter = virtual_meter_dict.get(row[5], None) |
|
1229 | if numerator_meter is None: |
|
1230 | numerator_meter = offline_meter_dict.get(row[5], None) |
|
1231 | # find denominator meter by uuid |
|
1232 | denominator_meter = meter_dict.get(row[6], None) |
|
1233 | if denominator_meter is None: |
|
1234 | denominator_meter = virtual_meter_dict.get(row[6], None) |
|
1235 | if denominator_meter is None: |
|
1236 | denominator_meter = offline_meter_dict.get(row[6], None) |
|
1237 | ||
1238 | meta_result = {"id": row[0], |
|
1239 | "name": row[1], |
|
1240 | "parameter_type": row[2], |
|
1241 | "constant": constant, |
|
1242 | "point": point, |
|
1243 | "numerator_meter": numerator_meter, |
|
1244 | "denominator_meter": denominator_meter} |
|
1245 | ||
1246 | resp.text = json.dumps(meta_result) |
|
1247 | ||
1248 | @staticmethod |
|
1249 | @user_logger |
|
1250 | def on_delete(req, resp, id_, pid): |
|
1251 | admin_control(req) |
|
1252 | if not id_.isdigit() or int(id_) <= 0: |
|
1253 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1254 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1255 | ||
1256 | if not pid.isdigit() or int(pid) <= 0: |
|
1257 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1258 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
1259 | ||
1260 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1261 | cursor = cnx.cursor() |
|
1262 | ||
1263 | cursor.execute(" SELECT name " |
|
1264 | " FROM tbl_combined_equipments " |
|
1265 | " WHERE id = %s ", |
|
1266 | (id_,)) |
|
1267 | row = cursor.fetchone() |
|
1268 | if row is None: |
|
1269 | cursor.close() |
|
1270 | cnx.close() |
|
1271 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1272 | title='API.NOT_FOUND', |
|
1273 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1274 | ||
1275 | cursor.execute(" SELECT name " |
|
1276 | " FROM tbl_combined_equipments_parameters " |
|
1277 | " WHERE combined_equipment_id = %s AND id = %s ", |
|
1278 | (id_, pid,)) |
|
1279 | row = cursor.fetchone() |
|
1280 | if row is None: |
|
1281 | cursor.close() |
|
1282 | cnx.close() |
|
1283 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1284 | title='API.NOT_FOUND', |
|
1285 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1286 | ||
1287 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
|
1288 | " WHERE id = %s ", (pid,)) |
|
1289 | cnx.commit() |
|
1290 | ||
1291 | cursor.close() |
|
1292 | cnx.close() |
|
1293 | ||
1294 | resp.status = falcon.HTTP_204 |
|
1295 | ||
1296 | @staticmethod |
|
1297 | @user_logger |
|
1298 | def on_put(req, resp, id_, pid): |
|
1299 | """Handles PUT requests""" |
|
1300 | admin_control(req) |
|
1301 | if not id_.isdigit() or int(id_) <= 0: |
|
1302 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1303 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
1304 | ||
1305 | if not pid.isdigit() or int(pid) <= 0: |
|
1306 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1307 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
1308 | ||
1309 | try: |
|
1310 | raw_json = req.stream.read().decode('utf-8') |
|
1311 | except Exception as ex: |
|
1312 | print(ex) |
|
1313 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1314 | title='API.BAD_REQUEST', |
|
1315 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1316 | ||
1317 | new_values = json.loads(raw_json) |
|
1318 | ||
1319 | if 'name' not in new_values['data'].keys() or \ |
|
1320 | not isinstance(new_values['data']['name'], str) or \ |
|
1321 | len(str.strip(new_values['data']['name'])) == 0: |
|
1322 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1323 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
|
1324 | name = str.strip(new_values['data']['name']) |
|
1325 | ||
1326 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
1327 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
1328 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
1329 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1330 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
1331 | ||
1332 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
1333 | ||
1334 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
1335 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1336 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
1337 | ||
1338 | constant = None |
|
1339 | if 'constant' in new_values['data'].keys(): |
|
1340 | if new_values['data']['constant'] is not None and \ |
|
1341 | isinstance(new_values['data']['constant'], str) and \ |
|
1342 | len(str.strip(new_values['data']['constant'])) > 0: |
|
1343 | constant = str.strip(new_values['data']['constant']) |
|
1344 | ||
1345 | point_id = None |
|
1346 | if 'point_id' in new_values['data'].keys(): |
|
1347 | if new_values['data']['point_id'] is not None and \ |
|
1348 | new_values['data']['point_id'] <= 0: |
|
1349 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1350 | description='API.INVALID_POINT_ID') |
|
1351 | point_id = new_values['data']['point_id'] |
|
1352 | ||
1353 | numerator_meter_uuid = None |
|
1354 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
1355 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
1356 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
1357 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
1358 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
1359 | ||
1360 | denominator_meter_uuid = None |
|
1361 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
1362 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
1363 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
1364 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
1365 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
1366 | ||
1367 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1368 | cursor = cnx.cursor() |
|
1369 | ||
1370 | cursor.execute(" SELECT name " |
|
1371 | " FROM tbl_combined_equipments " |
|
1372 | " WHERE id = %s ", (id_,)) |
|
1373 | if cursor.fetchone() is None: |
|
1374 | cursor.close() |
|
1375 | cnx.close() |
|
1376 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
|
1377 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
1378 | ||
1379 | cursor.execute(" SELECT name " |
|
1380 | " FROM tbl_combined_equipments_parameters " |
|
1381 | " WHERE combined_equipment_id = %s AND id = %s ", |
|
1382 | (id_, pid,)) |
|
1383 | row = cursor.fetchone() |
|
1384 | if row is None: |
|
1385 | cursor.close() |
|
1386 | cnx.close() |
|
1387 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1388 | title='API.NOT_FOUND', |
|
1389 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1390 | ||
1391 | cursor.execute(" SELECT name " |
|
1392 | " FROM tbl_combined_equipments_parameters " |
|
1393 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
|
1394 | row = cursor.fetchone() |
|
1395 | if row is not None: |
|
1396 | cursor.close() |
|
1397 | cnx.close() |
|
1398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1399 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
1400 | ||
1401 | # validate by parameter type |
|
1402 | if parameter_type == 'point': |
|
1403 | if point_id is None: |
|
1404 | cursor.close() |
|
1405 | cnx.close() |
|
1406 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1407 | description='API.INVALID_POINT_ID') |
|
1408 | ||
1409 | query = (" SELECT id, name " |
|
1410 | " FROM tbl_points " |
|
1411 | " WHERE id = %s ") |
|
1412 | cursor.execute(query, (point_id,)) |
|
1413 | row = cursor.fetchone() |
|
1414 | if row is None: |
|
1415 | cursor.close() |
|
1416 | cnx.close() |
|
1417 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1418 | description='API.POINT_NOT_FOUND') |
|
1419 | ||
1420 | elif parameter_type == 'constant': |
|
1421 | if constant is None: |
|
1422 | cursor.close() |
|
1423 | cnx.close() |
|
1424 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1425 | description='API.INVALID_CONSTANT_VALUE') |
|
1426 | ||
1427 | elif parameter_type == 'fraction': |
|
1428 | ||
1429 | query = (" SELECT id, name, uuid " |
|
1430 | " FROM tbl_meters ") |
|
1431 | cursor.execute(query) |
|
1432 | rows_meters = cursor.fetchall() |
|
1433 | ||
1434 | meter_dict = dict() |
|
1435 | if rows_meters is not None and len(rows_meters) > 0: |
|
1436 | for row in rows_meters: |
|
1437 | meter_dict[row[2]] = {"type": 'meter', |
|
1438 | "id": row[0], |
|
1439 | "name": row[1], |
|
1440 | "uuid": row[2]} |
|
1441 | ||
1442 | query = (" SELECT id, name, uuid " |
|
1443 | " FROM tbl_offline_meters ") |
|
1444 | cursor.execute(query) |
|
1445 | rows_offline_meters = cursor.fetchall() |
|
1446 | ||
1447 | offline_meter_dict = dict() |
|
1448 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
1449 | for row in rows_offline_meters: |
|
1450 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
1451 | "id": row[0], |
|
1452 | "name": row[1], |
|
1453 | "uuid": row[2]} |
|
1454 | ||
1455 | query = (" SELECT id, name, uuid " |
|
1456 | " FROM tbl_virtual_meters ") |
|
1457 | cursor.execute(query) |
|
1458 | rows_virtual_meters = cursor.fetchall() |
|
1459 | ||
1460 | virtual_meter_dict = dict() |
|
1461 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1462 | for row in rows_virtual_meters: |
|
1463 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1464 | "id": row[0], |
|
1465 | "name": row[1], |
|
1466 | "uuid": row[2]} |
|
1467 | ||
1468 | # validate numerator meter uuid |
|
1469 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
1470 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
1471 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
1472 | cursor.close() |
|
1473 | cnx.close() |
|
1474 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1475 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
1476 | ||
1477 | # validate denominator meter uuid |
|
1478 | if denominator_meter_uuid == numerator_meter_uuid: |
|
1479 | cursor.close() |
|
1480 | cnx.close() |
|
1481 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1482 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1483 | ||
1484 | if denominator_meter_uuid not in meter_dict and \ |
|
1485 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
1486 | denominator_meter_uuid not in offline_meter_dict: |
|
1487 | cursor.close() |
|
1488 | cnx.close() |
|
1489 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1490 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1491 | ||
1492 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
|
1493 | " SET name = %s , parameter_type = %s, constant = %s, " |
|
1494 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
|
1495 | " WHERE id = %s ") |
|
1496 | cursor.execute(add_values, (name, |
|
1497 | parameter_type, |
|
1498 | constant, |
|
1499 | point_id, |
|
1500 | numerator_meter_uuid, |
|
1501 | denominator_meter_uuid, |
|
1502 | pid)) |
|
1503 | cnx.commit() |
|
1504 | ||
1505 | cursor.close() |
|
1506 | cnx.close() |
|
1507 | ||
1508 | resp.status = falcon.HTTP_200 |
|
1509 | ||
1510 | ||
1511 | class CombinedEquipmentMeterCollection: |
@@ 936-1318 (lines=383) @@ | ||
933 | resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) |
|
934 | ||
935 | ||
936 | class EquipmentParameterItem: |
|
937 | @staticmethod |
|
938 | @user_logger |
|
939 | def __init__(): |
|
940 | """Initializes EquipmentParameterItem""" |
|
941 | pass |
|
942 | ||
943 | @staticmethod |
|
944 | def on_options(req, resp, id_, pid): |
|
945 | resp.status = falcon.HTTP_200 |
|
946 | ||
947 | @staticmethod |
|
948 | def on_get(req, resp, id_, pid): |
|
949 | if 'API-KEY' not in req.headers or \ |
|
950 | not isinstance(req.headers['API-KEY'], str) or \ |
|
951 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
952 | access_control(req) |
|
953 | else: |
|
954 | api_key_control(req) |
|
955 | if not id_.isdigit() or int(id_) <= 0: |
|
956 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
957 | description='API.INVALID_EQUIPMENT_ID') |
|
958 | ||
959 | if not pid.isdigit() or int(pid) <= 0: |
|
960 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
961 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
962 | ||
963 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
964 | cursor = cnx.cursor() |
|
965 | ||
966 | query = (" SELECT id, name " |
|
967 | " FROM tbl_points ") |
|
968 | cursor.execute(query) |
|
969 | rows_points = cursor.fetchall() |
|
970 | ||
971 | point_dict = dict() |
|
972 | if rows_points is not None and len(rows_points) > 0: |
|
973 | for row in rows_points: |
|
974 | point_dict[row[0]] = {"id": row[0], |
|
975 | "name": row[1]} |
|
976 | ||
977 | query = (" SELECT id, name, uuid " |
|
978 | " FROM tbl_meters ") |
|
979 | cursor.execute(query) |
|
980 | rows_meters = cursor.fetchall() |
|
981 | ||
982 | meter_dict = dict() |
|
983 | if rows_meters is not None and len(rows_meters) > 0: |
|
984 | for row in rows_meters: |
|
985 | meter_dict[row[2]] = {"type": 'meter', |
|
986 | "id": row[0], |
|
987 | "name": row[1], |
|
988 | "uuid": row[2]} |
|
989 | ||
990 | query = (" SELECT id, name, uuid " |
|
991 | " FROM tbl_offline_meters ") |
|
992 | cursor.execute(query) |
|
993 | rows_offline_meters = cursor.fetchall() |
|
994 | ||
995 | offline_meter_dict = dict() |
|
996 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
997 | for row in rows_offline_meters: |
|
998 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
999 | "id": row[0], |
|
1000 | "name": row[1], |
|
1001 | "uuid": row[2]} |
|
1002 | ||
1003 | query = (" SELECT id, name, uuid " |
|
1004 | " FROM tbl_virtual_meters ") |
|
1005 | cursor.execute(query) |
|
1006 | rows_virtual_meters = cursor.fetchall() |
|
1007 | ||
1008 | virtual_meter_dict = dict() |
|
1009 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1010 | for row in rows_virtual_meters: |
|
1011 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1012 | "id": row[0], |
|
1013 | "name": row[1], |
|
1014 | "uuid": row[2]} |
|
1015 | ||
1016 | query = (" SELECT id, name, parameter_type, " |
|
1017 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
1018 | " FROM tbl_equipments_parameters " |
|
1019 | " WHERE equipment_id = %s AND id = %s ") |
|
1020 | cursor.execute(query, (id_, pid)) |
|
1021 | row = cursor.fetchone() |
|
1022 | cursor.close() |
|
1023 | cnx.close() |
|
1024 | ||
1025 | if row is None: |
|
1026 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
1027 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1028 | else: |
|
1029 | constant = None |
|
1030 | point = None |
|
1031 | numerator_meter = None |
|
1032 | denominator_meter = None |
|
1033 | if row[2] == 'point': |
|
1034 | point = point_dict.get(row[4], None) |
|
1035 | constant = None |
|
1036 | numerator_meter = None |
|
1037 | denominator_meter = None |
|
1038 | elif row[2] == 'constant': |
|
1039 | constant = row[3] |
|
1040 | point = None |
|
1041 | numerator_meter = None |
|
1042 | denominator_meter = None |
|
1043 | elif row[2] == 'fraction': |
|
1044 | constant = None |
|
1045 | point = None |
|
1046 | # find numerator meter by uuid |
|
1047 | numerator_meter = meter_dict.get(row[5], None) |
|
1048 | if numerator_meter is None: |
|
1049 | numerator_meter = virtual_meter_dict.get(row[5], None) |
|
1050 | if numerator_meter is None: |
|
1051 | numerator_meter = offline_meter_dict.get(row[5], None) |
|
1052 | # find denominator meter by uuid |
|
1053 | denominator_meter = meter_dict.get(row[6], None) |
|
1054 | if denominator_meter is None: |
|
1055 | denominator_meter = virtual_meter_dict.get(row[6], None) |
|
1056 | if denominator_meter is None: |
|
1057 | denominator_meter = offline_meter_dict.get(row[6], None) |
|
1058 | ||
1059 | meta_result = {"id": row[0], |
|
1060 | "name": row[1], |
|
1061 | "parameter_type": row[2], |
|
1062 | "constant": constant, |
|
1063 | "point": point, |
|
1064 | "numerator_meter": numerator_meter, |
|
1065 | "denominator_meter": denominator_meter} |
|
1066 | ||
1067 | resp.text = json.dumps(meta_result) |
|
1068 | ||
1069 | @staticmethod |
|
1070 | @user_logger |
|
1071 | def on_delete(req, resp, id_, pid): |
|
1072 | admin_control(req) |
|
1073 | if not id_.isdigit() or int(id_) <= 0: |
|
1074 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1075 | description='API.INVALID_EQUIPMENT_ID') |
|
1076 | ||
1077 | if not pid.isdigit() or int(pid) <= 0: |
|
1078 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1079 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
1080 | ||
1081 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1082 | cursor = cnx.cursor() |
|
1083 | ||
1084 | cursor.execute(" SELECT name " |
|
1085 | " FROM tbl_equipments " |
|
1086 | " WHERE id = %s ", |
|
1087 | (id_,)) |
|
1088 | row = cursor.fetchone() |
|
1089 | if row is None: |
|
1090 | cursor.close() |
|
1091 | cnx.close() |
|
1092 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1093 | title='API.NOT_FOUND', |
|
1094 | description='API.EQUIPMENT_NOT_FOUND') |
|
1095 | ||
1096 | cursor.execute(" SELECT name " |
|
1097 | " FROM tbl_equipments_parameters " |
|
1098 | " WHERE equipment_id = %s AND id = %s ", |
|
1099 | (id_, pid,)) |
|
1100 | row = cursor.fetchone() |
|
1101 | if row is None: |
|
1102 | cursor.close() |
|
1103 | cnx.close() |
|
1104 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1105 | title='API.NOT_FOUND', |
|
1106 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1107 | ||
1108 | cursor.execute(" DELETE FROM tbl_equipments_parameters " |
|
1109 | " WHERE id = %s ", (pid, )) |
|
1110 | cnx.commit() |
|
1111 | ||
1112 | cursor.close() |
|
1113 | cnx.close() |
|
1114 | ||
1115 | resp.status = falcon.HTTP_204 |
|
1116 | ||
1117 | @staticmethod |
|
1118 | @user_logger |
|
1119 | def on_put(req, resp, id_, pid): |
|
1120 | """Handles PUT requests""" |
|
1121 | admin_control(req) |
|
1122 | if not id_.isdigit() or int(id_) <= 0: |
|
1123 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1124 | description='API.INVALID_EQUIPMENT_ID') |
|
1125 | ||
1126 | if not pid.isdigit() or int(pid) <= 0: |
|
1127 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1128 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
1129 | ||
1130 | try: |
|
1131 | raw_json = req.stream.read().decode('utf-8') |
|
1132 | except Exception as ex: |
|
1133 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1134 | title='API.BAD_REQUEST', |
|
1135 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
1136 | ||
1137 | new_values = json.loads(raw_json) |
|
1138 | ||
1139 | if 'name' not in new_values['data'].keys() or \ |
|
1140 | not isinstance(new_values['data']['name'], str) or \ |
|
1141 | len(str.strip(new_values['data']['name'])) == 0: |
|
1142 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1143 | description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
|
1144 | name = str.strip(new_values['data']['name']) |
|
1145 | ||
1146 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
1147 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
1148 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
1149 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1150 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
1151 | ||
1152 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
1153 | ||
1154 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
1155 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1156 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
1157 | ||
1158 | constant = None |
|
1159 | if 'constant' in new_values['data'].keys(): |
|
1160 | if new_values['data']['constant'] is not None and \ |
|
1161 | isinstance(new_values['data']['constant'], str) and \ |
|
1162 | len(str.strip(new_values['data']['constant'])) > 0: |
|
1163 | constant = str.strip(new_values['data']['constant']) |
|
1164 | ||
1165 | point_id = None |
|
1166 | if 'point_id' in new_values['data'].keys(): |
|
1167 | if new_values['data']['point_id'] is not None and \ |
|
1168 | new_values['data']['point_id'] <= 0: |
|
1169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1170 | description='API.INVALID_POINT_ID') |
|
1171 | point_id = new_values['data']['point_id'] |
|
1172 | ||
1173 | numerator_meter_uuid = None |
|
1174 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
1175 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
1176 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
1177 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
1178 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
1179 | ||
1180 | denominator_meter_uuid = None |
|
1181 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
1182 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
1183 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
1184 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
1185 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
1186 | ||
1187 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
1188 | cursor = cnx.cursor() |
|
1189 | ||
1190 | cursor.execute(" SELECT name " |
|
1191 | " FROM tbl_equipments " |
|
1192 | " WHERE id = %s ", (id_,)) |
|
1193 | if cursor.fetchone() is None: |
|
1194 | cursor.close() |
|
1195 | cnx.close() |
|
1196 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', |
|
1197 | description='API.EQUIPMENT_NOT_FOUND') |
|
1198 | ||
1199 | cursor.execute(" SELECT name " |
|
1200 | " FROM tbl_equipments_parameters " |
|
1201 | " WHERE equipment_id = %s AND id = %s ", |
|
1202 | (id_, pid,)) |
|
1203 | row = cursor.fetchone() |
|
1204 | if row is None: |
|
1205 | cursor.close() |
|
1206 | cnx.close() |
|
1207 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
1208 | title='API.NOT_FOUND', |
|
1209 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
1210 | ||
1211 | cursor.execute(" SELECT name " |
|
1212 | " FROM tbl_equipments_parameters " |
|
1213 | " WHERE name = %s AND equipment_id = %s AND id != %s ", (name, id_, pid)) |
|
1214 | row = cursor.fetchone() |
|
1215 | if row is not None: |
|
1216 | cursor.close() |
|
1217 | cnx.close() |
|
1218 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1219 | description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
1220 | ||
1221 | # validate by parameter type |
|
1222 | if parameter_type == 'point': |
|
1223 | if point_id is None: |
|
1224 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1225 | description='API.INVALID_POINT_ID') |
|
1226 | ||
1227 | query = (" SELECT id, name " |
|
1228 | " FROM tbl_points " |
|
1229 | " WHERE id = %s ") |
|
1230 | cursor.execute(query, (point_id, )) |
|
1231 | row = cursor.fetchone() |
|
1232 | if row is None: |
|
1233 | cursor.close() |
|
1234 | cnx.close() |
|
1235 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1236 | description='API.POINT_NOT_FOUND') |
|
1237 | ||
1238 | elif parameter_type == 'constant': |
|
1239 | if constant is None: |
|
1240 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1241 | description='API.INVALID_CONSTANT_VALUE') |
|
1242 | ||
1243 | elif parameter_type == 'fraction': |
|
1244 | ||
1245 | query = (" SELECT id, name, uuid " |
|
1246 | " FROM tbl_meters ") |
|
1247 | cursor.execute(query) |
|
1248 | rows_meters = cursor.fetchall() |
|
1249 | ||
1250 | meter_dict = dict() |
|
1251 | if rows_meters is not None and len(rows_meters) > 0: |
|
1252 | for row in rows_meters: |
|
1253 | meter_dict[row[2]] = {"type": 'meter', |
|
1254 | "id": row[0], |
|
1255 | "name": row[1], |
|
1256 | "uuid": row[2]} |
|
1257 | ||
1258 | query = (" SELECT id, name, uuid " |
|
1259 | " FROM tbl_offline_meters ") |
|
1260 | cursor.execute(query) |
|
1261 | rows_offline_meters = cursor.fetchall() |
|
1262 | ||
1263 | offline_meter_dict = dict() |
|
1264 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
1265 | for row in rows_offline_meters: |
|
1266 | offline_meter_dict[row[2]] = {"type": 'offline_meter', |
|
1267 | "id": row[0], |
|
1268 | "name": row[1], |
|
1269 | "uuid": row[2]} |
|
1270 | ||
1271 | query = (" SELECT id, name, uuid " |
|
1272 | " FROM tbl_virtual_meters ") |
|
1273 | cursor.execute(query) |
|
1274 | rows_virtual_meters = cursor.fetchall() |
|
1275 | ||
1276 | virtual_meter_dict = dict() |
|
1277 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
1278 | for row in rows_virtual_meters: |
|
1279 | virtual_meter_dict[row[2]] = {"type": 'virtual_meter', |
|
1280 | "id": row[0], |
|
1281 | "name": row[1], |
|
1282 | "uuid": row[2]} |
|
1283 | ||
1284 | # validate numerator meter uuid |
|
1285 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
1286 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
1287 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
1288 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1289 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
1290 | ||
1291 | # validate denominator meter uuid |
|
1292 | if denominator_meter_uuid == numerator_meter_uuid: |
|
1293 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1294 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1295 | ||
1296 | if denominator_meter_uuid not in meter_dict and \ |
|
1297 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
1298 | denominator_meter_uuid not in offline_meter_dict: |
|
1299 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
1300 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
1301 | ||
1302 | add_values = (" UPDATE tbl_equipments_parameters " |
|
1303 | " SET name = %s , parameter_type = %s, constant = %s, " |
|
1304 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " |
|
1305 | " WHERE id = %s ") |
|
1306 | cursor.execute(add_values, (name, |
|
1307 | parameter_type, |
|
1308 | constant, |
|
1309 | point_id, |
|
1310 | numerator_meter_uuid, |
|
1311 | denominator_meter_uuid, |
|
1312 | pid)) |
|
1313 | cnx.commit() |
|
1314 | ||
1315 | cursor.close() |
|
1316 | cnx.close() |
|
1317 | ||
1318 | resp.status = falcon.HTTP_200 |
|
1319 | ||
1320 | ||
1321 | class EquipmentMeterCollection: |