@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of PVs |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated PVs |
|
128 | ################################################################################################################ |
|
129 | ||
130 | pv_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_photovoltaics " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_pvs = cursor_system.fetchall() |
|
137 | if rows_pvs is not None and len(rows_pvs) > 0: |
|
138 | for row in rows_pvs: |
|
139 | current_pv = dict() |
|
140 | current_pv['id'] = row[0] |
|
141 | current_pv['name'] = row[1] |
|
142 | current_pv['uuid'] = row[2] |
|
143 | current_pv['points'] = list() |
|
144 | pv_list.append(current_pv) |
|
145 | print(pv_list) |
|
146 | for index, pv in enumerate(pv_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_pvs_points bp, tbl_points p " |
|
149 | " WHERE bp.pv_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (pv['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | pv_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(pv_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of Loads |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated Loads |
|
128 | ################################################################################################################ |
|
129 | ||
130 | load_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_loads " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_loads = cursor_system.fetchall() |
|
137 | if rows_loads is not None and len(rows_loads) > 0: |
|
138 | for row in rows_loads: |
|
139 | current_load = dict() |
|
140 | current_load['id'] = row[0] |
|
141 | current_load['name'] = row[1] |
|
142 | current_load['uuid'] = row[2] |
|
143 | current_load['points'] = list() |
|
144 | load_list.append(current_load) |
|
145 | print(load_list) |
|
146 | for index, load in enumerate(load_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_loads_points bp, tbl_points p " |
|
149 | " WHERE bp.load_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (load['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | load_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(load_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of Generators |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated Generators |
|
128 | ################################################################################################################ |
|
129 | ||
130 | generator_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_generators " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_generators = cursor_system.fetchall() |
|
137 | if rows_generators is not None and len(rows_generators) > 0: |
|
138 | for row in rows_generators: |
|
139 | current_generator = dict() |
|
140 | current_generator['id'] = row[0] |
|
141 | current_generator['name'] = row[1] |
|
142 | current_generator['uuid'] = row[2] |
|
143 | current_generator['points'] = list() |
|
144 | generator_list.append(current_generator) |
|
145 | print(generator_list) |
|
146 | for index, generator in enumerate(generator_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_generators_points bp, tbl_points p " |
|
149 | " WHERE bp.generator_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (generator['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | generator_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(generator_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of BMSes |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated BMSes |
|
128 | ################################################################################################################ |
|
129 | ||
130 | bms_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_batteries " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_bmses = cursor_system.fetchall() |
|
137 | if rows_bmses is not None and len(rows_bmses) > 0: |
|
138 | for row in rows_bmses: |
|
139 | current_bms = dict() |
|
140 | current_bms['id'] = row[0] |
|
141 | current_bms['name'] = row[1] |
|
142 | current_bms['uuid'] = row[2] |
|
143 | current_bms['points'] = list() |
|
144 | bms_list.append(current_bms) |
|
145 | print(bms_list) |
|
146 | for index, bms in enumerate(bms_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_bmses_points bp, tbl_points p " |
|
149 | " WHERE bp.bms_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (bms['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | bms_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(bms_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of PCSes |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated PCSes |
|
128 | ################################################################################################################ |
|
129 | ||
130 | pcs_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_power_conversion_systems " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_pcses = cursor_system.fetchall() |
|
137 | if rows_pcses is not None and len(rows_pcses) > 0: |
|
138 | for row in rows_pcses: |
|
139 | current_pcs = dict() |
|
140 | current_pcs['id'] = row[0] |
|
141 | current_pcs['name'] = row[1] |
|
142 | current_pcs['uuid'] = row[2] |
|
143 | current_pcs['points'] = list() |
|
144 | pcs_list.append(current_pcs) |
|
145 | print(pcs_list) |
|
146 | for index, pcs in enumerate(pcs_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_pcses_points bp, tbl_points p " |
|
149 | " WHERE bp.pcs_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (pcs['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | pcs_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(pcs_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of EVChargers |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated EVChargers |
|
128 | ################################################################################################################ |
|
129 | ||
130 | evcharger_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_evchargers " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_evchargers = cursor_system.fetchall() |
|
137 | if rows_evchargers is not None and len(rows_evchargers) > 0: |
|
138 | for row in rows_evchargers: |
|
139 | current_evcharger = dict() |
|
140 | current_evcharger['id'] = row[0] |
|
141 | current_evcharger['name'] = row[1] |
|
142 | current_evcharger['uuid'] = row[2] |
|
143 | current_evcharger['points'] = list() |
|
144 | evcharger_list.append(current_evcharger) |
|
145 | print(evcharger_list) |
|
146 | for index, evcharger in enumerate(evcharger_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_evchargers_points bp, tbl_points p " |
|
149 | " WHERE bp.evcharger_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (evcharger['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | evcharger_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(evcharger_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of Heatpumps |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated Heatpumps |
|
128 | ################################################################################################################ |
|
129 | ||
130 | heatpump_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_heatpumps " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_heatpumps = cursor_system.fetchall() |
|
137 | if rows_heatpumps is not None and len(rows_heatpumps) > 0: |
|
138 | for row in rows_heatpumps: |
|
139 | current_heatpump = dict() |
|
140 | current_heatpump['id'] = row[0] |
|
141 | current_heatpump['name'] = row[1] |
|
142 | current_heatpump['uuid'] = row[2] |
|
143 | current_heatpump['points'] = list() |
|
144 | heatpump_list.append(current_heatpump) |
|
145 | print(heatpump_list) |
|
146 | for index, heatpump in enumerate(heatpump_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_heatpumps_points bp, tbl_points p " |
|
149 | " WHERE bp.heatpump_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (heatpump['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | heatpump_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(heatpump_list) |
|
174 |
@@ 9-173 (lines=165) @@ | ||
6 | import config |
|
7 | ||
8 | ||
9 | class Reporting: |
|
10 | def __init__(self): |
|
11 | """Initializes Class""" |
|
12 | pass |
|
13 | ||
14 | @staticmethod |
|
15 | def on_options(req, resp, id_): |
|
16 | _ = req |
|
17 | resp.status = falcon.HTTP_200 |
|
18 | _ = id_ |
|
19 | ||
20 | #################################################################################################################### |
|
21 | # PROCEDURES |
|
22 | # Step 1: valid parameters |
|
23 | # Step 2: query the microgrid |
|
24 | # Step 3: query analog points latest values |
|
25 | # Step 4: query energy points latest values |
|
26 | # Step 5: query digital points latest values |
|
27 | # Step 6: query the points of Grids |
|
28 | # Step 7: construct the report |
|
29 | #################################################################################################################### |
|
30 | @staticmethod |
|
31 | def on_get(req, resp, id_): |
|
32 | if 'API-KEY' not in req.headers or \ |
|
33 | not isinstance(req.headers['API-KEY'], str) or \ |
|
34 | len(str.strip(req.headers['API-KEY'])) == 0: |
|
35 | access_control(req) |
|
36 | else: |
|
37 | api_key_control(req) |
|
38 | ||
39 | ################################################################################################################ |
|
40 | # Step 1: valid parameters |
|
41 | ################################################################################################################ |
|
42 | if not id_.isdigit() or int(id_) <= 0: |
|
43 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
44 | description='API.INVALID_MICROGRID_ID') |
|
45 | microgrid_id = id_ |
|
46 | ################################################################################################################ |
|
47 | # Step 2: query the microgrid |
|
48 | ################################################################################################################ |
|
49 | cnx_system = mysql.connector.connect(**config.myems_system_db) |
|
50 | cursor_system = cnx_system.cursor() |
|
51 | ||
52 | cnx_historical = mysql.connector.connect(**config.myems_historical_db) |
|
53 | cursor_historical = cnx_historical.cursor() |
|
54 | ||
55 | if microgrid_id is not None: |
|
56 | query = (" SELECT id, name, uuid " |
|
57 | " FROM tbl_microgrids " |
|
58 | " WHERE id = %s ") |
|
59 | cursor_system.execute(query, (microgrid_id,)) |
|
60 | row = cursor_system.fetchone() |
|
61 | ||
62 | if row is None: |
|
63 | cursor_system.close() |
|
64 | cnx_system.close() |
|
65 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
66 | description='API.MICROGRID_NOT_FOUND') |
|
67 | ||
68 | # query all points |
|
69 | query = (" SELECT id, name, units, description " |
|
70 | " FROM tbl_points ") |
|
71 | cursor_system.execute(query) |
|
72 | rows = cursor_system.fetchall() |
|
73 | ||
74 | points_dict = dict() |
|
75 | if rows is not None and len(rows) > 0: |
|
76 | for row in rows: |
|
77 | points_dict[row[0]] = [row[1], row[2], row[3]] |
|
78 | ||
79 | ################################################################################################################ |
|
80 | # Step 3: query analog points latest values |
|
81 | ################################################################################################################ |
|
82 | ||
83 | latest_value_dict = dict() |
|
84 | query = (" SELECT point_id, actual_value " |
|
85 | " FROM tbl_analog_value_latest " |
|
86 | " WHERE utc_date_time > %s ") |
|
87 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
88 | rows = cursor_historical.fetchall() |
|
89 | if rows is not None and len(rows) > 0: |
|
90 | for row in rows: |
|
91 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
92 | points_dict[row[0]][1], |
|
93 | points_dict[row[0]][2], |
|
94 | row[1]] |
|
95 | ||
96 | ################################################################################################################ |
|
97 | # Step 4: query energy points latest values |
|
98 | ################################################################################################################ |
|
99 | query = (" SELECT point_id, actual_value " |
|
100 | " FROM tbl_energy_value_latest " |
|
101 | " WHERE utc_date_time > %s ") |
|
102 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
103 | rows = cursor_historical.fetchall() |
|
104 | if rows is not None and len(rows) > 0: |
|
105 | for row in rows: |
|
106 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
107 | points_dict[row[0]][1], |
|
108 | points_dict[row[0]][2], |
|
109 | row[1]] |
|
110 | ||
111 | ################################################################################################################ |
|
112 | # Step 5: query digital points latest values |
|
113 | ################################################################################################################ |
|
114 | query = (" SELECT point_id, actual_value " |
|
115 | " FROM tbl_digital_value_latest " |
|
116 | " WHERE utc_date_time > %s ") |
|
117 | cursor_historical.execute(query, (datetime.utcnow() - timedelta(minutes=60),)) |
|
118 | rows = cursor_historical.fetchall() |
|
119 | if rows is not None and len(rows) > 0: |
|
120 | for row in rows: |
|
121 | latest_value_dict[row[0]] = [points_dict[row[0]][0], |
|
122 | points_dict[row[0]][1], |
|
123 | points_dict[row[0]][2], |
|
124 | row[1]] |
|
125 | ||
126 | ################################################################################################################ |
|
127 | # Step 6: query the points of associated Grids |
|
128 | ################################################################################################################ |
|
129 | ||
130 | grid_list = list() |
|
131 | cursor_system.execute(" SELECT id, name, uuid " |
|
132 | " FROM tbl_microgrids_grids " |
|
133 | " WHERE microgrid_id = %s " |
|
134 | " ORDER BY id ", |
|
135 | (microgrid_id,)) |
|
136 | rows_grids = cursor_system.fetchall() |
|
137 | if rows_grids is not None and len(rows_grids) > 0: |
|
138 | for row in rows_grids: |
|
139 | current_grid = dict() |
|
140 | current_grid['id'] = row[0] |
|
141 | current_grid['name'] = row[1] |
|
142 | current_grid['uuid'] = row[2] |
|
143 | current_grid['points'] = list() |
|
144 | grid_list.append(current_grid) |
|
145 | print(grid_list) |
|
146 | for index, grid in enumerate(grid_list): |
|
147 | cursor_system.execute(" SELECT p.id " |
|
148 | " FROM tbl_microgrids_grids_points bp, tbl_points p " |
|
149 | " WHERE bp.grid_id = %s AND bp.point_id = p.id " |
|
150 | " ORDER BY bp.id ", |
|
151 | (grid['id'],)) |
|
152 | rows_points = cursor_system.fetchall() |
|
153 | if rows_points is not None and len(rows_points) > 0: |
|
154 | point_list = list() |
|
155 | for row in rows_points: |
|
156 | point = latest_value_dict.get(row[0], None) |
|
157 | if point is not None: |
|
158 | point_list.append(point) |
|
159 | grid_list[index]['points'] = point_list |
|
160 | ||
161 | if cursor_system: |
|
162 | cursor_system.close() |
|
163 | if cnx_system: |
|
164 | cnx_system.close() |
|
165 | ||
166 | if cursor_historical: |
|
167 | cursor_historical.close() |
|
168 | if cnx_historical: |
|
169 | cnx_historical.close() |
|
170 | ################################################################################################################ |
|
171 | # Step 8: construct the report |
|
172 | ################################################################################################################ |
|
173 | resp.text = json.dumps(grid_list) |
|
174 |