Code Duplication    Length = 173-175 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 3158-3332 (lines=175) @@
3155
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3156
3157
3158
class EnergyStorageContainerHVACItem:
3159
    def __init__(self):
3160
        """Initializes Class"""
3161
        pass
3162
3163
    @staticmethod
3164
    def on_options(req, resp, id_, hid):
3165
        _=req
3166
        resp.status = falcon.HTTP_200
3167
        _=id_
3168
    @staticmethod
3169
    def on_get(req, resp, id_, hid):
3170
        access_control(req)
3171
        if not id_.isdigit() or int(id_) <= 0:
3172
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3173
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3174
        if not hid.isdigit() or int(hid) <= 0:
3175
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3176
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3177
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 "
3183
                       " WHERE id = %s ", (id_,))
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_NOT_FOUND')
3189
3190
        query = (" SELECT id, name, uuid "
3191
                 " FROM tbl_energy_storage_containers ")
3192
        cursor.execute(query)
3193
        rows_energystoragecontainers = cursor.fetchall()
3194
3195
        energy_storage_container_dict = dict()
3196
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
3197
            for row in rows_energystoragecontainers:
3198
                energy_storage_container_dict[row[0]] = {"id": row[0],
3199
                                                         "name": row[1],
3200
                                                         "uuid": row[2]}
3201
3202
        query = (" SELECT id, name, uuid "
3203
                 " FROM tbl_energy_storage_containers_hvacs "
3204
                 " WHERE id = %s ")
3205
        cursor.execute(query, (hid,))
3206
        row = cursor.fetchone()
3207
        cursor.close()
3208
        cnx.close()
3209
3210
        if row is None:
3211
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3212
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3213
        else:
3214
            meta_result = {"id": row[0],
3215
                           "name": row[1],
3216
                           "uuid": row[2]
3217
                           }
3218
3219
        resp.text = json.dumps(meta_result)
3220
3221
    @staticmethod
3222
    @user_logger
3223
    def on_delete(req, resp, id_, hid):
3224
        admin_control(req)
3225
        if not id_.isdigit() or int(id_) <= 0:
3226
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3227
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3228
        if not hid.isdigit() or int(hid) <= 0:
3229
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3230
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3231
3232
        cnx = mysql.connector.connect(**config.myems_system_db)
3233
        cursor = cnx.cursor()
3234
3235
        cursor.execute(" SELECT name "
3236
                       " FROM tbl_energy_storage_containers "
3237
                       " WHERE id = %s ", (id_,))
3238
        if cursor.fetchone() is None:
3239
            cursor.close()
3240
            cnx.close()
3241
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3242
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3243
3244
        cursor.execute(" SELECT name "
3245
                       " FROM tbl_energy_storage_containers_hvacs "
3246
                       " WHERE id = %s ", (hid,))
3247
        if cursor.fetchone() is None:
3248
            cursor.close()
3249
            cnx.close()
3250
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3251
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3252
3253
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_hvacs "
3254
                       " WHERE id = %s ", (hid,))
3255
        cnx.commit()
3256
3257
        cursor.close()
3258
        cnx.close()
3259
3260
        resp.status = falcon.HTTP_204
3261
3262
    @staticmethod
3263
    @user_logger
3264
    def on_put(req, resp, id_, hid):
3265
        """Handles PUT requests"""
3266
        admin_control(req)
3267
        try:
3268
            raw_json = req.stream.read().decode('utf-8')
3269
        except Exception as ex:
3270
            print(str(ex))
