Code Duplication    Length = 20-21 lines in 2 locations

meter.py 1 location

@@ 402-422 (lines=21) @@
399
    ####################################################################################################################
400
    # Step 4: Insert into energy database
401
    ####################################################################################################################
402
    if len(normalized_values) > 0:
403
        try:
404
            add_values = (" INSERT INTO tbl_meter_hourly (meter_id, start_datetime_utc, actual_value) "
405
                          " VALUES  ")
406
407
            for meta_data in normalized_values:
408
                add_values += " (" + str(meter['id']) + ","
409
                add_values += "'" + meta_data['start_datetime_utc'].isoformat()[0:19] + "',"
410
                add_values += str(meta_data['actual_value']) + "), "
411
            # trim ", " at the end of string and then execute
412
            cursor_energy_db.execute(add_values[:-2])
413
            cnx_energy_db.commit()
414
        except Exception as e:
415
            error_string = "Error in step 4.1 of meter.worker " + str(e) + " for '" + meter['name'] + "'"
416
            if cursor_energy_db:
417
                cursor_energy_db.close()
418
            if cnx_energy_db:
419
                cnx_energy_db.close()
420
421
            print(error_string)
422
            return error_string
423
424
    if cursor_energy_db:
425
        cursor_energy_db.close()

virtualmeter.py 1 location

@@ 449-468 (lines=20) @@
446
447
    print("saving energy values to table virtual meter hourly...")
448
449
    if len(normalized_values) > 0:
450
        try:
451
            add_values = (" INSERT INTO tbl_virtual_meter_hourly "
452
                          " (virtual_meter_id, start_datetime_utc, actual_value) "
453
                          " VALUES  ")
454
455
            for meta_data in normalized_values:
456
                add_values += " (" + str(virtual_meter['id']) + ","
457
                add_values += "'" + meta_data['start_datetime_utc'].isoformat()[0:19] + "',"
458
                add_values += str(meta_data['actual_value']) + "), "
459
            print("add_values:" + add_values)
460
            # trim ", " at the end of string and then execute
461
            cursor_energy_db.execute(add_values[:-2])
462
            cnx_energy_db.commit()
463
        except Exception as e:
464
            if cursor_energy_db:
465
                cursor_energy_db.close()
466
            if cnx_energy_db:
467
                cnx_energy_db.close()
468
            return "Error in step 4.2 virtual meter worker " + str(e) + " for '" + virtual_meter['name'] + "'"
469
470
    if cursor_energy_db:
471
        cursor_energy_db.close()