| @@ 242-307 (lines=66) @@ | ||
| 239 | ||
| 240 | resp.status = falcon.HTTP_204 |
|
| 241 | ||
| 242 | @staticmethod |
|
| 243 | @user_logger |
|
| 244 | def on_put(req, resp, id_): |
|
| 245 | """Handles PUT requests""" |
|
| 246 | admin_control(req) |
|
| 247 | try: |
|
| 248 | raw_json = req.stream.read().decode('utf-8') |
|
| 249 | except Exception as ex: |
|
| 250 | print(str(ex)) |
|
| 251 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 252 | title='API.BAD_REQUEST', |
|
| 253 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 254 | ||
| 255 | if not id_.isdigit() or int(id_) <= 0: |
|
| 256 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 257 | description='API.INVALID_SENSOR_ID') |
|
| 258 | ||
| 259 | new_values = json.loads(raw_json) |
|
| 260 | ||
| 261 | if 'name' not in new_values['data'].keys() or \ |
|
| 262 | not isinstance(new_values['data']['name'], str) or \ |
|
| 263 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 264 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 265 | description='API.INVALID_SENSOR_NAME') |
|
| 266 | name = str.strip(new_values['data']['name']) |
|
| 267 | ||
| 268 | if 'description' in new_values['data'].keys() and \ |
|
| 269 | new_values['data']['description'] is not None and \ |
|
| 270 | len(str(new_values['data']['description'])) > 0: |
|
| 271 | description = str.strip(new_values['data']['description']) |
|
| 272 | else: |
|
| 273 | description = None |
|
| 274 | ||
| 275 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 276 | cursor = cnx.cursor() |
|
| 277 | ||
| 278 | cursor.execute(" SELECT name " |
|
| 279 | " FROM tbl_sensors " |
|
| 280 | " WHERE id = %s ", (id_,)) |
|
| 281 | if cursor.fetchone() is None: |
|
| 282 | cursor.close() |
|
| 283 | cnx.close() |
|
| 284 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 285 | description='API.SENSOR_NOT_FOUND') |
|
| 286 | ||
| 287 | cursor.execute(" SELECT name " |
|
| 288 | " FROM tbl_sensors " |
|
| 289 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
| 290 | if cursor.fetchone() is not None: |
|
| 291 | cursor.close() |
|
| 292 | cnx.close() |
|
| 293 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 294 | description='API.SENSOR_NAME_IS_ALREADY_IN_USE') |
|
| 295 | ||
| 296 | update_row = (" UPDATE tbl_sensors " |
|
| 297 | " SET name = %s, description = %s " |
|
| 298 | " WHERE id = %s ") |
|
| 299 | cursor.execute(update_row, (name, |
|
| 300 | description, |
|
| 301 | id_,)) |
|
| 302 | cnx.commit() |
|
| 303 | ||
| 304 | cursor.close() |
|
| 305 | cnx.close() |
|
| 306 | ||
| 307 | resp.status = falcon.HTTP_200 |
|
| 308 | ||
| 309 | ||
| 310 | class SensorPointCollection: |
|
| @@ 205-270 (lines=66) @@ | ||
| 202 | cnx.close() |
|
| 203 | resp.status = falcon.HTTP_204 |
|
| 204 | ||
| 205 | @staticmethod |
|
| 206 | @user_logger |
|
| 207 | def on_put(req, resp, id_): |
|
| 208 | """Handles PUT requests""" |
|
| 209 | admin_control(req) |
|
| 210 | try: |
|
| 211 | raw_json = req.stream.read().decode('utf-8') |
|
| 212 | except Exception as ex: |
|
| 213 | print(str(ex)) |
|
| 214 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 215 | title='API.BAD_REQUEST', |
|
| 216 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 217 | ||
| 218 | if not id_.isdigit() or int(id_) <= 0: |
|
| 219 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 220 | description='API.INVALID_GATEWAY_ID') |
|
| 221 | ||
| 222 | new_values = json.loads(raw_json) |
|
| 223 | ||
| 224 | if 'name' not in new_values['data'].keys() or \ |
|
| 225 | not isinstance(new_values['data']['name'], str) or \ |
|
| 226 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 227 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 228 | description='API.INVALID_GATEWAY_NAME') |
|
| 229 | name = str.strip(new_values['data']['name']) |
|
| 230 | ||
| 231 | if 'description' in new_values['data'].keys() and \ |
|
| 232 | new_values['data']['description'] is not None and \ |
|
| 233 | len(str(new_values['data']['description'])) > 0: |
|
| 234 | description = str.strip(new_values['data']['description']) |
|
| 235 | else: |
|
| 236 | description = None |
|
| 237 | ||
| 238 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 239 | cursor = cnx.cursor() |
|
| 240 | ||
| 241 | cursor.execute(" SELECT name " |
|
| 242 | " FROM tbl_gateways " |
|
| 243 | " WHERE id = %s ", (id_,)) |
|
| 244 | if cursor.fetchone() is None: |
|
| 245 | cursor.close() |
|
| 246 | cnx.close() |
|
| 247 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 248 | description='API.GATEWAY_NOT_FOUND') |
|
| 249 | ||
| 250 | cursor.execute(" SELECT name " |
|
| 251 | " FROM tbl_gateways " |
|
| 252 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
| 253 | if cursor.fetchone() is not None: |
|
| 254 | cursor.close() |
|
| 255 | cnx.close() |
|
| 256 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 257 | description='API.GATEWAY_NAME_IS_ALREADY_IN_USE') |
|
| 258 | ||
| 259 | update_row = (" UPDATE tbl_gateways " |
|
| 260 | " SET name = %s, description = %s " |
|
| 261 | " WHERE id = %s ") |
|
| 262 | cursor.execute(update_row, (name, |
|
| 263 | description, |
|
| 264 | id_,)) |
|
| 265 | cnx.commit() |
|
| 266 | ||
| 267 | cursor.close() |
|
| 268 | cnx.close() |
|
| 269 | ||
| 270 | resp.status = falcon.HTTP_200 |
|
| 271 | ||
| 272 | ||
| 273 | class GatewayDataSourceCollection: |
|
| @@ 221-283 (lines=63) @@ | ||
| 218 | ||
| 219 | resp.status = falcon.HTTP_204 |
|
| 220 | ||
| 221 | @staticmethod |
|
| 222 | def on_put(req, resp, id_): |
|
| 223 | """Handles PUT requests""" |
|
| 224 | admin_control(req) |
|
| 225 | try: |
|
| 226 | raw_json = req.stream.read().decode('utf-8') |
|
| 227 | except Exception as ex: |
|
| 228 | print(str(ex)) |
|
| 229 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 230 | title='API.BAD_REQUEST', |
|
| 231 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 232 | ||
| 233 | if not id_.isdigit() or int(id_) <= 0: |
|
| 234 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 235 | description='API.INVALID_WORKING_CALENDAR_ID') |
|
| 236 | ||
| 237 | new_values = json.loads(raw_json) |
|
| 238 | ||
| 239 | if 'name' not in new_values['data'].keys() or \ |
|
| 240 | not isinstance(new_values['data']['name'], str) or \ |
|
| 241 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 242 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 243 | description='API.INVALID_WORKING_CALENDAR_NAME') |
|
| 244 | name = str.strip(new_values['data']['name']) |
|
| 245 | ||
| 246 | if 'description' in new_values['data'].keys() and \ |
|
| 247 | new_values['data']['description'] is not None and \ |
|
| 248 | len(str(new_values['data']['description'])) > 0: |
|
| 249 | description = str.strip(new_values['data']['description']) |
|
| 250 | else: |
|
| 251 | description = None |
|
| 252 | ||
| 253 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 254 | cursor = cnx.cursor() |
|
| 255 | ||
| 256 | cursor.execute(" SELECT name " |
|
| 257 | " FROM tbl_working_calendars " |
|
| 258 | " WHERE id = %s ", (id_,)) |
|
| 259 | if cursor.fetchone() is None: |
|
| 260 | cursor.close() |
|
| 261 | cnx.close() |
|
| 262 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 263 | description='API.WORKING_CALENDAR_NOT_FOUND') |
|
| 264 | ||
| 265 | cursor.execute(" SELECT name " |
|
| 266 | " FROM tbl_working_calendars " |
|
| 267 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
| 268 | if cursor.fetchone() is not None: |
|
| 269 | cursor.close() |
|
| 270 | cnx.close() |
|
| 271 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 272 | description='API.WORKING_CALENDAR_NAME_IS_ALREADY_IN_USE') |
|
| 273 | ||
| 274 | update_row = (" UPDATE tbl_working_calendars " |
|
| 275 | " SET name = %s, description = %s " |
|
| 276 | " WHERE id = %s ") |
|
| 277 | cursor.execute(update_row, (name, description, id_)) |
|
| 278 | cnx.commit() |
|
| 279 | ||
| 280 | cursor.close() |
|
| 281 | cnx.close() |
|
| 282 | ||
| 283 | resp.status = falcon.HTTP_200 |
|
| 284 | ||
| 285 | ||
| 286 | class NonWorkingDayCollection: |
|