Code Duplication    Length = 52-55 lines in 3 locations

acquisition.py 3 locations

@@ 307-361 (lines=55) @@
304
                        # ignore this exception
305
                        pass
306
307
            if len(energy_value_list) > 0:
308
                add_values = (" INSERT INTO tbl_energy_value (point_id, utc_date_time, actual_value) "
309
                              " VALUES  ")
310
                trend_value_count = 0
311
312
                for point_value in energy_value_list:
313
                    if point_value['is_trend']:
314
                        add_values += " (" + str(point_value['point_id']) + ","
315
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
316
                        add_values += str(point_value['value']) + "), "
317
                        trend_value_count += 1
318
319
                if trend_value_count > 0:
320
                    try:
321
                        # trim ", " at the end of string and then execute
322
                        cursor_historical_db.execute(add_values[:-2])
323
                        cnx_historical_db.commit()
324
                    except Exception as e:
325
                        logger.error("Error in step 4.3.1 of acquisition process: " + str(e))
326
                        # ignore this exception
327
                        pass
328
329
                # update tbl_energy_value_latest
330
                delete_values = " DELETE FROM tbl_energy_value_latest WHERE point_id IN ( "
331
                latest_values = (" INSERT INTO tbl_energy_value_latest (point_id, utc_date_time, actual_value) "
332
                                 " VALUES  ")
333
334
                latest_value_count = 0
335
                for point_value in energy_value_list:
336
                    delete_values += str(point_value['point_id']) + ","
337
                    latest_values += " (" + str(point_value['point_id']) + ","
338
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
339
                    latest_values += str(point_value['value']) + "), "
340
                    latest_value_count += 1
341
342
                if latest_value_count > 0:
343
                    try:
344
                        # replace "," at the end of string with ")"
345
                        cursor_historical_db.execute(delete_values[:-1] + ")")
346
                        cnx_historical_db.commit()
347
348
                    except Exception as e:
349
                        logger.error("Error in step 4.3.2 of acquisition process " + str(e))
350
                        # ignore this exception
351
                        pass
352
353
                    try:
354
                        # trim ", " at the end of string and then execute
355
                        cursor_historical_db.execute(latest_values[:-2])
356
                        cnx_historical_db.commit()
357
358
                    except Exception as e:
359
                        logger.error("Error in step 4.3.3 of acquisition process " + str(e))
360
                        # ignore this exception
361
                        pass
362
363
            if len(digital_value_list) > 0:
364
                add_values = (" INSERT INTO tbl_digital_value (point_id, utc_date_time, actual_value) "
@@ 253-305 (lines=53) @@
250
            current_datetime_utc = datetime.utcnow()
251
            # bulk insert values into historical database within a period
252
            # update latest values in the meanwhile
253
            if len(analog_value_list) > 0:
254
                add_values = (" INSERT INTO tbl_analog_value (point_id, utc_date_time, actual_value) "
255
                              " VALUES  ")
256
                trend_value_count = 0
257
258
                for point_value in analog_value_list:
259
                    if point_value['is_trend']:
260
                        add_values += " (" + str(point_value['point_id']) + ","
261
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
262
                        add_values += str(point_value['value']) + "), "
263
                        trend_value_count += 1
264
265
                if trend_value_count > 0:
266
                    try:
267
                        # trim ", " at the end of string and then execute
268
                        cursor_historical_db.execute(add_values[:-2])
269
                        cnx_historical_db.commit()
270
                    except Exception as e:
271
                        logger.error("Error in step 4.2.1 of acquisition process " + str(e))
272
                        # ignore this exception
273
                        pass
274
275
                # update tbl_analog_value_latest
276
                delete_values = " DELETE FROM tbl_analog_value_latest WHERE point_id IN ( "
277
                latest_values = (" INSERT INTO tbl_analog_value_latest (point_id, utc_date_time, actual_value) "
278
                                 " VALUES  ")
279
                latest_value_count = 0
280
281
                for point_value in analog_value_list:
282
                    delete_values += str(point_value['point_id']) + ","
283
                    latest_values += " (" + str(point_value['point_id']) + ","
284
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
285
                    latest_values += str(point_value['value']) + "), "
286
                    latest_value_count += 1
287
288
                if latest_value_count > 0:
289
                    try:
290
                        # replace "," at the end of string with ")"
291
                        cursor_historical_db.execute(delete_values[:-1] + ")")
292
                        cnx_historical_db.commit()
293
                    except Exception as e:
294
                        logger.error("Error in step 4.2.2 of acquisition process " + str(e))
295
                        # ignore this exception
296
                        pass
297
298
                    try:
299
                        # trim ", " at the end of string and then execute
300
                        cursor_historical_db.execute(latest_values[:-2])
301
                        cnx_historical_db.commit()
302
                    except Exception as e:
303
                        logger.error("Error in step 4.2.3 of acquisition process " + str(e))
304
                        # ignore this exception
305
                        pass
306
307
            if len(energy_value_list) > 0:
308
                add_values = (" INSERT INTO tbl_energy_value (point_id, utc_date_time, actual_value) "
@@ 363-414 (lines=52) @@
360
                        # ignore this exception
361
                        pass
362
363
            if len(digital_value_list) > 0:
364
                add_values = (" INSERT INTO tbl_digital_value (point_id, utc_date_time, actual_value) "
365
                              " VALUES  ")
366
                trend_value_count = 0
367
368
                for point_value in digital_value_list:
369
                    if point_value['is_trend']:
370
                        add_values += " (" + str(point_value['point_id']) + ","
371
                        add_values += "'" + current_datetime_utc.isoformat() + "',"
372
                        add_values += str(point_value['value']) + "), "
373
                        trend_value_count += 1
374
375
                if trend_value_count > 0:
376
                    try:
377
                        # trim ", " at the end of string and then execute
378
                        cursor_historical_db.execute(add_values[:-2])
379
                        cnx_historical_db.commit()
380
                    except Exception as e:
381
                        logger.error("Error in step 4.4.1 of acquisition process: " + str(e))
382
                        # ignore this exception
383
                        pass
384
385
                # update tbl_digital_value_latest
386
                delete_values = " DELETE FROM tbl_digital_value_latest WHERE point_id IN ( "
387
                latest_values = (" INSERT INTO tbl_digital_value_latest (point_id, utc_date_time, actual_value) "
388
                                 " VALUES  ")
389
                latest_value_count = 0
390
                for point_value in digital_value_list:
391
                    delete_values += str(point_value['point_id']) + ","
392
                    latest_values += " (" + str(point_value['point_id']) + ","
393
                    latest_values += "'" + current_datetime_utc.isoformat() + "',"
394
                    latest_values += str(point_value['value']) + "), "
395
                    latest_value_count += 1
396
397
                if latest_value_count > 0:
398
                    try:
399
                        # replace "," at the end of string with ")"
400
                        cursor_historical_db.execute(delete_values[:-1] + ")")
401
                        cnx_historical_db.commit()
402
                    except Exception as e:
403
                        logger.error("Error in step 4.4.2 of acquisition process " + str(e))
404
                        # ignore this exception
405
                        pass
406
407
                    try:
408
                        # trim ", " at the end of string and then execute
409
                        cursor_historical_db.execute(latest_values[:-2])
410
                        cnx_historical_db.commit()
411
                    except Exception as e:
412
                        logger.error("Error in step 4.4.3 of acquisition process " + str(e))
413
                        # ignore this exception
414
                        pass
415
416
            # sleep some seconds
417
            time.sleep(config.interval_in_seconds)