Code Duplication    Length = 176-176 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 5493-5668 (lines=176) @@
5490
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5491
5492
5493
class EnergyStorageContainerSTSItem:
5494
    def __init__(self):
5495
        pass
5496
5497
    @staticmethod
5498
    def on_options(req, resp, id_, fid):
5499
        _ = req
5500
        resp.status = falcon.HTTP_200
5501
        _ = id_
5502
5503
    @staticmethod
5504
    def on_get(req, resp, id_, fid):
5505
        access_control(req)
5506
        if not id_.isdigit() or int(id_) <= 0:
5507
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5508
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5509
        if not fid.isdigit() or int(fid) <= 0:
5510
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5511
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5512
5513
        cnx = mysql.connector.connect(**config.myems_system_db)
5514
        cursor = cnx.cursor()
5515
5516
        cursor.execute(" SELECT name "
5517
                       " FROM tbl_energy_storage_containers "
5518
                       " WHERE id = %s ", (id_,))
5519
        if cursor.fetchone() is None:
5520
            cursor.close()
5521
            cnx.close()
5522
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5523
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5524
5525
        query = (" SELECT id, name, uuid "
5526
                 " FROM tbl_energy_storage_containers ")
5527
        cursor.execute(query)
5528
        rows_energystoragecontainers = cursor.fetchall()
5529
5530
        energy_storage_container_dict = dict()
5531
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
5532
            for row in rows_energystoragecontainers:
5533
                energy_storage_container_dict[row[0]] = {"id": row[0],
5534
                                                         "name": row[1],
5535
                                                         "uuid": row[2]}
5536
5537
        query = (" SELECT id, name, uuid "
5538
                 " FROM tbl_energy_storage_containers_stses "
5539
                 " WHERE id = %s ")
5540
        cursor.execute(query, (fid,))
5541
        row = cursor.fetchone()
5542
        cursor.close()
5543
        cnx.close()
5544
5545
        if row is None:
5546
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5547
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5548
        else:
5549
            meta_result = {"id": row[0],
5550
                           "name": row[1],
5551
                           "uuid": row[2]
5552
                           }
5553
5554
        resp.text = json.dumps(meta_result)
5555
5556
    @staticmethod
5557
    @user_logger
5558
    def on_delete(req, resp, id_, fid):
5559
        admin_control(req)
5560
        if not id_.isdigit() or int(id_) <= 0:
5561
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5562
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5563
        if not fid.isdigit() or int(fid) <= 0:
5564
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5565
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5566
5567
        cnx = mysql.connector.connect(**config.myems_system_db)
5568
        cursor = cnx.cursor()
5569
5570
        cursor.execute(" SELECT name "
5571
                       " FROM tbl_energy_storage_containers "
5572
                       " WHERE id = %s ", (id_,))
5573
        if cursor.fetchone() is None:
5574
            cursor.close()
5575
            cnx.close()
5576
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5577
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5578
5579
        cursor.execute(" SELECT name "
5580
                       " FROM tbl_energy_storage_containers_stses "
5581
                       " WHERE id = %s ", (fid,))
5582
        if cursor.fetchone() is None:
5583
            cursor.close()
5584
            cnx.close()
5585
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5586
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5587
5588
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses "
5589
                       " WHERE id = %s ", (fid,))
5590
        cnx.commit()
5591
5592
        cursor.close()
5593
        cnx.close()
5594
5595
        resp.status = falcon.HTTP_204
5596
5597
    @staticmethod
5598
    @user_logger
5599
    def on_put(req, resp, id_, fid):
5600
        """Handles PUT requests"""
5601
        admin_control(req)
5602
        try:
5603
            raw_json = req.stream.read().decode('utf-8')
5604
        except Exception as ex:
5605
            print(str(ex))