3271
            raise falcon.HTTPError(status=falcon.HTTP_400,
3272
                                   title='API.BAD_REQUEST',
3273
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3274
        if not id_.isdigit() or int(id_) <= 0:
3275
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3276
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3277
3278
        if not hid.isdigit() or int(hid) <= 0:
3279
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3280
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_ID')
3281
3282
        new_values = json.loads(raw_json)
3283
3284
        if 'name' not in new_values['data'].keys() or \
3285
                not isinstance(new_values['data']['name'], str) or \
3286
                len(str.strip(new_values['data']['name'])) == 0:
3287
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3288
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3289
        name = str.strip(new_values['data']['name'])
3290
3291
        cnx = mysql.connector.connect(**config.myems_system_db)
3292
        cursor = cnx.cursor()
3293
3294
        cursor.execute(" SELECT name "
3295
                       " FROM tbl_energy_storage_containers "
3296
                       " WHERE id = %s ", (id_,))
3297
        if cursor.fetchone() is None:
3298
            cursor.close()
3299
            cnx.close()
3300
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3301
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3302
3303
        cursor.execute(" SELECT name "
3304
                       " FROM tbl_energy_storage_containers_hvacs "
3305
                       " WHERE id = %s ", (hid,))
3306
        if cursor.fetchone() is None:
3307
            cursor.close()
3308
            cnx.close()
3309
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3310
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NOT_FOUND')
3311
3312
        cursor.execute(" SELECT name "
3313
                       " FROM tbl_energy_storage_containers_hvacs "
3314
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
3315
                       (id_, name, hid))
3316
        if cursor.fetchone() is not None:
3317
            cursor.close()
3318
            cnx.close()
3319
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3320
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3321
3322
        update_row = (" UPDATE tbl_energy_storage_containers_hvacs "
3323
                      " SET name = %s "
3324
                      "     WHERE id = %s ")
3325
        cursor.execute(update_row, (name,
3326
                                    hid))
3327
        cnx.commit()
3328
3329
        cursor.close()
3330
        cnx.close()
3331
3332
        resp.status = falcon.HTTP_200
3333
3334
3335
class EnergyStorageContainerHVACPointCollection:
@@ 2001-2175 (lines=175) @@
1998
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
1999
2000
2001
class EnergyStorageContainerFirecontrolItem:
2002
    def __init__(self):
2003
        """Initializes Class"""
2004
        pass
2005
2006
    @staticmethod
2007
    def on_options(req, resp, id_, fid):
2008
        _=req
2009
        resp.status = falcon.HTTP_200
2010
        _=id_
2011
    @staticmethod
2012
    def on_get(req, resp, id_, fid):
2013
        access_control(req)
2014
        if not id_.isdigit() or int(id_) <= 0:
2015
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2016
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2017
        if not fid.isdigit() or int(fid) <= 0:
2018
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2019
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2020
2021
        cnx = mysql.connector.connect(**config.myems_system_db)
2022
        cursor = cnx.cursor()
2023
2024
        cursor.execute(" SELECT name "
2025
                       " FROM tbl_energy_storage_containers "
2026
                       " WHERE id = %s ", (id_,))
2027
        if cursor.fetchone() is None:
2028
            cursor.close()
2029
            cnx.close()
2030
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2031
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2032
2033
        query = (" SELECT id, name, uuid "
2034
                 " FROM tbl_energy_storage_containers ")
2035
        cursor.execute(query)
2036
        rows_energystoragecontainers = cursor.fetchall()
2037
2038
        energy_storage_container_dict = dict()
2039
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
2040
            for row in rows_energystoragecontainers:
2041
                energy_storage_container_dict[row[0]] = {"id": row[0],
2042
                                                         "name": row[1],
2043
                                                         "uuid": row[2]}
2044
2045
        query = (" SELECT id, name, uuid "
2046
                 " FROM tbl_energy_storage_containers_firecontrols "
2047
                 " WHERE id = %s ")
2048
        cursor.execute(query, (fid,))
2049
        row = cursor.fetchone()
2050
        cursor.close()
2051
        cnx.close()
2052
2053
        if row is None:
2054
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2055
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2056
        else:
2057
            meta_result = {"id": row[0],
2058
                           "name": row[1],
2059
                           "uuid": row[2]
2060
                           }
2061
2062
        resp.text = json.dumps(meta_result)
2063
2064
    @staticmethod
2065
    @user_logger
2066
    def on_delete(req, resp, id_, fid):
2067
        admin_control(req)
2068
        if not id_.isdigit() or int(id_) <= 0:
2069
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2070
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2071
        if not fid.isdigit() or int(fid) <= 0:
2072
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2073
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2074
2075
        cnx = mysql.connector.connect(**config.myems_system_db)
2076
        cursor = cnx.cursor()
2077
2078
        cursor.execute(" SELECT name "
2079
                       " FROM tbl_energy_storage_containers "
2080
                       " WHERE id = %s ", (id_,))
2081
        if cursor.fetchone() is None:
2082
            cursor.close()
2083
            cnx.close()
2084
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2085
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2086
2087
        cursor.execute(" SELECT name "
2088
                       " FROM tbl_energy_storage_containers_firecontrols "
2089
                       " WHERE id = %s ", (fid,))
2090
        if cursor.fetchone() is None:
2091
            cursor.close()
2092
            cnx.close()
2093
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2094
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2095
2096
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_firecontrols "
2097
                       " WHERE id = %s ", (fid,))
