myems-normalization/virtualmeter.py 1 location
|
@@ 17-83 (lines=67) @@
|
14 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
15 |
|
######################################################################################################################## |
16 |
|
|
17 |
|
def calculate_hourly(logger): |
18 |
|
|
19 |
|
while True: |
20 |
|
# the outermost while loop to reconnect server if there is a connection error |
21 |
|
cnx_system_db = None |
22 |
|
cursor_system_db = None |
23 |
|
try: |
24 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
25 |
|
cursor_system_db = cnx_system_db.cursor() |
26 |
|
except Exception as e: |
27 |
|
logger.error("Error in step 0 of virtual_meter.calculate_hourly " + str(e)) |
28 |
|
if cursor_system_db: |
29 |
|
cursor_system_db.close() |
30 |
|
if cnx_system_db: |
31 |
|
cnx_system_db.close() |
32 |
|
# sleep and continue the outer loop to reconnect the database |
33 |
|
time.sleep(60) |
34 |
|
continue |
35 |
|
|
36 |
|
print("Connected to MyEMS System Database") |
37 |
|
|
38 |
|
virtual_meter_list = list() |
39 |
|
try: |
40 |
|
cursor_system_db.execute(" SELECT id, name, equation " |
41 |
|
" FROM tbl_virtual_meters " |
42 |
|
" ORDER BY id ") |
43 |
|
rows_virtual_meters = cursor_system_db.fetchall() |
44 |
|
|
45 |
|
if rows_virtual_meters is None or len(rows_virtual_meters) == 0: |
46 |
|
# sleep several minutes and continue the outer loop to reconnect the database |
47 |
|
time.sleep(60) |
48 |
|
continue |
49 |
|
|
50 |
|
for row in rows_virtual_meters: |
51 |
|
meta_result = {"id": row[0], "name": row[1], "equation": row[2]} |
52 |
|
virtual_meter_list.append(meta_result) |
53 |
|
|
54 |
|
except Exception as e: |
55 |
|
logger.error("Error in step 1 of virtual meter calculate hourly " + str(e)) |
56 |
|
# sleep and continue the outer loop to reconnect the database |
57 |
|
time.sleep(60) |
58 |
|
continue |
59 |
|
finally: |
60 |
|
if cursor_system_db: |
61 |
|
cursor_system_db.close() |
62 |
|
if cnx_system_db: |
63 |
|
cnx_system_db.close() |
64 |
|
|
65 |
|
# shuffle the virtual meter list for randomly calculating the meter hourly value |
66 |
|
random.shuffle(virtual_meter_list) |
67 |
|
|
68 |
|
print("Got all virtual meters in MyEMS System Database") |
69 |
|
################################################################################################################ |
70 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
71 |
|
################################################################################################################ |
72 |
|
p = Pool(processes=config.pool_size) |
73 |
|
error_list = p.map(worker, virtual_meter_list) |
74 |
|
p.close() |
75 |
|
p.join() |
76 |
|
|
77 |
|
for error in error_list: |
78 |
|
if error is not None and len(error) > 0: |
79 |
|
logger.error(error) |
80 |
|
|
81 |
|
print("go to sleep ...") |
82 |
|
time.sleep(60) |
83 |
|
print("wake from sleep, and continue to work...") |
84 |
|
|
85 |
|
|
86 |
|
######################################################################################################################## |
myems-aggregation/store_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all stores |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of store_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
store_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_stores " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_stores = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_stores is None or len(rows_stores) == 0: |
50 |
|
print("There isn't any stores ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_stores: |
56 |
|
store_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of store_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all stores in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the store list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(store_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, store_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/tenant_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all tenants |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of tenant_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
tenant_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_tenants " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_tenants = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_tenants is None or len(rows_tenants) == 0: |
50 |
|
print("There isn't any tenants ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_tenants: |
56 |
|
tenant_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of tenant_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all tenants in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the tenant list for randomly calculating the hourly values |
72 |
|
random.shuffle(tenant_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, tenant_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/equipment_energy_output_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of equipment_energy_output_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_equipments is None or len(rows_equipments) == 0: |
50 |
|
print("There isn't any equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_equipments: |
56 |
|
equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of equipment_energy_output_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/space_energy_output_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all spaces |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of space_energy_output_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
space_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_spaces " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_spaces = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_spaces is None or len(rows_spaces) == 0: |
50 |
|
print("There isn't any spaces ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_spaces: |
56 |
|
space_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of space_energy_output_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all spaces in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the space list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(space_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, space_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/equipment_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of equipment_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_equipments is None or len(rows_equipments) == 0: |
50 |
|
print("There isn't any equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_equipments: |
56 |
|
equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of equipment_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/space_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all spaces |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of space_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
space_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_spaces " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_spaces = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_spaces is None or len(rows_spaces) == 0: |
50 |
|
print("There isn't any spaces ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_spaces: |
56 |
|
space_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of space_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all spaces in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the space list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(space_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, space_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/equipment_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of equipment_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_equipments is None or len(rows_equipments) == 0: |
50 |
|
print("There isn't any equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_equipments: |
56 |
|
equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of equipment_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/tenant_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all tenants |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of tenant_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
tenant_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_tenants " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_tenants = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_tenants is None or len(rows_tenants) == 0: |
50 |
|
print("There isn't any tenants ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_tenants: |
56 |
|
tenant_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of tenant_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all tenants in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the tenant list for randomly calculating the hourly values |
72 |
|
random.shuffle(tenant_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, tenant_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/combined_equipment_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all combined equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of combined_equipment_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
combined_equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_combined_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_combined_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_combined_equipments is None or len(rows_combined_equipments) == 0: |
50 |
|
print("There isn't any combined equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_combined_equipments: |
56 |
|
combined_equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of combined_equipment_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all combined equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the combined equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(combined_equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, combined_equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/shopfloor_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all shopfloors |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of shopfloor_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
shopfloor_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_shopfloors " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_shopfloors = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_shopfloors is None or len(rows_shopfloors) == 0: |
50 |
|
print("There isn't any shopfloors ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_shopfloors: |
56 |
|
shopfloor_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of shopfloor_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all shopfloors in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the shopfloor list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(shopfloor_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, shopfloor_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/shopfloor_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all shopfloors |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of shopfloor_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
shopfloor_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_shopfloors " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_shopfloors = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_shopfloors is None or len(rows_shopfloors) == 0: |
50 |
|
print("There isn't any shopfloors ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_shopfloors: |
56 |
|
shopfloor_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of shopfloor_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all shopfloors in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the shopfloor list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(shopfloor_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, shopfloor_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/store_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all stores |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of store_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
store_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_stores " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_stores = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_stores is None or len(rows_stores) == 0: |
50 |
|
print("There isn't any stores ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_stores: |
56 |
|
store_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of store_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all stores in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the store list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(store_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, store_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/combined_equipment_energy_input_item.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all combined equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of combined_equipment_energy_input_item.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
combined_equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_combined_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_combined_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_combined_equipments is None or len(rows_combined_equipments) == 0: |
50 |
|
print("There isn't any combined equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_combined_equipments: |
56 |
|
combined_equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of combined_equipment_energy_input_item.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all combined equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the combined equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(combined_equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, combined_equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/space_energy_input_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all spaces |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of space_energy_input_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
space_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_spaces " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_spaces = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_spaces is None or len(rows_spaces) == 0: |
50 |
|
print("There isn't any spaces ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_spaces: |
56 |
|
space_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of space_energy_input_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all spaces in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the space list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(space_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, space_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|
myems-aggregation/combined_equipment_energy_output_category.py 1 location
|
@@ 19-88 (lines=70) @@
|
16 |
|
######################################################################################################################## |
17 |
|
|
18 |
|
|
19 |
|
def main(logger): |
20 |
|
|
21 |
|
while True: |
22 |
|
# the outermost while loop |
23 |
|
################################################################################################################ |
24 |
|
# Step 1: get all combined equipments |
25 |
|
################################################################################################################ |
26 |
|
cnx_system_db = None |
27 |
|
cursor_system_db = None |
28 |
|
try: |
29 |
|
cnx_system_db = mysql.connector.connect(**config.myems_system_db) |
30 |
|
cursor_system_db = cnx_system_db.cursor() |
31 |
|
except Exception as e: |
32 |
|
logger.error("Error in step 1.1 of combined_equipment_energy_output_category.main " + str(e)) |
33 |
|
if cursor_system_db: |
34 |
|
cursor_system_db.close() |
35 |
|
if cnx_system_db: |
36 |
|
cnx_system_db.close() |
37 |
|
# sleep and continue the outer loop to reconnect the database |
38 |
|
time.sleep(60) |
39 |
|
continue |
40 |
|
print("Connected to MyEMS System Database") |
41 |
|
|
42 |
|
combined_equipment_list = list() |
43 |
|
try: |
44 |
|
cursor_system_db.execute(" SELECT id, name " |
45 |
|
" FROM tbl_combined_equipments " |
46 |
|
" ORDER BY id ") |
47 |
|
rows_combined_equipments = cursor_system_db.fetchall() |
48 |
|
|
49 |
|
if rows_combined_equipments is None or len(rows_combined_equipments) == 0: |
50 |
|
print("There isn't any combined equipments ") |
51 |
|
# sleep and continue the outer loop to reconnect the database |
52 |
|
time.sleep(60) |
53 |
|
continue |
54 |
|
|
55 |
|
for row in rows_combined_equipments: |
56 |
|
combined_equipment_list.append({"id": row[0], "name": row[1]}) |
57 |
|
|
58 |
|
except Exception as e: |
59 |
|
logger.error("Error in step 1.2 of combined_equipment_energy_output_category.main " + str(e)) |
60 |
|
# sleep and continue the outer loop to reconnect the database |
61 |
|
time.sleep(60) |
62 |
|
continue |
63 |
|
finally: |
64 |
|
if cursor_system_db: |
65 |
|
cursor_system_db.close() |
66 |
|
if cnx_system_db: |
67 |
|
cnx_system_db.close() |
68 |
|
|
69 |
|
print("Got all combined equipments in MyEMS System Database") |
70 |
|
|
71 |
|
# shuffle the combined equipment list for randomly calculating the meter hourly value |
72 |
|
random.shuffle(combined_equipment_list) |
73 |
|
|
74 |
|
################################################################################################################ |
75 |
|
# Step 2: Create multiprocessing pool to call worker in parallel |
76 |
|
################################################################################################################ |
77 |
|
p = Pool(processes=config.pool_size) |
78 |
|
error_list = p.map(worker, combined_equipment_list) |
79 |
|
p.close() |
80 |
|
p.join() |
81 |
|
|
82 |
|
for error in error_list: |
83 |
|
if error is not None and len(error) > 0: |
84 |
|
logger.error(error) |
85 |
|
|
86 |
|
print("go to sleep 300 seconds...") |
87 |
|
time.sleep(300) |
88 |
|
print("wake from sleep, and continue to work...") |
89 |
|
# end of outer while |
90 |
|
|
91 |
|
|