5606
            raise falcon.HTTPError(status=falcon.HTTP_400,
5607
                                   title='API.BAD_REQUEST',
5608
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5609
        if not id_.isdigit() or int(id_) <= 0:
5610
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5611
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5612
5613
        if not fid.isdigit() or int(fid) <= 0:
5614
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5615
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5616
5617
        new_values = json.loads(raw_json)
5618
5619
        if 'name' not in new_values['data'].keys() or \
5620
                not isinstance(new_values['data']['name'], str) or \
5621
                len(str.strip(new_values['data']['name'])) == 0:
5622
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5623
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5624
        name = str.strip(new_values['data']['name'])
5625
5626
        cnx = mysql.connector.connect(**config.myems_system_db)
5627
        cursor = cnx.cursor()
5628
5629
        cursor.execute(" SELECT name "
5630
                       " FROM tbl_energy_storage_containers "
5631
                       " WHERE id = %s ", (id_,))
5632
        if cursor.fetchone() is None:
5633
            cursor.close()
5634
            cnx.close()
5635
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5636
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5637
5638
        cursor.execute(" SELECT name "
5639
                       " FROM tbl_energy_storage_containers_stses "
5640
                       " WHERE id = %s ", (fid,))
5641
        if cursor.fetchone() is None:
5642
            cursor.close()
5643
            cnx.close()
5644
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5645
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5646
5647
        cursor.execute(" SELECT name "
5648
                       " FROM tbl_energy_storage_containers_stses "
5649
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
5650
                       (id_, name, fid))
5651
        if cursor.fetchone() is not None:
5652
            cursor.close()
5653
            cnx.close()
5654
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5655
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5656
5657
        update_row = (" UPDATE tbl_energy_storage_containers_stses "
5658
                      " SET name = %s "
5659
                      "     WHERE id = %s ")
5660
        cursor.execute(update_row, (name,
5661
                                    fid))
5662
        cnx.commit()
5663
5664
        cursor.close()
5665
        cnx.close()
5666
5667
        resp.status = falcon.HTTP_200
5668
5669
5670
class EnergyStorageContainerSTSPointCollection:
5671
    def __init__(self):
@@ 3496-3671 (lines=176) @@
3493
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3494
3495
3496
class EnergyStorageContainerHVACItem:
3497
    def __init__(self):
3498
        pass
3499
3500
    @staticmethod
3501
    def on_options(req, resp, id_, hid):
3502
        _ = req
3503
        resp.status = falcon.HTTP_200
3504
        _ = id_
3505
3506
    @staticmethod
3507
    def on_get(req, resp, id_, hid):
3508
        access_control(req)
3509
        if not id_.isdigit() or int(id_) <= 0:
3510
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3511
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3512
        if not hid.isdigit() or int(hid) <= 0:
3513
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3514
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3515
3516
        cnx = mysql.connector.connect(**config.myems_system_db)
3517
        cursor = cnx.cursor()
3518
3519
        cursor.execute(" SELECT name "
3520
                       " FROM tbl_energy_storage_containers "
3521
                       " WHERE id = %s ", (id_,))
3522
        if cursor.fetchone() is None:
3523
            cursor.close()
3524
            cnx.close()
3525
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3526
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3527
3528
        query = (" SELECT id, name, uuid "
3529
                 " FROM tbl_energy_storage_containers ")
3530
        cursor.execute(query)
3531
        rows_energystoragecontainers = cursor.fetchall()
3532
3533
        energy_storage_container_dict = dict()
3534
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
3535
            for row in rows_energystoragecontainers:
3536
                energy_storage_container_dict[row[0]] = {"id": row[0],
3537
                                                         "name": row[1],
3538
                                                         "uuid": row[2]}
3539
3540
        query = (" SELECT id, name, uuid "
3541
                 " FROM tbl_energy_storage_containers_hvacs "
3542
                 " WHERE id = %s ")
3543
        cursor.execute(query, (hid,))
3544
        row = cursor.fetchone()
3545
        cursor.close()
3546
        cnx.close()
3547
3548
        if row is None:
3549
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3550
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3551
        else:
3552
            meta_result = {"id": row[0],
3553
                           "name": row[1],
3554
                           "uuid": row[2]
3555
                           }
3556
3557
        resp.text = json.dumps(meta_result)
3558
3559
    @staticmethod
3560
    @user_logger
3561
    def on_delete(req, resp, id_, hid):
3562
        admin_control(req)
3563
        if not id_.isdigit() or int(id_) <= 0:
3564
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3565
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3566
        if not hid.isdigit() or int(hid) <= 0:
3567
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3568
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3569
3570
        cnx = mysql.connector.connect(**config.myems_system_db)
3571
        cursor = cnx.cursor()
3572
3573
        cursor.execute(" SELECT name "
3574
                       " FROM tbl_energy_storage_containers "
3575
                       " WHERE id = %s ", (id_,))
3576
        if cursor.fetchone() is None:
3577
            cursor.close()
3578
            cnx.close()
3579
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3580
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3581
3582
        cursor.execute(" SELECT name "
3583
                       " FROM tbl_energy_storage_containers_hvacs "
3584
                       " WHERE id = %s ", (hid,))
3585
        if cursor.fetchone() is None:
3586
            cursor.close()
3587
            cnx.close()
3588
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3589
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3590
3591
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs "
3592
                       " WHERE id = %s ", (hid,))
