shopfloor_energy_input_item.main()   F
last analyzed

Complexity

Conditions 14

Size

Total Lines 70
Code Lines 46

Duplication

Lines 70
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 46
dl 70
loc 70
rs 3.6
c 0
b 0
f 0
cc 14
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like shopfloor_energy_input_item.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import time
2
from datetime import datetime, timedelta
3
from decimal import Decimal
4
import mysql.connector
5
from multiprocessing import Pool
6
import random
7
import config
8
9
10
########################################################################################################################
11
# PROCEDURES
12
# Step 1: get all shopfloors
13
# Step 2: Create multiprocessing pool to call worker in parallel
14
########################################################################################################################
15
16
17 View Code Duplication
def main(logger):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
19
    while True:
20
        # the outermost while loop
21
        ################################################################################################################
22
        # Step 1: get all shopfloors
23
        ################################################################################################################
24
        cnx_system_db = None
25
        cursor_system_db = None
26
        try:
27
            cnx_system_db = mysql.connector.connect(**config.myems_system_db)
28
            cursor_system_db = cnx_system_db.cursor()
29
        except Exception as e:
30
            logger.error("Error in step 1.1 of shopfloor_energy_input_item.main " + str(e))
31
            if cursor_system_db:
32
                cursor_system_db.close()
33
            if cnx_system_db:
34
                cnx_system_db.close()
35
            # sleep and continue the outer loop to reconnect the database
36
            time.sleep(60)
37
            continue
38
        print("Connected to MyEMS System Database")
39
40
        try:
41
            cursor_system_db.execute(" SELECT id, name "
42
                                     " FROM tbl_shopfloors "
43
                                     " ORDER BY id ")
44
            rows_shopfloors = cursor_system_db.fetchall()
45
46
            if rows_shopfloors is None or len(rows_shopfloors) == 0:
47
                print("There isn't any shopfloors ")
48
                # sleep and continue the outer loop to reconnect the database
49
                time.sleep(60)
50
                continue
51
52
            shopfloor_list = list()
53
            for row in rows_shopfloors:
54
                shopfloor_list.append({"id": row[0], "name": row[1]})
55
56
        except Exception as e:
57
            logger.error("Error in step 1.2 of shopfloor_energy_input_item.main " + str(e))
58
            # sleep and continue the outer loop to reconnect the database
59
            time.sleep(60)
60
            continue
61
        finally:
62
            if cursor_system_db:
63
                cursor_system_db.close()
64
            if cnx_system_db:
65
                cnx_system_db.close()
66
67
        print("Got all shopfloors in MyEMS System Database")
68
69
        # shuffle the shopfloor list for randomly calculating the meter hourly value
70
        random.shuffle(shopfloor_list)
0 ignored issues
show
introduced by
The variable shopfloor_list does not seem to be defined for all execution paths.
Loading history...
71
72
        ################################################################################################################
73
        # Step 2: Create multiprocessing pool to call worker in parallel
74
        ################################################################################################################
75
        p = Pool(processes=config.pool_size)
76
        error_list = p.map(worker, shopfloor_list)
77
        p.close()
78
        p.join()
79
80
        for error in error_list:
81
            if error is not None and len(error) > 0:
82
                logger.error(error)
83
84
        print("go to sleep 300 seconds...")
85
        time.sleep(300)
86
        print("wake from sleep, and continue to work...")
87
    # end of outer while
88
89
90
########################################################################################################################
91
# PROCEDURES:
92
#   Step 1: get all input meters associated with the shopfloor
93
#   Step 2: get all input virtual meters associated with the shopfloor
94
#   Step 3: get all input offline meters associated with the shopfloor
95
#   Step 4: get all equipments associated with the shopfloor
96
#   Step 5: determine start datetime and end datetime to aggregate
97
#   Step 6: for each meter in list, get energy input data from energy database
98
#   Step 7: for each virtual meter in list, get energy input data from energy database
99
#   Step 8: for each offline meter in list, get energy input data from energy database
100
#   Step 9: for each equipment in list, get energy input data from energy database
101
#   Step 10: determine common time slot to aggregate
102
#   Step 11: aggregate energy data in the common time slot by energy items and hourly
103
#   Step 12: save energy data to energy database
104
#
105
# NOTE: returns None or the error string because that the logger object cannot be passed in as parameter
106
########################################################################################################################
107
108 View Code Duplication
def worker(shopfloor):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
    ####################################################################################################################
