Code Duplication    Length = 117-117 lines in 15 locations

myems-api/core/energystoragecontainer.py 8 locations

@@ 5554-5670 (lines=117) @@
5551
        resp.status = falcon.HTTP_200
5552
5553
5554
class EnergyStorageContainerSTSPointCollection:
5555
    def __init__(self):
5556
        """Initializes EnergyStorageContainerSTSPointCollection"""
5557
        pass
5558
5559
    @staticmethod
5560
    def on_options(req, resp, id_, fid):
5561
        resp.status = falcon.HTTP_200
5562
5563
    @staticmethod
5564
    def on_get(req, resp, id_, fid):
5565
        if 'API-KEY' not in req.headers or \
5566
                not isinstance(req.headers['API-KEY'], str) or \
5567
                len(str.strip(req.headers['API-KEY'])) == 0:
5568
            access_control(req)
5569
        else:
5570
            api_key_control(req)
5571
        if not id_.isdigit() or int(id_) <= 0:
5572
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5573
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5574
        if not fid.isdigit() or int(fid) <= 0:
5575
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5576
                                   description='API.INVALID_STS_ID')
5577
5578
        cnx = mysql.connector.connect(**config.myems_system_db)
5579
        cursor = cnx.cursor()
5580
5581
        cursor.execute(" SELECT name "
5582
                       " FROM tbl_energy_storage_containers_stses "
5583
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, ))
5584
        if cursor.fetchone() is None:
5585
            cursor.close()
5586
            cnx.close()
5587
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5588
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5589
5590
        query = (" SELECT p.id, p.name, "
5591
                 "        ds.id, ds.name, ds.uuid, "
5592
                 "        p.address "
5593
                 " FROM tbl_points p, tbl_energy_storage_containers_stses_points mp, tbl_data_sources ds "
5594
                 " WHERE mp.sts_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
5595
                 " ORDER BY p.name ")
5596
        cursor.execute(query, (fid,))
5597
        rows = cursor.fetchall()
5598
5599
        result = list()
5600
        if rows is not None and len(rows) > 0:
5601
            for row in rows:
5602
                meta_result = {"id": row[0], "name": row[1],
5603
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
5604
                               "address": row[5]}
5605
                result.append(meta_result)
5606
5607
        resp.text = json.dumps(result)
5608
5609
    @staticmethod
5610
    @user_logger
5611
    def on_post(req, resp, id_, fid):
5612
        """Handles POST requests"""
5613
        admin_control(req)
5614
        try:
5615
            raw_json = req.stream.read().decode('utf-8')
5616
        except Exception as ex:
5617
            raise falcon.HTTPError(status=falcon.HTTP_400,
5618
                                   title='API.BAD_REQUEST',
5619
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5620
5621
        if not id_.isdigit() or int(id_) <= 0:
5622
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5623
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5624
        if not fid.isdigit() or int(fid) <= 0:
5625
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5626
                                   description='API.INVALID_STS_ID')
5627
5628
        new_values = json.loads(raw_json)
5629
        cnx = mysql.connector.connect(**config.myems_system_db)
5630
        cursor = cnx.cursor()
5631
5632
        cursor.execute(" SELECT name "
5633
                       " FROM tbl_energy_storage_containers_stses "
5634
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,))
5635
        if cursor.fetchone() is None:
5636
            cursor.close()
5637
            cnx.close()
5638
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5639
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5640
5641
        cursor.execute(" SELECT name, object_type "
5642
                       " FROM tbl_points "
5643
                       " WHERE id = %s ", (new_values['data']['point_id'],))
5644
        row = cursor.fetchone()
5645
        if row is None:
5646
            cursor.close()
5647
            cnx.close()
5648
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5649
                                   description='API.POINT_NOT_FOUND')
5650
5651
        query = (" SELECT id " 
5652
                 " FROM tbl_energy_storage_containers_stses_points "
5653
                 " WHERE sts_id = %s AND point_id = %s")
5654
        cursor.execute(query, (fid, new_values['data']['point_id'],))
5655
        if cursor.fetchone() is not None:
5656
            cursor.close()
5657
            cnx.close()
5658
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
5659
                                   description='API.STS_POINT_RELATION_EXISTS')
5660
5661
        add_row = (" INSERT INTO tbl_energy_storage_containers_stses_points (sts_id, point_id) "
5662
                   " VALUES (%s, %s) ")
5663
        cursor.execute(add_row, (fid, new_values['data']['point_id'],))
5664
        cnx.commit()
5665
        cursor.close()
5666
        cnx.close()
5667
5668
        resp.status = falcon.HTTP_201
5669
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(fid) + '/points/' + \
5670
                        str(new_values['data']['point_id'])
5671
5672
5673
class EnergyStorageContainerSTSPointItem:
@@ 4780-4896 (lines=117) @@
4777
        resp.status = falcon.HTTP_200
4778
4779
4780
class EnergyStorageContainerPCSPointCollection:
4781
    def __init__(self):
4782
        """Initializes EnergyStorageContainerPCSPointCollection"""
4783
        pass
4784
4785
    @staticmethod
4786
    def on_options(req, resp, id_, pcsid):
4787
        resp.status = falcon.HTTP_200
4788
4789
    @staticmethod
4790
    def on_get(req, resp, id_, pcsid):
4791
        if 'API-KEY' not in req.headers or \
4792
                not isinstance(req.headers['API-KEY'], str) or \
4793
                len(str.strip(req.headers['API-KEY'])) == 0:
4794
            access_control(req)
4795
        else:
4796
            api_key_control(req)
4797
        if not id_.isdigit() or int(id_) <= 0:
4798
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4799
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4800
        if not pcsid.isdigit() or int(pcsid) <= 0:
4801
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4802
                                   description='API.INVALID_PCS_ID')
4803
4804
        cnx = mysql.connector.connect(**config.myems_system_db)
4805
        cursor = cnx.cursor()
4806
4807
        cursor.execute(" SELECT name "
4808
                       " FROM tbl_energy_storage_containers_power_conversion_systems "
4809
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid, ))
4810
        if cursor.fetchone() is None:
4811
            cursor.close()
4812
            cnx.close()
4813
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4814
                                   description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND')
4815
4816
        query = (" SELECT p.id, p.name, "
4817
                 "        ds.id, ds.name, ds.uuid, "
4818
                 "        p.address "
4819
                 " FROM tbl_points p, tbl_energy_storage_containers_pcses_points mp, tbl_data_sources ds "
4820
                 " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
4821
                 " ORDER BY p.name ")
4822
        cursor.execute(query, (pcsid,))
4823
        rows = cursor.fetchall()
4824
4825
        result = list()
4826
        if rows is not None and len(rows) > 0:
4827
            for row in rows:
4828
                meta_result = {"id": row[0], "name": row[1],
4829
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
4830
                               "address": row[5]}
4831
                result.append(meta_result)
4832
4833
        resp.text = json.dumps(result)
4834
4835
    @staticmethod
4836
    @user_logger
4837
    def on_post(req, resp, id_, pcsid):
4838
        """Handles POST requests"""
4839
        admin_control(req)
4840
        try:
4841
            raw_json = req.stream.read().decode('utf-8')
4842
        except Exception as ex:
4843
            raise falcon.HTTPError(status=falcon.HTTP_400,
4844
                                   title='API.BAD_REQUEST',
4845
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4846
4847
        if not id_.isdigit() or int(id_) <= 0:
4848
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4849
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4850
        if not pcsid.isdigit() or int(pcsid) <= 0:
4851
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4852
                                   description='API.INVALID_PCS_ID')
4853
4854
        new_values = json.loads(raw_json)
4855
        cnx = mysql.connector.connect(**config.myems_system_db)
4856
        cursor = cnx.cursor()
4857
4858
        cursor.execute(" SELECT name "
4859
                       " FROM tbl_energy_storage_containers_power_conversion_systems "
4860
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, pcsid,))
4861
        if cursor.fetchone() is None:
4862
            cursor.close()
4863
            cnx.close()
4864
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4865
                                   description='API.ENERGY_STORAGE_CONTAINER_PCS_NOT_FOUND')
4866
4867
        cursor.execute(" SELECT name, object_type "
4868
                       " FROM tbl_points "
4869
                       " WHERE id = %s ", (new_values['data']['point_id'],))
4870
        row = cursor.fetchone()
4871
        if row is None:
4872
            cursor.close()
4873
            cnx.close()
4874
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4875
                                   description='API.POINT_NOT_FOUND')
4876
4877
        query = (" SELECT id " 
4878
                 " FROM tbl_energy_storage_containers_pcses_points "
4879
                 " WHERE pcs_id = %s AND point_id = %s")
4880
        cursor.execute(query, (pcsid, new_values['data']['point_id'],))
4881
        if cursor.fetchone() is not None:
4882
            cursor.close()
4883
            cnx.close()
4884
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
4885
                                   description='API.PCS_POINT_RELATION_EXISTS')
