1
|
|
|
import uuid |
2
|
|
|
from datetime import datetime, timedelta |
3
|
|
|
import falcon |
4
|
|
|
import mysql.connector |
5
|
|
|
import simplejson as json |
6
|
|
|
from core.useractivity import user_logger, admin_control, access_control, api_key_control |
7
|
|
|
import config |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class EnergyStoragePowerStationCollection: |
11
|
|
|
def __init__(self): |
12
|
|
|
""""Initializes Class""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
_ = req |
18
|
|
|
resp.status = falcon.HTTP_200 |
19
|
|
|
|
20
|
|
|
@staticmethod |
21
|
|
|
def on_get(req, resp): |
22
|
|
|
access_control(req) |
23
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
24
|
|
|
cursor = cnx.cursor() |
25
|
|
|
|
26
|
|
|
# query contact dict |
27
|
|
|
contact_dict = dict() |
28
|
|
|
query = (" SELECT id, name, uuid " |
29
|
|
|
" FROM tbl_contacts ") |
30
|
|
|
cursor.execute(query) |
31
|
|
|
rows_contacts = cursor.fetchall() |
32
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
33
|
|
|
for row in rows_contacts: |
34
|
|
|
contact_dict[row[0]] = {"id": row[0], |
35
|
|
|
"name": row[1], |
36
|
|
|
"uuid": row[2]} |
37
|
|
|
# query cost center dict |
38
|
|
|
cost_center_dict = dict() |
39
|
|
|
query = (" SELECT id, name, uuid " |
40
|
|
|
" FROM tbl_cost_centers ") |
41
|
|
|
cursor.execute(query) |
42
|
|
|
rows_cost_centers = cursor.fetchall() |
43
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
44
|
|
|
for row in rows_cost_centers: |
45
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
46
|
|
|
"name": row[1], |
47
|
|
|
"uuid": row[2]} |
48
|
|
|
|
49
|
|
|
# query svg dict |
50
|
|
|
svg_dict = dict() |
51
|
|
|
query = (" SELECT id, name, uuid " |
52
|
|
|
" FROM tbl_svgs ") |
53
|
|
|
cursor.execute(query) |
54
|
|
|
rows_svgs = cursor.fetchall() |
55
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
56
|
|
|
for row in rows_svgs: |
57
|
|
|
svg_dict[row[0]] = {"id": row[0], |
58
|
|
|
"name": row[1], |
59
|
|
|
"uuid": row[2]} |
60
|
|
|
|
61
|
|
|
# query point dict |
62
|
|
|
point_dict = dict() |
63
|
|
|
query = (" SELECT id, name " |
64
|
|
|
" FROM tbl_points ") |
65
|
|
|
cursor.execute(query) |
66
|
|
|
rows_points = cursor.fetchall() |
67
|
|
|
if rows_points is not None and len(rows_points) > 0: |
68
|
|
|
for row in rows_points: |
69
|
|
|
point_dict[row[0]] = {"id": row[0], |
70
|
|
|
"name": row[1]} |
71
|
|
|
|
72
|
|
|
query = (" SELECT id, name, uuid, " |
73
|
|
|
" address, latitude, longitude, latitude_point_id, longitude_point_id, " |
74
|
|
|
" rated_capacity, rated_power, contact_id, cost_center_id, " |
75
|
|
|
" svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
76
|
|
|
" is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
77
|
|
|
" FROM tbl_energy_storage_power_stations " |
78
|
|
|
" ORDER BY id ") |
79
|
|
|
cursor.execute(query) |
80
|
|
|
rows_energy_storage_power_stations = cursor.fetchall() |
81
|
|
|
|
82
|
|
|
result = list() |
83
|
|
|
if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
84
|
|
|
for row in rows_energy_storage_power_stations: |
85
|
|
|
meta_result = {"id": row[0], |
86
|
|
|
"name": row[1], |
87
|
|
|
"uuid": row[2], |
88
|
|
|
"address": row[3], |
89
|
|
|
"latitude": row[4], |
90
|
|
|
"longitude": row[5], |
91
|
|
|
"latitude_point": point_dict.get(row[6], None), |
92
|
|
|
"longitude_point": point_dict.get(row[7], None), |
93
|
|
|
"rated_capacity": row[8], |
94
|
|
|
"rated_power": row[9], |
95
|
|
|
"contact": contact_dict.get(row[10], None), |
96
|
|
|
"cost_center": cost_center_dict.get(row[11], None), |
97
|
|
|
"svg": svg_dict.get(row[12], None), |
98
|
|
|
"svg2": svg_dict.get(row[13], None), |
99
|
|
|
"svg3": svg_dict.get(row[14], None), |
100
|
|
|
"svg4": svg_dict.get(row[15], None), |
101
|
|
|
"svg5": svg_dict.get(row[16], None), |
102
|
|
|
"is_cost_data_displayed": bool(row[17]), |
103
|
|
|
"phase_of_lifecycle": row[18], |
104
|
|
|
"commissioning_date": row[19].isoformat() if row[19] else None, |
105
|
|
|
"description": row[20], |
106
|
|
|
"qrcode": 'energystoragepowerstation:' + row[2]} |
107
|
|
|
result.append(meta_result) |
108
|
|
|
|
109
|
|
|
cursor.close() |
110
|
|
|
cnx.close() |
111
|
|
|
resp.text = json.dumps(result) |
112
|
|
|
|
113
|
|
|
@staticmethod |
114
|
|
|
@user_logger |
115
|
|
|
def on_post(req, resp): |
116
|
|
|
"""Handles POST requests""" |
117
|
|
|
admin_control(req) |
118
|
|
|
try: |
119
|
|
|
raw_json = req.stream.read().decode('utf-8') |
120
|
|
|
except Exception as ex: |
121
|
|
|
print(str(ex)) |
122
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
123
|
|
|
title='API.BAD_REQUEST', |
124
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
125
|
|
|
|
126
|
|
|
new_values = json.loads(raw_json) |
127
|
|
|
|
128
|
|
|
if 'name' not in new_values['data'].keys() or \ |
129
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
130
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
131
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
132
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
133
|
|
|
name = str.strip(new_values['data']['name']) |
134
|
|
|
|
135
|
|
|
if 'address' not in new_values['data'].keys() or \ |
136
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
137
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
138
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
139
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
140
|
|
|
address = str.strip(new_values['data']['address']) |
141
|
|
|
|
142
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
143
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
144
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
145
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
146
|
|
|
new_values['data']['latitude'] > 90.0: |
147
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
148
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
149
|
|
|
latitude = new_values['data']['latitude'] |
150
|
|
|
|
151
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
152
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
153
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
154
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
155
|
|
|
new_values['data']['longitude'] > 180.0: |
156
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
157
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
158
|
|
|
longitude = new_values['data']['longitude'] |
159
|
|
|
|
160
|
|
|
if 'latitude_point_id' in new_values['data'].keys() and \ |
161
|
|
|
isinstance(new_values['data']['latitude_point_id'], int) and \ |
162
|
|
|
new_values['data']['latitude_point_id'] > 0: |
163
|
|
|
latitude_point_id = new_values['data']['latitude_point_id'] |
164
|
|
|
else: |
165
|
|
|
latitude_point_id = None |
166
|
|
|
|
167
|
|
|
if 'longitude_point_id' in new_values['data'].keys() and \ |
168
|
|
|
isinstance(new_values['data']['longitude_point_id'], int) and \ |
169
|
|
|
new_values['data']['longitude_point_id'] > 0: |
170
|
|
|
longitude_point_id = new_values['data']['longitude_point_id'] |
171
|
|
|
else: |
172
|
|
|
longitude_point_id = None |
173
|
|
|
|
174
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
175
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
176
|
|
|
isinstance(new_values['data']['rated_capacity'], int)) or \ |
177
|
|
|
new_values['data']['rated_capacity'] < 0.0: |
178
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
179
|
|
|
description='API.INVALID_RATED_CAPACITY') |
180
|
|
|
rated_capacity = new_values['data']['rated_capacity'] |
181
|
|
|
|
182
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
183
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
184
|
|
|
isinstance(new_values['data']['rated_power'], int)) or \ |
185
|
|
|
new_values['data']['rated_power'] < 0.0: |
186
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
187
|
|
|
description='API.INVALID_RATED_POWER') |
188
|
|
|
rated_power = new_values['data']['rated_power'] |
189
|
|
|
|
190
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
191
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
192
|
|
|
new_values['data']['contact_id'] <= 0: |
193
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
194
|
|
|
description='API.INVALID_CONTACT_ID') |
195
|
|
|
contact_id = new_values['data']['contact_id'] |
196
|
|
|
|
197
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
198
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
199
|
|
|
new_values['data']['cost_center_id'] <= 0: |
200
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
201
|
|
|
description='API.INVALID_COST_CENTER_ID') |
202
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
203
|
|
|
|
204
|
|
|
if 'svg_id' not in new_values['data'].keys() or \ |
205
|
|
|
not isinstance(new_values['data']['svg_id'], int) or \ |
206
|
|
|
new_values['data']['svg_id'] <= 0: |
207
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
208
|
|
|
description='API.INVALID_SVG_ID') |
209
|
|
|
svg_id = new_values['data']['svg_id'] |
210
|
|
|
|
211
|
|
|
if 'svg2_id' in new_values['data'].keys() and \ |
212
|
|
|
isinstance(new_values['data']['svg2_id'], int) and \ |
213
|
|
|
new_values['data']['svg2_id'] > 0: |
214
|
|
|
svg2_id = new_values['data']['svg2_id'] |
215
|
|
|
else: |
216
|
|
|
svg2_id = None |
217
|
|
|
|
218
|
|
|
if 'svg3_id' in new_values['data'].keys() and \ |
219
|
|
|
isinstance(new_values['data']['svg3_id'], int) and \ |
220
|
|
|
new_values['data']['svg3_id'] > 0: |
221
|
|
|
svg3_id = new_values['data']['svg3_id'] |
222
|
|
|
else: |
223
|
|
|
svg3_id = None |
224
|
|
|
|
225
|
|
|
if 'svg4_id' in new_values['data'].keys() and \ |
226
|
|
|
isinstance(new_values['data']['svg4_id'], int) and \ |
227
|
|
|
new_values['data']['svg4_id'] > 0: |
228
|
|
|
svg4_id = new_values['data']['svg4_id'] |
229
|
|
|
else: |
230
|
|
|
svg4_id = None |
231
|
|
|
|
232
|
|
|
if 'svg5_id' in new_values['data'].keys() and \ |
233
|
|
|
isinstance(new_values['data']['svg5_id'], int) and \ |
234
|
|
|
new_values['data']['svg5_id'] > 0: |
235
|
|
|
svg5_id = new_values['data']['svg5_id'] |
236
|
|
|
else: |
237
|
|
|
svg5_id = None |
238
|
|
|
|
239
|
|
|
if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
240
|
|
|
not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
241
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
242
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED') |
243
|
|
|
is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
244
|
|
|
|
245
|
|
|
if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
246
|
|
|
not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
247
|
|
|
len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
248
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
249
|
|
|
description='API.INVALID_PHASE_OF_LIFECYCLE') |
250
|
|
|
phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
251
|
|
|
|
252
|
|
|
if 'commissioning_date' not in new_values['data'].keys() or \ |
253
|
|
|
not isinstance(new_values['data']['commissioning_date'], str) or \ |
254
|
|
|
len(str.strip(new_values['data']['commissioning_date'])) == 0: |
255
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
256
|
|
|
description='API.INVALID_COMMISSIONING_DATE') |
257
|
|
|
commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
258
|
|
|
|
259
|
|
|
if 'description' in new_values['data'].keys() and \ |
260
|
|
|
new_values['data']['description'] is not None and \ |
261
|
|
|
len(str(new_values['data']['description'])) > 0: |
262
|
|
|
description = str.strip(new_values['data']['description']) |
263
|
|
|
else: |
264
|
|
|
description = None |
265
|
|
|
|
266
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
267
|
|
|
cursor = cnx.cursor() |
268
|
|
|
|
269
|
|
|
cursor.execute(" SELECT name " |
270
|
|
|
" FROM tbl_energy_storage_power_stations " |
271
|
|
|
" WHERE name = %s ", (name,)) |
272
|
|
|
if cursor.fetchone() is not None: |
273
|
|
|
cursor.close() |
274
|
|
|
cnx.close() |
275
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
276
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
277
|
|
|
|
278
|
|
|
cursor.execute(" SELECT name " |
279
|
|
|
" FROM tbl_contacts " |
280
|
|
|
" WHERE id = %s ", |
281
|
|
|
(contact_id,)) |
282
|
|
|
row = cursor.fetchone() |
283
|
|
|
if row is None: |
284
|
|
|
cursor.close() |
285
|
|
|
cnx.close() |
286
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
287
|
|
|
description='API.CONTACT_NOT_FOUND') |
288
|
|
|
|
289
|
|
|
cursor.execute(" SELECT name " |
290
|
|
|
" FROM tbl_cost_centers " |
291
|
|
|
" WHERE id = %s ", |
292
|
|
|
(cost_center_id,)) |
293
|
|
|
row = cursor.fetchone() |
294
|
|
|
if row is None: |
295
|
|
|
cursor.close() |
296
|
|
|
cnx.close() |
297
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
298
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
299
|
|
|
|
300
|
|
|
cursor.execute(" SELECT name " |
301
|
|
|
" FROM tbl_svgs " |
302
|
|
|
" WHERE id = %s ", |
303
|
|
|
(svg_id,)) |
304
|
|
|
row = cursor.fetchone() |
305
|
|
|
if row is None: |
306
|
|
|
cursor.close() |
307
|
|
|
cnx.close() |
308
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
309
|
|
|
description='API.SVG_NOT_FOUND') |
310
|
|
|
|
311
|
|
|
add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
312
|
|
|
" (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
313
|
|
|
" rated_capacity, rated_power, contact_id, cost_center_id, " |
314
|
|
|
" svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
315
|
|
|
" is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description) " |
316
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
317
|
|
|
cursor.execute(add_values, (name, |
318
|
|
|
str(uuid.uuid4()), |
319
|
|
|
address, |
320
|
|
|
latitude, |
321
|
|
|
longitude, |
322
|
|
|
latitude_point_id, |
323
|
|
|
longitude_point_id, |
324
|
|
|
rated_capacity, |
325
|
|
|
rated_power, |
326
|
|
|
contact_id, |
327
|
|
|
cost_center_id, |
328
|
|
|
svg_id, |
329
|
|
|
svg2_id, |
330
|
|
|
svg3_id, |
331
|
|
|
svg4_id, |
332
|
|
|
svg5_id, |
333
|
|
|
is_cost_data_displayed, |
334
|
|
|
phase_of_lifecycle, |
335
|
|
|
commissioning_date, |
336
|
|
|
description)) |
337
|
|
|
new_id = cursor.lastrowid |
338
|
|
|
cnx.commit() |
339
|
|
|
cursor.close() |
340
|
|
|
cnx.close() |
341
|
|
|
|
342
|
|
|
resp.status = falcon.HTTP_201 |
343
|
|
|
resp.location = '/energystoragepowerstations/' + str(new_id) |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
class EnergyStoragePowerStationItem: |
347
|
|
|
def __init__(self): |
348
|
|
|
""""Initializes Class""" |
349
|
|
|
pass |
350
|
|
|
|
351
|
|
|
@staticmethod |
352
|
|
|
def on_options(req, resp, id_): |
353
|
|
|
_ = req |
354
|
|
|
resp.status = falcon.HTTP_200 |
355
|
|
|
_ = id_ |
356
|
|
|
|
357
|
|
|
@staticmethod |
358
|
|
|
def on_get(req, resp, id_): |
359
|
|
|
access_control(req) |
360
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
361
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
362
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
363
|
|
|
|
364
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
365
|
|
|
cursor = cnx.cursor() |
366
|
|
|
|
367
|
|
|
contact_dict = dict() |
368
|
|
|
query = (" SELECT id, name, uuid " |
369
|
|
|
" FROM tbl_contacts ") |
370
|
|
|
cursor.execute(query) |
371
|
|
|
rows_contacts = cursor.fetchall() |
372
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
373
|
|
|
for row in rows_contacts: |
374
|
|
|
contact_dict[row[0]] = {"id": row[0], |
375
|
|
|
"name": row[1], |
376
|
|
|
"uuid": row[2]} |
377
|
|
|
|
378
|
|
|
cost_center_dict = dict() |
379
|
|
|
query = (" SELECT id, name, uuid " |
380
|
|
|
" FROM tbl_cost_centers ") |
381
|
|
|
cursor.execute(query) |
382
|
|
|
rows_cost_centers = cursor.fetchall() |
383
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
384
|
|
|
for row in rows_cost_centers: |
385
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
386
|
|
|
"name": row[1], |
387
|
|
|
"uuid": row[2]} |
388
|
|
|
|
389
|
|
|
svg_dict = dict() |
390
|
|
|
query = (" SELECT id, name, uuid " |
391
|
|
|
" FROM tbl_svgs ") |
392
|
|
|
cursor.execute(query) |
393
|
|
|
rows_svgs = cursor.fetchall() |
394
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
395
|
|
|
for row in rows_svgs: |
396
|
|
|
svg_dict[row[0]] = {"id": row[0], |
397
|
|
|
"name": row[1], |
398
|
|
|
"uuid": row[2]} |
399
|
|
|
|
400
|
|
|
# query point dict |
401
|
|
|
point_dict = dict() |
402
|
|
|
query = (" SELECT id, name " |
403
|
|
|
" FROM tbl_points ") |
404
|
|
|
cursor.execute(query) |
405
|
|
|
rows_points = cursor.fetchall() |
406
|
|
|
if rows_points is not None and len(rows_points) > 0: |
407
|
|
|
for row in rows_points: |
408
|
|
|
point_dict[row[0]] = {"id": row[0], |
409
|
|
|
"name": row[1]} |
410
|
|
|
|
411
|
|
|
query = (" SELECT id, name, uuid, " |
412
|
|
|
" address, latitude, longitude, latitude_point_id, longitude_point_id, " |
413
|
|
|
" rated_capacity, rated_power, contact_id, cost_center_id, " |
414
|
|
|
" svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
415
|
|
|
" is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
416
|
|
|
" FROM tbl_energy_storage_power_stations " |
417
|
|
|
" WHERE id = %s ") |
418
|
|
|
cursor.execute(query, (id_,)) |
419
|
|
|
row = cursor.fetchone() |
420
|
|
|
cursor.close() |
421
|
|
|
cnx.close() |
422
|
|
|
|
423
|
|
|
if row is None: |
424
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
425
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
426
|
|
|
else: |
427
|
|
|
meta_result = {"id": row[0], |
428
|
|
|
"name": row[1], |
429
|
|
|
"uuid": row[2], |
430
|
|
|
"address": row[3], |
431
|
|
|
"latitude": row[4], |
432
|
|
|
"longitude": row[5], |
433
|
|
|
"latitude_point": point_dict.get(row[6], None), |
434
|
|
|
"longitude_point": point_dict.get(row[7], None), |
435
|
|
|
"rated_capacity": row[8], |
436
|
|
|
"rated_power": row[9], |
437
|
|
|
"contact": contact_dict.get(row[10], None), |
438
|
|
|
"cost_center": cost_center_dict.get(row[11], None), |
439
|
|
|
"svg": svg_dict.get(row[12], None), |
440
|
|
|
"svg2": svg_dict.get(row[13], None), |
441
|
|
|
"svg3": svg_dict.get(row[14], None), |
442
|
|
|
"svg4": svg_dict.get(row[15], None), |
443
|
|
|
"svg5": svg_dict.get(row[16], None), |
444
|
|
|
"is_cost_data_displayed": bool(row[17]), |
445
|
|
|
"phase_of_lifecycle": row[18], |
446
|
|
|
"commissioning_date": row[19].isoformat() if row[19] else None, |
447
|
|
|
"description": row[20], |
448
|
|
|
"qrcode": 'energystoragepowerstation:' + row[2]} |
449
|
|
|
|
450
|
|
|
resp.text = json.dumps(meta_result) |
451
|
|
|
|
452
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
453
|
|
|
@user_logger |
454
|
|
|
def on_delete(req, resp, id_): |
455
|
|
|
admin_control(req) |
456
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
457
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
458
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
459
|
|
|
|
460
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
461
|
|
|
cursor = cnx.cursor() |
462
|
|
|
|
463
|
|
|
cursor.execute(" SELECT name " |
464
|
|
|
" FROM tbl_energy_storage_power_stations " |
465
|
|
|
" WHERE id = %s ", (id_,)) |
466
|
|
|
if cursor.fetchone() is None: |
467
|
|
|
cursor.close() |
468
|
|
|
cnx.close() |
469
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
470
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
471
|
|
|
|
472
|
|
|
# check relation with spaces |
473
|
|
|
cursor.execute(" SELECT id " |
474
|
|
|
" FROM tbl_spaces_energy_storage_power_stations " |
475
|
|
|
" WHERE energy_storage_power_station_id = %s ", (id_,)) |
476
|
|
|
rows_spaces = cursor.fetchall() |
477
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0: |
478
|
|
|
cursor.close() |
479
|
|
|
cnx.close() |
480
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
481
|
|
|
title='API.BAD_REQUEST', |
482
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACES') |
483
|
|
|
|
484
|
|
|
cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
485
|
|
|
" WHERE energy_storage_power_station_id = %s ", (id_,)) |
486
|
|
|
|
487
|
|
|
cursor.execute(" DELETE FROM tbl_energy_storage_power_stations " |
488
|
|
|
" WHERE id = %s ", (id_,)) |
489
|
|
|
cnx.commit() |
490
|
|
|
|
491
|
|
|
cursor.close() |
492
|
|
|
cnx.close() |
493
|
|
|
|
494
|
|
|
resp.status = falcon.HTTP_204 |
495
|
|
|
|
496
|
|
|
@staticmethod |
497
|
|
|
@user_logger |
498
|
|
|
def on_put(req, resp, id_): |
499
|
|
|
"""Handles PUT requests""" |
500
|
|
|
admin_control(req) |
501
|
|
|
try: |
502
|
|
|
raw_json = req.stream.read().decode('utf-8') |
503
|
|
|
except Exception as ex: |
504
|
|
|
print(str(ex)) |
505
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
506
|
|
|
title='API.BAD_REQUEST', |
507
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
508
|
|
|
|
509
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
510
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
511
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
512
|
|
|
|
513
|
|
|
new_values = json.loads(raw_json) |
514
|
|
|
|
515
|
|
|
if 'name' not in new_values['data'].keys() or \ |
516
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
517
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
518
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
519
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
520
|
|
|
name = str.strip(new_values['data']['name']) |
521
|
|
|
|
522
|
|
|
if 'address' not in new_values['data'].keys() or \ |
523
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
524
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
525
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
526
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
527
|
|
|
address = str.strip(new_values['data']['address']) |
528
|
|
|
|
529
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
530
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
531
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
532
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
533
|
|
|
new_values['data']['latitude'] > 90.0: |
534
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
535
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
536
|
|
|
latitude = new_values['data']['latitude'] |
537
|
|
|
|
538
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
539
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
540
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
541
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
542
|
|
|
new_values['data']['longitude'] > 180.0: |
543
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
544
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
545
|
|
|
longitude = new_values['data']['longitude'] |
546
|
|
|
|
547
|
|
|
if 'latitude_point_id' in new_values['data'].keys() and \ |
548
|
|
|
isinstance(new_values['data']['latitude_point_id'], int) and \ |
549
|
|
|
new_values['data']['latitude_point_id'] > 0: |
550
|
|
|
latitude_point_id = new_values['data']['latitude_point_id'] |
551
|
|
|
else: |
552
|
|
|
latitude_point_id = None |
553
|
|
|
|
554
|
|
|
if 'longitude_point_id' in new_values['data'].keys() and \ |
555
|
|
|
isinstance(new_values['data']['longitude_point_id'], int) and \ |
556
|
|
|
new_values['data']['longitude_point_id'] > 0: |
557
|
|
|
longitude_point_id = new_values['data']['longitude_point_id'] |
558
|
|
|
else: |
559
|
|
|
longitude_point_id = None |
560
|
|
|
|
561
|
|
|
if 'rated_capacity' not in new_values['data'].keys() or \ |
562
|
|
|
not (isinstance(new_values['data']['rated_capacity'], float) or |
563
|
|
|
isinstance(new_values['data']['rated_capacity'], int)) or \ |
564
|
|
|
new_values['data']['rated_capacity'] < 0.0: |
565
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
566
|
|
|
description='API.INVALID_RATED_CAPACITY') |
567
|
|
|
rated_capacity = new_values['data']['rated_capacity'] |
568
|
|
|
|
569
|
|
|
if 'rated_power' not in new_values['data'].keys() or \ |
570
|
|
|
not (isinstance(new_values['data']['rated_power'], float) or |
571
|
|
|
isinstance(new_values['data']['rated_power'], int)) or \ |
572
|
|
|
new_values['data']['rated_power'] < 0.0: |
573
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
574
|
|
|
description='API.INVALID_RATED_POWER') |
575
|
|
|
rated_power = new_values['data']['rated_power'] |
576
|
|
|
|
577
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
578
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
579
|
|
|
new_values['data']['contact_id'] <= 0: |
580
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
581
|
|
|
description='API.INVALID_CONTACT_ID') |
582
|
|
|
contact_id = new_values['data']['contact_id'] |
583
|
|
|
|
584
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
585
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
586
|
|
|
new_values['data']['cost_center_id'] <= 0: |
587
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
588
|
|
|
description='API.INVALID_COST_CENTER_ID') |
589
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
590
|
|
|
|
591
|
|
|
if 'svg_id' not in new_values['data'].keys() or \ |
592
|
|
|
not isinstance(new_values['data']['svg_id'], int) or \ |
593
|
|
|
new_values['data']['svg_id'] <= 0: |
594
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
595
|
|
|
description='API.INVALID_SVG_ID') |
596
|
|
|
svg_id = new_values['data']['svg_id'] |
597
|
|
|
|
598
|
|
|
if 'svg2_id' in new_values['data'].keys() and \ |
599
|
|
|
isinstance(new_values['data']['svg2_id'], int) and \ |
600
|
|
|
new_values['data']['svg2_id'] > 0: |
601
|
|
|
svg2_id = new_values['data']['svg2_id'] |
602
|
|
|
else: |
603
|
|
|
svg2_id = None |
604
|
|
|
|
605
|
|
|
if 'svg3_id' in new_values['data'].keys() and \ |
606
|
|
|
isinstance(new_values['data']['svg3_id'], int) and \ |
607
|
|
|
new_values['data']['svg3_id'] > 0: |
608
|
|
|
svg3_id = new_values['data']['svg3_id'] |
609
|
|
|
else: |
610
|
|
|
svg3_id = None |
611
|
|
|
|
612
|
|
|
if 'svg4_id' in new_values['data'].keys() and \ |
613
|
|
|
isinstance(new_values['data']['svg4_id'], int) and \ |
614
|
|
|
new_values['data']['svg4_id'] > 0: |
615
|
|
|
svg4_id = new_values['data']['svg4_id'] |
616
|
|
|
else: |
617
|
|
|
svg4_id = None |
618
|
|
|
|
619
|
|
|
if 'svg5_id' in new_values['data'].keys() and \ |
620
|
|
|
isinstance(new_values['data']['svg5_id'], int) and \ |
621
|
|
|
new_values['data']['svg5_id'] > 0: |
622
|
|
|
svg5_id = new_values['data']['svg5_id'] |
623
|
|
|
else: |
624
|
|
|
svg5_id = None |
625
|
|
|
|
626
|
|
|
if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
627
|
|
|
not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
628
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
629
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE') |
630
|
|
|
is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
631
|
|
|
|
632
|
|
|
if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
633
|
|
|
not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
634
|
|
|
len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
635
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
636
|
|
|
description='API.INVALID_PHASE_OF_LIFECYCLE') |
637
|
|
|
phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
638
|
|
|
|
639
|
|
|
if 'commissioning_date' not in new_values['data'].keys() or \ |
640
|
|
|
not isinstance(new_values['data']['commissioning_date'], str) or \ |
641
|
|
|
len(str.strip(new_values['data']['commissioning_date'])) == 0: |
642
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
643
|
|
|
description='API.INVALID_COMMISSIONING_DATE') |
644
|
|
|
commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
645
|
|
|
|
646
|
|
|
if 'description' in new_values['data'].keys() and \ |
647
|
|
|
new_values['data']['description'] is not None and \ |
648
|
|
|
len(str(new_values['data']['description'])) > 0: |
649
|
|
|
description = str.strip(new_values['data']['description']) |
650
|
|
|
else: |
651
|
|
|
description = None |
652
|
|
|
|
653
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
654
|
|
|
cursor = cnx.cursor() |
655
|
|
|
|
656
|
|
|
cursor.execute(" SELECT name " |
657
|
|
|
" FROM tbl_energy_storage_power_stations " |
658
|
|
|
" WHERE id = %s ", (id_,)) |
659
|
|
|
if cursor.fetchone() is None: |
660
|
|
|
cursor.close() |
661
|
|
|
cnx.close() |
662
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
663
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
664
|
|
|
|
665
|
|
|
cursor.execute(" SELECT name " |
666
|
|
|
" FROM tbl_energy_storage_power_stations " |
667
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
668
|
|
|
if cursor.fetchone() is not None: |
669
|
|
|
cursor.close() |
670
|
|
|
cnx.close() |
671
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
672
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
673
|
|
|
|
674
|
|
|
cursor.execute(" SELECT name " |
675
|
|
|
" FROM tbl_contacts " |
676
|
|
|
" WHERE id = %s ", |
677
|
|
|
(new_values['data']['contact_id'],)) |
678
|
|
|
row = cursor.fetchone() |
679
|
|
|
if row is None: |
680
|
|
|
cursor.close() |
681
|
|
|
cnx.close() |
682
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
683
|
|
|
description='API.CONTACT_NOT_FOUND') |
684
|
|
|
|
685
|
|
|
cursor.execute(" SELECT name " |
686
|
|
|
" FROM tbl_cost_centers " |
687
|
|
|
" WHERE id = %s ", |
688
|
|
|
(new_values['data']['cost_center_id'],)) |
689
|
|
|
row = cursor.fetchone() |
690
|
|
|
if row is None: |
691
|
|
|
cursor.close() |
692
|
|
|
cnx.close() |
693
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
694
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
695
|
|
|
|
696
|
|
|
cursor.execute(" SELECT name " |
697
|
|
|
" FROM tbl_svgs " |
698
|
|
|
" WHERE id = %s ", |
699
|
|
|
(new_values['data']['svg_id'],)) |
700
|
|
|
row = cursor.fetchone() |
701
|
|
|
if row is None: |
702
|
|
|
cursor.close() |
703
|
|
|
cnx.close() |
704
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
705
|
|
|
description='API.SVG_NOT_FOUND') |
706
|
|
|
|
707
|
|
|
update_row = (" UPDATE tbl_energy_storage_power_stations " |
708
|
|
|
" SET name = %s, address = %s, latitude = %s, longitude = %s, " |
709
|
|
|
" latitude_point_id = %s, longitude_point_id = %s, " |
710
|
|
|
" rated_capacity = %s, rated_power = %s, " |
711
|
|
|
" contact_id = %s, cost_center_id = %s, " |
712
|
|
|
" svg_id = %s, svg2_id = %s, svg3_id = %s, svg4_id = %s, svg5_id = %s, " |
713
|
|
|
" is_cost_data_displayed = %s, phase_of_lifecycle = %s, commissioning_date = %s, " |
714
|
|
|
" description = %s " |
715
|
|
|
" WHERE id = %s ") |
716
|
|
|
cursor.execute(update_row, (name, |
717
|
|
|
address, |
718
|
|
|
latitude, |
719
|
|
|
longitude, |
720
|
|
|
latitude_point_id, |
721
|
|
|
longitude_point_id, |
722
|
|
|
rated_capacity, |
723
|
|
|
rated_power, |
724
|
|
|
contact_id, |
725
|
|
|
cost_center_id, |
726
|
|
|
svg_id, |
727
|
|
|
svg2_id, |
728
|
|
|
svg3_id, |
729
|
|
|
svg4_id, |
730
|
|
|
svg5_id, |
731
|
|
|
is_cost_data_displayed, |
732
|
|
|
phase_of_lifecycle, |
733
|
|
|
commissioning_date, |
734
|
|
|
description, |
735
|
|
|
id_)) |
736
|
|
|
cnx.commit() |
737
|
|
|
|
738
|
|
|
cursor.close() |
739
|
|
|
cnx.close() |
740
|
|
|
|
741
|
|
|
resp.status = falcon.HTTP_200 |
742
|
|
|
|
743
|
|
|
|
744
|
|
View Code Duplication |
class EnergyStoragePowerStationContainerCollection: |
|
|
|
|
745
|
|
|
def __init__(self): |
746
|
|
|
"""Initializes Class""" |
747
|
|
|
pass |
748
|
|
|
|
749
|
|
|
@staticmethod |
750
|
|
|
def on_options(req, resp, id_): |
751
|
|
|
_ = req |
752
|
|
|
resp.status = falcon.HTTP_200 |
753
|
|
|
_ = id_ |
754
|
|
|
|
755
|
|
|
@staticmethod |
756
|
|
|
def on_get(req, resp, id_): |
757
|
|
|
access_control(req) |
758
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
759
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
760
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
761
|
|
|
|
762
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
763
|
|
|
cursor = cnx.cursor() |
764
|
|
|
|
765
|
|
|
cursor.execute(" SELECT name " |
766
|
|
|
" FROM tbl_energy_storage_power_stations " |
767
|
|
|
" WHERE id = %s ", (id_,)) |
768
|
|
|
if cursor.fetchone() is None: |
769
|
|
|
cursor.close() |
770
|
|
|
cnx.close() |
771
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
772
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
773
|
|
|
|
774
|
|
|
query = (" SELECT s.id, s.name, s.uuid " |
775
|
|
|
" FROM tbl_energy_storage_power_stations e, " |
776
|
|
|
" tbl_energy_storage_power_stations_containers es, tbl_energy_storage_containers s " |
777
|
|
|
" WHERE es.energy_storage_power_station_id = e.id " |
778
|
|
|
" AND s.id = es.energy_storage_container_id " |
779
|
|
|
" AND e.id = %s " |
780
|
|
|
" ORDER BY s.id ") |
781
|
|
|
cursor.execute(query, (id_,)) |
782
|
|
|
rows = cursor.fetchall() |
783
|
|
|
|
784
|
|
|
result = list() |
785
|
|
|
if rows is not None and len(rows) > 0: |
786
|
|
|
for row in rows: |
787
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
788
|
|
|
result.append(meta_result) |
789
|
|
|
|
790
|
|
|
resp.text = json.dumps(result) |
791
|
|
|
|
792
|
|
|
@staticmethod |
793
|
|
|
@user_logger |
794
|
|
|
def on_post(req, resp, id_): |
795
|
|
|
"""Handles POST requests""" |
796
|
|
|
admin_control(req) |
797
|
|
|
try: |
798
|
|
|
raw_json = req.stream.read().decode('utf-8') |
799
|
|
|
except Exception as ex: |
800
|
|
|
print(str(ex)) |
801
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
802
|
|
|
title='API.BAD_REQUEST', |
803
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
804
|
|
|
|
805
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
806
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
807
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
808
|
|
|
|
809
|
|
|
new_values = json.loads(raw_json) |
810
|
|
|
|
811
|
|
|
if 'energy_storage_container_id' not in new_values['data'].keys() or \ |
812
|
|
|
not isinstance(new_values['data']['energy_storage_container_id'], int) or \ |
813
|
|
|
new_values['data']['energy_storage_container_id'] <= 0: |
814
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
815
|
|
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
816
|
|
|
energy_storage_container_id = new_values['data']['energy_storage_container_id'] |
817
|
|
|
|
818
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
819
|
|
|
cursor = cnx.cursor() |
820
|
|
|
|
821
|
|
|
cursor.execute(" SELECT name " |
822
|
|
|
" FROM tbl_energy_storage_power_stations " |
823
|
|
|
" WHERE id = %s ", (id_,)) |
824
|
|
|
if cursor.fetchone() is None: |
825
|
|
|
cursor.close() |
826
|
|
|
cnx.close() |
827
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
828
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
829
|
|
|
|
830
|
|
|
cursor.execute(" SELECT name " |
831
|
|
|
" FROM tbl_energy_storage_containers " |
832
|
|
|
" WHERE id = %s ", (energy_storage_container_id,)) |
833
|
|
|
if cursor.fetchone() is None: |
834
|
|
|
cursor.close() |
835
|
|
|
cnx.close() |
836
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
837
|
|
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
838
|
|
|
|
839
|
|
|
query = (" SELECT id " |
840
|
|
|
" FROM tbl_energy_storage_power_stations_containers " |
841
|
|
|
" WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s") |
842
|
|
|
cursor.execute(query, (id_, energy_storage_container_id,)) |
843
|
|
|
if cursor.fetchone() is not None: |
844
|
|
|
cursor.close() |
845
|
|
|
cnx.close() |
846
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
847
|
|
|
description='API.ENERGY_STORAGE_CONTAINER_SENSOR_RELATION_EXISTS') |
848
|
|
|
|
849
|
|
|
add_row = (" INSERT INTO tbl_energy_storage_power_stations_containers " |
850
|
|
|
" (energy_storage_power_station_id, energy_storage_container_id) " |
851
|
|
|
" VALUES (%s, %s) ") |
852
|
|
|
cursor.execute(add_row, (id_, energy_storage_container_id,)) |
853
|
|
|
cnx.commit() |
854
|
|
|
cursor.close() |
855
|
|
|
cnx.close() |
856
|
|
|
|
857
|
|
|
resp.status = falcon.HTTP_201 |
858
|
|
|
resp.location = '/energystoragepowerstationss/' + str(id_) + '/containers/' + str(energy_storage_container_id) |
859
|
|
|
|
860
|
|
|
|
861
|
|
View Code Duplication |
class EnergyStoragePowerStationContainerItem: |
|
|
|
|
862
|
|
|
def __init__(self): |
863
|
|
|
"""Initializes Class""" |
864
|
|
|
pass |
865
|
|
|
|
866
|
|
|
@staticmethod |
867
|
|
|
def on_options(req, resp, id_, sid): |
868
|
|
|
_ = req |
869
|
|
|
resp.status = falcon.HTTP_200 |
870
|
|
|
_ = id_ |
871
|
|
|
|
872
|
|
|
@staticmethod |
873
|
|
|
@user_logger |
874
|
|
|
def on_delete(req, resp, id_, sid): |
875
|
|
|
admin_control(req) |
876
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
877
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
878
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
879
|
|
|
|
880
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
881
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
882
|
|
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
883
|
|
|
|
884
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
885
|
|
|
cursor = cnx.cursor() |
886
|
|
|
|
887
|
|
|
cursor.execute(" SELECT name " |
888
|
|
|
" FROM tbl_energy_storage_power_stations " |
889
|
|
|
" WHERE id = %s ", (id_,)) |
890
|
|
|
if cursor.fetchone() is None: |
891
|
|
|
cursor.close() |
892
|
|
|
cnx.close() |
893
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
894
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
895
|
|
|
|
896
|
|
|
cursor.execute(" SELECT name " |
897
|
|
|
" FROM tbl_energy_storage_containers " |
898
|
|
|
" WHERE id = %s ", (sid,)) |
899
|
|
|
if cursor.fetchone() is None: |
900
|
|
|
cursor.close() |
901
|
|
|
cnx.close() |
902
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
903
|
|
|
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
904
|
|
|
|
905
|
|
|
cursor.execute(" SELECT id " |
906
|
|
|
" FROM tbl_energy_storage_power_stations_containers " |
907
|
|
|
" WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
908
|
|
|
if cursor.fetchone() is None: |
909
|
|
|
cursor.close() |
910
|
|
|
cnx.close() |
911
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
912
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_CONTAINER_RELATION_NOT_FOUND') |
913
|
|
|
|
914
|
|
|
cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
915
|
|
|
" WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
916
|
|
|
cnx.commit() |
917
|
|
|
|
918
|
|
|
cursor.close() |
919
|
|
|
cnx.close() |
920
|
|
|
|
921
|
|
|
resp.status = falcon.HTTP_204 |
922
|
|
|
|
923
|
|
|
|
924
|
|
View Code Duplication |
class EnergyStoragePowerStationDataSourcePointCollection: |
|
|
|
|
925
|
|
|
def __init__(self): |
926
|
|
|
"""Initializes""" |
927
|
|
|
pass |
928
|
|
|
|
929
|
|
|
@staticmethod |
930
|
|
|
def on_options(req, resp, id_,): |
931
|
|
|
_ = req |
932
|
|
|
resp.status = falcon.HTTP_200 |
933
|
|
|
_ = id_ |
934
|
|
|
|
935
|
|
|
@staticmethod |
936
|
|
|
def on_get(req, resp, id_,): |
937
|
|
|
if 'API-KEY' not in req.headers or \ |
938
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
939
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
940
|
|
|
access_control(req) |
941
|
|
|
else: |
942
|
|
|
api_key_control(req) |
943
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
944
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
945
|
|
|
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
946
|
|
|
|
947
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
948
|
|
|
cursor = cnx.cursor() |
949
|
|
|
|
950
|
|
|
query = (" SELECT p.id, p.name " |
951
|
|
|
" FROM tbl_points p, tbl_energy_storage_power_stations_containers espsc, " |
952
|
|
|
" tbl_energy_storage_containers_data_sources ecds, tbl_data_sources ds " |
953
|
|
|
" WHERE espsc.energy_storage_power_station_id = %s " |
954
|
|
|
" AND espsc.energy_storage_container_id = ecds.energy_storage_container_id " |
955
|
|
|
" AND ecds.data_source_id = ds.id " |
956
|
|
|
" AND p.data_source_id = ds.id " |
957
|
|
|
" ORDER BY p.id ") |
958
|
|
|
cursor.execute(query, (id_,)) |
959
|
|
|
rows = cursor.fetchall() |
960
|
|
|
|
961
|
|
|
result = list() |
962
|
|
|
if rows is not None and len(rows) > 0: |
963
|
|
|
for row in rows: |
964
|
|
|
meta_result = {"id": row[0], "name": row[1]} |
965
|
|
|
result.append(meta_result) |
966
|
|
|
|
967
|
|
|
resp.text = json.dumps(result) |
968
|
|
|
|
969
|
|
|
|
970
|
|
View Code Duplication |
class EnergyStoragePowerStationUserCollection: |
|
|
|
|
971
|
|
|
def __init__(self): |
972
|
|
|
"""Initializes Class""" |
973
|
|
|
pass |
974
|
|
|
|
975
|
|
|
@staticmethod |
976
|
|
|
def on_options(req, resp, id_): |
977
|
|
|
_ = req |
978
|
|
|
resp.status = falcon.HTTP_200 |
979
|
|
|
_ = id_ |
980
|
|
|
|
981
|
|
|
@staticmethod |
982
|
|
|
def on_get(req, resp, id_): |
983
|
|
|
access_control(req) |
984
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
985
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
986
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
987
|
|
|
|
988
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
989
|
|
|
cursor = cnx.cursor() |
990
|
|
|
cursor.execute(" SELECT name " |
991
|
|
|
" FROM tbl_energy_storage_power_stations " |
992
|
|
|
" WHERE id = %s ", (id_,)) |
993
|
|
|
if cursor.fetchone() is None: |
994
|
|
|
cursor.close() |
995
|
|
|
cnx.close() |
996
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
997
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
998
|
|
|
|
999
|
|
|
query = (" SELECT u.id, u.name, u.uuid " |
1000
|
|
|
" FROM tbl_energy_storage_power_stations m, tbl_energy_storage_power_stations_users mu, " |
1001
|
|
|
+ config.myems_user_db['database'] + ".tbl_users u " |
1002
|
|
|
" WHERE mu.energy_storage_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
1003
|
|
|
" ORDER BY u.id ") |
1004
|
|
|
cursor.execute(query, (id_,)) |
1005
|
|
|
rows = cursor.fetchall() |
1006
|
|
|
result = list() |
1007
|
|
|
if rows is not None and len(rows) > 0: |
1008
|
|
|
for row in rows: |
1009
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
1010
|
|
|
result.append(meta_result) |
1011
|
|
|
|
1012
|
|
|
cursor.close() |
1013
|
|
|
cnx.close() |
1014
|
|
|
resp.text = json.dumps(result) |
1015
|
|
|
|
1016
|
|
|
@staticmethod |
1017
|
|
|
@user_logger |
1018
|
|
|
def on_post(req, resp, id_): |
1019
|
|
|
"""Handles POST requests""" |
1020
|
|
|
admin_control(req) |
1021
|
|
|
try: |
1022
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1023
|
|
|
except Exception as ex: |
1024
|
|
|
print(str(ex)) |
1025
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1026
|
|
|
title='API.BAD_REQUEST', |
1027
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1028
|
|
|
|
1029
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1030
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1031
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
1032
|
|
|
|
1033
|
|
|
new_values = json.loads(raw_json) |
1034
|
|
|
if 'user_id' not in new_values['data'].keys() or \ |
1035
|
|
|
not isinstance(new_values['data']['user_id'], int) or \ |
1036
|
|
|
new_values['data']['user_id'] <= 0: |
1037
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1038
|
|
|
description='API.INVALID_USER_ID') |
1039
|
|
|
user_id = new_values['data']['user_id'] |
1040
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1041
|
|
|
cursor = cnx.cursor() |
1042
|
|
|
cursor.execute(" SELECT name " |
1043
|
|
|
" FROM tbl_energy_storage_power_stations " |
1044
|
|
|
" WHERE id = %s ", (id_,)) |
1045
|
|
|
if cursor.fetchone() is None: |
1046
|
|
|
cursor.close() |
1047
|
|
|
cnx.close() |
1048
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1049
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
1050
|
|
|
|
1051
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
1052
|
|
|
cursor_user = cnx_user.cursor() |
1053
|
|
|
cursor_user.execute(" SELECT name" |
1054
|
|
|
" FROM tbl_users " |
1055
|
|
|
" WHERE id = %s ", (user_id,)) |
1056
|
|
|
if cursor_user.fetchone() is None: |
1057
|
|
|
cursor_user.close() |
1058
|
|
|
cnx_user.close() |
1059
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1060
|
|
|
description='API.USER_NOT_FOUND') |
1061
|
|
|
query = (" SELECT id " |
1062
|
|
|
" FROM tbl_energy_storage_power_stations_users " |
1063
|
|
|
" WHERE energy_storage_power_station_id = %s AND user_id = %s") |
1064
|
|
|
cursor.execute(query, (id_, user_id,)) |
1065
|
|
|
if cursor.fetchone() is not None: |
1066
|
|
|
cursor.close() |
1067
|
|
|
cnx.close() |
1068
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1069
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_EXISTS') |
1070
|
|
|
add_row = (" INSERT INTO tbl_energy_storage_power_stations_users (energy_storage_power_station_id, user_id) " |
1071
|
|
|
" VALUES (%s, %s) ") |
1072
|
|
|
cursor.execute(add_row, (id_, user_id,)) |
1073
|
|
|
cnx.commit() |
1074
|
|
|
cursor.close() |
1075
|
|
|
cnx.close() |
1076
|
|
|
cursor_user.close() |
1077
|
|
|
cnx_user.close() |
1078
|
|
|
|
1079
|
|
|
resp.status = falcon.HTTP_201 |
1080
|
|
|
resp.location = '/energystoragepowerstations/' + str(id_) + '/users/' + str(user_id) |
1081
|
|
|
|
1082
|
|
|
|
1083
|
|
View Code Duplication |
class EnergyStoragePowerStationUserItem: |
|
|
|
|
1084
|
|
|
def __init__(self): |
1085
|
|
|
"""Initializes Class""" |
1086
|
|
|
pass |
1087
|
|
|
|
1088
|
|
|
@staticmethod |
1089
|
|
|
def on_options(req, resp, id_, uid): |
1090
|
|
|
_ = req |
1091
|
|
|
resp.status = falcon.HTTP_200 |
1092
|
|
|
_ = id_ |
1093
|
|
|
|
1094
|
|
|
@staticmethod |
1095
|
|
|
@user_logger |
1096
|
|
|
def on_delete(req, resp, id_, uid): |
1097
|
|
|
# todo Verify if the user is bound when deleting it |
1098
|
|
|
admin_control(req) |
1099
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1100
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1101
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
1102
|
|
|
|
1103
|
|
|
if not uid.isdigit() or int(uid) <= 0: |
1104
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1105
|
|
|
description='API.INVALID_USER_ID') |
1106
|
|
|
|
1107
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1108
|
|
|
cursor = cnx.cursor() |
1109
|
|
|
cursor.execute(" SELECT name " |
1110
|
|
|
" FROM tbl_energy_storage_power_stations " |
1111
|
|
|
" WHERE id = %s ", (id_,)) |
1112
|
|
|
if cursor.fetchone() is None: |
1113
|
|
|
cursor.close() |
1114
|
|
|
cnx.close() |
1115
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1116
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
1117
|
|
|
|
1118
|
|
|
cnx_user = mysql.connector.connect(**config.myems_user_db) |
1119
|
|
|
cursor_user = cnx_user.cursor() |
1120
|
|
|
cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,)) |
1121
|
|
|
if cursor_user.fetchone() is None: |
1122
|
|
|
cursor_user.close() |
1123
|
|
|
cnx_user.close() |
1124
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1125
|
|
|
description='API.USER_NOT_FOUND') |
1126
|
|
|
|
1127
|
|
|
cursor.execute(" SELECT id " |
1128
|
|
|
" FROM tbl_energy_storage_power_stations_users " |
1129
|
|
|
" WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
1130
|
|
|
if cursor.fetchone() is None: |
1131
|
|
|
cursor.close() |
1132
|
|
|
cnx.close() |
1133
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1134
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_NOT_FOUND') |
1135
|
|
|
|
1136
|
|
|
cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_users " |
1137
|
|
|
" WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
1138
|
|
|
cnx.commit() |
1139
|
|
|
|
1140
|
|
|
cursor.close() |
1141
|
|
|
cnx.close() |
1142
|
|
|
cursor_user.close() |
1143
|
|
|
cnx_user.close() |
1144
|
|
|
|
1145
|
|
|
resp.status = falcon.HTTP_204 |
1146
|
|
|
|
1147
|
|
|
|
1148
|
|
|
class EnergyStoragePowerStationExport: |
1149
|
|
|
def __init__(self): |
1150
|
|
|
""""Initializes Class""" |
1151
|
|
|
pass |
1152
|
|
|
|
1153
|
|
|
@staticmethod |
1154
|
|
|
def on_options(req, resp, id_): |
1155
|
|
|
_ = req |
1156
|
|
|
resp.status = falcon.HTTP_200 |
1157
|
|
|
_ = id_ |
1158
|
|
|
|
1159
|
|
|
@staticmethod |
1160
|
|
|
def on_get(req, resp, id_): |
1161
|
|
|
access_control(req) |
1162
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1163
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1164
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
1165
|
|
|
|
1166
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1167
|
|
|
cursor = cnx.cursor() |
1168
|
|
|
|
1169
|
|
|
query = (" SELECT id, name, uuid " |
1170
|
|
|
" FROM tbl_contacts ") |
1171
|
|
|
cursor.execute(query) |
1172
|
|
|
rows_contacts = cursor.fetchall() |
1173
|
|
|
|
1174
|
|
|
contact_dict = dict() |
1175
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
1176
|
|
|
for row in rows_contacts: |
1177
|
|
|
contact_dict[row[0]] = {"id": row[0], |
1178
|
|
|
"name": row[1], |
1179
|
|
|
"uuid": row[2]} |
1180
|
|
|
|
1181
|
|
|
query = (" SELECT id, name, uuid " |
1182
|
|
|
" FROM tbl_cost_centers ") |
1183
|
|
|
cursor.execute(query) |
1184
|
|
|
rows_cost_centers = cursor.fetchall() |
1185
|
|
|
|
1186
|
|
|
cost_center_dict = dict() |
1187
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
1188
|
|
|
for row in rows_cost_centers: |
1189
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
1190
|
|
|
"name": row[1], |
1191
|
|
|
"uuid": row[2]} |
1192
|
|
|
|
1193
|
|
|
query = (" SELECT id, name, uuid " |
1194
|
|
|
" FROM tbl_svgs ") |
1195
|
|
|
cursor.execute(query) |
1196
|
|
|
rows_svgs = cursor.fetchall() |
1197
|
|
|
|
1198
|
|
|
svg_dict = dict() |
1199
|
|
|
if rows_svgs is not None and len(rows_svgs) > 0: |
1200
|
|
|
for row in rows_svgs: |
1201
|
|
|
svg_dict[row[0]] = {"id": row[0], |
1202
|
|
|
"name": row[1], |
1203
|
|
|
"uuid": row[2]} |
1204
|
|
|
|
1205
|
|
|
query = (" SELECT id, name, uuid, " |
1206
|
|
|
" address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
1207
|
|
|
" rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
1208
|
|
|
" is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
1209
|
|
|
" FROM tbl_energy_storage_power_stations " |
1210
|
|
|
" WHERE id = %s ") |
1211
|
|
|
cursor.execute(query, (id_,)) |
1212
|
|
|
row = cursor.fetchone() |
1213
|
|
|
|
1214
|
|
|
if row is None: |
1215
|
|
|
cursor.close() |
1216
|
|
|
cnx.close() |
1217
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1218
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
1219
|
|
|
else: |
1220
|
|
|
meta_result = {"id": row[0], |
1221
|
|
|
"name": row[1], |
1222
|
|
|
"uuid": row[2], |
1223
|
|
|
"address": row[3], |
1224
|
|
|
"latitude": row[4], |
1225
|
|
|
"longitude": row[5], |
1226
|
|
|
"latitude_point_id": row[6], |
1227
|
|
|
"longitude_point_id": row[7], |
1228
|
|
|
"rated_capacity": row[8], |
1229
|
|
|
"rated_power": row[9], |
1230
|
|
|
"contact": contact_dict.get(row[10], None), |
1231
|
|
|
"cost_center": cost_center_dict.get(row[11], None), |
1232
|
|
|
"svg": svg_dict.get(row[12], None), |
1233
|
|
|
"svg2": svg_dict.get(row[13], None), |
1234
|
|
|
"svg3": svg_dict.get(row[14], None), |
1235
|
|
|
"svg4": svg_dict.get(row[15], None), |
1236
|
|
|
"svg5": svg_dict.get(row[16], None), |
1237
|
|
|
"is_cost_data_displayed": bool(row[17]), |
1238
|
|
|
"phase_of_lifecycle": row[18], |
1239
|
|
|
"commissioning_date": row[19].isoformat() if row[19] else None, |
1240
|
|
|
"description": row[20]} |
1241
|
|
|
|
1242
|
|
|
resp.text = json.dumps(meta_result) |
1243
|
|
|
|
1244
|
|
|
|
1245
|
|
|
class EnergyStoragePowerStationImport: |
1246
|
|
|
def __init__(self): |
1247
|
|
|
""""Initializes Class""" |
1248
|
|
|
pass |
1249
|
|
|
|
1250
|
|
|
@staticmethod |
1251
|
|
|
def on_options(req, resp): |
1252
|
|
|
_ = req |
1253
|
|
|
resp.status = falcon.HTTP_200 |
1254
|
|
|
|
1255
|
|
|
@staticmethod |
1256
|
|
|
@user_logger |
1257
|
|
|
def on_post(req, resp): |
1258
|
|
|
"""Handles POST requests""" |
1259
|
|
|
admin_control(req) |
1260
|
|
|
try: |
1261
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1262
|
|
|
except Exception as ex: |
1263
|
|
|
print(str(ex)) |
1264
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1265
|
|
|
title='API.BAD_REQUEST', |
1266
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1267
|
|
|
|
1268
|
|
|
new_values = json.loads(raw_json) |
1269
|
|
|
|
1270
|
|
|
print(new_values) |
1271
|
|
|
if 'name' not in new_values.keys() or \ |
1272
|
|
|
not isinstance(new_values['name'], str) or \ |
1273
|
|
|
len(str.strip(new_values['name'])) == 0: |
1274
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1275
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
1276
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
1277
|
|
|
if config.utc_offset[0] == '-': |
1278
|
|
|
timezone_offset = -timezone_offset |
1279
|
|
|
name = str.strip(new_values['name']) + \ |
1280
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
1281
|
|
|
|
1282
|
|
|
if 'address' not in new_values.keys() or \ |
1283
|
|
|
not isinstance(new_values['address'], str) or \ |
1284
|
|
|
len(str.strip(new_values['address'])) == 0: |
1285
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1286
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
1287
|
|
|
address = str.strip(new_values['address']) |
1288
|
|
|
|
1289
|
|
|
if 'latitude' not in new_values.keys() or \ |
1290
|
|
|
not (isinstance(new_values['latitude'], float) or |
1291
|
|
|
isinstance(new_values['latitude'], int)) or \ |
1292
|
|
|
new_values['latitude'] < -90.0 or \ |
1293
|
|
|
new_values['latitude'] > 90.0: |
1294
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1295
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
1296
|
|
|
latitude = new_values['latitude'] |
1297
|
|
|
|
1298
|
|
|
if 'longitude' not in new_values.keys() or \ |
1299
|
|
|
not (isinstance(new_values['longitude'], float) or |
1300
|
|
|
isinstance(new_values['longitude'], int)) or \ |
1301
|
|
|
new_values['longitude'] < -180.0 or \ |
1302
|
|
|
new_values['longitude'] > 180.0: |
1303
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1304
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
1305
|
|
|
longitude = new_values['longitude'] |
1306
|
|
|
|
1307
|
|
|
latitude_point_id = None |
1308
|
|
|
longitude_point_id = None |
1309
|
|
|
|
1310
|
|
|
if 'rated_capacity' not in new_values.keys() or \ |
1311
|
|
|
not (isinstance(new_values['rated_capacity'], float) or |
1312
|
|
|
isinstance(new_values['rated_capacity'], int)) or \ |
1313
|
|
|
new_values['rated_capacity'] < 0.0: |
1314
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1315
|
|
|
description='API.INVALID_RATED_CAPACITY') |
1316
|
|
|
rated_capacity = new_values['rated_capacity'] |
1317
|
|
|
|
1318
|
|
|
if 'rated_power' not in new_values.keys() or \ |
1319
|
|
|
not (isinstance(new_values['rated_power'], float) or |
1320
|
|
|
isinstance(new_values['rated_power'], int)) or \ |
1321
|
|
|
new_values['rated_power'] < 0.0: |
1322
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1323
|
|
|
description='API.INVALID_RATED_POWER') |
1324
|
|
|
rated_power = new_values['rated_power'] |
1325
|
|
|
|
1326
|
|
|
if 'contact' not in new_values.keys() or \ |
1327
|
|
|
'id' not in new_values['contact'].keys() or \ |
1328
|
|
|
not isinstance(new_values['contact']['id'], int) or \ |
1329
|
|
|
new_values['contact']['id'] <= 0: |
1330
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1331
|
|
|
description='API.INVALID_CONTACT_ID') |
1332
|
|
|
contact_id = new_values['contact']['id'] |
1333
|
|
|
|
1334
|
|
|
if 'cost_center' not in new_values.keys() or \ |
1335
|
|
|
'id' not in new_values['cost_center'].keys() or \ |
1336
|
|
|
not isinstance(new_values['cost_center']['id'], int) or \ |
1337
|
|
|
new_values['cost_center']['id'] <= 0: |
1338
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1339
|
|
|
description='API.INVALID_COST_CENTER_ID') |
1340
|
|
|
cost_center_id = new_values['cost_center']['id'] |
1341
|
|
|
|
1342
|
|
|
if 'svg' not in new_values.keys() or \ |
1343
|
|
|
'id' not in new_values['svg'].keys() or \ |
1344
|
|
|
not isinstance(new_values['svg']['id'], int) or \ |
1345
|
|
|
new_values['svg']['id'] <= 0: |
1346
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1347
|
|
|
description='API.INVALID_SVG_ID') |
1348
|
|
|
svg_id = new_values['svg']['id'] |
1349
|
|
|
|
1350
|
|
View Code Duplication |
if 'svg2' in new_values.keys() and \ |
|
|
|
|
1351
|
|
|
new_values['svg2'] is not None and \ |
1352
|
|
|
'id' in new_values['svg2'].keys() and \ |
1353
|
|
|
new_values['svg2']['id'] is not None: |
1354
|
|
|
if not isinstance(new_values['svg2']['id'], int) or \ |
1355
|
|
|
new_values['svg2']['id'] <= 0: |
1356
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1357
|
|
|
description='API.INVALID_SVG2_ID') |
1358
|
|
|
svg2_id = new_values['svg2']['id'] |
1359
|
|
|
else: |
1360
|
|
|
svg2_id = None |
1361
|
|
|
|
1362
|
|
View Code Duplication |
if 'svg3' in new_values.keys() and \ |
|
|
|
|
1363
|
|
|
new_values['svg3'] is not None and \ |
1364
|
|
|
'id' in new_values['svg3'].keys() and \ |
1365
|
|
|
new_values['svg3']['id'] is not None: |
1366
|
|
|
if not isinstance(new_values['svg3']['id'], int) or \ |
1367
|
|
|
new_values['svg3']['id'] <= 0: |
1368
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1369
|
|
|
description='API.INVALID_SVG3_ID') |
1370
|
|
|
svg3_id = new_values['svg3']['id'] |
1371
|
|
|
else: |
1372
|
|
|
svg3_id = None |
1373
|
|
|
|
1374
|
|
View Code Duplication |
if 'svg4' in new_values.keys() and \ |
|
|
|
|
1375
|
|
|
new_values['svg4'] is not None and \ |
1376
|
|
|
'id' in new_values['svg4'].keys() and \ |
1377
|
|
|
new_values['svg4']['id'] is not None: |
1378
|
|
|
if not isinstance(new_values['svg4']['id'], int) or \ |
1379
|
|
|
new_values['svg4']['id'] <= 0: |
1380
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1381
|
|
|
description='API.INVALID_SVG4_ID') |
1382
|
|
|
svg4_id = new_values['svg4']['id'] |
1383
|
|
|
else: |
1384
|
|
|
svg4_id = None |
1385
|
|
|
|
1386
|
|
View Code Duplication |
if 'svg5' in new_values.keys() and \ |
|
|
|
|
1387
|
|
|
new_values['svg5'] is not None and \ |
1388
|
|
|
'id' in new_values['svg5'].keys() and \ |
1389
|
|
|
new_values['svg5']['id'] is not None: |
1390
|
|
|
if not isinstance(new_values['svg5']['id'], int) or \ |
1391
|
|
|
new_values['svg5']['id'] <= 0: |
1392
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1393
|
|
|
description='API.INVALID_SVG5_ID') |
1394
|
|
|
svg5_id = new_values['svg5']['id'] |
1395
|
|
|
else: |
1396
|
|
|
svg5_id = None |
1397
|
|
|
|
1398
|
|
|
if 'is_cost_data_displayed' not in new_values.keys() or \ |
1399
|
|
|
not isinstance(new_values['is_cost_data_displayed'], bool): |
1400
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1401
|
|
|
description='API.INVALID_IS_COST_DATA_DISPLAYED') |
1402
|
|
|
is_cost_data_displayed = new_values['is_cost_data_displayed'] |
1403
|
|
|
|
1404
|
|
|
if 'phase_of_lifecycle' not in new_values.keys() or \ |
1405
|
|
|
not isinstance(new_values['phase_of_lifecycle'], str) or \ |
1406
|
|
|
len(str.strip(new_values['phase_of_lifecycle'])) == 0: |
1407
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1408
|
|
|
description='API.INVALID_PHASE_OF_LIFECYCLE') |
1409
|
|
|
phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle']) |
1410
|
|
|
|
1411
|
|
|
if 'commissioning_date' not in new_values.keys() or \ |
1412
|
|
|
not isinstance(new_values['commissioning_date'], str) or \ |
1413
|
|
|
len(str.strip(new_values['commissioning_date'])) == 0: |
1414
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1415
|
|
|
description='API.INVALID_COMMISSIONING_DATE') |
1416
|
|
|
commissioning_date = datetime.strptime(new_values['commissioning_date'], '%Y-%m-%d') |
1417
|
|
|
|
1418
|
|
|
if 'description' in new_values.keys() and \ |
1419
|
|
|
new_values['description'] is not None and \ |
1420
|
|
|
len(str(new_values['description'])) > 0: |
1421
|
|
|
description = str.strip(new_values['description']) |
1422
|
|
|
else: |
1423
|
|
|
description = None |
1424
|
|
|
|
1425
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1426
|
|
|
cursor = cnx.cursor() |
1427
|
|
|
|
1428
|
|
|
cursor.execute(" SELECT name " |
1429
|
|
|
" FROM tbl_energy_storage_power_stations " |
1430
|
|
|
" WHERE name = %s ", (name,)) |
1431
|
|
|
if cursor.fetchone() is not None: |
1432
|
|
|
cursor.close() |
1433
|
|
|
cnx.close() |
1434
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1435
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
1436
|
|
|
|
1437
|
|
|
cursor.execute(" SELECT name " |
1438
|
|
|
" FROM tbl_contacts " |
1439
|
|
|
" WHERE id = %s ", |
1440
|
|
|
(contact_id,)) |
1441
|
|
|
row = cursor.fetchone() |
1442
|
|
|
if row is None: |
1443
|
|
|
cursor.close() |
1444
|
|
|
cnx.close() |
1445
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1446
|
|
|
description='API.CONTACT_NOT_FOUND') |
1447
|
|
|
|
1448
|
|
|
cursor.execute(" SELECT name " |
1449
|
|
|
" FROM tbl_cost_centers " |
1450
|
|
|
" WHERE id = %s ", |
1451
|
|
|
(cost_center_id,)) |
1452
|
|
|
row = cursor.fetchone() |
1453
|
|
|
if row is None: |
1454
|
|
|
cursor.close() |
1455
|
|
|
cnx.close() |
1456
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1457
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
1458
|
|
|
|
1459
|
|
|
cursor.execute(" SELECT name " |
1460
|
|
|
" FROM tbl_svgs " |
1461
|
|
|
" WHERE id = %s ", |
1462
|
|
|
(svg_id,)) |
1463
|
|
|
row = cursor.fetchone() |
1464
|
|
|
if row is None: |
1465
|
|
|
cursor.close() |
1466
|
|
|
cnx.close() |
1467
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1468
|
|
|
description='API.SVG_NOT_FOUND') |
1469
|
|
|
|
1470
|
|
|
add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
1471
|
|
|
" (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
1472
|
|
|
" rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
1473
|
|
|
" svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
1474
|
|
|
" description) " |
1475
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
1476
|
|
|
|
1477
|
|
|
cursor.execute(add_values, (name, |
1478
|
|
|
str(uuid.uuid4()), |
1479
|
|
|
address, |
1480
|
|
|
latitude, |
1481
|
|
|
longitude, |
1482
|
|
|
latitude_point_id, |
1483
|
|
|
longitude_point_id, |
1484
|
|
|
rated_capacity, |
1485
|
|
|
rated_power, |
1486
|
|
|
contact_id, |
1487
|
|
|
cost_center_id, |
1488
|
|
|
svg_id, |
1489
|
|
|
svg2_id, |
1490
|
|
|
svg3_id, |
1491
|
|
|
svg4_id, |
1492
|
|
|
svg5_id, |
1493
|
|
|
is_cost_data_displayed, |
1494
|
|
|
phase_of_lifecycle, |
1495
|
|
|
commissioning_date, |
1496
|
|
|
description)) |
1497
|
|
|
|
1498
|
|
|
new_id = cursor.lastrowid |
1499
|
|
|
cnx.commit() |
1500
|
|
|
cursor.close() |
1501
|
|
|
cnx.close() |
1502
|
|
|
|
1503
|
|
|
resp.status = falcon.HTTP_201 |
1504
|
|
|
resp.location = '/energystoragepowerstations/' + str(new_id) |
1505
|
|
|
|
1506
|
|
|
|
1507
|
|
|
class EnergyStoragePowerStationClone: |
1508
|
|
|
def __init__(self): |
1509
|
|
|
"""Initializes Class""" |
1510
|
|
|
pass |
1511
|
|
|
|
1512
|
|
|
@staticmethod |
1513
|
|
|
def on_options(req, resp, id_): |
1514
|
|
|
_ = req |
1515
|
|
|
resp.status = falcon.HTTP_200 |
1516
|
|
|
_ = id_ |
1517
|
|
|
|
1518
|
|
|
@staticmethod |
1519
|
|
|
@user_logger |
1520
|
|
|
def on_post(req, resp, id_): |
1521
|
|
|
"""Handles POST requests""" |
1522
|
|
|
admin_control(req) |
1523
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1524
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1525
|
|
|
description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
1526
|
|
|
|
1527
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1528
|
|
|
cursor = cnx.cursor() |
1529
|
|
|
|
1530
|
|
|
query = (" SELECT id, name, uuid, " |
1531
|
|
|
" address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
1532
|
|
|
" rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
1533
|
|
|
" is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
1534
|
|
|
" FROM tbl_energy_storage_power_stations " |
1535
|
|
|
" WHERE id = %s ") |
1536
|
|
|
cursor.execute(query, (id_,)) |
1537
|
|
|
row = cursor.fetchone() |
1538
|
|
|
|
1539
|
|
|
if row is None: |
1540
|
|
|
cursor.close() |
1541
|
|
|
cnx.close() |
1542
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1543
|
|
|
description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
1544
|
|
|
else: |
1545
|
|
|
meta_result = {"name": row[1], |
1546
|
|
|
"uuid": row[2], |
1547
|
|
|
"address": row[3], |
1548
|
|
|
"latitude": row[4], |
1549
|
|
|
"longitude": row[5], |
1550
|
|
|
"latitude_point_id": row[6], |
1551
|
|
|
"longitude_point_id": row[7], |
1552
|
|
|
"rated_capacity": row[8], |
1553
|
|
|
"rated_power": row[9], |
1554
|
|
|
"contact_id": row[10], |
1555
|
|
|
"cost_center_id": row[11], |
1556
|
|
|
"svg_id": row[12], |
1557
|
|
|
"svg2_id": row[13], |
1558
|
|
|
"svg3_id": row[14], |
1559
|
|
|
"svg4_id": row[15], |
1560
|
|
|
"svg5_id": row[16], |
1561
|
|
|
"is_cost_data_displayed": row[17], |
1562
|
|
|
"phase_of_lifecycle": row[18], |
1563
|
|
|
"commissioning_date": row[19], |
1564
|
|
|
"description": row[20]} |
1565
|
|
|
|
1566
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
1567
|
|
|
if config.utc_offset[0] == '-': |
1568
|
|
|
timezone_offset = -timezone_offset |
1569
|
|
|
new_name = str.strip(meta_result['name']) + \ |
1570
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
1571
|
|
|
|
1572
|
|
|
add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
1573
|
|
|
" (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
1574
|
|
|
" rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
1575
|
|
|
" svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
1576
|
|
|
" description) " |
1577
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
1578
|
|
|
cursor.execute(add_values, (new_name, |
1579
|
|
|
str(uuid.uuid4()), |
1580
|
|
|
meta_result['address'], |
1581
|
|
|
meta_result['latitude'], |
1582
|
|
|
meta_result['longitude'], |
1583
|
|
|
meta_result['latitude_point_id'], |
1584
|
|
|
meta_result['longitude_point_id'], |
1585
|
|
|
meta_result['rated_capacity'], |
1586
|
|
|
meta_result['rated_power'], |
1587
|
|
|
meta_result['contact_id'], |
1588
|
|
|
meta_result['cost_center_id'], |
1589
|
|
|
meta_result['svg_id'], |
1590
|
|
|
meta_result['svg2_id'], |
1591
|
|
|
meta_result['svg3_id'], |
1592
|
|
|
meta_result['svg4_id'], |
1593
|
|
|
meta_result['svg5_id'], |
1594
|
|
|
meta_result['is_cost_data_displayed'], |
1595
|
|
|
meta_result['phase_of_lifecycle'], |
1596
|
|
|
meta_result['commissioning_date'], |
1597
|
|
|
meta_result['description'])) |
1598
|
|
|
new_id = cursor.lastrowid |
1599
|
|
|
cnx.commit() |
1600
|
|
|
cursor.close() |
1601
|
|
|
cnx.close() |
1602
|
|
|
|
1603
|
|
|
resp.status = falcon.HTTP_201 |
1604
|
|
|
resp.location = '/energystoragepowerstations/' + str(new_id) |
1605
|
|
|
|