110
    # Step 1: get all input meters associated with the shopfloor
111
    ####################################################################################################################
112
    print("Step 1: get all input meters associated with the shopfloor " + str(shopfloor['name']))
113
114
    meter_list = list()
115
    cnx_system_db = None
116
    cursor_system_db = None
117
    try:
118
        cnx_system_db = mysql.connector.connect(**config.myems_system_db)
119
        cursor_system_db = cnx_system_db.cursor()
120
    except Exception as e:
121
        error_string = "Error in step 1.1 of shopfloor_energy_input_item.worker " + str(e)
122
        if cursor_system_db:
123
            cursor_system_db.close()
124
        if cnx_system_db:
125
            cnx_system_db.close()
126
        print(error_string)
127
        return error_string
128
129
    try:
130
        cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id "
131
                                 " FROM tbl_meters m, tbl_shopfloors_meters tm "
132
                                 " WHERE m.id = tm.meter_id "
133
                                 "       AND m.is_counted = true "
134
                                 "       AND m.energy_item_id is NOT NULL "
135
                                 "       AND tm.shopfloor_id = %s ",
136
                                 (shopfloor['id'],))
137
        rows_meters = cursor_system_db.fetchall()
138
139
        if rows_meters is not None and len(rows_meters) > 0:
140
            for row in rows_meters:
141
                meter_list.append({"id": row[0],
142
                                   "name": row[1],
143
                                   "energy_item_id": row[2]})
144
145
    except Exception as e:
146
        error_string = "Error in step 1.2 of shopfloor_energy_input_item.worker " + str(e)
147
        if cursor_system_db:
148
            cursor_system_db.close()
149
        if cnx_system_db:
150
            cnx_system_db.close()
151
        print(error_string)
152
        return error_string
153
154
    ####################################################################################################################
155
    # Step 2: get all input virtual meters associated with the shopfloor
156
    ####################################################################################################################
157
    print("Step 2: get all input virtual meters associated with the shopfloor")
158
    virtual_meter_list = list()
159
160
    try:
161
        cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id "
162
                                 " FROM tbl_virtual_meters m, tbl_shopfloors_virtual_meters tm "
163
                                 " WHERE m.id = tm.virtual_meter_id "
164
                                 "       AND m.energy_item_id is NOT NULL "
165
                                 "       AND m.is_counted = true "
166
                                 "       AND tm.shopfloor_id = %s ",
167
                                 (shopfloor['id'],))
168
        rows_virtual_meters = cursor_system_db.fetchall()
169
170
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
171
            for row in rows_virtual_meters:
172
                virtual_meter_list.append({"id": row[0],
173
                                           "name": row[1],
174
                                           "energy_item_id": row[2]})
175
176
    except Exception as e:
177
        error_string = "Error in step 2.1 of shopfloor_energy_input_item.worker " + str(e)
178
        if cursor_system_db:
179
            cursor_system_db.close()
180
        if cnx_system_db:
181
            cnx_system_db.close()
182
        print(error_string)
183
        return error_string
184
185
    ####################################################################################################################
186
    # Step 3: get all input offline meters associated with the shopfloor
187
    ####################################################################################################################
188
    print("Step 3: get all input offline meters associated with the shopfloor")
189
190
    offline_meter_list = list()
191
192
    try:
193
        cursor_system_db.execute(" SELECT m.id, m.name, m.energy_item_id "
194
                                 " FROM tbl_offline_meters m, tbl_shopfloors_offline_meters tm "
195
                                 " WHERE m.id = tm.offline_meter_id "
196
                                 "       AND m.energy_item_id is NOT NULL "
197
                                 "       AND m.is_counted = true "
198
                                 "       AND tm.shopfloor_id = %s ",
199
                                 (shopfloor['id'],))
