Code Duplication    Length = 60-60 lines in 2 locations

myems-api/core/controlmode.py 2 locations

@@ 354-413 (lines=60) @@
351
        resp.status = falcon.HTTP_200
352
        _ = id_
353
354
    @staticmethod
355
    def on_get(req, resp, id_):
356
        """Handles GET requests"""
357
        if 'API-KEY' not in req.headers or \
358
                not isinstance(req.headers['API-KEY'], str) or \
359
                len(str.strip(req.headers['API-KEY'])) == 0:
360
            access_control(req)
361
        else:
362
            api_key_control(req)
363
        if not id_.isdigit() or int(id_) <= 0:
364
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
365
                                   description='API.INVALID_CONTROL_MODE_ID')
366
367
        cnx = mysql.connector.connect(**config.myems_system_db)
368
        cursor = cnx.cursor()
369
370
        query = (" SELECT id, name, uuid, is_active "
371
                 " FROM tbl_control_modes "
372
                 " WHERE id = %s ")
373
        cursor.execute(query, (id_,))
374
        row = cursor.fetchone()
375
        if row is None:
376
            cursor.close()
377
            cnx.close()
378
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
379
                                   description='API.CONTROL_MODE_NOT_FOUND')
380
381
        result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()}
382
383
        query = (" SELECT start_time_of_day, end_time_of_day, power_value "
384
                 " FROM tbl_control_modes_times "
385
                 " WHERE control_mode_id = %s  "
386
                 " ORDER BY id")
387
        cursor.execute(query, (result['id'],))
388
        rows_times = cursor.fetchall()
389
        if rows_times is not None and len(rows_times) > 0:
390
            for row_time in rows_times:
391
                start_time_of_day = str(row_time[0])
392
                parts = start_time_of_day.split(':')
393
                hour = parts[0].zfill(2)
394
                minute = parts[1].zfill(2) if len(parts) > 1 else '00'
395
                second = parts[2].zfill(2) if len(parts) > 2 else '00'
396
                start_time_of_day = f"{hour}:{minute}:{second}"
397
398
                end_time_of_day = str(row_time[1])
399
                parts = end_time_of_day.split(':')
400
                hour = parts[0].zfill(2)
401
                minute = parts[1].zfill(2) if len(parts) > 1 else '00'
402
                second = parts[2].zfill(2) if len(parts) > 2 else '00'
403
                end_time_of_day = f"{hour}:{minute}:{second}"
404
405
                meta_data = {"start_time_of_day": start_time_of_day,
406
                             "end_time_of_day": end_time_of_day,
407
                             "power_value": row_time[2]}
408
                result['times'].append(meta_data)
409
410
        cursor.close()
411
        cnx.close()
412
413
        resp.text = json.dumps(result)
414
415
416
class ControlModeImport:
@@ 156-215 (lines=60) @@
153
        resp.status = falcon.HTTP_200
154
        _ = id_
155
156
    @staticmethod
157
    def on_get(req, resp, id_):
158
        """Handles GET requests"""
159
        if 'API-KEY' not in req.headers or \
160
                not isinstance(req.headers['API-KEY'], str) or \
161
                len(str.strip(req.headers['API-KEY'])) == 0:
162
            access_control(req)
163
        else:
164
            api_key_control(req)
165
        if not id_.isdigit() or int(id_) <= 0:
166
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
167
                                   description='API.INVALID_CONTROL_MODE_ID')
168
169
        cnx = mysql.connector.connect(**config.myems_system_db)
170
        cursor = cnx.cursor()
171
172
        query = (" SELECT id, name, uuid, is_active "
173
                 " FROM tbl_control_modes "
174
                 " WHERE id = %s ")
175
        cursor.execute(query, (id_,))
176
        row = cursor.fetchone()
177
        if row is None:
178
            cursor.close()
179
            cnx.close()
180
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
181
                                   description='API.CONTROL_MODE_NOT_FOUND')
182
183
        result = {"id": row[0], "name": row[1], "uuid": row[2], "is_active": bool(row[3]), 'times': list()}
184
185
        query = (" SELECT start_time_of_day, end_time_of_day, power_value "
186
                 " FROM tbl_control_modes_times "
187
                 " WHERE control_mode_id = %s  "
188
                 " ORDER BY id ")
189
        cursor.execute(query, (result['id'],))
190
        rows_times = cursor.fetchall()
191
        if rows_times is not None and len(rows_times) > 0:
192
            for row_time in rows_times:
193
                start_time_of_day = str(row_time[0])
194
                parts = start_time_of_day.split(':')
195
                hour = parts[0].zfill(2)
196
                minute = parts[1].zfill(2) if len(parts) > 1 else '00'
197
                second = parts[2].zfill(2) if len(parts) > 2 else '00'
198
                start_time_of_day = f"{hour}:{minute}:{second}"
199
200
                end_time_of_day = str(row_time[1])
201
                parts = end_time_of_day.split(':')
202
                hour = parts[0].zfill(2)
203
                minute = parts[1].zfill(2) if len(parts) > 1 else '00'
204
                second = parts[2].zfill(2) if len(parts) > 2 else '00'
205
                end_time_of_day = f"{hour}:{minute}:{second}"
206
207
                meta_data = {"start_time_of_day": start_time_of_day,
208
                             "end_time_of_day": end_time_of_day,
209
                             "power_value": row_time[2]}
210
                result['times'].append(meta_data)
211
212
        cursor.close()
213
        cnx.close()
214
215
        resp.text = json.dumps(result)
216
217
    @staticmethod
218
    @user_logger