1 | from datetime import datetime, timedelta |
||
2 | import falcon |
||
3 | import mysql.connector |
||
4 | import simplejson as json |
||
5 | from core.useractivity import access_control, api_key_control |
||
6 | import config |
||
7 | |||
8 | |||
9 | View Code Duplication | class Reporting: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
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: |
||
0 ignored issues
–
show
|
|||
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 |