200
        rows_offline_meters = cursor_system_db.fetchall()
201
202
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
203
            for row in rows_offline_meters:
204
                offline_meter_list.append({"id": row[0],
205
                                           "name": row[1],
206
                                           "energy_item_id": row[2]})
207
208
    except Exception as e:
209
        error_string = "Error in step 3.1 of shopfloor_energy_input_item.worker " + str(e)
210
        if cursor_system_db:
211
            cursor_system_db.close()
212
        if cnx_system_db:
213
            cnx_system_db.close()
214
        print(error_string)
215
        return error_string
216
217
    ####################################################################################################################
218
    # Step 4: get all equipments associated with the shopfloor
219
    ####################################################################################################################
220
    print("Step 4: get all equipments associated with the shopfloor")
221
222
    equipment_list = list()
223
224
    try:
225
        cursor_system_db.execute(" SELECT e.id, e.name "
226
                                 " FROM tbl_equipments e, tbl_shopfloors_equipments se "
227
                                 " WHERE e.id = se.equipment_id "
228
                                 "       AND e.is_input_counted = true "
229
                                 "       AND se.shopfloor_id = %s ",
230
                                 (shopfloor['id'],))
231
        rows_equipments = cursor_system_db.fetchall()
232
233
        if rows_equipments is not None and len(rows_equipments) > 0:
234
            for row in rows_equipments:
235
                equipment_list.append({"id": row[0],
236
                                       "name": row[1]})
237
238
    except Exception as e:
239
        error_string = "Error in step 4 of shopfloor_energy_input_item.worker " + str(e)
240
        print(error_string)
241
        return error_string
242
    finally:
243
        if cursor_system_db:
244
            cursor_system_db.close()
245
        if cnx_system_db:
246
            cnx_system_db.close()
247
248
    ####################################################################################################################
249
    # stop to the next shopfloor if this shopfloor is empty
250
    ####################################################################################################################
251
    if (meter_list is None or len(meter_list) == 0) and \
252
            (virtual_meter_list is None or len(virtual_meter_list) == 0) and \
253
            (offline_meter_list is None or len(offline_meter_list) == 0) and \
254
            (equipment_list is None or len(equipment_list) == 0):
255
        print("This is an empty shopfloor ")
256
        return None
257
258
    ####################################################################################################################
259
    # Step 5: determine start datetime and end datetime to aggregate
260
    ####################################################################################################################
261
    print("Step 5: determine start datetime and end datetime to aggregate")
262
    cnx_energy_db = None
263
    cursor_energy_db = None
264
    try:
265
        cnx_energy_db = mysql.connector.connect(**config.myems_energy_db)
266
        cursor_energy_db = cnx_energy_db.cursor()
267
    except Exception as e:
268
        error_string = "Error in step 5.1 of shopfloor_energy_input_item.worker " + str(e)
269
        if cursor_energy_db:
270
            cursor_energy_db.close()
271
        if cnx_energy_db:
272
            cnx_energy_db.close()
273
        print(error_string)
274
        return error_string
275
276
    try:
277
        query = (" SELECT MAX(start_datetime_utc) "
278
                 " FROM tbl_shopfloor_input_item_hourly "
279
                 " WHERE shopfloor_id = %s ")
280
        cursor_energy_db.execute(query, (shopfloor['id'],))
281
        row_datetime = cursor_energy_db.fetchone()
282
        start_datetime_utc = datetime.strptime(config.start_datetime_utc, '%Y-%m-%d %H:%M:%S')
283
        start_datetime_utc = start_datetime_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
284
285
        if row_datetime is not None and len(row_datetime) > 0 and isinstance(row_datetime[0], datetime):
286
            # replace second and microsecond with 0
287
            # note: do not replace minute in case of calculating in half hourly
288
            start_datetime_utc = row_datetime[0].replace(second=0, microsecond=0, tzinfo=None)
289
            # start from the next time slot
290
            start_datetime_utc += timedelta(minutes=config.minutes_to_count)