4886
4887
        add_row = (" INSERT INTO tbl_energy_storage_containers_pcses_points (pcs_id, point_id) "
4888
                   " VALUES (%s, %s) ")
4889
        cursor.execute(add_row, (pcsid, new_values['data']['point_id'],))
4890
        cnx.commit()
4891
        cursor.close()
4892
        cnx.close()
4893
4894
        resp.status = falcon.HTTP_201
4895
        resp.location = '/energystoragecontainers/' + str(id_) + '/pcses/' + str(pcsid) + '/points/' + \
4896
                        str(new_values['data']['point_id'])
4897
4898
4899
class EnergyStorageContainerPCSPointItem:
@@ 4206-4322 (lines=117) @@
4203
        resp.status = falcon.HTTP_200
4204
4205
4206
class EnergyStorageContainerLoadPointCollection:
4207
    def __init__(self):
4208
        """Initializes EnergyStorageContainerLoadPointCollection"""
4209
        pass
4210
4211
    @staticmethod
4212
    def on_options(req, resp, id_, lid):
4213
        resp.status = falcon.HTTP_200
4214
4215
    @staticmethod
4216
    def on_get(req, resp, id_, lid):
4217
        if 'API-KEY' not in req.headers or \
4218
                not isinstance(req.headers['API-KEY'], str) or \
4219
                len(str.strip(req.headers['API-KEY'])) == 0:
4220
            access_control(req)
4221
        else:
4222
            api_key_control(req)
4223
        if not id_.isdigit() or int(id_) <= 0:
4224
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4225
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4226
        if not lid.isdigit() or int(lid) <= 0:
4227
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4228
                                   description='API.INVALID_LOAD_ID')
4229
4230
        cnx = mysql.connector.connect(**config.myems_system_db)
4231
        cursor = cnx.cursor()
4232
4233
        cursor.execute(" SELECT name "
4234
                       " FROM tbl_energy_storage_containers_loads "
4235
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid, ))
4236
        if cursor.fetchone() is None:
4237
            cursor.close()
4238
            cnx.close()
4239
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4240
                                   description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND')
4241
4242
        query = (" SELECT p.id, p.name, "
4243
                 "        ds.id, ds.name, ds.uuid, "
4244
                 "        p.address "
4245
                 " FROM tbl_points p, tbl_energy_storage_containers_loads_points mp, tbl_data_sources ds "
4246
                 " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
4247
                 " ORDER BY p.name ")
4248
        cursor.execute(query, (lid,))
4249
        rows = cursor.fetchall()
4250
4251
        result = list()
4252
        if rows is not None and len(rows) > 0:
4253
            for row in rows:
4254
                meta_result = {"id": row[0], "name": row[1],
4255
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
4256
                               "address": row[5]}
4257
                result.append(meta_result)
4258
4259
        resp.text = json.dumps(result)
4260
4261
    @staticmethod
4262
    @user_logger
4263
    def on_post(req, resp, id_, lid):
4264
        """Handles POST requests"""
4265
        admin_control(req)
4266
        try:
4267
            raw_json = req.stream.read().decode('utf-8')
4268
        except Exception as ex:
4269
            raise falcon.HTTPError(status=falcon.HTTP_400,
4270
                                   title='API.BAD_REQUEST',
4271
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4272
4273
        if not id_.isdigit() or int(id_) <= 0:
4274
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4275
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4276
        if not lid.isdigit() or int(lid) <= 0:
4277
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4278
                                   description='API.INVALID_LOAD_ID')
4279
4280
        new_values = json.loads(raw_json)
4281
        cnx = mysql.connector.connect(**config.myems_system_db)
4282
        cursor = cnx.cursor()
4283
4284
        cursor.execute(" SELECT name "
4285
                       " FROM tbl_energy_storage_containers_loads "
4286
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, lid,))
4287
        if cursor.fetchone() is None:
4288
            cursor.close()
4289
            cnx.close()
4290
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4291
                                   description='API.ENERGY_STORAGE_CONTAINER_LOAD_NOT_FOUND')
4292
4293
        cursor.execute(" SELECT name, object_type "
4294
                       " FROM tbl_points "
4295
                       " WHERE id = %s ", (new_values['data']['point_id'],))
4296
        row = cursor.fetchone()
4297
        if row is None:
4298
            cursor.close()
4299
            cnx.close()
4300
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4301
                                   description='API.POINT_NOT_FOUND')
4302
4303
        query = (" SELECT id " 
4304
                 " FROM tbl_energy_storage_containers_loads_points "
4305
                 " WHERE load_id = %s AND point_id = %s")
4306
        cursor.execute(query, (lid, new_values['data']['point_id'],))
4307
        if cursor.fetchone() is not None:
4308
            cursor.close()
4309
            cnx.close()
4310
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
4311
                                   description='API.LOAD_POINT_RELATION_EXISTS')
4312
4313
        add_row = (" INSERT INTO tbl_energy_storage_containers_loads_points (load_id, point_id) "
4314
                   " VALUES (%s, %s) ")
4315
        cursor.execute(add_row, (lid, new_values['data']['point_id'],))
4316
        cnx.commit()
4317
        cursor.close()
4318
        cnx.close()
4319
4320
        resp.status = falcon.HTTP_201
4321
        resp.location = '/energystoragecontainers/' + str(id_) + '/loads/' + str(lid) + '/points/' + \
4322
                        str(new_values['data']['point_id'])
4323
4324
4325
class EnergyStorageContainerLoadPointItem:
@@ 3582-3698 (lines=117) @@
3579
        resp.status = falcon.HTTP_200
3580
3581
3582
class EnergyStorageContainerHVACPointCollection:
3583
    def __init__(self):
3584
        """Initializes EnergyStorageContainerHVACPointCollection"""
3585
        pass
3586
3587
    @staticmethod
3588
    def on_options(req, resp, id_, hid):
3589
        resp.status = falcon.HTTP_200
3590
3591
    @staticmethod
3592
    def on_get(req, resp, id_, hid):
3593
        if 'API-KEY' not in req.headers or \
3594
                not isinstance(req.headers['API-KEY'], str) or \
3595
                len(str.strip(req.headers['API-KEY'])) == 0:
3596
            access_control(req)
3597
        else:
3598
            api_key_control(req)
3599
        if not id_.isdigit() or int(id_) <= 0:
3600
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3601
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3602
        if not hid.isdigit() or int(hid) <= 0:
3603
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3604
                                   description='API.INVALID_HVAC_ID')
3605
3606
        cnx = mysql.connector.connect(**config.myems_system_db)
3607
        cursor = cnx.cursor()
3608
3609
        cursor.execute(" SELECT name "
3610
                       " FROM tbl_energy_storage_containers_hvacs "
3611
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid, ))
3612
        if cursor.fetchone() is None:
3613
            cursor.close()
3614
            cnx.close()
3615
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3616
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3617
3618
        query = (" SELECT p.id, p.name, "
3619
                 "        ds.id, ds.name, ds.uuid, "
3620
                 "        p.address "
3621
                 " FROM tbl_points p, tbl_energy_storage_containers_hvacs_points mp, tbl_data_sources ds "
3622
                 " WHERE mp.hvac_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
3623
                 " ORDER BY p.name ")
3624
        cursor.execute(query, (hid,))
3625
        rows = cursor.fetchall()
3626
3627
        result = list()
3628
        if rows is not None and len(rows) > 0:
3629
            for row in rows:
3630
                meta_result = {"id": row[0], "name": row[1],
3631
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
3632
                               "address": row[5]}
3633
                result.append(meta_result)
3634
3635
        resp.text = json.dumps(result)
3636
3637
    @staticmethod
3638
    @user_logger
3639
    def on_post(req, resp, id_, hid):
3640
        """Handles POST requests"""
3641
        admin_control(req)
3642
        try:
3643
            raw_json = req.stream.read().decode('utf-8')
3644
        except Exception as ex:
3645
            raise falcon.HTTPError(status=falcon.HTTP_400,
3646
                                   title='API.BAD_REQUEST',
3647
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3648
3649
        if not id_.isdigit() or int(id_) <= 0:
3650
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3651
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3652
        if not hid.isdigit() or int(hid) <= 0:
3653
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3654
                                   description='API.INVALID_HVAC_ID')
3655
3656
        new_values = json.loads(raw_json)
3657
        cnx = mysql.connector.connect(**config.myems_system_db)
3658
        cursor = cnx.cursor()
3659
3660
        cursor.execute(" SELECT name "
3661
                       " FROM tbl_energy_storage_containers_hvacs "
3662
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, hid,))
3663
        if cursor.fetchone() is None:
3664
            cursor.close()
3665
            cnx.close()
3666
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3667
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3668
3669
        cursor.execute(" SELECT name, object_type "
3670
                       " FROM tbl_points "
3671
                       " WHERE id = %s ", (new_values['data']['point_id'],))
3672
        row = cursor.fetchone()
3673
        if row is None:
3674
            cursor.close()
3675
            cnx.close()
3676
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3677
                                   description='API.POINT_NOT_FOUND')
3678
3679
        query = (" SELECT id " 
3680
                 " FROM tbl_energy_storage_containers_hvacs_points "
3681
                 " WHERE hvac_id = %s AND point_id = %s")
3682
        cursor.execute(query, (hid, new_values['data']['point_id'],))
3683
        if cursor.fetchone() is not None:
3684
            cursor.close()
3685
            cnx.close()
3686
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
3687
                                   description='API.HVAC_POINT_RELATION_EXISTS')
3688
3689
        add_row = (" INSERT INTO tbl_energy_storage_containers_hvacs_points (hvac_id, point_id) "
3690
                   " VALUES (%s, %s) ")
3691
        cursor.execute(add_row, (hid, new_values['data']['point_id'],))
3692
        cnx.commit()
3693
        cursor.close()
3694
        cnx.close()
3695
3696
        resp.status = falcon.HTTP_201
3697
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(hid) + '/points/' + \
3698
                        str(new_values['data']['point_id'])
3699
3700
3701
class EnergyStorageContainerHVACPointItem:
@@ 3103-3219 (lines=117) @@
3100
        resp.status = falcon.HTTP_200
3101
3102
3103
class EnergyStorageContainerGridPointCollection:
3104
    def __init__(self):
3105
        """Initializes EnergyStorageContainerGridPointCollection"""
3106
        pass
3107
3108
    @staticmethod
3109
    def on_options(req, resp, id_, gid):
3110
        resp.status = falcon.HTTP_200
3111
3112
    @staticmethod
3113
    def on_get(req, resp, id_, gid):
3114
        if 'API-KEY' not in req.headers or \
3115
                not isinstance(req.headers['API-KEY'], str) or \
3116
                len(str.strip(req.headers['API-KEY'])) == 0:
3117
            access_control(req)
3118
        else:
3119
            api_key_control(req)
3120
        if not id_.isdigit() or int(id_) <= 0:
3121
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3122
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3123
        if not gid.isdigit() or int(gid) <= 0:
3124
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3125
                                   description='API.INVALID_GRID_ID')
3126
3127
        cnx = mysql.connector.connect(**config.myems_system_db)
3128
        cursor = cnx.cursor()
3129
3130
        cursor.execute(" SELECT name "
3131
                       " FROM tbl_energy_storage_containers_grids "
3132
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid, ))
3133
        if cursor.fetchone() is None:
3134
            cursor.close()
3135
            cnx.close()
3136
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3137
                                   description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND')
3138
3139
        query = (" SELECT p.id, p.name, "
3140
                 "        ds.id, ds.name, ds.uuid, "
3141
                 "        p.address "
3142
                 " FROM tbl_points p, tbl_energy_storage_containers_grids_points mp, tbl_data_sources ds "
3143
                 " WHERE mp.grid_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
3144
                 " ORDER BY p.name ")
3145
        cursor.execute(query, (gid,))
3146
        rows = cursor.fetchall()
3147
3148
        result = list()
3149
        if rows is not None and len(rows) > 0:
3150
            for row in rows:
3151
                meta_result = {"id": row[0], "name": row[1],
3152
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
3153
                               "address": row[5]}
3154
                result.append(meta_result)
3155
3156
        resp.text = json.dumps(result)
3157
3158
    @staticmethod
3159
    @user_logger
3160
    def on_post(req, resp, id_, gid):
3161
        """Handles POST requests"""
3162
        admin_control(req)
3163
        try:
3164
            raw_json = req.stream.read().decode('utf-8')
3165
        except Exception as ex:
3166
            raise falcon.HTTPError(status=falcon.HTTP_400,
3167
                                   title='API.BAD_REQUEST',
3168
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3169
3170
        if not id_.isdigit() or int(id_) <= 0:
3171
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3172
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3173
        if not gid.isdigit() or int(gid) <= 0:
3174
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3175
                                   description='API.INVALID_GRID_ID')
3176
3177
        new_values = json.loads(raw_json)
3178
        cnx = mysql.connector.connect(**config.myems_system_db)
3179
        cursor = cnx.cursor()
3180
3181
        cursor.execute(" SELECT name "
3182
                       " FROM tbl_energy_storage_containers_grids "
3183
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, gid,))
3184
        if cursor.fetchone() is None:
3185
            cursor.close()
3186
            cnx.close()
3187
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3188
                                   description='API.ENERGY_STORAGE_CONTAINER_GRID_NOT_FOUND')
3189
3190
        cursor.execute(" SELECT name, object_type "
3191
                       " FROM tbl_points "
3192
                       " WHERE id = %s ", (new_values['data']['point_id'],))
3193
        row = cursor.fetchone()
3194
        if row is None:
3195
            cursor.close()
3196
            cnx.close()
3197
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3198
                                   description='API.POINT_NOT_FOUND')
3199
3200
        query = (" SELECT id " 
3201
                 " FROM tbl_energy_storage_containers_grids_points "
3202
                 " WHERE grid_id = %s AND point_id = %s")
3203
        cursor.execute(query, (gid, new_values['data']['point_id'],))
3204
        if cursor.fetchone() is not None:
3205
            cursor.close()
3206
            cnx.close()
3207
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
3208
                                   description='API.GRID_POINT_RELATION_EXISTS')
3209
3210
        add_row = (" INSERT INTO tbl_energy_storage_containers_grids_points (grid_id, point_id) "
3211
                   " VALUES (%s, %s) ")
3212
        cursor.execute(add_row, (gid, new_values['data']['point_id'],))
3213
        cnx.commit()
3214
        cursor.close()
3215
        cnx.close()
3216
3217
        resp.status = falcon.HTTP_201
3218
        resp.location = '/energystoragecontainers/' + str(id_) + '/grids/' + str(gid) + '/points/' + \
3219
                        str(new_values['data']['point_id'])
3220
3221
3222
class EnergyStorageContainerGridPointItem:
@@ 2439-2555 (lines=117) @@
2436
        resp.status = falcon.HTTP_200
2437
2438
2439
class EnergyStorageContainerFirecontrolPointCollection:
2440
    def __init__(self):
2441
        """Initializes EnergyStorageContainerFirecontrolPointCollection"""
2442
        pass
2443
2444
    @staticmethod
2445
    def on_options(req, resp, id_, fid):
2446
        resp.status = falcon.HTTP_200
2447
2448
    @staticmethod
2449
    def on_get(req, resp, id_, fid):
2450
        if 'API-KEY' not in req.headers or \
2451
                not isinstance(req.headers['API-KEY'], str) or \
2452
                len(str.strip(req.headers['API-KEY'])) == 0:
2453
            access_control(req)
2454
        else:
2455
            api_key_control(req)
2456
        if not id_.isdigit() or int(id_) <= 0:
2457
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2458
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
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_FIRECONTROL_ID')
2462
2463
        cnx = mysql.connector.connect(**config.myems_system_db)
2464
        cursor = cnx.cursor()
2465
2466
        cursor.execute(" SELECT name "
2467
                       " FROM tbl_energy_storage_containers_firecontrols "
2468
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid, ))
2469
        if cursor.fetchone() is None:
2470
            cursor.close()
2471
            cnx.close()
2472
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2473
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2474
2475
        query = (" SELECT p.id, p.name, "
2476
                 "        ds.id, ds.name, ds.uuid, "
2477
                 "        p.address "
2478
                 " FROM tbl_points p, tbl_energy_storage_containers_firecontrols_points mp, tbl_data_sources ds "
2479
                 " WHERE mp.firecontrol_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
2480
                 " ORDER BY p.name ")
2481
        cursor.execute(query, (fid,))
2482
        rows = cursor.fetchall()
2483
2484
        result = list()
2485
        if rows is not None and len(rows) > 0:
2486
            for row in rows:
2487
                meta_result = {"id": row[0], "name": row[1],
2488
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
2489
                               "address": row[5]}
2490
                result.append(meta_result)
2491
2492
        resp.text = json.dumps(result)
2493
2494
    @staticmethod
2495
    @user_logger
2496
    def on_post(req, resp, id_, fid):
2497
        """Handles POST requests"""
2498
        admin_control(req)
2499
        try:
2500
            raw_json = req.stream.read().decode('utf-8')
2501
        except Exception as ex:
2502
            raise falcon.HTTPError(status=falcon.HTTP_400,
2503
                                   title='API.BAD_REQUEST',
2504
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2505
2506
        if not id_.isdigit() or int(id_) <= 0:
2507
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2508
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2509
        if not fid.isdigit() or int(fid) <= 0:
2510
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2511
                                   description='API.INVALID_FIRECONTROL_ID')
2512
2513
        new_values = json.loads(raw_json)
2514
        cnx = mysql.connector.connect(**config.myems_system_db)
2515
        cursor = cnx.cursor()
2516
2517
        cursor.execute(" SELECT name "
2518
                       " FROM tbl_energy_storage_containers_firecontrols "
2519
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, fid,))
2520
        if cursor.fetchone() is None:
2521
            cursor.close()
2522
            cnx.close()
2523
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2524
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2525
2526
        cursor.execute(" SELECT name, object_type "
2527
                       " FROM tbl_points "
2528
                       " WHERE id = %s ", (new_values['data']['point_id'],))
2529
        row = cursor.fetchone()
2530
        if row is None:
2531
            cursor.close()
2532
            cnx.close()
2533
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2534
                                   description='API.POINT_NOT_FOUND')
2535
2536
        query = (" SELECT id " 
2537
                 " FROM tbl_energy_storage_containers_firecontrols_points "
2538
                 " WHERE firecontrol_id = %s AND point_id = %s")
2539
        cursor.execute(query, (fid, new_values['data']['point_id'],))
2540
        if cursor.fetchone() is not None:
2541
            cursor.close()
2542
            cnx.close()
2543
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
2544
                                   description='API.FIRECONTROL_POINT_RELATION_EXISTS')
2545
2546
        add_row = (" INSERT INTO tbl_energy_storage_containers_firecontrols_points (firecontrol_id, point_id) "
2547
                   " VALUES (%s, %s) ")
2548
        cursor.execute(add_row, (fid, new_values['data']['point_id'],))
2549
        cnx.commit()
2550
        cursor.close()
2551
        cnx.close()
2552
2553
        resp.status = falcon.HTTP_201
2554
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(fid) + '/points/' + \
2555
                        str(new_values['data']['point_id'])
2556
2557
2558
class EnergyStorageContainerFirecontrolPointItem:
@@ 1960-2076 (lines=117) @@
1957
        resp.status = falcon.HTTP_200
1958
1959
1960
class EnergyStorageContainerDCDCPointCollection:
1961
    def __init__(self):
1962
        """Initializes EnergyStorageContainerDCDCPointCollection"""
1963
        pass
1964
1965
    @staticmethod
1966
    def on_options(req, resp, id_, did):
1967
        resp.status = falcon.HTTP_200
1968
1969
    @staticmethod
1970
    def on_get(req, resp, id_, did):
1971
        if 'API-KEY' not in req.headers or \
1972
                not isinstance(req.headers['API-KEY'], str) or \
1973
                len(str.strip(req.headers['API-KEY'])) == 0:
1974
            access_control(req)
1975
        else:
1976
            api_key_control(req)
1977
        if not id_.isdigit() or int(id_) <= 0:
1978
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1979
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1980
        if not did.isdigit() or int(did) <= 0:
1981
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1982
                                   description='API.INVALID_DCDC_ID')
1983
1984
        cnx = mysql.connector.connect(**config.myems_system_db)
1985
        cursor = cnx.cursor()
1986
1987
        cursor.execute(" SELECT name "
1988
                       " FROM tbl_energy_storage_containers_dcdcs "
1989
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did, ))
1990
        if cursor.fetchone() is None:
1991
            cursor.close()
1992
            cnx.close()
1993
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1994
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1995
1996
        query = (" SELECT p.id, p.name, "
1997
                 "        ds.id, ds.name, ds.uuid, "
1998
                 "        p.address "
1999
                 " FROM tbl_points p, tbl_energy_storage_containers_dcdcs_points mp, tbl_data_sources ds "
2000
                 " WHERE mp.dcdc_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
2001
                 " ORDER BY p.name ")
2002
        cursor.execute(query, (did,))
2003
        rows = cursor.fetchall()
2004
2005
        result = list()
2006
        if rows is not None and len(rows) > 0:
2007
            for row in rows:
2008
                meta_result = {"id": row[0], "name": row[1],
2009
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
2010
                               "address": row[5]}
2011
                result.append(meta_result)
2012
2013
        resp.text = json.dumps(result)
2014
2015
    @staticmethod
2016
    @user_logger
2017
    def on_post(req, resp, id_, did):
2018
        """Handles POST requests"""
2019
        admin_control(req)
2020
        try:
2021
            raw_json = req.stream.read().decode('utf-8')
2022
        except Exception as ex:
2023
            raise falcon.HTTPError(status=falcon.HTTP_400,
2024
                                   title='API.BAD_REQUEST',
2025
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2026
2027
        if not id_.isdigit() or int(id_) <= 0:
2028
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2029
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2030
        if not did.isdigit() or int(did) <= 0:
2031
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2032
                                   description='API.INVALID_DCDC_ID')
2033
2034
        new_values = json.loads(raw_json)
2035
        cnx = mysql.connector.connect(**config.myems_system_db)
2036
        cursor = cnx.cursor()
2037
2038
        cursor.execute(" SELECT name "
2039
                       " FROM tbl_energy_storage_containers_dcdcs "
2040
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, did,))
2041
        if cursor.fetchone() is None:
2042
            cursor.close()
2043
            cnx.close()
2044
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2045
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
2046
2047
        cursor.execute(" SELECT name, object_type "
2048
                       " FROM tbl_points "
2049
                       " WHERE id = %s ", (new_values['data']['point_id'],))
2050
        row = cursor.fetchone()
2051
        if row is None:
2052
            cursor.close()
2053
            cnx.close()
2054
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2055
                                   description='API.POINT_NOT_FOUND')
2056
2057
        query = (" SELECT id " 
2058
                 " FROM tbl_energy_storage_containers_dcdcs_points "
2059
                 " WHERE dcdc_id = %s AND point_id = %s")
2060
        cursor.execute(query, (did, new_values['data']['point_id'],))
2061
        if cursor.fetchone() is not None:
2062
            cursor.close()
2063
            cnx.close()
2064
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
2065
                                   description='API.DCDC_POINT_RELATION_EXISTS')
2066
2067
        add_row = (" INSERT INTO tbl_energy_storage_containers_dcdcs_points (dcdc_id, point_id) "
2068
                   " VALUES (%s, %s) ")
2069
        cursor.execute(add_row, (did, new_values['data']['point_id'],))
2070
        cnx.commit()
2071
        cursor.close()
2072
        cnx.close()
2073
2074
        resp.status = falcon.HTTP_201
2075
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(did) + '/points/' + \
2076
                        str(new_values['data']['point_id'])
2077
2078
2079
class EnergyStorageContainerDCDCPointItem:
@@ 1305-1421 (lines=117) @@
1302
        resp.status = falcon.HTTP_200
1303
1304
1305
class EnergyStorageContainerBatteryPointCollection:
1306
    def __init__(self):
1307
        """Initializes EnergyStorageContainerBatteryPointCollection"""
1308
        pass
1309
1310
    @staticmethod
1311
    def on_options(req, resp, id_, bid):
1312
        resp.status = falcon.HTTP_200
1313
1314
    @staticmethod
1315
    def on_get(req, resp, id_, bid):
1316
        if 'API-KEY' not in req.headers or \
1317
                not isinstance(req.headers['API-KEY'], str) or \
1318
                len(str.strip(req.headers['API-KEY'])) == 0:
1319
            access_control(req)
1320
        else:
1321
            api_key_control(req)
1322
        if not id_.isdigit() or int(id_) <= 0:
1323
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1324
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1325
        if not bid.isdigit() or int(bid) <= 0:
1326
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1327
                                   description='API.INVALID_BMS_ID')
1328
1329
        cnx = mysql.connector.connect(**config.myems_system_db)
1330
        cursor = cnx.cursor()
1331
1332
        cursor.execute(" SELECT name "
1333
                       " FROM tbl_energy_storage_containers_batteries "
1334
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid, ))
1335
        if cursor.fetchone() is None:
1336
            cursor.close()
1337
            cnx.close()
1338
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1339
                                   description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND')
1340
1341
        query = (" SELECT p.id, p.name, "
1342
                 "        ds.id, ds.name, ds.uuid, "
1343
                 "        p.address "
1344
                 " FROM tbl_points p, tbl_energy_storage_containers_bmses_points mp, tbl_data_sources ds "
1345
                 " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
1346
                 " ORDER BY p.name ")
1347
        cursor.execute(query, (bid,))
1348
        rows = cursor.fetchall()
1349
1350
        result = list()
1351
        if rows is not None and len(rows) > 0:
1352
            for row in rows:
1353
                meta_result = {"id": row[0], "name": row[1],
1354
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
1355
                               "address": row[5]}
1356
                result.append(meta_result)
1357
1358
        resp.text = json.dumps(result)
1359
1360
    @staticmethod
1361
    @user_logger
1362
    def on_post(req, resp, id_, bid):
1363
        """Handles POST requests"""
1364
        admin_control(req)
1365
        try:
1366
            raw_json = req.stream.read().decode('utf-8')
1367
        except Exception as ex:
1368
            raise falcon.HTTPError(status=falcon.HTTP_400,
1369
                                   title='API.BAD_REQUEST',
1370
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1371
1372
        if not id_.isdigit() or int(id_) <= 0:
1373
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1374
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1375
        if not bid.isdigit() or int(bid) <= 0:
1376
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1377
                                   description='API.INVALID_BMS_ID')
1378
1379
        new_values = json.loads(raw_json)
1380
        cnx = mysql.connector.connect(**config.myems_system_db)
1381
        cursor = cnx.cursor()
1382
1383
        cursor.execute(" SELECT name "
1384
                       " FROM tbl_energy_storage_containers_batteries "
1385
                       " WHERE energy_storage_container_id = %s AND id = %s ", (id_, bid,))
1386
        if cursor.fetchone() is None:
1387
            cursor.close()
1388
            cnx.close()
1389
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1390
                                   description='API.ENERGY_STORAGE_CONTAINER_BMS_NOT_FOUND')
1391
1392
        cursor.execute(" SELECT name, object_type "
1393
                       " FROM tbl_points "
1394
                       " WHERE id = %s ", (new_values['data']['point_id'],))
1395
        row = cursor.fetchone()
1396
        if row is None:
1397
            cursor.close()
1398
            cnx.close()
1399
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1400
                                   description='API.POINT_NOT_FOUND')
1401
1402
        query = (" SELECT id " 
1403
                 " FROM tbl_energy_storage_containers_bmses_points "
1404
                 " WHERE bms_id = %s AND point_id = %s")
1405
        cursor.execute(query, (bid, new_values['data']['point_id'],))
1406
        if cursor.fetchone() is not None:
1407
            cursor.close()
1408
            cnx.close()
1409
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
1410
                                   description='API.BMS_POINT_RELATION_EXISTS')
1411
1412
        add_row = (" INSERT INTO tbl_energy_storage_containers_bmses_points (bms_id, point_id) "
1413
                   " VALUES (%s, %s) ")
1414
        cursor.execute(add_row, (bid, new_values['data']['point_id'],))
1415
        cnx.commit()
1416
        cursor.close()
1417
        cnx.close()
1418
1419
        resp.status = falcon.HTTP_201
1420
        resp.location = '/energystoragecontainers/' + str(id_) + '/batteries/' + str(bid) + '/points/' + \
1421
                        str(new_values['data']['point_id'])
1422
1423
1424
class EnergyStorageContainerBatteryPointItem:

myems-api/core/hybridpowerstation.py 7 locations

@@ 4771-4887 (lines=117) @@
4768
        resp.status = falcon.HTTP_200
4769
4770
4771
class HybridPowerStationPVPointCollection:
4772
    def __init__(self):
4773
        """Initializes Class"""
4774
        pass
4775
4776
    @staticmethod
4777
    def on_options(req, resp, id_, pvid):
4778
        resp.status = falcon.HTTP_200
4779
4780
    @staticmethod
4781
    def on_get(req, resp, id_, pvid):
4782
        if 'API-KEY' not in req.headers or \
4783
                not isinstance(req.headers['API-KEY'], str) or \
4784
                len(str.strip(req.headers['API-KEY'])) == 0:
4785
            access_control(req)
4786
        else:
4787
            api_key_control(req)
4788
        if not id_.isdigit() or int(id_) <= 0:
4789
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4790
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
4791
        if not pvid.isdigit() or int(pvid) <= 0:
4792
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4793
                                   description='API.INVALID_PV_ID')
4794
4795
        cnx = mysql.connector.connect(**config.myems_system_db)
4796
        cursor = cnx.cursor()
4797
4798
        cursor.execute(" SELECT name "
4799
                       " FROM tbl_hybrid_power_stations_pvs "
4800
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, pvid, ))
4801
        if cursor.fetchone() is None:
4802
            cursor.close()
4803
            cnx.close()
4804
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4805
                                   description='API.HYBRID_POWER_STATION_PV_NOT_FOUND')
4806
4807
        query = (" SELECT p.id, p.name, "
4808
                 "        ds.id, ds.name, ds.uuid, "
4809
                 "        p.address "
4810
                 " FROM tbl_points p, tbl_hybrid_power_stations_pvs_points mp, tbl_data_sources ds "
4811
                 " WHERE mp.pv_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
4812
                 " ORDER BY p.name ")
4813
        cursor.execute(query, (pvid,))
4814
        rows = cursor.fetchall()
4815
4816
        result = list()
4817
        if rows is not None and len(rows) > 0:
4818
            for row in rows:
4819
                meta_result = {"id": row[0], "name": row[1],
4820
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
4821
                               "address": row[5]}
4822
                result.append(meta_result)
4823
4824
        resp.text = json.dumps(result)
4825
4826
    @staticmethod
4827
    @user_logger
4828
    def on_post(req, resp, id_, pvid):
4829
        """Handles POST requests"""
4830
        admin_control(req)
4831
        try:
4832
            raw_json = req.stream.read().decode('utf-8')
4833
        except Exception as ex:
4834
            raise falcon.HTTPError(status=falcon.HTTP_400,
4835
                                   title='API.BAD_REQUEST',
4836
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4837
4838
        if not id_.isdigit() or int(id_) <= 0:
4839
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4840
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
4841
        if not pvid.isdigit() or int(pvid) <= 0:
4842
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4843
                                   description='API.INVALID_PV_ID')
4844
4845
        new_values = json.loads(raw_json)
4846
        cnx = mysql.connector.connect(**config.myems_system_db)
4847
        cursor = cnx.cursor()
4848
4849
        cursor.execute(" SELECT name "
4850
                       " FROM tbl_hybrid_power_stations_pvs "
4851
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, pvid,))
4852
        if cursor.fetchone() is None:
4853
            cursor.close()
4854
            cnx.close()
4855
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4856
                                   description='API.HYBRID_POWER_STATION_PV_NOT_FOUND')
4857
4858
        cursor.execute(" SELECT name, object_type "
4859
                       " FROM tbl_points "
4860
                       " WHERE id = %s ", (new_values['data']['point_id'],))
4861
        row = cursor.fetchone()
4862
        if row is None:
4863
            cursor.close()
4864
            cnx.close()
4865
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4866
                                   description='API.POINT_NOT_FOUND')
4867
4868
        query = (" SELECT id " 
4869
                 " FROM tbl_hybrid_power_stations_pvs_points "
4870
                 " WHERE pv_id = %s AND point_id = %s")
4871
        cursor.execute(query, (pvid, new_values['data']['point_id'],))
4872
        if cursor.fetchone() is not None:
4873
            cursor.close()
4874
            cnx.close()
4875
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
4876
                                   description='API.PV_POINT_RELATION_EXISTS')
4877
4878
        add_row = (" INSERT INTO tbl_hybrid_power_stations_pvs_points (pv_id, point_id) "
4879
                   " VALUES (%s, %s) ")
4880
        cursor.execute(add_row, (pvid, new_values['data']['point_id'],))
4881
        cnx.commit()
4882
        cursor.close()
4883
        cnx.close()
4884
4885
        resp.status = falcon.HTTP_201
4886
        resp.location = '/hybridpowerstations/' + str(id_) + '/pvs/' + str(pvid) + '/points/' + \
4887
                        str(new_values['data']['point_id'])
4888
4889
4890
class HybridPowerStationPVPointItem:
@@ 4209-4325 (lines=117) @@
4206
        resp.status = falcon.HTTP_200
4207
4208
4209
class HybridPowerStationPCSPointCollection:
4210
    def __init__(self):
4211
        """Initializes Class"""
4212
        pass
4213
4214
    @staticmethod
4215
    def on_options(req, resp, id_, pcsid):
4216
        resp.status = falcon.HTTP_200
4217
4218
    @staticmethod
4219
    def on_get(req, resp, id_, pcsid):
4220
        if 'API-KEY' not in req.headers or \
4221
                not isinstance(req.headers['API-KEY'], str) or \
4222
                len(str.strip(req.headers['API-KEY'])) == 0:
4223
            access_control(req)
4224
        else:
4225
            api_key_control(req)
4226
        if not id_.isdigit() or int(id_) <= 0:
4227
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4228
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
4229
        if not pcsid.isdigit() or int(pcsid) <= 0:
4230
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4231
                                   description='API.INVALID_PCS_ID')
4232
4233
        cnx = mysql.connector.connect(**config.myems_system_db)
4234
        cursor = cnx.cursor()
4235
4236
        cursor.execute(" SELECT name "
4237
                       " FROM tbl_hybrid_power_stations_pcses "
4238
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, pcsid, ))
4239
        if cursor.fetchone() is None:
4240
            cursor.close()
4241
            cnx.close()
4242
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4243
                                   description='API.HYBRID_POWER_STATION_PCS_NOT_FOUND')
4244
4245
        query = (" SELECT p.id, p.name, "
4246
                 "        ds.id, ds.name, ds.uuid, "
4247
                 "        p.address "
4248
                 " FROM tbl_points p, tbl_hybrid_power_stations_pcses_points mp, tbl_data_sources ds "
4249
                 " WHERE mp.pcs_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
4250
                 " ORDER BY p.name ")
4251
        cursor.execute(query, (pcsid,))
4252
        rows = cursor.fetchall()
4253
4254
        result = list()
4255
        if rows is not None and len(rows) > 0:
4256
            for row in rows:
4257
                meta_result = {"id": row[0], "name": row[1],
4258
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
4259
                               "address": row[5]}
4260
                result.append(meta_result)
4261
4262
        resp.text = json.dumps(result)
4263
4264
    @staticmethod
4265
    @user_logger
4266
    def on_post(req, resp, id_, pcsid):
4267
        """Handles POST requests"""
4268
        admin_control(req)
4269
        try:
4270
            raw_json = req.stream.read().decode('utf-8')
4271
        except Exception as ex:
4272
            raise falcon.HTTPError(status=falcon.HTTP_400,
4273
                                   title='API.BAD_REQUEST',
4274
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4275
4276
        if not id_.isdigit() or int(id_) <= 0:
4277
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4278
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
4279
        if not pcsid.isdigit() or int(pcsid) <= 0:
4280
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4281
                                   description='API.INVALID_PCS_ID')
4282
4283
        new_values = json.loads(raw_json)
4284
        cnx = mysql.connector.connect(**config.myems_system_db)
4285
        cursor = cnx.cursor()
4286
4287
        cursor.execute(" SELECT name "
4288
                       " FROM tbl_hybrid_power_stations_pcses "
4289
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, pcsid,))
4290
        if cursor.fetchone() is None:
4291
            cursor.close()
4292
            cnx.close()
4293
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4294
                                   description='API.HYBRID_POWER_STATION_PCS_NOT_FOUND')
4295
4296
        cursor.execute(" SELECT name, object_type "
4297
                       " FROM tbl_points "
4298
                       " WHERE id = %s ", (new_values['data']['point_id'],))
4299
        row = cursor.fetchone()
4300
        if row is None:
4301
            cursor.close()
4302
            cnx.close()
4303
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4304
                                   description='API.POINT_NOT_FOUND')
4305
4306
        query = (" SELECT id " 
4307
                 " FROM tbl_hybrid_power_stations_pcses_points "
4308
                 " WHERE pcs_id = %s AND point_id = %s")
4309
        cursor.execute(query, (pcsid, new_values['data']['point_id'],))
4310
        if cursor.fetchone() is not None:
4311
            cursor.close()
4312
            cnx.close()
4313
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
4314
                                   description='API.PCS_POINT_RELATION_EXISTS')
4315
4316
        add_row = (" INSERT INTO tbl_hybrid_power_stations_pcses_points (pcs_id, point_id) "
4317
                   " VALUES (%s, %s) ")
4318
        cursor.execute(add_row, (pcsid, new_values['data']['point_id'],))
4319
        cnx.commit()
4320
        cursor.close()
4321
        cnx.close()
4322
4323
        resp.status = falcon.HTTP_201
4324
        resp.location = '/hybridpowerstations/' + str(id_) + '/pcses/' + str(pcsid) + '/points/' + \
4325
                        str(new_values['data']['point_id'])
4326
4327
4328
class HybridPowerStationPCSPointItem:
@@ 3627-3743 (lines=117) @@
3624
        resp.status = falcon.HTTP_200
3625
3626
3627
class HybridPowerStationMCUPointCollection:
3628
    def __init__(self):
3629
        """Initializes Class"""
3630
        pass
3631
3632
    @staticmethod
3633
    def on_options(req, resp, id_, mid):
3634
        resp.status = falcon.HTTP_200
3635
3636
    @staticmethod
3637
    def on_get(req, resp, id_, mid):
3638
        if 'API-KEY' not in req.headers or \
3639
                not isinstance(req.headers['API-KEY'], str) or \
3640
                len(str.strip(req.headers['API-KEY'])) == 0:
3641
            access_control(req)
3642
        else:
3643
            api_key_control(req)
3644
        if not id_.isdigit() or int(id_) <= 0:
3645
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3646
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
3647
        if not mid.isdigit() or int(mid) <= 0:
3648
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3649
                                   description='API.INVALID_MCU_ID')
3650
3651
        cnx = mysql.connector.connect(**config.myems_system_db)
3652
        cursor = cnx.cursor()
3653
3654
        cursor.execute(" SELECT name "
3655
                       " FROM tbl_hybrid_power_stations_mcus "
3656
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, mid, ))
3657
        if cursor.fetchone() is None:
3658
            cursor.close()
3659
            cnx.close()
3660
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3661
                                   description='API.HYBRID_POWER_STATION_MCU_NOT_FOUND')
3662
3663
        query = (" SELECT p.id, p.name, "
3664
                 "        ds.id, ds.name, ds.uuid, "
3665
                 "        p.address "
3666
                 " FROM tbl_points p, tbl_hybrid_power_stations_mcus_points mp, tbl_data_sources ds "
3667
                 " WHERE mp.mcu_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
3668
                 " ORDER BY p.name ")
3669
        cursor.execute(query, (mid,))
3670
        rows = cursor.fetchall()
3671
3672
        result = list()
3673
        if rows is not None and len(rows) > 0:
3674
            for row in rows:
3675
                meta_result = {"id": row[0], "name": row[1],
3676
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
3677
                               "address": row[5]}
3678
                result.append(meta_result)
3679
3680
        resp.text = json.dumps(result)
3681
3682
    @staticmethod
3683
    @user_logger
3684
    def on_post(req, resp, id_, mid):
3685
        """Handles POST requests"""
3686
        admin_control(req)
3687
        try:
3688
            raw_json = req.stream.read().decode('utf-8')
3689
        except Exception as ex:
3690
            raise falcon.HTTPError(status=falcon.HTTP_400,
3691
                                   title='API.BAD_REQUEST',
3692
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3693
3694
        if not id_.isdigit() or int(id_) <= 0:
3695
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3696
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
3697
        if not mid.isdigit() or int(mid) <= 0:
3698
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3699
                                   description='API.INVALID_MCU_ID')
3700
3701
        new_values = json.loads(raw_json)
3702
        cnx = mysql.connector.connect(**config.myems_system_db)
3703
        cursor = cnx.cursor()
3704
3705
        cursor.execute(" SELECT name "
3706
                       " FROM tbl_hybrid_power_stations_mcus "
3707
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, mid,))
3708
        if cursor.fetchone() is None:
3709
            cursor.close()
3710
            cnx.close()
3711
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3712
                                   description='API.HYBRID_POWER_STATION_MCU_NOT_FOUND')
3713
3714
        cursor.execute(" SELECT name, object_type "
3715
                       " FROM tbl_points "
3716
                       " WHERE id = %s ", (new_values['data']['point_id'],))
3717
        row = cursor.fetchone()
3718
        if row is None:
3719
            cursor.close()
3720
            cnx.close()
3721
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3722
                                   description='API.POINT_NOT_FOUND')
3723
3724
        query = (" SELECT id " 
3725
                 " FROM tbl_hybrid_power_stations_mcus_points "
3726
                 " WHERE mcu_id = %s AND point_id = %s")
3727
        cursor.execute(query, (mid, new_values['data']['point_id'],))
3728
        if cursor.fetchone() is not None:
3729
            cursor.close()
3730
            cnx.close()
3731
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
3732
                                   description='API.MCU_POINT_RELATION_EXISTS')
3733
3734
        add_row = (" INSERT INTO tbl_hybrid_power_stations_mcus_points (mcu_id, point_id) "
3735
                   " VALUES (%s, %s) ")
3736
        cursor.execute(add_row, (mid, new_values['data']['point_id'],))
3737
        cnx.commit()
3738
        cursor.close()
3739
        cnx.close()
3740
3741
        resp.status = falcon.HTTP_201
3742
        resp.location = '/hybridpowerstations/' + str(id_) + '/mcus/' + str(mid) + '/points/' + \
3743
                        str(new_values['data']['point_id'])
3744
3745
3746
class HybridPowerStationMCUPointItem:
@@ 3059-3175 (lines=117) @@
3056
        resp.status = falcon.HTTP_200
3057
3058
3059
class HybridPowerStationLoadPointCollection:
3060
    def __init__(self):
3061
        """Initializes Class"""
3062
        pass
3063
3064
    @staticmethod
3065
    def on_options(req, resp, id_, lid):
3066
        resp.status = falcon.HTTP_200
3067
3068
    @staticmethod
3069
    def on_get(req, resp, id_, lid):
3070
        if 'API-KEY' not in req.headers or \
3071
                not isinstance(req.headers['API-KEY'], str) or \
3072
                len(str.strip(req.headers['API-KEY'])) == 0:
3073
            access_control(req)
3074
        else:
3075
            api_key_control(req)
3076
        if not id_.isdigit() or int(id_) <= 0:
3077
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3078
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
3079
        if not lid.isdigit() or int(lid) <= 0:
3080
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3081
                                   description='API.INVALID_LOAD_ID')
3082
3083
        cnx = mysql.connector.connect(**config.myems_system_db)
3084
        cursor = cnx.cursor()
3085
3086
        cursor.execute(" SELECT name "
3087
                       " FROM tbl_hybrid_power_stations_loads "
3088
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, lid, ))
3089
        if cursor.fetchone() is None:
3090
            cursor.close()
3091
            cnx.close()
3092
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3093
                                   description='API.HYBRID_POWER_STATION_LOAD_NOT_FOUND')
3094
3095
        query = (" SELECT p.id, p.name, "
3096
                 "        ds.id, ds.name, ds.uuid, "
3097
                 "        p.address "
3098
                 " FROM tbl_points p, tbl_hybrid_power_stations_loads_points mp, tbl_data_sources ds "
3099
                 " WHERE mp.load_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
3100
                 " ORDER BY p.name ")
3101
        cursor.execute(query, (lid,))
3102
        rows = cursor.fetchall()
3103
3104
        result = list()
3105
        if rows is not None and len(rows) > 0:
3106
            for row in rows:
3107
                meta_result = {"id": row[0], "name": row[1],
3108
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
3109
                               "address": row[5]}
3110
                result.append(meta_result)
3111
3112
        resp.text = json.dumps(result)
3113
3114
    @staticmethod
3115
    @user_logger
3116
    def on_post(req, resp, id_, lid):
3117
        """Handles POST requests"""
3118
        admin_control(req)
3119
        try:
3120
            raw_json = req.stream.read().decode('utf-8')
3121
        except Exception as ex:
3122
            raise falcon.HTTPError(status=falcon.HTTP_400,
3123
                                   title='API.BAD_REQUEST',
3124
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3125
3126
        if not id_.isdigit() or int(id_) <= 0:
3127
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3128
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
3129
        if not lid.isdigit() or int(lid) <= 0:
3130
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3131
                                   description='API.INVALID_LOAD_ID')
3132
3133
        new_values = json.loads(raw_json)
3134
        cnx = mysql.connector.connect(**config.myems_system_db)
3135
        cursor = cnx.cursor()
3136
3137
        cursor.execute(" SELECT name "
3138
                       " FROM tbl_hybrid_power_stations_loads "
3139
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, lid,))
3140
        if cursor.fetchone() is None:
3141
            cursor.close()
3142
            cnx.close()
3143
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3144
                                   description='API.HYBRID_POWER_STATION_LOAD_NOT_FOUND')
3145
3146
        cursor.execute(" SELECT name, object_type "
3147
                       " FROM tbl_points "
3148
                       " WHERE id = %s ", (new_values['data']['point_id'],))
3149
        row = cursor.fetchone()
3150
        if row is None:
3151
            cursor.close()
3152
            cnx.close()
3153
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3154
                                   description='API.POINT_NOT_FOUND')
3155
3156
        query = (" SELECT id " 
3157
                 " FROM tbl_hybrid_power_stations_loads_points "
3158
                 " WHERE load_id = %s AND point_id = %s")
3159
        cursor.execute(query, (lid, new_values['data']['point_id'],))
3160
        if cursor.fetchone() is not None:
3161
            cursor.close()
3162
            cnx.close()
3163
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
3164
                                   description='API.LOAD_POINT_RELATION_EXISTS')
3165
3166
        add_row = (" INSERT INTO tbl_hybrid_power_stations_loads_points (load_id, point_id) "
3167
                   " VALUES (%s, %s) ")
3168
        cursor.execute(add_row, (lid, new_values['data']['point_id'],))
3169
        cnx.commit()
3170
        cursor.close()
3171
        cnx.close()
3172
3173
        resp.status = falcon.HTTP_201
3174
        resp.location = '/hybridpowerstations/' + str(id_) + '/loads/' + str(lid) + '/points/' + \
3175
                        str(new_values['data']['point_id'])
3176
3177
3178
class HybridPowerStationLoadPointItem:
@@ 2463-2579 (lines=117) @@
2460
        resp.status = falcon.HTTP_200
2461
2462
2463
class HybridPowerStationGeneratorPointCollection:
2464
    def __init__(self):
2465
        """Initializes Class"""
2466
        pass
2467
2468
    @staticmethod
2469
    def on_options(req, resp, id_, gid):
2470
        resp.status = falcon.HTTP_200
2471
2472
    @staticmethod
2473
    def on_get(req, resp, id_, gid):
2474
        if 'API-KEY' not in req.headers or \
2475
                not isinstance(req.headers['API-KEY'], str) or \
2476
                len(str.strip(req.headers['API-KEY'])) == 0:
2477
            access_control(req)
2478
        else:
2479
            api_key_control(req)
2480
        if not id_.isdigit() or int(id_) <= 0:
2481
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2482
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
2483
        if not gid.isdigit() or int(gid) <= 0:
2484
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2485
                                   description='API.INVALID_GENERATOR_ID')
2486
2487
        cnx = mysql.connector.connect(**config.myems_system_db)
2488
        cursor = cnx.cursor()
2489
2490
        cursor.execute(" SELECT name "
2491
                       " FROM tbl_hybrid_power_stations_generators "
2492
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, gid, ))
2493
        if cursor.fetchone() is None:
2494
            cursor.close()
2495
            cnx.close()
2496
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2497
                                   description='API.HYBRID_POWER_STATION_GENERATOR_NOT_FOUND')
2498
2499
        query = (" SELECT p.id, p.name, "
2500
                 "        ds.id, ds.name, ds.uuid, "
2501
                 "        p.address "
2502
                 " FROM tbl_points p, tbl_hybrid_power_stations_generators_points mp, tbl_data_sources ds "
2503
                 " WHERE mp.generator_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
2504
                 " ORDER BY p.name ")
2505
        cursor.execute(query, (gid,))
2506
        rows = cursor.fetchall()
2507
2508
        result = list()
2509
        if rows is not None and len(rows) > 0:
2510
            for row in rows:
2511
                meta_result = {"id": row[0], "name": row[1],
2512
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
2513
                               "address": row[5]}
2514
                result.append(meta_result)
2515
2516
        resp.text = json.dumps(result)
2517
2518
    @staticmethod
2519
    @user_logger
2520
    def on_post(req, resp, id_, gid):
2521
        """Handles POST requests"""
2522
        admin_control(req)
2523
        try:
2524
            raw_json = req.stream.read().decode('utf-8')
2525
        except Exception as ex:
2526
            raise falcon.HTTPError(status=falcon.HTTP_400,
2527
                                   title='API.BAD_REQUEST',
2528
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2529
2530
        if not id_.isdigit() or int(id_) <= 0:
2531
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2532
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
2533
        if not gid.isdigit() or int(gid) <= 0:
2534
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2535
                                   description='API.INVALID_GENERATOR_ID')
2536
2537
        new_values = json.loads(raw_json)
2538
        cnx = mysql.connector.connect(**config.myems_system_db)
2539
        cursor = cnx.cursor()
2540
2541
        cursor.execute(" SELECT name "
2542
                       " FROM tbl_hybrid_power_stations_generators "
2543
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, gid,))
2544
        if cursor.fetchone() is None:
2545
            cursor.close()
2546
            cnx.close()
2547
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2548
                                   description='API.HYBRID_POWER_STATION_GENERATOR_NOT_FOUND')
2549
2550
        cursor.execute(" SELECT name, object_type "
2551
                       " FROM tbl_points "
2552
                       " WHERE id = %s ", (new_values['data']['point_id'],))
2553
        row = cursor.fetchone()
2554
        if row is None:
2555
            cursor.close()
2556
            cnx.close()
2557
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2558
                                   description='API.POINT_NOT_FOUND')
2559
2560
        query = (" SELECT id " 
2561
                 " FROM tbl_hybrid_power_stations_generators_points "
2562
                 " WHERE generator_id = %s AND point_id = %s")
2563
        cursor.execute(query, (gid, new_values['data']['point_id'],))
2564
        if cursor.fetchone() is not None:
2565
            cursor.close()
2566
            cnx.close()
2567
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
2568
                                   description='API.GENERATOR_POINT_RELATION_EXISTS')
2569
2570
        add_row = (" INSERT INTO tbl_hybrid_power_stations_generators_points (generator_id, point_id) "
2571
                   " VALUES (%s, %s) ")
2572
        cursor.execute(add_row, (gid, new_values['data']['point_id'],))
2573
        cnx.commit()
2574
        cursor.close()
2575
        cnx.close()
2576
2577
        resp.status = falcon.HTTP_201
2578
        resp.location = '/hybridpowerstations/' + str(id_) + '/generators/' + str(gid) + '/points/' + \
2579
                        str(new_values['data']['point_id'])
2580
2581
2582
class HybridPowerStationGeneratorPointItem:
@@ 1879-1995 (lines=117) @@
1876
        resp.status = falcon.HTTP_200
1877
1878
1879
class HybridPowerStationCMPointCollection:
1880
    def __init__(self):
1881
        """Initializes Class"""
1882
        pass
1883
1884
    @staticmethod
1885
    def on_options(req, resp, id_, did):
1886
        resp.status = falcon.HTTP_200
1887
1888
    @staticmethod
1889
    def on_get(req, resp, id_, did):
1890
        if 'API-KEY' not in req.headers or \
1891
                not isinstance(req.headers['API-KEY'], str) or \
1892
                len(str.strip(req.headers['API-KEY'])) == 0:
1893
            access_control(req)
1894
        else:
1895
            api_key_control(req)
1896
        if not id_.isdigit() or int(id_) <= 0:
1897
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1898
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
1899
        if not did.isdigit() or int(did) <= 0:
1900
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1901
                                   description='API.INVALID_CM_ID')
1902
1903
        cnx = mysql.connector.connect(**config.myems_system_db)
1904
        cursor = cnx.cursor()
1905
1906
        cursor.execute(" SELECT name "
1907
                       " FROM tbl_hybrid_power_stations_cms "
1908
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, did, ))
1909
        if cursor.fetchone() is None:
1910
            cursor.close()
1911
            cnx.close()
1912
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1913
                                   description='API.HYBRID_POWER_STATION_CM_NOT_FOUND')
1914
1915
        query = (" SELECT p.id, p.name, "
1916
                 "        ds.id, ds.name, ds.uuid, "
1917
                 "        p.address "
1918
                 " FROM tbl_points p, tbl_hybrid_power_stations_cms_points mp, tbl_data_sources ds "
1919
                 " WHERE mp.cm_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
1920
                 " ORDER BY p.name ")
1921
        cursor.execute(query, (did,))
1922
        rows = cursor.fetchall()
1923
1924
        result = list()
1925
        if rows is not None and len(rows) > 0:
1926
            for row in rows:
1927
                meta_result = {"id": row[0], "name": row[1],
1928
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
1929
                               "address": row[5]}
1930
                result.append(meta_result)
1931
1932
        resp.text = json.dumps(result)
1933
1934
    @staticmethod
1935
    @user_logger
1936
    def on_post(req, resp, id_, did):
1937
        """Handles POST requests"""
1938
        admin_control(req)
1939
        try:
1940
            raw_json = req.stream.read().decode('utf-8')
1941
        except Exception as ex:
1942
            raise falcon.HTTPError(status=falcon.HTTP_400,
1943
                                   title='API.BAD_REQUEST',
1944
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1945
1946
        if not id_.isdigit() or int(id_) <= 0:
1947
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1948
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
1949
        if not did.isdigit() or int(did) <= 0:
1950
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1951
                                   description='API.INVALID_CM_ID')
1952
1953
        new_values = json.loads(raw_json)
1954
        cnx = mysql.connector.connect(**config.myems_system_db)
1955
        cursor = cnx.cursor()
1956
1957
        cursor.execute(" SELECT name "
1958
                       " FROM tbl_hybrid_power_stations_cms "
1959
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, did,))
1960
        if cursor.fetchone() is None:
1961
            cursor.close()
1962
            cnx.close()
1963
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1964
                                   description='API.HYBRID_POWER_STATION_CM_NOT_FOUND')
1965
1966
        cursor.execute(" SELECT name, object_type "
1967
                       " FROM tbl_points "
1968
                       " WHERE id = %s ", (new_values['data']['point_id'],))
1969
        row = cursor.fetchone()
1970
        if row is None:
1971
            cursor.close()
1972
            cnx.close()
1973
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1974
                                   description='API.POINT_NOT_FOUND')
1975
1976
        query = (" SELECT id " 
1977
                 " FROM tbl_hybrid_power_stations_cms_points "
1978
                 " WHERE cm_id = %s AND point_id = %s")
1979
        cursor.execute(query, (did, new_values['data']['point_id'],))
1980
        if cursor.fetchone() is not None:
1981
            cursor.close()
1982
            cnx.close()
1983
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
1984
                                   description='API.CM_POINT_RELATION_EXISTS')
1985
1986
        add_row = (" INSERT INTO tbl_hybrid_power_stations_cms_points (cm_id, point_id) "
1987
                   " VALUES (%s, %s) ")
1988
        cursor.execute(add_row, (did, new_values['data']['point_id'],))
1989
        cnx.commit()
1990
        cursor.close()
1991
        cnx.close()
1992
1993
        resp.status = falcon.HTTP_201
1994
        resp.location = '/hybridpowerstations/' + str(id_) + '/cms/' + str(did) + '/points/' + \
1995
                        str(new_values['data']['point_id'])
1996
1997
1998
class HybridPowerStationCMPointItem:
@@ 1141-1257 (lines=117) @@
1138
        resp.status = falcon.HTTP_200
1139
1140
1141
class HybridPowerStationBMSPointCollection:
1142
    def __init__(self):
1143
        """Initializes Class"""
1144
        pass
1145
1146
    @staticmethod
1147
    def on_options(req, resp, id_, bid):
1148
        resp.status = falcon.HTTP_200
1149
1150
    @staticmethod
1151
    def on_get(req, resp, id_, bid):
1152
        if 'API-KEY' not in req.headers or \
1153
                not isinstance(req.headers['API-KEY'], str) or \
1154
                len(str.strip(req.headers['API-KEY'])) == 0:
1155
            access_control(req)
1156
        else:
1157
            api_key_control(req)
1158
        if not id_.isdigit() or int(id_) <= 0:
1159
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1160
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
1161
        if not bid.isdigit() or int(bid) <= 0:
1162
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1163
                                   description='API.INVALID_BMS_ID')
1164
1165
        cnx = mysql.connector.connect(**config.myems_system_db)
1166
        cursor = cnx.cursor()
1167
1168
        cursor.execute(" SELECT name "
1169
                       " FROM tbl_hybrid_power_stations_bmses "
1170
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, bid, ))
1171
        if cursor.fetchone() is None:
1172
            cursor.close()
1173
            cnx.close()
1174
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1175
                                   description='API.HYBRID_POWER_STATION_BMS_NOT_FOUND')
1176
1177
        query = (" SELECT p.id, p.name, "
1178
                 "        ds.id, ds.name, ds.uuid, "
1179
                 "        p.address "
1180
                 " FROM tbl_points p, tbl_hybrid_power_stations_bmses_points mp, tbl_data_sources ds "
1181
                 " WHERE mp.bms_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
1182
                 " ORDER BY p.name ")
1183
        cursor.execute(query, (bid,))
1184
        rows = cursor.fetchall()
1185
1186
        result = list()
1187
        if rows is not None and len(rows) > 0:
1188
            for row in rows:
1189
                meta_result = {"id": row[0], "name": row[1],
1190
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
1191
                               "address": row[5]}
1192
                result.append(meta_result)
1193
1194
        resp.text = json.dumps(result)
1195
1196
    @staticmethod
1197
    @user_logger
1198
    def on_post(req, resp, id_, bid):
1199
        """Handles POST requests"""
1200
        admin_control(req)
1201
        try:
1202
            raw_json = req.stream.read().decode('utf-8')
1203
        except Exception as ex:
1204
            raise falcon.HTTPError(status=falcon.HTTP_400,
1205
                                   title='API.BAD_REQUEST',
1206
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1207
1208
        if not id_.isdigit() or int(id_) <= 0:
1209
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1210
                                   description='API.INVALID_HYBRID_POWER_STATION_ID')
1211
        if not bid.isdigit() or int(bid) <= 0:
1212
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1213
                                   description='API.INVALID_BMS_ID')
1214
1215
        new_values = json.loads(raw_json)
1216
        cnx = mysql.connector.connect(**config.myems_system_db)
1217
        cursor = cnx.cursor()
1218
1219
        cursor.execute(" SELECT name "
1220
                       " FROM tbl_hybrid_power_stations_bmses "
1221
                       " WHERE hybrid_power_station_id = %s AND id = %s ", (id_, bid,))
1222
        if cursor.fetchone() is None:
1223
            cursor.close()
1224
            cnx.close()
1225
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1226
                                   description='API.HYBRID_POWER_STATION_BMS_NOT_FOUND')
1227
1228
        cursor.execute(" SELECT name, object_type "
1229
                       " FROM tbl_points "
1230
                       " WHERE id = %s ", (new_values['data']['point_id'],))
1231
        row = cursor.fetchone()
1232
        if row is None:
1233
            cursor.close()
1234
            cnx.close()
1235
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1236
                                   description='API.POINT_NOT_FOUND')
1237
1238
        query = (" SELECT id " 
1239
                 " FROM tbl_hybrid_power_stations_bmses_points "
1240
                 " WHERE bms_id = %s AND point_id = %s")
1241
        cursor.execute(query, (bid, new_values['data']['point_id'],))
1242
        if cursor.fetchone() is not None:
1243
            cursor.close()
1244
            cnx.close()
1245
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
1246
                                   description='API.BMS_POINT_RELATION_EXISTS')
1247
1248
        add_row = (" INSERT INTO tbl_hybrid_power_stations_bmses_points (bms_id, point_id) "
1249
                   " VALUES (%s, %s) ")
1250
        cursor.execute(add_row, (bid, new_values['data']['point_id'],))
1251
        cnx.commit()
1252
        cursor.close()
1253
        cnx.close()
1254
1255
        resp.status = falcon.HTTP_201
1256
        resp.location = '/hybridpowerstations/' + str(id_) + '/bmses/' + str(bid) + '/points/' + \
1257
                        str(new_values['data']['point_id'])
1258
1259
1260
class HybridPowerStationBMSPointItem: