Code Duplication    Length = 51-53 lines in 3 locations

myems-modbus-tcp/acquisition.py 3 locations

@@ 389-441 (lines=53) @@
386
                        logger.error("Error in step 5.3.3 of acquisition process " + str(e))
387
                        # ignore this exception
388
389
            while len(energy_value_list) > 0:
390
                energy_value_list_100 = energy_value_list[:100]
391
                energy_value_list = energy_value_list[100:]
392
393
                add_values = (" INSERT INTO tbl_energy_value (point_id, utc_date_time, actual_value) "
394
                              " VALUES  ")
395
                trend_value_count = 0
396
397
                for point_value in energy_value_list_100:
398
                    if point_value['is_trend']:
399
                        add_values += " (" + str(point_value['point_id']) + ","
400
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
401
                        add_values += str(point_value['value']) + "), "
402
                        trend_value_count += 1
403
404
                if trend_value_count > 0:
405
                    try:
406
                        # trim ", " at the end of string and then execute
407
                        cursor_historical_db.execute(add_values[:-2])
408
                        cnx_historical_db.commit()
409
                    except Exception as e:
410
                        logger.error("Error in step 5.4.1 of acquisition process: " + str(e))
411
                        # ignore this exception
412
413
                # update tbl_energy_value_latest
414
                delete_values = " DELETE FROM tbl_energy_value_latest WHERE point_id IN ( "
415
                latest_values = (" INSERT INTO tbl_energy_value_latest (point_id, utc_date_time, actual_value) "
416
                                 " VALUES  ")
417
                latest_value_count = 0
418
                for point_value in energy_value_list_100:
419
                    delete_values += str(point_value['point_id']) + ","
420
                    latest_values += " (" + str(point_value['point_id']) + ","
421
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
422
                    latest_values += str(point_value['value']) + "), "
423
                    latest_value_count += 1
424
425
                if latest_value_count > 0:
426
                    try:
427
                        # replace "," at the end of string with ")"
428
                        cursor_historical_db.execute(delete_values[:-1] + ")")
429
                        cnx_historical_db.commit()
430
431
                    except Exception as e:
432
                        logger.error("Error in step 5.4.2 of acquisition process " + str(e))
433
                        # ignore this exception
434
435
                    try:
436
                        # trim ", " at the end of string and then execute
437
                        cursor_historical_db.execute(latest_values[:-2])
438
                        cnx_historical_db.commit()
439
440
                    except Exception as e:
441
                        logger.error("Error in step 5.4.3 of acquisition process " + str(e))
442
                        # ignore this exception
443
444
            while len(digital_value_list) > 0:
@@ 335-386 (lines=52) @@
332
            current_datetime_utc = datetime.utcnow()
333
            # bulk insert values into historical database within a period
334
            # and then update latest values
335
            while len(analog_value_list) > 0:
336
                analog_value_list_100 = analog_value_list[:100]
337
                analog_value_list = analog_value_list[100:]
338
339
                add_values = (" INSERT INTO tbl_analog_value (point_id, utc_date_time, actual_value) "
340
                              " VALUES  ")
341
                trend_value_count = 0
342
343
                for point_value in analog_value_list_100:
344
                    if point_value['is_trend']:
345
                        add_values += " (" + str(point_value['point_id']) + ","
346
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
347
                        add_values += str(point_value['value']) + "), "
348
                        trend_value_count += 1
349
350
                if trend_value_count > 0:
351
                    try:
352
                        # trim ", " at the end of string and then execute
353
                        cursor_historical_db.execute(add_values[:-2])
354
                        cnx_historical_db.commit()
355
                    except Exception as e:
356
                        logger.error("Error in step 5.3.1 of acquisition process " + str(e))
357
                        # ignore this exception
358
359
                # update tbl_analog_value_latest
360
                delete_values = " DELETE FROM tbl_analog_value_latest WHERE point_id IN ( "
361
                latest_values = (" INSERT INTO tbl_analog_value_latest (point_id, utc_date_time, actual_value) "
362
                                 " VALUES  ")
363
                latest_value_count = 0
364
365
                for point_value in analog_value_list_100:
366
                    delete_values += str(point_value['point_id']) + ","
367
                    latest_values += " (" + str(point_value['point_id']) + ","
368
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
369
                    latest_values += str(point_value['value']) + "), "
370
                    latest_value_count += 1
371
372
                if latest_value_count > 0:
373
                    try:
374
                        # replace "," at the end of string with ")"
375
                        cursor_historical_db.execute(delete_values[:-1] + ")")
376
                        cnx_historical_db.commit()
377
                    except Exception as e:
378
                        logger.error("Error in step 5.3.2 of acquisition process " + str(e))
379
                        # ignore this exception
380
381
                    try:
382
                        # trim ", " at the end of string and then execute
383
                        cursor_historical_db.execute(latest_values[:-2])
384
                        cnx_historical_db.commit()
385
                    except Exception as e:
386
                        logger.error("Error in step 5.3.3 of acquisition process " + str(e))
387
                        # ignore this exception
388
389
            while len(energy_value_list) > 0:
@@ 444-494 (lines=51) @@
441
                        logger.error("Error in step 5.4.3 of acquisition process " + str(e))
442
                        # ignore this exception
443
444
            while len(digital_value_list) > 0:
445
                digital_value_list_100 = digital_value_list[:100]
446
                digital_value_list = digital_value_list[100:]
447
448
                add_values = (" INSERT INTO tbl_digital_value (point_id, utc_date_time, actual_value) "
449
                              " VALUES  ")
450
                trend_value_count = 0
451
452
                for point_value in digital_value_list_100:
453
                    if point_value['is_trend']:
454
                        add_values += " (" + str(point_value['point_id']) + ","
455
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
456
                        add_values += str(point_value['value']) + "), "
457
                        trend_value_count += 1
458
459
                if trend_value_count > 0:
460
                    try:
461
                        # trim ", " at the end of string and then execute
462
                        cursor_historical_db.execute(add_values[:-2])
463
                        cnx_historical_db.commit()
464
                    except Exception as e:
465
                        logger.error("Error in step 5.5.1 of acquisition process: " + str(e))
466
                        # ignore this exception
467
468
                # update tbl_digital_value_latest
469
                delete_values = " DELETE FROM tbl_digital_value_latest WHERE point_id IN ( "
470
                latest_values = (" INSERT INTO tbl_digital_value_latest (point_id, utc_date_time, actual_value) "
471
                                 " VALUES  ")
472
                latest_value_count = 0
473
                for point_value in digital_value_list_100:
474
                    delete_values += str(point_value['point_id']) + ","
475
                    latest_values += " (" + str(point_value['point_id']) + ","
476
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
477
                    latest_values += str(point_value['value']) + "), "
478
                    latest_value_count += 1
479
480
                if latest_value_count > 0:
481
                    try:
482
                        # replace "," at the end of string with ")"
483
                        cursor_historical_db.execute(delete_values[:-1] + ")")
484
                        cnx_historical_db.commit()
485
                    except Exception as e:
486
                        logger.error("Error in step 5.5.2 of acquisition process " + str(e))
487
                        # ignore this exception
488
489
                    try:
490
                        # trim ", " at the end of string and then execute
491
                        cursor_historical_db.execute(latest_values[:-2])
492
                        cnx_historical_db.commit()
493
                    except Exception as e:
494
                        logger.error("Error in step 5.5.3 of acquisition process " + str(e))
495
                        # ignore this exception
496
497
            # update data source last seen datetime