291
292
        end_datetime_utc = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=None)
293
294
        print("start_datetime_utc: " + start_datetime_utc.isoformat()[0:19]
295
              + "end_datetime_utc: " + end_datetime_utc.isoformat()[0:19])
296
297
    except Exception as e:
298
        error_string = "Error in step 5.2 of shopfloor_energy_input_item.worker " + str(e)
299
        if cursor_energy_db:
300
            cursor_energy_db.close()
301
        if cnx_energy_db:
302
            cnx_energy_db.close()
303
        print(error_string)
304
        return error_string
305
306
    ####################################################################################################################
307
    # Step 6: for each meter in list, get energy input data from energy database
308
    ####################################################################################################################
309
    energy_meter_hourly = dict()
310
    try:
311
        if meter_list is not None and len(meter_list) > 0:
312
            for meter in meter_list:
313
                meter_id = str(meter['id'])
314
315
                query = (" SELECT start_datetime_utc, actual_value "
316
                         " FROM tbl_meter_hourly "
317
                         " WHERE meter_id = %s "
318
                         "       AND start_datetime_utc >= %s "
319
                         "       AND start_datetime_utc < %s "
320
                         " ORDER BY start_datetime_utc ")
321
                cursor_energy_db.execute(query, (meter_id, start_datetime_utc, end_datetime_utc,))
322
                rows_energy_values = cursor_energy_db.fetchall()
323
                if rows_energy_values is None or len(rows_energy_values) == 0:
324
                    energy_meter_hourly[meter_id] = None
325
                else:
326
                    energy_meter_hourly[meter_id] = dict()
327
                    for row_energy_value in rows_energy_values:
328
                        energy_meter_hourly[meter_id][row_energy_value[0]] = row_energy_value[1]
329
    except Exception as e:
330
        error_string = "Error in step 6.1 of shopfloor_energy_input_item.worker " + str(e)
331
        if cursor_energy_db:
332
            cursor_energy_db.close()
333
        if cnx_energy_db:
334
            cnx_energy_db.close()
335
        print(error_string)
336
        return error_string
337
338
    ####################################################################################################################
339
    # Step 7: for each virtual meter in list, get energy input data from energy database
340
    ####################################################################################################################
341
    energy_virtual_meter_hourly = dict()
342
    if virtual_meter_list is not None and len(virtual_meter_list) > 0:
343
        try:
344
            for virtual_meter in virtual_meter_list:
345
                virtual_meter_id = str(virtual_meter['id'])
346
347
                query = (" SELECT start_datetime_utc, actual_value "
348
                         " FROM tbl_virtual_meter_hourly "
349
                         " WHERE virtual_meter_id = %s "
350
                         "       AND start_datetime_utc >= %s "
351
                         "       AND start_datetime_utc < %s "
352
                         " ORDER BY start_datetime_utc ")
353
                cursor_energy_db.execute(query, (virtual_meter_id, start_datetime_utc, end_datetime_utc,))
354
                rows_energy_values = cursor_energy_db.fetchall()
355
                if rows_energy_values is None or len(rows_energy_values) == 0:
356
                    energy_virtual_meter_hourly[virtual_meter_id] = None
357
                else:
358
                    energy_virtual_meter_hourly[virtual_meter_id] = dict()
359
                    for row_energy_value in rows_energy_values:
360
                        energy_virtual_meter_hourly[virtual_meter_id][row_energy_value[0]] = row_energy_value[1]
361
        except Exception as e:
362
            error_string = "Error in step 7.1 of shopfloor_energy_input_item.worker " + str(e)
363
            if cursor_energy_db:
364
                cursor_energy_db.close()
365
            if cnx_energy_db:
366
                cnx_energy_db.close()
367
            print(error_string)
368
            return error_string
369
370
    ####################################################################################################################
371
    # Step 8: for each offline meter in list, get energy input data from energy database
372
    ####################################################################################################################
373
    energy_offline_meter_hourly = dict()
374
    if offline_meter_list is not None and len(offline_meter_list) > 0:
375
        try:
376
            for offline_meter in offline_meter_list:
377
                offline_meter_id = str(offline_meter['id'])
378
379
                query = (" SELECT start_datetime_utc, actual_value "
380
                         " FROM tbl_offline_meter_hourly "
381
                         " WHERE offline_meter_id = %s "
382
                         "       AND start_datetime_utc >= %s "
383
                         "       AND start_datetime_utc < %s "
384
                         " ORDER BY start_datetime_utc ")
385
                cursor_energy_db.execute(query, (offline_meter_id, start_datetime_utc, end_datetime_utc,))
386
                rows_energy_values = cursor_energy_db.fetchall()
387
                if rows_energy_values is None or len(rows_energy_values) == 0:
388
                    energy_offline_meter_hourly[offline_meter_id] = None
389
                else:
390
                    energy_offline_meter_hourly[offline_meter_id] = dict()
391
                    for row_energy_value in rows_energy_values:
392
                        energy_offline_meter_hourly[offline_meter_id][row_energy_value[0]] = row_energy_value[1]
393
394
        except Exception as e:
395
            error_string = "Error in step 8.1 of shopfloor_energy_input_item.worker " + str(e)
396
            if cursor_energy_db:
397
                cursor_energy_db.close()
398
            if cnx_energy_db:
399
                cnx_energy_db.close()
400
            print(error_string)
401
            return error_string
402
403
    ####################################################################################################################
404
    # Step 9: for each equipment in list, get energy input data from energy database
405
    ####################################################################################################################
406
    energy_equipment_hourly = dict()
407
    if equipment_list is not None and len(equipment_list) > 0:
408
        try:
409
            for equipment in equipment_list:
410
                equipment_id = str(equipment['id'])
411
                query = (" SELECT start_datetime_utc, energy_item_id, actual_value "
412
                         " FROM tbl_equipment_input_item_hourly "
413
                         " WHERE equipment_id = %s "
414
                         "       AND start_datetime_utc >= %s "
415
                         "       AND start_datetime_utc < %s "
416
                         " ORDER BY start_datetime_utc ")
417
                cursor_energy_db.execute(query, (equipment_id, start_datetime_utc, end_datetime_utc,))
418
                rows_energy_values = cursor_energy_db.fetchall()
419
                if rows_energy_values is None or len(rows_energy_values) == 0:
420
                    energy_equipment_hourly[equipment_id] = None
421
                else:
422
                    energy_equipment_hourly[equipment_id] = dict()
423
                    for row_value in rows_energy_values:
424
                        current_datetime_utc = row_value[0]
425
                        if current_datetime_utc not in energy_equipment_hourly[equipment_id]:
426
                            energy_equipment_hourly[equipment_id][current_datetime_utc] = dict()
427
                        energy_item_id = row_value[1]
428
                        actual_value = row_value[2]
429
                        energy_equipment_hourly[equipment_id][current_datetime_utc][energy_item_id] = \
430
                            actual_value
431
        except Exception as e:
432
            error_string = "Error in step 9 of shopfloor_energy_input_item.worker " + str(e)
433
            if cursor_energy_db:
434
                cursor_energy_db.close()
435
            if cnx_energy_db:
436
                cnx_energy_db.close()
437
            print(error_string)
438
            return error_string
439
440
    ####################################################################################################################
441
    # Step 10: determine common time slot to aggregate
442
    ####################################################################################################################
443
444
    common_start_datetime_utc = start_datetime_utc
445
    common_end_datetime_utc = end_datetime_utc
446
447
    print("Getting common time slot of energy values for all meters")
448
    if energy_meter_hourly is not None and len(energy_meter_hourly) > 0:
449
        for meter_id, energy_hourly in energy_meter_hourly.items():
450
            if energy_hourly is None or len(energy_hourly) == 0:
451
                common_start_datetime_utc = None
452
                common_end_datetime_utc = None
453
                break
454
            else:
455
                if common_start_datetime_utc < min(energy_hourly.keys()):
456
                    common_start_datetime_utc = min(energy_hourly.keys())
457
                if common_end_datetime_utc > max(energy_hourly.keys()):