2098
        cnx.commit()
2099
2100
        cursor.close()
2101
        cnx.close()
2102
2103
        resp.status = falcon.HTTP_204
2104
2105
    @staticmethod
2106
    @user_logger
2107
    def on_put(req, resp, id_, fid):
2108
        """Handles PUT requests"""
2109
        admin_control(req)
2110
        try:
2111
            raw_json = req.stream.read().decode('utf-8')
2112
        except Exception as ex:
2113
            print(str(ex))
2114
            raise falcon.HTTPError(status=falcon.HTTP_400,
2115
                                   title='API.BAD_REQUEST',
2116
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2117
        if not id_.isdigit() or int(id_) <= 0:
2118
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2119
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2120
2121
        if not fid.isdigit() or int(fid) <= 0:
2122
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2123
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_ID')
2124
2125
        new_values = json.loads(raw_json)
2126
2127
        if 'name' not in new_values['data'].keys() or \
2128
                not isinstance(new_values['data']['name'], str) or \
2129
                len(str.strip(new_values['data']['name'])) == 0:
2130
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2131
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2132
        name = str.strip(new_values['data']['name'])
2133
2134
        cnx = mysql.connector.connect(**config.myems_system_db)
2135
        cursor = cnx.cursor()
2136
2137
        cursor.execute(" SELECT name "
2138
                       " FROM tbl_energy_storage_containers "
2139
                       " WHERE id = %s ", (id_,))
2140
        if cursor.fetchone() is None:
2141
            cursor.close()
2142
            cnx.close()
2143
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2144
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2145
2146
        cursor.execute(" SELECT name "
2147
                       " FROM tbl_energy_storage_containers_firecontrols "
2148
                       " WHERE id = %s ", (fid,))
2149
        if cursor.fetchone() is None:
2150
            cursor.close()
2151
            cnx.close()
2152
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2153
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NOT_FOUND')
2154
2155
        cursor.execute(" SELECT name "
2156
                       " FROM tbl_energy_storage_containers_firecontrols "
2157
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
2158
                       (id_, name, fid))
2159
        if cursor.fetchone() is not None:
2160
            cursor.close()
2161
            cnx.close()
2162
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2163
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2164
2165
        update_row = (" UPDATE tbl_energy_storage_containers_firecontrols "
2166
                      " SET name = %s "
2167
                      "     WHERE id = %s ")
2168
        cursor.execute(update_row, (name,
2169
                                    fid))
2170
        cnx.commit()
2171
2172
        cursor.close()
2173
        cnx.close()
2174
2175
        resp.status = falcon.HTTP_200
2176
2177
2178
class EnergyStorageContainerFirecontrolPointCollection:
@@ 5134-5306 (lines=173) @@
5131
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5132
5133
5134
class EnergyStorageContainerSTSItem:
5135
    def __init__(self):
5136
        """Initializes Class"""
5137
        pass
5138
5139
    @staticmethod
5140
    def on_options(req, resp, id_, fid):
5141
        resp.status = falcon.HTTP_200
5142
5143
    @staticmethod
5144
    def on_get(req, resp, id_, fid):
5145
        access_control(req)
5146
        if not id_.isdigit() or int(id_) <= 0:
5147
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5148
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5149
        if not fid.isdigit() or int(fid) <= 0:
5150
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5151
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5152
5153
        cnx = mysql.connector.connect(**config.myems_system_db)
5154
        cursor = cnx.cursor()
5155
5156
        cursor.execute(" SELECT name "
5157
                       " FROM tbl_energy_storage_containers "
5158
                       " WHERE id = %s ", (id_,))
5159
        if cursor.fetchone() is None:
5160
            cursor.close()
5161
            cnx.close()
5162
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5163
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5164
5165
        query = (" SELECT id, name, uuid "
5166
                 " FROM tbl_energy_storage_containers ")
5167
        cursor.execute(query)
5168
        rows_energystoragecontainers = cursor.fetchall()
5169
5170
        energy_storage_container_dict = dict()
5171
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
5172
            for row in rows_energystoragecontainers:
5173
                energy_storage_container_dict[row[0]] = {"id": row[0],
5174
                                                         "name": row[1],
5175
                                                         "uuid": row[2]}
5176
5177
        query = (" SELECT id, name, uuid "
5178
                 " FROM tbl_energy_storage_containers_stses "
5179
                 " WHERE id = %s ")
5180
        cursor.execute(query, (fid,))
5181
        row = cursor.fetchone()
5182
        cursor.close()
5183
        cnx.close()
5184
5185
        if row is None:
5186
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5187
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5188
        else:
5189
            meta_result = {"id": row[0],
5190
                           "name": row[1],
5191
                           "uuid": row[2]
5192
                           }
5193
5194
        resp.text = json.dumps(meta_result)
5195
5196
    @staticmethod
5197
    @user_logger
5198
    def on_delete(req, resp, id_, fid):
5199
        admin_control(req)
5200
        if not id_.isdigit() or int(id_) <= 0:
5201
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5202
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5203
        if not fid.isdigit() or int(fid) <= 0:
5204
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5205
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5206
5207
        cnx = mysql.connector.connect(**config.myems_system_db)
5208
        cursor = cnx.cursor()
5209
5210
        cursor.execute(" SELECT name "
5211
                       " FROM tbl_energy_storage_containers "
5212
                       " WHERE id = %s ", (id_,))
5213
        if cursor.fetchone() is None:
5214
            cursor.close()
5215
            cnx.close()
5216
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5217
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5218
5219
        cursor.execute(" SELECT name "
5220
                       " FROM tbl_energy_storage_containers_stses "
5221
                       " WHERE id = %s ", (fid,))
5222
        if cursor.fetchone() is None:
5223
            cursor.close()
5224
            cnx.close()
5225
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5226
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5227
5228
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_stses "
5229
                       " WHERE id = %s ", (fid,))