3593
        cnx.commit()
3594
3595
        cursor.close()
3596
        cnx.close()
3597
3598
        resp.status = falcon.HTTP_204
3599
3600
    @staticmethod
3601
    @user_logger
3602
    def on_put(req, resp, id_, hid):
3603
        """Handles PUT requests"""
3604
        admin_control(req)
3605
        try:
3606
            raw_json = req.stream.read().decode('utf-8')
3607
        except Exception as ex:
3608
            print(str(ex))
3609
            raise falcon.HTTPError(status=falcon.HTTP_400,
3610
                                   title='API.BAD_REQUEST',
3611
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3612
        if not id_.isdigit() or int(id_) <= 0:
3613
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3614
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3615
3616
        if not hid.isdigit() or int(hid) <= 0:
3617
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3618
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3619
3620
        new_values = json.loads(raw_json)
3621
3622
        if 'name' not in new_values['data'].keys() or \
3623
                not isinstance(new_values['data']['name'], str) or \
3624
                len(str.strip(new_values['data']['name'])) == 0:
3625
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3626
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3627
        name = str.strip(new_values['data']['name'])
3628
3629
        cnx = mysql.connector.connect(**config.myems_system_db)
3630
        cursor = cnx.cursor()
3631
3632
        cursor.execute(" SELECT name "
3633
                       " FROM tbl_energy_storage_containers "
3634
                       " WHERE id = %s ", (id_,))
3635
        if cursor.fetchone() is None:
3636
            cursor.close()
3637
            cnx.close()
3638
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3639
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3640
3641
        cursor.execute(" SELECT name "
3642
                       " FROM tbl_energy_storage_containers_hvacs "
3643
                       " WHERE id = %s ", (hid,))
3644
        if cursor.fetchone() is None:
3645
            cursor.close()
3646
            cnx.close()
3647
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3648
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3649
3650
        cursor.execute(" SELECT name "
3651
                       " FROM tbl_energy_storage_containers_hvacs "
3652
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
3653
                       (id_, name, hid))
3654
        if cursor.fetchone() is not None:
3655
            cursor.close()
3656
            cnx.close()
3657
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3658
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3659
3660
        update_row = (" UPDATE tbl_energy_storage_containers_hvacs "
3661
                      " SET name = %s "
3662
                      "     WHERE id = %s ")
3663
        cursor.execute(update_row, (name,
3664
                                    hid))
3665
        cnx.commit()
3666
3667
        cursor.close()
3668
        cnx.close()
3669
3670
        resp.status = falcon.HTTP_200
3671
3672
3673
class EnergyStorageContainerHVACPointCollection:
3674
    def __init__(self):
@@ 2339-2514 (lines=176) @@
2336
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
2337
2338
2339
class EnergyStorageContainerFirecontrolItem:
2340
    def __init__(self):
2341
        pass
2342
2343
    @staticmethod
2344
    def on_options(req, resp, id_, fid):
2345
        _ = req
2346
        resp.status = falcon.HTTP_200
2347
        _ = id_
2348
2349
    @staticmethod
2350
    def on_get(req, resp, id_, fid):
2351
        access_control(req)
2352
        if not id_.isdigit() or int(id_) <= 0:
2353
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2354
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2355
        if not fid.isdigit() or int(fid) <= 0:
2356
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2357
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2358
2359
        cnx = mysql.connector.connect(**config.myems_system_db)
2360
        cursor = cnx.cursor()
2361
2362
        cursor.execute(" SELECT name "
2363
                       " FROM tbl_energy_storage_containers "
2364
                       " WHERE id = %s ", (id_,))
2365
        if cursor.fetchone() is None:
2366
            cursor.close()
2367
            cnx.close()
2368
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2369
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2370
2371
        query = (" SELECT id, name, uuid "
2372
                 " FROM tbl_energy_storage_containers ")
2373
        cursor.execute(query)
2374
        rows_energystoragecontainers = cursor.fetchall()
2375
2376
        energy_storage_container_dict = dict()
2377
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
2378
            for row in rows_energystoragecontainers:
2379
                energy_storage_container_dict[row[0]] = {"id": row[0],
2380
                                                         "name": row[1],
2381
                                                         "uuid": row[2]}
2382
2383
        query = (" SELECT id, name, uuid "
2384
                 " FROM tbl_energy_storage_containers_firecontrols "
2385
                 " WHERE id = %s ")
2386
        cursor.execute(query, (fid,))
2387
        row = cursor.fetchone()
2388
        cursor.close()
2389
        cnx.close()
2390
2391
        if row is None:
2392
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2393
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2394
        else:
2395
            meta_result = {"id": row[0],
2396
                           "name": row[1],
2397
                           "uuid": row[2]
2398
                           }
2399
2400
        resp.text = json.dumps(meta_result)
2401
2402
    @staticmethod
2403
    @user_logger
2404
    def on_delete(req, resp, id_, fid):
2405
        admin_control(req)
2406
        if not id_.isdigit() or int(id_) <= 0:
2407
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2408
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2409
        if not fid.isdigit() or int(fid) <= 0:
2410
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2411
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2412
2413
        cnx = mysql.connector.connect(**config.myems_system_db)
2414
        cursor = cnx.cursor()
2415
2416
        cursor.execute(" SELECT name "
2417
                       " FROM tbl_energy_storage_containers "
2418
                       " WHERE id = %s ", (id_,))
2419
        if cursor.fetchone() is None:
2420
            cursor.close()
2421
            cnx.close()
2422
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2423
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2424
2425
        cursor.execute(" SELECT name "
2426
                       " FROM tbl_energy_storage_containers_firecontrols "
2427
                       " WHERE id = %s ", (fid,))
2428
        if cursor.fetchone() is None:
2429
            cursor.close()
2430
            cnx.close()
2431
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2432
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2433
2434
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols "
2435
                       " WHERE id = %s ", (fid,))
2436
        cnx.commit()
2437
2438
        cursor.close()
2439
        cnx.close()
2440
2441
        resp.status = falcon.HTTP_204
2442
2443
    @staticmethod
2444
    @user_logger
2445
    def on_put(req, resp, id_, fid):
2446
        """Handles PUT requests"""
2447
        admin_control(req)
2448
        try:
2449
            raw_json = req.stream.read().decode('utf-8')
2450
        except Exception as ex:
2451
            print(str(ex))
2452
            raise falcon.HTTPError(status=falcon.HTTP_400,
2453
                                   title='API.BAD_REQUEST',
2454
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2455
        if not id_.isdigit() or int(id_) <= 0:
2456
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2457
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2458
2459
        if not fid.isdigit() or int(fid) <= 0:
2460
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2461
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2462
2463
        new_values = json.loads(raw_json)
2464
2465
        if 'name' not in new_values['data'].keys() or \
2466
                not isinstance(new_values['data']['name'], str) or \
2467
                len(str.strip(new_values['data']['name'])) == 0:
2468
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2469
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2470
        name = str.strip(new_values['data']['name'])
2471
2472
        cnx = mysql.connector.connect(**config.myems_system_db)
2473
        cursor = cnx.cursor()
2474
2475
        cursor.execute(" SELECT name "
2476
                       " FROM tbl_energy_storage_containers "
2477
                       " WHERE id = %s ", (id_,))
2478
        if cursor.fetchone() is None:
2479
            cursor.close()
2480
            cnx.close()
2481
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2482
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2483
2484
        cursor.execute(" SELECT name "
2485
                       " FROM tbl_energy_storage_containers_firecontrols "
2486
                       " WHERE id = %s ", (fid,))
2487
        if cursor.fetchone() is None:
2488
            cursor.close()
2489
            cnx.close()
2490
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2491
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2492
2493
        cursor.execute(" SELECT name "
2494
                       " FROM tbl_energy_storage_containers_firecontrols "
2495
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
2496
                       (id_, name, fid))
2497
        if cursor.fetchone() is not None:
2498
            cursor.close()
2499
            cnx.close()
2500
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2501
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2502
2503
        update_row = (" UPDATE tbl_energy_storage_containers_firecontrols "
2504
                      " SET name = %s "
2505
                      "     WHERE id = %s ")
2506
        cursor.execute(update_row, (name,
2507
                                    fid))
2508
        cnx.commit()
2509
2510
        cursor.close()
2511
        cnx.close()
2512
2513
        resp.status = falcon.HTTP_200
2514
2515
2516
class EnergyStorageContainerFirecontrolPointCollection:
2517
    def __init__(self):
@@ 1853-2028 (lines=176) @@
1850
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1851
1852
1853
class EnergyStorageContainerDCDCItem:
1854
    def __init__(self):
1855
        pass
1856
1857
    @staticmethod
1858
    def on_options(req, resp, id_, did):
1859
        _ = req
1860
        resp.status = falcon.HTTP_200
1861
        _ = id_
1862
1863
    @staticmethod
1864
    def on_get(req, resp, id_, did):
1865
        access_control(req)
1866
        if not id_.isdigit() or int(id_) <= 0:
1867
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1868
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1869
        if not did.isdigit() or int(did) <= 0:
1870
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1871
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1872
1873
        cnx = mysql.connector.connect(**config.myems_system_db)
1874
        cursor = cnx.cursor()
1875
1876
        cursor.execute(" SELECT name "
1877
                       " FROM tbl_energy_storage_containers "
1878
                       " WHERE id = %s ", (id_,))
1879
        if cursor.fetchone() is None:
1880
            cursor.close()
1881
            cnx.close()
1882
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1883
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1884
1885
        query = (" SELECT id, name, uuid "
1886
                 " FROM tbl_energy_storage_containers ")
1887
        cursor.execute(query)
1888
        rows_energystoragecontainers = cursor.fetchall()
1889
1890
        energy_storage_container_dict = dict()
1891
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
1892
            for row in rows_energystoragecontainers:
1893
                energy_storage_container_dict[row[0]] = {"id": row[0],
1894
                                                         "name": row[1],
1895
                                                         "uuid": row[2]}
1896
1897
        query = (" SELECT id, name, uuid "
1898
                 " FROM tbl_energy_storage_containers_dcdcs "
1899
                 " WHERE id = %s ")
1900
        cursor.execute(query, (did,))
1901
        row = cursor.fetchone()
1902
        cursor.close()
1903
        cnx.close()
1904
1905
        if row is None:
1906
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1907
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1908
        else:
1909
            meta_result = {"id": row[0],
1910
                           "name": row[1],
1911
                           "uuid": row[2]
1912
                           }
1913
1914
        resp.text = json.dumps(meta_result)
1915
1916
    @staticmethod
1917
    @user_logger
1918
    def on_delete(req, resp, id_, did):
1919
        admin_control(req)
1920
        if not id_.isdigit() or int(id_) <= 0:
1921
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1922
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1923
        if not did.isdigit() or int(did) <= 0:
1924
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1925
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1926
1927
        cnx = mysql.connector.connect(**config.myems_system_db)
1928
        cursor = cnx.cursor()
1929
1930
        cursor.execute(" SELECT name "
1931
                       " FROM tbl_energy_storage_containers "
1932
                       " WHERE id = %s ", (id_,))
1933
        if cursor.fetchone() is None:
1934
            cursor.close()
1935
            cnx.close()
1936
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1937
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1938
1939
        cursor.execute(" SELECT name "
1940
                       " FROM tbl_energy_storage_containers_dcdcs "
1941
                       " WHERE id = %s ", (did,))
1942
        if cursor.fetchone() is None:
1943
            cursor.close()
1944
            cnx.close()
1945
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1946
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1947
1948
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs "
1949
                       " WHERE id = %s ", (did,))
1950
        cnx.commit()
1951
1952
        cursor.close()
1953
        cnx.close()
1954
1955
        resp.status = falcon.HTTP_204
1956
1957
    @staticmethod
1958
    @user_logger
1959
    def on_put(req, resp, id_, did):
1960
        """Handles PUT requests"""
1961
        admin_control(req)
1962
        try:
1963
            raw_json = req.stream.read().decode('utf-8')
1964
        except Exception as ex:
1965
            print(str(ex))
1966
            raise falcon.HTTPError(status=falcon.HTTP_400,
1967
                                   title='API.BAD_REQUEST',
1968
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1969
        if not id_.isdigit() or int(id_) <= 0:
1970
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1971
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1972
1973
        if not did.isdigit() or int(did) <= 0:
1974
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1975
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1976
1977
        new_values = json.loads(raw_json)
1978
1979
        if 'name' not in new_values['data'].keys() or \
1980
                not isinstance(new_values['data']['name'], str) or \
1981
                len(str.strip(new_values['data']['name'])) == 0:
1982
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1983
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1984
        name = str.strip(new_values['data']['name'])
1985
1986
        cnx = mysql.connector.connect(**config.myems_system_db)
1987
        cursor = cnx.cursor()
1988
1989
        cursor.execute(" SELECT name "
1990
                       " FROM tbl_energy_storage_containers "
1991
                       " WHERE id = %s ", (id_,))
1992
        if cursor.fetchone() is None:
1993
            cursor.close()
1994
            cnx.close()
1995
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1996
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1997
1998
        cursor.execute(" SELECT name "
1999
                       " FROM tbl_energy_storage_containers_dcdcs "
2000
                       " WHERE id = %s ", (did,))
2001
        if cursor.fetchone() is None:
2002
            cursor.close()
2003
            cnx.close()
2004
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2005
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
2006
2007
        cursor.execute(" SELECT name "
2008
                       " FROM tbl_energy_storage_containers_dcdcs "
2009
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
2010
                       (id_, name, did))
2011
        if cursor.fetchone() is not None:
2012
            cursor.close()
2013
            cnx.close()
2014
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2015
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
2016
2017
        update_row = (" UPDATE tbl_energy_storage_containers_dcdcs "
2018
                      " SET name = %s "
2019
                      "     WHERE id = %s ")
2020
        cursor.execute(update_row, (name,
2021
                                    did))
2022
        cnx.commit()
2023
2024
        cursor.close()
2025
        cnx.close()
2026
2027
        resp.status = falcon.HTTP_200
2028
2029
2030
class EnergyStorageContainerDCDCPointCollection:
2031
    def __init__(self):