458
                    common_end_datetime_utc = max(energy_hourly.keys())
459
460
    print("Getting common time slot of energy values for all virtual meters")
461
    if common_start_datetime_utc is not None and common_start_datetime_utc is not None:
462
        if energy_virtual_meter_hourly is not None and len(energy_virtual_meter_hourly) > 0:
463
            for meter_id, energy_hourly in energy_virtual_meter_hourly.items():
464
                if energy_hourly is None or len(energy_hourly) == 0:
465
                    common_start_datetime_utc = None
466
                    common_end_datetime_utc = None
467
                    break
468
                else:
469
                    if common_start_datetime_utc < min(energy_hourly.keys()):
470
                        common_start_datetime_utc = min(energy_hourly.keys())
471
                    if common_end_datetime_utc > max(energy_hourly.keys()):
472
                        common_end_datetime_utc = max(energy_hourly.keys())
473
474
    print("Getting common time slot of energy values for all offline meters")
475
    if common_start_datetime_utc is not None and common_start_datetime_utc is not None:
476
        if energy_offline_meter_hourly is not None and len(energy_offline_meter_hourly) > 0:
477
            for meter_id, energy_hourly in energy_offline_meter_hourly.items():
478
                if energy_hourly is None or len(energy_hourly) == 0:
479
                    common_start_datetime_utc = None
480
                    common_end_datetime_utc = None
481
                    break
482
                else:
483
                    if common_start_datetime_utc < min(energy_hourly.keys()):
484
                        common_start_datetime_utc = min(energy_hourly.keys())
485
                    if common_end_datetime_utc > max(energy_hourly.keys()):
486
                        common_end_datetime_utc = max(energy_hourly.keys())
487
488
    print("Getting common time slot of energy values for all equipments...")
489
    if common_start_datetime_utc is not None and common_start_datetime_utc is not None:
490
        if energy_equipment_hourly is not None and len(energy_equipment_hourly) > 0:
491
            for equipment_id, energy_hourly in energy_equipment_hourly.items():
492
                if energy_hourly is None or len(energy_hourly) == 0:
493
                    common_start_datetime_utc = None
494
                    common_end_datetime_utc = None
495
                    break
496
                else:
497
                    if common_start_datetime_utc < min(energy_hourly.keys()):
498
                        common_start_datetime_utc = min(energy_hourly.keys())
499
                    if common_end_datetime_utc > max(energy_hourly.keys()):
500
                        common_end_datetime_utc = max(energy_hourly.keys())
501
502
    if (energy_meter_hourly is None or len(energy_meter_hourly) == 0) and \
503
            (energy_virtual_meter_hourly is None or len(energy_virtual_meter_hourly) == 0) and \
504
            (energy_offline_meter_hourly is None or len(energy_offline_meter_hourly) == 0) and \
505
            (energy_equipment_hourly is None or len(energy_equipment_hourly) == 0):
506
        # There isn't any energy data
507
        print("There isn't any energy data")
508
        # continue the for shopfloor loop to the next shopfloor
509
        print("continue the for shopfloor loop to the next shopfloor")
510
        if cursor_energy_db:
511
            cursor_energy_db.close()
512
        if cnx_energy_db:
513
            cnx_energy_db.close()
514
        return None
515
516
    print("common_start_datetime_utc: " + str(common_start_datetime_utc))
517
    print("common_end_datetime_utc: " + str(common_end_datetime_utc))
518
519
    ####################################################################################################################
520
    # Step 11: aggregate energy data in the common time slot by energy items and hourly
521
    ####################################################################################################################
522
523
    print("Step 11: aggregate energy data in the common time slot by energy items and hourly")
524
    aggregated_values = list()
525
    try:
526
        current_datetime_utc = common_start_datetime_utc
527
        while common_start_datetime_utc is not None \
528
                and common_end_datetime_utc is not None \
529
                and current_datetime_utc <= common_end_datetime_utc:
530
            aggregated_value = dict()
531
            aggregated_value['start_datetime_utc'] = current_datetime_utc
532
            aggregated_value['meta_data'] = dict()
533
534
            if meter_list is not None and len(meter_list) > 0:
535
                for meter in meter_list:
536
                    meter_id = str(meter['id'])
537
                    energy_item_id = meter['energy_item_id']
538
                    actual_value = energy_meter_hourly[meter_id].get(current_datetime_utc, Decimal(0.0))
539
                    aggregated_value['meta_data'][energy_item_id] = \
540
                        aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value
541
542
            if virtual_meter_list is not None and len(virtual_meter_list) > 0:
543
                for virtual_meter in virtual_meter_list:
544
                    virtual_meter_id = str(virtual_meter['id'])
545
                    energy_item_id = virtual_meter['energy_item_id']
546
                    actual_value = energy_virtual_meter_hourly[virtual_meter_id].get(current_datetime_utc, Decimal(0.0))
547
                    aggregated_value['meta_data'][energy_item_id] = \
548
                        aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value
549
550
            if offline_meter_list is not None and len(offline_meter_list) > 0:
551
                for offline_meter in offline_meter_list:
552
                    offline_meter_id = str(offline_meter['id'])
553
                    energy_item_id = offline_meter['energy_item_id']
554
                    actual_value = energy_offline_meter_hourly[offline_meter_id].get(current_datetime_utc, Decimal(0.0))
555
                    aggregated_value['meta_data'][energy_item_id] = \
556
                        aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value
557
558
            if equipment_list is not None and len(equipment_list) > 0:
559
                for equipment in equipment_list:
560
                    equipment_id = str(equipment['id'])
561
                    meta_data_dict = energy_equipment_hourly[equipment_id].get(current_datetime_utc, None)
562
                    if meta_data_dict is not None and len(meta_data_dict) > 0:
563
                        for energy_item_id, actual_value in meta_data_dict.items():
564
                            aggregated_value['meta_data'][energy_item_id] = \
565
                                aggregated_value['meta_data'].get(energy_item_id, Decimal(0.0)) + actual_value
566
567
            aggregated_values.append(aggregated_value)
568
569
            current_datetime_utc += timedelta(minutes=config.minutes_to_count)
570
571
    except Exception as e:
572
        error_string = "Error in step 11 of shopfloor_energy_input_item.worker " + str(e)
573
        if cursor_energy_db:
574
            cursor_energy_db.close()
575
        if cnx_energy_db:
576
            cnx_energy_db.close()
577
        print(error_string)
578
        return error_string
579
580
    ####################################################################################################################
581
    # Step 12: save energy data to energy database
582
    ####################################################################################################################
583
    print("Step 12: save energy data to energy database")
584
585
    if len(aggregated_values) > 0:
586
        try:
587
            add_values = (" INSERT INTO tbl_shopfloor_input_item_hourly "
588
                          "             (shopfloor_id, "
589
                          "              energy_item_id, "
590
                          "              start_datetime_utc, "
591
                          "              actual_value) "
592
                          " VALUES  ")
593
594
            for aggregated_value in aggregated_values:
595
                for energy_item_id, actual_value in aggregated_value['meta_data'].items():
596
                    add_values += " (" + str(shopfloor['id']) + ","
597
                    add_values += " " + str(energy_item_id) + ","
598
                    add_values += "'" + aggregated_value['start_datetime_utc'].isoformat()[0:19] + "',"
599
                    add_values += str(actual_value) + "), "
600
            print("add_values:" + add_values)
601
            # trim ", " at the end of string and then execute
602
            cursor_energy_db.execute(add_values[:-2])
603
            cnx_energy_db.commit()
604
605
        except Exception as e:
606
            error_string = "Error in step 12.1 of shopfloor_energy_input_item.worker " + str(e)
607
            print(error_string)
608
            return error_string
609
        finally:
610
            if cursor_energy_db:
611
                cursor_energy_db.close()
612
            if cnx_energy_db:
613
                cnx_energy_db.close()
614
    else:
615
        if cursor_energy_db:
616
            cursor_energy_db.close()
617
        if cnx_energy_db:
618
            cnx_energy_db.close()
619