5230
        cnx.commit()
5231
5232
        cursor.close()
5233
        cnx.close()
5234
5235
        resp.status = falcon.HTTP_204
5236
5237
    @staticmethod
5238
    @user_logger
5239
    def on_put(req, resp, id_, fid):
5240
        """Handles PUT requests"""
5241
        admin_control(req)
5242
        try:
5243
            raw_json = req.stream.read().decode('utf-8')
5244
        except Exception as ex:
5245
            raise falcon.HTTPError(status=falcon.HTTP_400,
5246
                                   title='API.BAD_REQUEST',
5247
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5248
        if not id_.isdigit() or int(id_) <= 0:
5249
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5250
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5251
5252
        if not fid.isdigit() or int(fid) <= 0:
5253
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5254
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_ID')
5255
5256
        new_values = json.loads(raw_json)
5257
5258
        if 'name' not in new_values['data'].keys() or \
5259
                not isinstance(new_values['data']['name'], str) or \
5260
                len(str.strip(new_values['data']['name'])) == 0:
5261
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5262
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5263
        name = str.strip(new_values['data']['name'])
5264
5265
        cnx = mysql.connector.connect(**config.myems_system_db)
5266
        cursor = cnx.cursor()
5267
5268
        cursor.execute(" SELECT name "
5269
                       " FROM tbl_energy_storage_containers "
5270
                       " WHERE id = %s ", (id_,))
5271
        if cursor.fetchone() is None:
5272
            cursor.close()
5273
            cnx.close()
5274
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5275
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5276
5277
        cursor.execute(" SELECT name "
5278
                       " FROM tbl_energy_storage_containers_stses "
5279
                       " WHERE id = %s ", (fid,))
5280
        if cursor.fetchone() is None:
5281
            cursor.close()
5282
            cnx.close()
5283
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5284
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NOT_FOUND')
5285
5286
        cursor.execute(" SELECT name "
5287
                       " FROM tbl_energy_storage_containers_stses "
5288
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
5289
                       (id_, name, fid))
5290
        if cursor.fetchone() is not None:
5291
            cursor.close()
5292
            cnx.close()
5293
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5294
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5295
5296
        update_row = (" UPDATE tbl_energy_storage_containers_stses "
5297
                      " SET name = %s "
5298
                      "     WHERE id = %s ")
5299
        cursor.execute(update_row, (name,
5300
                                    fid))
5301
        cnx.commit()
5302
5303
        cursor.close()
5304
        cnx.close()
5305
5306
        resp.status = falcon.HTTP_200
5307
5308
5309
class EnergyStorageContainerSTSPointCollection:
@@ 1518-1690 (lines=173) @@
1515
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1516
1517
1518
class EnergyStorageContainerDCDCItem:
1519
    def __init__(self):
1520
        """Initializes Class"""
1521
        pass
1522
1523
    @staticmethod
1524
    def on_options(req, resp, id_, did):
1525
        resp.status = falcon.HTTP_200
1526
1527
    @staticmethod
1528
    def on_get(req, resp, id_, did):
1529
        access_control(req)
1530
        if not id_.isdigit() or int(id_) <= 0:
1531
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1532
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1533
        if not did.isdigit() or int(did) <= 0:
1534
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1535
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1536
1537
        cnx = mysql.connector.connect(**config.myems_system_db)
1538
        cursor = cnx.cursor()
1539
1540
        cursor.execute(" SELECT name "
1541
                       " FROM tbl_energy_storage_containers "
1542
                       " WHERE id = %s ", (id_,))
1543
        if cursor.fetchone() is None:
1544
            cursor.close()
1545
            cnx.close()
1546
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1547
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1548
1549
        query = (" SELECT id, name, uuid "
1550
                 " FROM tbl_energy_storage_containers ")
1551
        cursor.execute(query)
1552
        rows_energystoragecontainers = cursor.fetchall()
1553
1554
        energy_storage_container_dict = dict()
1555
        if rows_energystoragecontainers is not None and len(rows_energystoragecontainers) > 0:
1556
            for row in rows_energystoragecontainers:
1557
                energy_storage_container_dict[row[0]] = {"id": row[0],
1558
                                                         "name": row[1],
1559
                                                         "uuid": row[2]}
1560
1561
        query = (" SELECT id, name, uuid "
1562
                 " FROM tbl_energy_storage_containers_dcdcs "
1563
                 " WHERE id = %s ")
1564
        cursor.execute(query, (did,))
1565
        row = cursor.fetchone()
1566
        cursor.close()
1567
        cnx.close()
1568
1569
        if row is None:
1570
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1571
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1572
        else:
1573
            meta_result = {"id": row[0],
1574
                           "name": row[1],
1575
                           "uuid": row[2]
1576
                           }
1577
1578
        resp.text = json.dumps(meta_result)
1579
1580
    @staticmethod
1581
    @user_logger
1582
    def on_delete(req, resp, id_, did):
1583
        admin_control(req)
1584
        if not id_.isdigit() or int(id_) <= 0:
1585
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1586
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1587
        if not did.isdigit() or int(did) <= 0:
1588
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1589
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1590
1591
        cnx = mysql.connector.connect(**config.myems_system_db)
1592
        cursor = cnx.cursor()
1593
1594
        cursor.execute(" SELECT name "
1595
                       " FROM tbl_energy_storage_containers "
1596
                       " WHERE id = %s ", (id_,))
1597
        if cursor.fetchone() is None:
1598
            cursor.close()
1599
            cnx.close()
1600
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1601
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1602
1603
        cursor.execute(" SELECT name "
1604
                       " FROM tbl_energy_storage_containers_dcdcs "
1605
                       " WHERE id = %s ", (did,))
1606
        if cursor.fetchone() is None:
1607
            cursor.close()
1608
            cnx.close()
1609
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1610
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1611
1612
        cursor.execute(" DELETE FROM tbl_energy_storage_containers_dcdcs "
1613
                       " WHERE id = %s ", (did,))
1614
        cnx.commit()
1615
1616
        cursor.close()
1617
        cnx.close()
1618
1619
        resp.status = falcon.HTTP_204
1620
1621
    @staticmethod
1622
    @user_logger
1623
    def on_put(req, resp, id_, did):
1624
        """Handles PUT requests"""
1625
        admin_control(req)
1626
        try:
1627
            raw_json = req.stream.read().decode('utf-8')
1628
        except Exception as ex:
1629
            raise falcon.HTTPError(status=falcon.HTTP_400,
1630
                                   title='API.BAD_REQUEST',
1631
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1632
        if not id_.isdigit() or int(id_) <= 0:
1633
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1634
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1635
1636
        if not did.isdigit() or int(did) <= 0:
1637
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1638
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_ID')
1639
1640
        new_values = json.loads(raw_json)
1641
1642
        if 'name' not in new_values['data'].keys() or \
1643
                not isinstance(new_values['data']['name'], str) or \
1644
                len(str.strip(new_values['data']['name'])) == 0:
1645
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1646
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1647
        name = str.strip(new_values['data']['name'])
1648
1649
        cnx = mysql.connector.connect(**config.myems_system_db)
1650
        cursor = cnx.cursor()
1651
1652
        cursor.execute(" SELECT name "
1653
                       " FROM tbl_energy_storage_containers "
1654
                       " WHERE id = %s ", (id_,))
1655
        if cursor.fetchone() is None:
1656
            cursor.close()
1657
            cnx.close()
1658
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1659
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1660
1661
        cursor.execute(" SELECT name "
1662
                       " FROM tbl_energy_storage_containers_dcdcs "
1663
                       " WHERE id = %s ", (did,))
1664
        if cursor.fetchone() is None:
1665
            cursor.close()
1666
            cnx.close()
1667
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1668
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NOT_FOUND')
1669
1670
        cursor.execute(" SELECT name "
1671
                       " FROM tbl_energy_storage_containers_dcdcs "
1672
                       " WHERE energy_storage_container_id = %s AND name = %s AND id != %s ",
1673
                       (id_, name, did))
1674
        if cursor.fetchone() is not None:
1675
            cursor.close()
1676
            cnx.close()
1677
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1678
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1679
1680
        update_row = (" UPDATE tbl_energy_storage_containers_dcdcs "
1681
                      " SET name = %s "
1682
                      "     WHERE id = %s ")
1683
        cursor.execute(update_row, (name,
1684
                                    did))
1685
        cnx.commit()
1686
1687
        cursor.close()
1688
        cnx.close()
1689
1690
        resp.status = falcon.HTTP_200
1691
1692
1693
class EnergyStorageContainerDCDCPointCollection: