| @@ 993-1361 (lines=369) @@ | ||
| 990 | resp.location = '/combinedequipments/' + str(id_) + 'parameters/' + str(new_id) |
|
| 991 | ||
| 992 | ||
| 993 | class CombinedEquipmentParameterItem: |
|
| 994 | @staticmethod |
|
| 995 | def __init__(): |
|
| 996 | pass |
|
| 997 | ||
| 998 | @staticmethod |
|
| 999 | def on_options(req, resp, id_, pid): |
|
| 1000 | resp.status = falcon.HTTP_200 |
|
| 1001 | ||
| 1002 | @staticmethod |
|
| 1003 | def on_get(req, resp, id_, pid): |
|
| 1004 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1005 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1006 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 1007 | ||
| 1008 | if not pid.isdigit() or int(pid) <= 0: |
|
| 1009 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1010 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
| 1011 | ||
| 1012 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1013 | cursor = cnx.cursor(dictionary=True) |
|
| 1014 | ||
| 1015 | query = (" SELECT id, name " |
|
| 1016 | " FROM tbl_points ") |
|
| 1017 | cursor.execute(query) |
|
| 1018 | rows_points = cursor.fetchall() |
|
| 1019 | ||
| 1020 | point_dict = dict() |
|
| 1021 | if rows_points is not None and len(rows_points) > 0: |
|
| 1022 | for row in rows_points: |
|
| 1023 | point_dict[row['id']] = {"id": row['id'], |
|
| 1024 | "name": row['name']} |
|
| 1025 | ||
| 1026 | query = (" SELECT id, name, uuid " |
|
| 1027 | " FROM tbl_meters ") |
|
| 1028 | cursor.execute(query) |
|
| 1029 | rows_meters = cursor.fetchall() |
|
| 1030 | ||
| 1031 | meter_dict = dict() |
|
| 1032 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 1033 | for row in rows_meters: |
|
| 1034 | meter_dict[row['uuid']] = {"type": 'meter', |
|
| 1035 | "id": row['id'], |
|
| 1036 | "name": row['name'], |
|
| 1037 | "uuid": row['uuid']} |
|
| 1038 | ||
| 1039 | query = (" SELECT id, name, uuid " |
|
| 1040 | " FROM tbl_offline_meters ") |
|
| 1041 | cursor.execute(query) |
|
| 1042 | rows_offline_meters = cursor.fetchall() |
|
| 1043 | ||
| 1044 | offline_meter_dict = dict() |
|
| 1045 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 1046 | for row in rows_offline_meters: |
|
| 1047 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
|
| 1048 | "id": row['id'], |
|
| 1049 | "name": row['name'], |
|
| 1050 | "uuid": row['uuid']} |
|
| 1051 | ||
| 1052 | query = (" SELECT id, name, uuid " |
|
| 1053 | " FROM tbl_virtual_meters ") |
|
| 1054 | cursor.execute(query) |
|
| 1055 | rows_virtual_meters = cursor.fetchall() |
|
| 1056 | ||
| 1057 | virtual_meter_dict = dict() |
|
| 1058 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 1059 | for row in rows_virtual_meters: |
|
| 1060 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
|
| 1061 | "id": row['id'], |
|
| 1062 | "name": row['name'], |
|
| 1063 | "uuid": row['uuid']} |
|
| 1064 | ||
| 1065 | query = (" SELECT id, name, parameter_type, " |
|
| 1066 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
| 1067 | " FROM tbl_combined_equipments_parameters " |
|
| 1068 | " WHERE combined_equipment_id = %s AND id = %s ") |
|
| 1069 | cursor.execute(query, (id_, pid)) |
|
| 1070 | row = cursor.fetchone() |
|
| 1071 | cursor.close() |
|
| 1072 | cnx.disconnect() |
|
| 1073 | ||
| 1074 | if row is None: |
|
| 1075 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 1076 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 1077 | else: |
|
| 1078 | constant = None |
|
| 1079 | point = None |
|
| 1080 | numerator_meter = None |
|
| 1081 | denominator_meter = None |
|
| 1082 | if row['parameter_type'] == 'point': |
|
| 1083 | point = point_dict.get(row['point_id'], None) |
|
| 1084 | constant = None |
|
| 1085 | numerator_meter = None |
|
| 1086 | denominator_meter = None |
|
| 1087 | elif row['parameter_type'] == 'constant': |
|
| 1088 | constant = row['constant'] |
|
| 1089 | point = None |
|
| 1090 | numerator_meter = None |
|
| 1091 | denominator_meter = None |
|
| 1092 | elif row['parameter_type'] == 'fraction': |
|
| 1093 | constant = None |
|
| 1094 | point = None |
|
| 1095 | # find numerator meter by uuid |
|
| 1096 | numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 1097 | if numerator_meter is None: |
|
| 1098 | numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 1099 | if numerator_meter is None: |
|
| 1100 | numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 1101 | # find denominator meter by uuid |
|
| 1102 | denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 1103 | if denominator_meter is None: |
|
| 1104 | denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 1105 | if denominator_meter is None: |
|
| 1106 | denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 1107 | ||
| 1108 | meta_result = {"id": row['id'], |
|
| 1109 | "name": row['name'], |
|
| 1110 | "parameter_type": row['parameter_type'], |
|
| 1111 | "constant": constant, |
|
| 1112 | "point": point, |
|
| 1113 | "numerator_meter": numerator_meter, |
|
| 1114 | "denominator_meter": denominator_meter} |
|
| 1115 | ||
| 1116 | resp.body = json.dumps(meta_result) |
|
| 1117 | ||
| 1118 | @staticmethod |
|
| 1119 | def on_delete(req, resp, id_, pid): |
|
| 1120 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1121 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1122 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 1123 | ||
| 1124 | if not pid.isdigit() or int(pid) <= 0: |
|
| 1125 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1126 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
| 1127 | ||
| 1128 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1129 | cursor = cnx.cursor() |
|
| 1130 | ||
| 1131 | cursor.execute(" SELECT name " |
|
| 1132 | " FROM tbl_combined_equipments " |
|
| 1133 | " WHERE id = %s ", |
|
| 1134 | (id_,)) |
|
| 1135 | row = cursor.fetchone() |
|
| 1136 | if row is None: |
|
| 1137 | cursor.close() |
|
| 1138 | cnx.disconnect() |
|
| 1139 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 1140 | title='API.NOT_FOUND', |
|
| 1141 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 1142 | ||
| 1143 | cursor.execute(" SELECT name " |
|
| 1144 | " FROM tbl_combined_equipments_parameters " |
|
| 1145 | " WHERE combined_equipment_id = %s AND id = %s ", |
|
| 1146 | (id_, pid,)) |
|
| 1147 | row = cursor.fetchone() |
|
| 1148 | if row is None: |
|
| 1149 | cursor.close() |
|
| 1150 | cnx.disconnect() |
|
| 1151 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 1152 | title='API.NOT_FOUND', |
|
| 1153 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 1154 | ||
| 1155 | cursor.execute(" DELETE FROM tbl_combined_equipments_parameters " |
|
| 1156 | " WHERE id = %s ", (pid, )) |
|
| 1157 | cnx.commit() |
|
| 1158 | ||
| 1159 | cursor.close() |
|
| 1160 | cnx.disconnect() |
|
| 1161 | ||
| 1162 | resp.status = falcon.HTTP_204 |
|
| 1163 | ||
| 1164 | @staticmethod |
|
| 1165 | def on_put(req, resp, id_, pid): |
|
| 1166 | """Handles POST requests""" |
|
| 1167 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1168 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1169 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
| 1170 | ||
| 1171 | if not pid.isdigit() or int(pid) <= 0: |
|
| 1172 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1173 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_ID') |
|
| 1174 | ||
| 1175 | try: |
|
| 1176 | raw_json = req.stream.read().decode('utf-8') |
|
| 1177 | except Exception as ex: |
|
| 1178 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
|
| 1179 | ||
| 1180 | new_values = json.loads(raw_json) |
|
| 1181 | ||
| 1182 | if 'name' not in new_values['data'].keys() or \ |
|
| 1183 | not isinstance(new_values['data']['name'], str) or \ |
|
| 1184 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 1185 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1186 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_NAME') |
|
| 1187 | name = str.strip(new_values['data']['name']) |
|
| 1188 | ||
| 1189 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
| 1190 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
| 1191 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
| 1192 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1193 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
| 1194 | ||
| 1195 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
| 1196 | ||
| 1197 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
| 1198 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1199 | description='API.INVALID_COMBINED_EQUIPMENT_PARAMETER_TYPE') |
|
| 1200 | ||
| 1201 | constant = None |
|
| 1202 | if 'constant' in new_values['data'].keys(): |
|
| 1203 | if new_values['data']['constant'] is not None and \ |
|
| 1204 | isinstance(new_values['data']['constant'], str) and \ |
|
| 1205 | len(str.strip(new_values['data']['constant'])) > 0: |
|
| 1206 | constant = str.strip(new_values['data']['constant']) |
|
| 1207 | ||
| 1208 | point_id = None |
|
| 1209 | if 'point_id' in new_values['data'].keys(): |
|
| 1210 | if new_values['data']['point_id'] is not None and \ |
|
| 1211 | new_values['data']['point_id'] <= 0: |
|
| 1212 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1213 | description='API.INVALID_POINT_ID') |
|
| 1214 | point_id = new_values['data']['point_id'] |
|
| 1215 | ||
| 1216 | numerator_meter_uuid = None |
|
| 1217 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
| 1218 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
| 1219 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
| 1220 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
| 1221 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
| 1222 | ||
| 1223 | denominator_meter_uuid = None |
|
| 1224 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
| 1225 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
| 1226 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
| 1227 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
| 1228 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
| 1229 | ||
| 1230 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1231 | cursor = cnx.cursor(dictionary=True) |
|
| 1232 | ||
| 1233 | cursor.execute(" SELECT name " |
|
| 1234 | " FROM tbl_combined_equipments " |
|
| 1235 | " WHERE id = %s ", (id_,)) |
|
| 1236 | if cursor.fetchone() is None: |
|
| 1237 | cursor.close() |
|
| 1238 | cnx.disconnect() |
|
| 1239 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
|
| 1240 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
| 1241 | ||
| 1242 | cursor.execute(" SELECT name " |
|
| 1243 | " FROM tbl_combined_equipments_parameters " |
|
| 1244 | " WHERE combined_equipment_id = %s AND id = %s ", |
|
| 1245 | (id_, pid,)) |
|
| 1246 | row = cursor.fetchone() |
|
| 1247 | if row is None: |
|
| 1248 | cursor.close() |
|
| 1249 | cnx.disconnect() |
|
| 1250 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 1251 | title='API.NOT_FOUND', |
|
| 1252 | description='API.COMBINED_EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 1253 | ||
| 1254 | cursor.execute(" SELECT name " |
|
| 1255 | " FROM tbl_combined_equipments_parameters " |
|
| 1256 | " WHERE name = %s AND combined_equipment_id = %s AND id != %s ", (name, id_, pid)) |
|
| 1257 | row = cursor.fetchone() |
|
| 1258 | if row is not None: |
|
| 1259 | cursor.close() |
|
| 1260 | cnx.disconnect() |
|
| 1261 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1262 | description='API.COMBINED_EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
| 1263 | ||
| 1264 | # validate by parameter type |
|
| 1265 | if parameter_type == 'point': |
|
| 1266 | if point_id is None: |
|
| 1267 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1268 | description='API.INVALID_POINT_ID') |
|
| 1269 | ||
| 1270 | query = (" SELECT id, name " |
|
| 1271 | " FROM tbl_points " |
|
| 1272 | " WHERE id = %s ") |
|
| 1273 | cursor.execute(query, (point_id, )) |
|
| 1274 | row = cursor.fetchone() |
|
| 1275 | if row is None: |
|
| 1276 | cursor.close() |
|
| 1277 | cnx.disconnect() |
|
| 1278 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1279 | description='API.POINT_NOT_FOUND') |
|
| 1280 | ||
| 1281 | elif parameter_type == 'constant': |
|
| 1282 | if constant is None: |
|
| 1283 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1284 | description='API.INVALID_CONSTANT_VALUE') |
|
| 1285 | ||
| 1286 | elif parameter_type == 'fraction': |
|
| 1287 | ||
| 1288 | query = (" SELECT id, name, uuid " |
|
| 1289 | " FROM tbl_meters ") |
|
| 1290 | cursor.execute(query) |
|
| 1291 | rows_meters = cursor.fetchall() |
|
| 1292 | ||
| 1293 | meter_dict = dict() |
|
| 1294 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 1295 | for row in rows_meters: |
|
| 1296 | meter_dict[row['uuid']] = {"type": 'meter', |
|
| 1297 | "id": row['id'], |
|
| 1298 | "name": row['name'], |
|
| 1299 | "uuid": row['uuid']} |
|
| 1300 | ||
| 1301 | query = (" SELECT id, name, uuid " |
|
| 1302 | " FROM tbl_offline_meters ") |
|
| 1303 | cursor.execute(query) |
|
| 1304 | rows_offline_meters = cursor.fetchall() |
|
| 1305 | ||
| 1306 | offline_meter_dict = dict() |
|
| 1307 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 1308 | for row in rows_offline_meters: |
|
| 1309 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
|
| 1310 | "id": row['id'], |
|
| 1311 | "name": row['name'], |
|
| 1312 | "uuid": row['uuid']} |
|
| 1313 | ||
| 1314 | query = (" SELECT id, name, uuid " |
|
| 1315 | " FROM tbl_virtual_meters ") |
|
| 1316 | cursor.execute(query) |
|
| 1317 | rows_virtual_meters = cursor.fetchall() |
|
| 1318 | ||
| 1319 | virtual_meter_dict = dict() |
|
| 1320 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 1321 | for row in rows_virtual_meters: |
|
| 1322 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
|
| 1323 | "id": row['id'], |
|
| 1324 | "name": row['name'], |
|
| 1325 | "uuid": row['uuid']} |
|
| 1326 | ||
| 1327 | # validate numerator meter uuid |
|
| 1328 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
| 1329 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
| 1330 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
| 1331 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1332 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
| 1333 | ||
| 1334 | # validate denominator meter uuid |
|
| 1335 | if denominator_meter_uuid == numerator_meter_uuid: |
|
| 1336 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1337 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
| 1338 | ||
| 1339 | if denominator_meter_uuid not in meter_dict and \ |
|
| 1340 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
| 1341 | denominator_meter_uuid not in offline_meter_dict: |
|
| 1342 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1343 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
| 1344 | ||
| 1345 | add_values = (" UPDATE tbl_combined_equipments_parameters " |
|
| 1346 | " SET name = %s , parameter_type = %s, constant = %s, " |
|
| 1347 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid =%s " |
|
| 1348 | " WHERE id = %s ") |
|
| 1349 | cursor.execute(add_values, (name, |
|
| 1350 | parameter_type, |
|
| 1351 | constant, |
|
| 1352 | point_id, |
|
| 1353 | numerator_meter_uuid, |
|
| 1354 | denominator_meter_uuid, |
|
| 1355 | pid)) |
|
| 1356 | cnx.commit() |
|
| 1357 | ||
| 1358 | cursor.close() |
|
| 1359 | cnx.disconnect() |
|
| 1360 | ||
| 1361 | resp.status = falcon.HTTP_200 |
|
| 1362 | ||
| 1363 | ||
| 1364 | class CombinedEquipmentMeterCollection: |
|
| @@ 839-1207 (lines=369) @@ | ||
| 836 | resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) |
|
| 837 | ||
| 838 | ||
| 839 | class EquipmentParameterItem: |
|
| 840 | @staticmethod |
|
| 841 | def __init__(): |
|
| 842 | pass |
|
| 843 | ||
| 844 | @staticmethod |
|
| 845 | def on_options(req, resp, id_, pid): |
|
| 846 | resp.status = falcon.HTTP_200 |
|
| 847 | ||
| 848 | @staticmethod |
|
| 849 | def on_get(req, resp, id_, pid): |
|
| 850 | if not id_.isdigit() or int(id_) <= 0: |
|
| 851 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 852 | description='API.INVALID_EQUIPMENT_ID') |
|
| 853 | ||
| 854 | if not pid.isdigit() or int(pid) <= 0: |
|
| 855 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 856 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
| 857 | ||
| 858 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 859 | cursor = cnx.cursor(dictionary=True) |
|
| 860 | ||
| 861 | query = (" SELECT id, name " |
|
| 862 | " FROM tbl_points ") |
|
| 863 | cursor.execute(query) |
|
| 864 | rows_points = cursor.fetchall() |
|
| 865 | ||
| 866 | point_dict = dict() |
|
| 867 | if rows_points is not None and len(rows_points) > 0: |
|
| 868 | for row in rows_points: |
|
| 869 | point_dict[row['id']] = {"id": row['id'], |
|
| 870 | "name": row['name']} |
|
| 871 | ||
| 872 | query = (" SELECT id, name, uuid " |
|
| 873 | " FROM tbl_meters ") |
|
| 874 | cursor.execute(query) |
|
| 875 | rows_meters = cursor.fetchall() |
|
| 876 | ||
| 877 | meter_dict = dict() |
|
| 878 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 879 | for row in rows_meters: |
|
| 880 | meter_dict[row['uuid']] = {"type": 'meter', |
|
| 881 | "id": row['id'], |
|
| 882 | "name": row['name'], |
|
| 883 | "uuid": row['uuid']} |
|
| 884 | ||
| 885 | query = (" SELECT id, name, uuid " |
|
| 886 | " FROM tbl_offline_meters ") |
|
| 887 | cursor.execute(query) |
|
| 888 | rows_offline_meters = cursor.fetchall() |
|
| 889 | ||
| 890 | offline_meter_dict = dict() |
|
| 891 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 892 | for row in rows_offline_meters: |
|
| 893 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
|
| 894 | "id": row['id'], |
|
| 895 | "name": row['name'], |
|
| 896 | "uuid": row['uuid']} |
|
| 897 | ||
| 898 | query = (" SELECT id, name, uuid " |
|
| 899 | " FROM tbl_virtual_meters ") |
|
| 900 | cursor.execute(query) |
|
| 901 | rows_virtual_meters = cursor.fetchall() |
|
| 902 | ||
| 903 | virtual_meter_dict = dict() |
|
| 904 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 905 | for row in rows_virtual_meters: |
|
| 906 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
|
| 907 | "id": row['id'], |
|
| 908 | "name": row['name'], |
|
| 909 | "uuid": row['uuid']} |
|
| 910 | ||
| 911 | query = (" SELECT id, name, parameter_type, " |
|
| 912 | " constant, point_id, numerator_meter_uuid, denominator_meter_uuid " |
|
| 913 | " FROM tbl_equipments_parameters " |
|
| 914 | " WHERE equipment_id = %s AND id = %s ") |
|
| 915 | cursor.execute(query, (id_, pid)) |
|
| 916 | row = cursor.fetchone() |
|
| 917 | cursor.close() |
|
| 918 | cnx.disconnect() |
|
| 919 | ||
| 920 | if row is None: |
|
| 921 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 922 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 923 | else: |
|
| 924 | constant = None |
|
| 925 | point = None |
|
| 926 | numerator_meter = None |
|
| 927 | denominator_meter = None |
|
| 928 | if row['parameter_type'] == 'point': |
|
| 929 | point = point_dict.get(row['point_id'], None) |
|
| 930 | constant = None |
|
| 931 | numerator_meter = None |
|
| 932 | denominator_meter = None |
|
| 933 | elif row['parameter_type'] == 'constant': |
|
| 934 | constant = row['constant'] |
|
| 935 | point = None |
|
| 936 | numerator_meter = None |
|
| 937 | denominator_meter = None |
|
| 938 | elif row['parameter_type'] == 'fraction': |
|
| 939 | constant = None |
|
| 940 | point = None |
|
| 941 | # find numerator meter by uuid |
|
| 942 | numerator_meter = meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 943 | if numerator_meter is None: |
|
| 944 | numerator_meter = virtual_meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 945 | if numerator_meter is None: |
|
| 946 | numerator_meter = offline_meter_dict.get(row['numerator_meter_uuid'], None) |
|
| 947 | # find denominator meter by uuid |
|
| 948 | denominator_meter = meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 949 | if denominator_meter is None: |
|
| 950 | denominator_meter = virtual_meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 951 | if denominator_meter is None: |
|
| 952 | denominator_meter = offline_meter_dict.get(row['denominator_meter_uuid'], None) |
|
| 953 | ||
| 954 | meta_result = {"id": row['id'], |
|
| 955 | "name": row['name'], |
|
| 956 | "parameter_type": row['parameter_type'], |
|
| 957 | "constant": constant, |
|
| 958 | "point": point, |
|
| 959 | "numerator_meter": numerator_meter, |
|
| 960 | "denominator_meter": denominator_meter} |
|
| 961 | ||
| 962 | resp.body = json.dumps(meta_result) |
|
| 963 | ||
| 964 | @staticmethod |
|
| 965 | def on_delete(req, resp, id_, pid): |
|
| 966 | if not id_.isdigit() or int(id_) <= 0: |
|
| 967 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 968 | description='API.INVALID_EQUIPMENT_ID') |
|
| 969 | ||
| 970 | if not pid.isdigit() or int(pid) <= 0: |
|
| 971 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 972 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
| 973 | ||
| 974 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 975 | cursor = cnx.cursor() |
|
| 976 | ||
| 977 | cursor.execute(" SELECT name " |
|
| 978 | " FROM tbl_equipments " |
|
| 979 | " WHERE id = %s ", |
|
| 980 | (id_,)) |
|
| 981 | row = cursor.fetchone() |
|
| 982 | if row is None: |
|
| 983 | cursor.close() |
|
| 984 | cnx.disconnect() |
|
| 985 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 986 | title='API.NOT_FOUND', |
|
| 987 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 988 | ||
| 989 | cursor.execute(" SELECT name " |
|
| 990 | " FROM tbl_equipments_parameters " |
|
| 991 | " WHERE equipment_id = %s AND id = %s ", |
|
| 992 | (id_, pid,)) |
|
| 993 | row = cursor.fetchone() |
|
| 994 | if row is None: |
|
| 995 | cursor.close() |
|
| 996 | cnx.disconnect() |
|
| 997 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 998 | title='API.NOT_FOUND', |
|
| 999 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 1000 | ||
| 1001 | cursor.execute(" DELETE FROM tbl_equipments_parameters " |
|
| 1002 | " WHERE id = %s ", (pid, )) |
|
| 1003 | cnx.commit() |
|
| 1004 | ||
| 1005 | cursor.close() |
|
| 1006 | cnx.disconnect() |
|
| 1007 | ||
| 1008 | resp.status = falcon.HTTP_204 |
|
| 1009 | ||
| 1010 | @staticmethod |
|
| 1011 | def on_put(req, resp, id_, pid): |
|
| 1012 | """Handles POST requests""" |
|
| 1013 | if not id_.isdigit() or int(id_) <= 0: |
|
| 1014 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1015 | description='API.INVALID_EQUIPMENT_ID') |
|
| 1016 | ||
| 1017 | if not pid.isdigit() or int(pid) <= 0: |
|
| 1018 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1019 | description='API.INVALID_EQUIPMENT_PARAMETER_ID') |
|
| 1020 | ||
| 1021 | try: |
|
| 1022 | raw_json = req.stream.read().decode('utf-8') |
|
| 1023 | except Exception as ex: |
|
| 1024 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
|
| 1025 | ||
| 1026 | new_values = json.loads(raw_json) |
|
| 1027 | ||
| 1028 | if 'name' not in new_values['data'].keys() or \ |
|
| 1029 | not isinstance(new_values['data']['name'], str) or \ |
|
| 1030 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 1031 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1032 | description='API.INVALID_EQUIPMENT_PARAMETER_NAME') |
|
| 1033 | name = str.strip(new_values['data']['name']) |
|
| 1034 | ||
| 1035 | if 'parameter_type' not in new_values['data'].keys() or \ |
|
| 1036 | not isinstance(new_values['data']['parameter_type'], str) or \ |
|
| 1037 | len(str.strip(new_values['data']['parameter_type'])) == 0: |
|
| 1038 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1039 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
| 1040 | ||
| 1041 | parameter_type = str.strip(new_values['data']['parameter_type']) |
|
| 1042 | ||
| 1043 | if parameter_type not in ('constant', 'point', 'fraction'): |
|
| 1044 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1045 | description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') |
|
| 1046 | ||
| 1047 | constant = None |
|
| 1048 | if 'constant' in new_values['data'].keys(): |
|
| 1049 | if new_values['data']['constant'] is not None and \ |
|
| 1050 | isinstance(new_values['data']['constant'], str) and \ |
|
| 1051 | len(str.strip(new_values['data']['constant'])) > 0: |
|
| 1052 | constant = str.strip(new_values['data']['constant']) |
|
| 1053 | ||
| 1054 | point_id = None |
|
| 1055 | if 'point_id' in new_values['data'].keys(): |
|
| 1056 | if new_values['data']['point_id'] is not None and \ |
|
| 1057 | new_values['data']['point_id'] <= 0: |
|
| 1058 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1059 | description='API.INVALID_POINT_ID') |
|
| 1060 | point_id = new_values['data']['point_id'] |
|
| 1061 | ||
| 1062 | numerator_meter_uuid = None |
|
| 1063 | if 'numerator_meter_uuid' in new_values['data'].keys(): |
|
| 1064 | if new_values['data']['numerator_meter_uuid'] is not None and \ |
|
| 1065 | isinstance(new_values['data']['numerator_meter_uuid'], str) and \ |
|
| 1066 | len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: |
|
| 1067 | numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) |
|
| 1068 | ||
| 1069 | denominator_meter_uuid = None |
|
| 1070 | if 'denominator_meter_uuid' in new_values['data'].keys(): |
|
| 1071 | if new_values['data']['denominator_meter_uuid'] is not None and \ |
|
| 1072 | isinstance(new_values['data']['denominator_meter_uuid'], str) and \ |
|
| 1073 | len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: |
|
| 1074 | denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) |
|
| 1075 | ||
| 1076 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 1077 | cursor = cnx.cursor(dictionary=True) |
|
| 1078 | ||
| 1079 | cursor.execute(" SELECT name " |
|
| 1080 | " FROM tbl_equipments " |
|
| 1081 | " WHERE id = %s ", (id_,)) |
|
| 1082 | if cursor.fetchone() is None: |
|
| 1083 | cursor.close() |
|
| 1084 | cnx.disconnect() |
|
| 1085 | raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND', |
|
| 1086 | description='API.EQUIPMENT_NOT_FOUND') |
|
| 1087 | ||
| 1088 | cursor.execute(" SELECT name " |
|
| 1089 | " FROM tbl_equipments_parameters " |
|
| 1090 | " WHERE equipment_id = %s AND id = %s ", |
|
| 1091 | (id_, pid,)) |
|
| 1092 | row = cursor.fetchone() |
|
| 1093 | if row is None: |
|
| 1094 | cursor.close() |
|
| 1095 | cnx.disconnect() |
|
| 1096 | raise falcon.HTTPError(falcon.HTTP_400, |
|
| 1097 | title='API.NOT_FOUND', |
|
| 1098 | description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') |
|
| 1099 | ||
| 1100 | cursor.execute(" SELECT name " |
|
| 1101 | " FROM tbl_equipments_parameters " |
|
| 1102 | " WHERE name = %s AND equipment_id = %s AND id != %s ", (name, id_, pid)) |
|
| 1103 | row = cursor.fetchone() |
|
| 1104 | if row is not None: |
|
| 1105 | cursor.close() |
|
| 1106 | cnx.disconnect() |
|
| 1107 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1108 | description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') |
|
| 1109 | ||
| 1110 | # validate by parameter type |
|
| 1111 | if parameter_type == 'point': |
|
| 1112 | if point_id is None: |
|
| 1113 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1114 | description='API.INVALID_POINT_ID') |
|
| 1115 | ||
| 1116 | query = (" SELECT id, name " |
|
| 1117 | " FROM tbl_points " |
|
| 1118 | " WHERE id = %s ") |
|
| 1119 | cursor.execute(query, (point_id, )) |
|
| 1120 | row = cursor.fetchone() |
|
| 1121 | if row is None: |
|
| 1122 | cursor.close() |
|
| 1123 | cnx.disconnect() |
|
| 1124 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1125 | description='API.POINT_NOT_FOUND') |
|
| 1126 | ||
| 1127 | elif parameter_type == 'constant': |
|
| 1128 | if constant is None: |
|
| 1129 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1130 | description='API.INVALID_CONSTANT_VALUE') |
|
| 1131 | ||
| 1132 | elif parameter_type == 'fraction': |
|
| 1133 | ||
| 1134 | query = (" SELECT id, name, uuid " |
|
| 1135 | " FROM tbl_meters ") |
|
| 1136 | cursor.execute(query) |
|
| 1137 | rows_meters = cursor.fetchall() |
|
| 1138 | ||
| 1139 | meter_dict = dict() |
|
| 1140 | if rows_meters is not None and len(rows_meters) > 0: |
|
| 1141 | for row in rows_meters: |
|
| 1142 | meter_dict[row['uuid']] = {"type": 'meter', |
|
| 1143 | "id": row['id'], |
|
| 1144 | "name": row['name'], |
|
| 1145 | "uuid": row['uuid']} |
|
| 1146 | ||
| 1147 | query = (" SELECT id, name, uuid " |
|
| 1148 | " FROM tbl_offline_meters ") |
|
| 1149 | cursor.execute(query) |
|
| 1150 | rows_offline_meters = cursor.fetchall() |
|
| 1151 | ||
| 1152 | offline_meter_dict = dict() |
|
| 1153 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
|
| 1154 | for row in rows_offline_meters: |
|
| 1155 | offline_meter_dict[row['uuid']] = {"type": 'offline_meter', |
|
| 1156 | "id": row['id'], |
|
| 1157 | "name": row['name'], |
|
| 1158 | "uuid": row['uuid']} |
|
| 1159 | ||
| 1160 | query = (" SELECT id, name, uuid " |
|
| 1161 | " FROM tbl_virtual_meters ") |
|
| 1162 | cursor.execute(query) |
|
| 1163 | rows_virtual_meters = cursor.fetchall() |
|
| 1164 | ||
| 1165 | virtual_meter_dict = dict() |
|
| 1166 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
|
| 1167 | for row in rows_virtual_meters: |
|
| 1168 | virtual_meter_dict[row['uuid']] = {"type": 'virtual_meter', |
|
| 1169 | "id": row['id'], |
|
| 1170 | "name": row['name'], |
|
| 1171 | "uuid": row['uuid']} |
|
| 1172 | ||
| 1173 | # validate numerator meter uuid |
|
| 1174 | if meter_dict.get(numerator_meter_uuid) is None and \ |
|
| 1175 | virtual_meter_dict.get(numerator_meter_uuid) is None and \ |
|
| 1176 | offline_meter_dict.get(numerator_meter_uuid) is None: |
|
| 1177 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1178 | description='API.INVALID_NUMERATOR_METER_UUID') |
|
| 1179 | ||
| 1180 | # validate denominator meter uuid |
|
| 1181 | if denominator_meter_uuid == numerator_meter_uuid: |
|
| 1182 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1183 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
| 1184 | ||
| 1185 | if denominator_meter_uuid not in meter_dict and \ |
|
| 1186 | denominator_meter_uuid not in virtual_meter_dict and \ |
|
| 1187 | denominator_meter_uuid not in offline_meter_dict: |
|
| 1188 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 1189 | description='API.INVALID_DENOMINATOR_METER_UUID') |
|
| 1190 | ||
| 1191 | add_values = (" UPDATE tbl_equipments_parameters " |
|
| 1192 | " SET name = %s , parameter_type = %s, constant = %s, " |
|
| 1193 | " point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid =%s " |
|
| 1194 | " WHERE id = %s ") |
|
| 1195 | cursor.execute(add_values, (name, |
|
| 1196 | parameter_type, |
|
| 1197 | constant, |
|
| 1198 | point_id, |
|
| 1199 | numerator_meter_uuid, |
|
| 1200 | denominator_meter_uuid, |
|
| 1201 | pid)) |
|
| 1202 | cnx.commit() |
|
| 1203 | ||
| 1204 | cursor.close() |
|
| 1205 | cnx.disconnect() |
|
| 1206 | ||
| 1207 | resp.status = falcon.HTTP_200 |
|
| 1208 | ||
| 1209 | ||
| 1210 | class EquipmentMeterCollection: |
|