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 StoreCollection: |
11
|
|
|
def __init__(self): |
12
|
|
|
""""Initializes StoreCollection""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
|
19
|
|
|
@staticmethod |
20
|
|
|
def on_get(req, resp): |
21
|
|
|
if 'API-KEY' not in req.headers or \ |
22
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
23
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
24
|
|
|
access_control(req) |
25
|
|
|
else: |
26
|
|
|
api_key_control(req) |
27
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
28
|
|
|
cursor = cnx.cursor() |
29
|
|
|
|
30
|
|
|
query = (" SELECT id, name, uuid " |
31
|
|
|
" FROM tbl_store_types ") |
32
|
|
|
cursor.execute(query) |
33
|
|
|
rows_store_types = cursor.fetchall() |
34
|
|
|
|
35
|
|
|
store_type_dict = dict() |
36
|
|
|
if rows_store_types is not None and len(rows_store_types) > 0: |
37
|
|
|
for row in rows_store_types: |
38
|
|
|
store_type_dict[row[0]] = {"id": row[0], |
39
|
|
|
"name": row[1], |
40
|
|
|
"uuid": row[2]} |
41
|
|
|
|
42
|
|
|
query = (" SELECT id, name, uuid " |
43
|
|
|
" FROM tbl_contacts ") |
44
|
|
|
cursor.execute(query) |
45
|
|
|
rows_contacts = cursor.fetchall() |
46
|
|
|
|
47
|
|
|
contact_dict = dict() |
48
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
49
|
|
|
for row in rows_contacts: |
50
|
|
|
contact_dict[row[0]] = {"id": row[0], |
51
|
|
|
"name": row[1], |
52
|
|
|
"uuid": row[2]} |
53
|
|
|
|
54
|
|
|
query = (" SELECT id, name, uuid " |
55
|
|
|
" FROM tbl_cost_centers ") |
56
|
|
|
cursor.execute(query) |
57
|
|
|
rows_cost_centers = cursor.fetchall() |
58
|
|
|
|
59
|
|
|
cost_center_dict = dict() |
60
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
61
|
|
|
for row in rows_cost_centers: |
62
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
63
|
|
|
"name": row[1], |
64
|
|
|
"uuid": row[2]} |
65
|
|
|
|
66
|
|
|
query = (" SELECT id, name, uuid, " |
67
|
|
|
" address, latitude, longitude, area, store_type_id, " |
68
|
|
|
" is_input_counted, contact_id, cost_center_id, description " |
69
|
|
|
" FROM tbl_stores " |
70
|
|
|
" ORDER BY id ") |
71
|
|
|
cursor.execute(query) |
72
|
|
|
rows_spaces = cursor.fetchall() |
73
|
|
|
|
74
|
|
|
result = list() |
75
|
|
View Code Duplication |
if rows_spaces is not None and len(rows_spaces) > 0: |
|
|
|
|
76
|
|
|
for row in rows_spaces: |
77
|
|
|
|
78
|
|
|
meta_result = {"id": row[0], |
79
|
|
|
"name": row[1], |
80
|
|
|
"uuid": row[2], |
81
|
|
|
"address": row[3], |
82
|
|
|
"latitude": row[4], |
83
|
|
|
"longitude": row[5], |
84
|
|
|
"area": row[6], |
85
|
|
|
"store_type": store_type_dict.get(row[7], None), |
86
|
|
|
"is_input_counted": bool(row[8]), |
87
|
|
|
"contact": contact_dict.get(row[9], None), |
88
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
89
|
|
|
"description": row[11], |
90
|
|
|
"qrcode": 'store:' + row[2]} |
91
|
|
|
result.append(meta_result) |
92
|
|
|
|
93
|
|
|
cursor.close() |
94
|
|
|
cnx.close() |
95
|
|
|
resp.text = json.dumps(result) |
96
|
|
|
|
97
|
|
|
@staticmethod |
98
|
|
|
@user_logger |
99
|
|
|
def on_post(req, resp): |
100
|
|
|
"""Handles POST requests""" |
101
|
|
|
admin_control(req) |
102
|
|
|
try: |
103
|
|
|
raw_json = req.stream.read().decode('utf-8') |
104
|
|
|
except Exception as ex: |
105
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
106
|
|
|
title='API.BAD_REQUEST', |
107
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
108
|
|
|
|
109
|
|
|
new_values = json.loads(raw_json) |
110
|
|
|
|
111
|
|
|
if 'name' not in new_values['data'].keys() or \ |
112
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
113
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
114
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
115
|
|
|
description='API.INVALID_STORE_NAME') |
116
|
|
|
name = str.strip(new_values['data']['name']) |
117
|
|
|
|
118
|
|
|
if 'address' not in new_values['data'].keys() or \ |
119
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
120
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
121
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
122
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
123
|
|
|
address = str.strip(new_values['data']['address']) |
124
|
|
|
|
125
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
126
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
127
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
128
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
129
|
|
|
new_values['data']['latitude'] > 90.0: |
130
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
131
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
132
|
|
|
latitude = new_values['data']['latitude'] |
133
|
|
|
|
134
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
135
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
136
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
137
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
138
|
|
|
new_values['data']['longitude'] > 180.0: |
139
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
140
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
141
|
|
|
longitude = new_values['data']['longitude'] |
142
|
|
|
|
143
|
|
|
if 'area' not in new_values['data'].keys() or \ |
144
|
|
|
not (isinstance(new_values['data']['area'], float) or |
145
|
|
|
isinstance(new_values['data']['area'], int)) or \ |
146
|
|
|
new_values['data']['area'] <= 0.0: |
147
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
148
|
|
|
description='API.INVALID_AREA_VALUE') |
149
|
|
|
area = new_values['data']['area'] |
150
|
|
|
|
151
|
|
|
if 'store_type_id' not in new_values['data'].keys() or \ |
152
|
|
|
not isinstance(new_values['data']['store_type_id'], int) or \ |
153
|
|
|
new_values['data']['store_type_id'] <= 0: |
154
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
155
|
|
|
description='API.INVALID_STORE_TYPE_ID') |
156
|
|
|
store_type_id = new_values['data']['store_type_id'] |
157
|
|
|
|
158
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
159
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
160
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
161
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
162
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
163
|
|
|
|
164
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
165
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
166
|
|
|
new_values['data']['contact_id'] <= 0: |
167
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
168
|
|
|
description='API.INVALID_CONTACT_ID') |
169
|
|
|
contact_id = new_values['data']['contact_id'] |
170
|
|
|
|
171
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
172
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
173
|
|
|
new_values['data']['cost_center_id'] <= 0: |
174
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
175
|
|
|
description='API.INVALID_COST_CENTER_ID') |
176
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
177
|
|
|
|
178
|
|
|
if 'description' in new_values['data'].keys() and \ |
179
|
|
|
new_values['data']['description'] is not None and \ |
180
|
|
|
len(str(new_values['data']['description'])) > 0: |
181
|
|
|
description = str.strip(new_values['data']['description']) |
182
|
|
|
else: |
183
|
|
|
description = None |
184
|
|
|
|
185
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
186
|
|
|
cursor = cnx.cursor() |
187
|
|
|
|
188
|
|
|
cursor.execute(" SELECT name " |
189
|
|
|
" FROM tbl_stores " |
190
|
|
|
" WHERE name = %s ", (name,)) |
191
|
|
|
if cursor.fetchone() is not None: |
192
|
|
|
cursor.close() |
193
|
|
|
cnx.close() |
194
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
195
|
|
|
description='API.STORE_NAME_IS_ALREADY_IN_USE') |
196
|
|
|
|
197
|
|
|
cursor.execute(" SELECT name " |
198
|
|
|
" FROM tbl_store_types " |
199
|
|
|
" WHERE id = %s ", |
200
|
|
|
(store_type_id,)) |
201
|
|
|
if cursor.fetchone() is None: |
202
|
|
|
cursor.close() |
203
|
|
|
cnx.close() |
204
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
205
|
|
|
description='API.STORE_TYPE_NOT_FOUND') |
206
|
|
|
|
207
|
|
|
cursor.execute(" SELECT name " |
208
|
|
|
" FROM tbl_contacts " |
209
|
|
|
" WHERE id = %s ", |
210
|
|
|
(new_values['data']['contact_id'],)) |
211
|
|
|
row = cursor.fetchone() |
212
|
|
|
if row is None: |
213
|
|
|
cursor.close() |
214
|
|
|
cnx.close() |
215
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
216
|
|
|
description='API.CONTACT_NOT_FOUND') |
217
|
|
|
|
218
|
|
|
cursor.execute(" SELECT name " |
219
|
|
|
" FROM tbl_cost_centers " |
220
|
|
|
" WHERE id = %s ", |
221
|
|
|
(new_values['data']['cost_center_id'],)) |
222
|
|
|
row = cursor.fetchone() |
223
|
|
|
if row is None: |
224
|
|
|
cursor.close() |
225
|
|
|
cnx.close() |
226
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
227
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
228
|
|
|
|
229
|
|
|
add_values = (" INSERT INTO tbl_stores " |
230
|
|
|
" (name, uuid, address, latitude, longitude, area, store_type_id, " |
231
|
|
|
" is_input_counted, " |
232
|
|
|
" contact_id, cost_center_id, description) " |
233
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
234
|
|
|
cursor.execute(add_values, (name, |
235
|
|
|
str(uuid.uuid4()), |
236
|
|
|
address, |
237
|
|
|
latitude, |
238
|
|
|
longitude, |
239
|
|
|
area, |
240
|
|
|
store_type_id, |
241
|
|
|
is_input_counted, |
242
|
|
|
contact_id, |
243
|
|
|
cost_center_id, |
244
|
|
|
description)) |
245
|
|
|
new_id = cursor.lastrowid |
246
|
|
|
cnx.commit() |
247
|
|
|
cursor.close() |
248
|
|
|
cnx.close() |
249
|
|
|
|
250
|
|
|
resp.status = falcon.HTTP_201 |
251
|
|
|
resp.location = '/stores/' + str(new_id) |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
class StoreItem: |
255
|
|
|
def __init__(self): |
256
|
|
|
""""Initializes StoreItem""" |
257
|
|
|
pass |
258
|
|
|
|
259
|
|
|
@staticmethod |
260
|
|
|
def on_options(req, resp, id_): |
261
|
|
|
resp.status = falcon.HTTP_200 |
262
|
|
|
|
263
|
|
|
@staticmethod |
264
|
|
|
def on_get(req, resp, id_): |
265
|
|
|
if 'API-KEY' not in req.headers or \ |
266
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
267
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
268
|
|
|
access_control(req) |
269
|
|
|
else: |
270
|
|
|
api_key_control(req) |
271
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
272
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
273
|
|
|
description='API.INVALID_STORE_ID') |
274
|
|
|
|
275
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
276
|
|
|
cursor = cnx.cursor() |
277
|
|
|
|
278
|
|
|
query = (" SELECT id, name, uuid " |
279
|
|
|
" FROM tbl_store_types ") |
280
|
|
|
cursor.execute(query) |
281
|
|
|
rows_store_types = cursor.fetchall() |
282
|
|
|
|
283
|
|
|
store_type_dict = dict() |
284
|
|
|
if rows_store_types is not None and len(rows_store_types) > 0: |
285
|
|
|
for row in rows_store_types: |
286
|
|
|
store_type_dict[row[0]] = {"id": row[0], |
287
|
|
|
"name": row[1], |
288
|
|
|
"uuid": row[2]} |
289
|
|
|
|
290
|
|
|
query = (" SELECT id, name, uuid " |
291
|
|
|
" FROM tbl_contacts ") |
292
|
|
|
cursor.execute(query) |
293
|
|
|
rows_contacts = cursor.fetchall() |
294
|
|
|
|
295
|
|
|
contact_dict = dict() |
296
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
297
|
|
|
for row in rows_contacts: |
298
|
|
|
contact_dict[row[0]] = {"id": row[0], |
299
|
|
|
"name": row[1], |
300
|
|
|
"uuid": row[2]} |
301
|
|
|
|
302
|
|
|
query = (" SELECT id, name, uuid " |
303
|
|
|
" FROM tbl_cost_centers ") |
304
|
|
|
cursor.execute(query) |
305
|
|
|
rows_cost_centers = cursor.fetchall() |
306
|
|
|
|
307
|
|
|
cost_center_dict = dict() |
308
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
309
|
|
|
for row in rows_cost_centers: |
310
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
311
|
|
|
"name": row[1], |
312
|
|
|
"uuid": row[2]} |
313
|
|
|
|
314
|
|
|
query = (" SELECT id, name, uuid, " |
315
|
|
|
" address, latitude, longitude, area, store_type_id," |
316
|
|
|
" is_input_counted, " |
317
|
|
|
" contact_id, cost_center_id, description " |
318
|
|
|
" FROM tbl_stores " |
319
|
|
|
" WHERE id = %s ") |
320
|
|
|
cursor.execute(query, (id_,)) |
321
|
|
|
row = cursor.fetchone() |
322
|
|
|
cursor.close() |
323
|
|
|
cnx.close() |
324
|
|
|
|
325
|
|
|
if row is None: |
326
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
327
|
|
|
description='API.STORE_NOT_FOUND') |
328
|
|
|
else: |
329
|
|
|
meta_result = {"id": row[0], |
330
|
|
|
"name": row[1], |
331
|
|
|
"uuid": row[2], |
332
|
|
|
"address": row[3], |
333
|
|
|
"latitude": row[4], |
334
|
|
|
"longitude": row[5], |
335
|
|
|
"area": row[6], |
336
|
|
|
"store_type": store_type_dict.get(row[7], None), |
337
|
|
|
"is_input_counted": bool(row[8]), |
338
|
|
|
"contact": contact_dict.get(row[9], None), |
339
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
340
|
|
|
"description": row[11], |
341
|
|
|
"qrcode": 'store:' + row[2]} |
342
|
|
|
|
343
|
|
|
resp.text = json.dumps(meta_result) |
344
|
|
|
|
345
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
346
|
|
|
@user_logger |
347
|
|
|
def on_delete(req, resp, id_): |
348
|
|
|
admin_control(req) |
349
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
350
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
351
|
|
|
description='API.INVALID_STORE_ID') |
352
|
|
|
|
353
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
354
|
|
|
cursor = cnx.cursor() |
355
|
|
|
|
356
|
|
|
cursor.execute(" SELECT name " |
357
|
|
|
" FROM tbl_stores " |
358
|
|
|
" WHERE id = %s ", (id_,)) |
359
|
|
|
if cursor.fetchone() is None: |
360
|
|
|
cursor.close() |
361
|
|
|
cnx.close() |
362
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
363
|
|
|
description='API.STORE_NOT_FOUND') |
364
|
|
|
|
365
|
|
|
# check relation with space |
366
|
|
|
cursor.execute(" SELECT space_id " |
367
|
|
|
" FROM tbl_spaces_stores " |
368
|
|
|
" WHERE store_id = %s ", |
369
|
|
|
(id_,)) |
370
|
|
|
rows_spaces = cursor.fetchall() |
371
|
|
|
if rows_spaces is not None and len(rows_spaces) > 0: |
372
|
|
|
cursor.close() |
373
|
|
|
cnx.close() |
374
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
375
|
|
|
title='API.BAD_REQUEST', |
376
|
|
|
description='API.THERE_IS_RELATION_WITH_SPACES') |
377
|
|
|
|
378
|
|
|
# delete relation with meter |
379
|
|
|
cursor.execute(" DELETE FROM tbl_stores_meters WHERE store_id = %s ", (id_,)) |
380
|
|
|
|
381
|
|
|
# delete relation with offline meter |
382
|
|
|
cursor.execute(" DELETE FROM tbl_stores_offline_meters WHERE store_id = %s ", (id_,)) |
383
|
|
|
|
384
|
|
|
# delete relation with points |
385
|
|
|
cursor.execute(" DELETE FROM tbl_stores_points WHERE store_id = %s ", (id_,)) |
386
|
|
|
|
387
|
|
|
# delete relation with sensor |
388
|
|
|
cursor.execute(" DELETE FROM tbl_stores_sensors WHERE store_id = %s ", (id_,)) |
389
|
|
|
|
390
|
|
|
# delete relation with virtual meter |
391
|
|
|
cursor.execute(" DELETE FROM tbl_stores_virtual_meters WHERE store_id = %s ", (id_,)) |
392
|
|
|
|
393
|
|
|
# delete relation with command |
394
|
|
|
cursor.execute(" DELETE FROM tbl_stores_commands WHERE store_id = %s ", (id_,)) |
395
|
|
|
|
396
|
|
|
# delete relation with working calendar |
397
|
|
|
cursor.execute(" DELETE FROM tbl_stores_working_calendars WHERE store_id = %s ", (id_,)) |
398
|
|
|
|
399
|
|
|
cursor.execute(" DELETE FROM tbl_stores WHERE id = %s ", (id_,)) |
400
|
|
|
cnx.commit() |
401
|
|
|
|
402
|
|
|
cursor.close() |
403
|
|
|
cnx.close() |
404
|
|
|
|
405
|
|
|
resp.status = falcon.HTTP_204 |
406
|
|
|
|
407
|
|
|
@staticmethod |
408
|
|
|
@user_logger |
409
|
|
|
def on_put(req, resp, id_): |
410
|
|
|
"""Handles PUT requests""" |
411
|
|
|
admin_control(req) |
412
|
|
|
try: |
413
|
|
|
raw_json = req.stream.read().decode('utf-8') |
414
|
|
|
except Exception as ex: |
415
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
416
|
|
|
title='API.BAD_REQUEST', |
417
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
418
|
|
|
|
419
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
420
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
421
|
|
|
description='API.INVALID_STORE_ID') |
422
|
|
|
|
423
|
|
|
new_values = json.loads(raw_json) |
424
|
|
|
|
425
|
|
|
if 'name' not in new_values['data'].keys() or \ |
426
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
427
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
428
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
429
|
|
|
description='API.INVALID_STORE_NAME') |
430
|
|
|
name = str.strip(new_values['data']['name']) |
431
|
|
|
|
432
|
|
|
if 'address' not in new_values['data'].keys() or \ |
433
|
|
|
not isinstance(new_values['data']['address'], str) or \ |
434
|
|
|
len(str.strip(new_values['data']['address'])) == 0: |
435
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
436
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
437
|
|
|
address = str.strip(new_values['data']['address']) |
438
|
|
|
|
439
|
|
|
if 'latitude' not in new_values['data'].keys() or \ |
440
|
|
|
not (isinstance(new_values['data']['latitude'], float) or |
441
|
|
|
isinstance(new_values['data']['latitude'], int)) or \ |
442
|
|
|
new_values['data']['latitude'] < -90.0 or \ |
443
|
|
|
new_values['data']['latitude'] > 90.0: |
444
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
445
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
446
|
|
|
latitude = new_values['data']['latitude'] |
447
|
|
|
|
448
|
|
|
if 'longitude' not in new_values['data'].keys() or \ |
449
|
|
|
not (isinstance(new_values['data']['longitude'], float) or |
450
|
|
|
isinstance(new_values['data']['longitude'], int)) or \ |
451
|
|
|
new_values['data']['longitude'] < -180.0 or \ |
452
|
|
|
new_values['data']['longitude'] > 180.0: |
453
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
454
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
455
|
|
|
longitude = new_values['data']['longitude'] |
456
|
|
|
|
457
|
|
|
if 'area' not in new_values['data'].keys() or \ |
458
|
|
|
not (isinstance(new_values['data']['area'], float) or |
459
|
|
|
isinstance(new_values['data']['area'], int)) or \ |
460
|
|
|
new_values['data']['area'] <= 0.0: |
461
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
462
|
|
|
description='API.INVALID_AREA_VALUE') |
463
|
|
|
area = new_values['data']['area'] |
464
|
|
|
|
465
|
|
|
if 'store_type_id' not in new_values['data'].keys() or \ |
466
|
|
|
not isinstance(new_values['data']['store_type_id'], int) or \ |
467
|
|
|
new_values['data']['store_type_id'] <= 0: |
468
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
469
|
|
|
description='API.INVALID_STORE_TYPE_ID') |
470
|
|
|
store_type_id = new_values['data']['store_type_id'] |
471
|
|
|
|
472
|
|
|
if 'is_input_counted' not in new_values['data'].keys() or \ |
473
|
|
|
not isinstance(new_values['data']['is_input_counted'], bool): |
474
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
475
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
476
|
|
|
is_input_counted = new_values['data']['is_input_counted'] |
477
|
|
|
|
478
|
|
|
if 'contact_id' not in new_values['data'].keys() or \ |
479
|
|
|
not isinstance(new_values['data']['contact_id'], int) or \ |
480
|
|
|
new_values['data']['contact_id'] <= 0: |
481
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
482
|
|
|
description='API.INVALID_CONTACT_ID') |
483
|
|
|
contact_id = new_values['data']['contact_id'] |
484
|
|
|
|
485
|
|
|
if 'cost_center_id' not in new_values['data'].keys() or \ |
486
|
|
|
not isinstance(new_values['data']['cost_center_id'], int) or \ |
487
|
|
|
new_values['data']['cost_center_id'] <= 0: |
488
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
489
|
|
|
description='API.INVALID_COST_CENTER_ID') |
490
|
|
|
cost_center_id = new_values['data']['cost_center_id'] |
491
|
|
|
|
492
|
|
|
if 'description' in new_values['data'].keys() and \ |
493
|
|
|
new_values['data']['description'] is not None and \ |
494
|
|
|
len(str(new_values['data']['description'])) > 0: |
495
|
|
|
description = str.strip(new_values['data']['description']) |
496
|
|
|
else: |
497
|
|
|
description = None |
498
|
|
|
|
499
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
500
|
|
|
cursor = cnx.cursor() |
501
|
|
|
|
502
|
|
|
cursor.execute(" SELECT name " |
503
|
|
|
" FROM tbl_stores " |
504
|
|
|
" WHERE id = %s ", (id_,)) |
505
|
|
|
if cursor.fetchone() is None: |
506
|
|
|
cursor.close() |
507
|
|
|
cnx.close() |
508
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
509
|
|
|
description='API.STORE_NOT_FOUND') |
510
|
|
|
|
511
|
|
|
cursor.execute(" SELECT name " |
512
|
|
|
" FROM tbl_stores " |
513
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
514
|
|
|
if cursor.fetchone() is not None: |
515
|
|
|
cursor.close() |
516
|
|
|
cnx.close() |
517
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
518
|
|
|
description='API.STORE_NAME_IS_ALREADY_IN_USE') |
519
|
|
|
|
520
|
|
|
cursor.execute(" SELECT name " |
521
|
|
|
" FROM tbl_store_types " |
522
|
|
|
" WHERE id = %s ", |
523
|
|
|
(store_type_id,)) |
524
|
|
|
if cursor.fetchone() is None: |
525
|
|
|
cursor.close() |
526
|
|
|
cnx.close() |
527
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
528
|
|
|
description='API.STORE_TYPE_NOT_FOUND') |
529
|
|
|
|
530
|
|
|
cursor.execute(" SELECT name " |
531
|
|
|
" FROM tbl_contacts " |
532
|
|
|
" WHERE id = %s ", |
533
|
|
|
(new_values['data']['contact_id'],)) |
534
|
|
|
row = cursor.fetchone() |
535
|
|
|
if row is None: |
536
|
|
|
cursor.close() |
537
|
|
|
cnx.close() |
538
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
539
|
|
|
description='API.CONTACT_NOT_FOUND') |
540
|
|
|
|
541
|
|
|
cursor.execute(" SELECT name " |
542
|
|
|
" FROM tbl_cost_centers " |
543
|
|
|
" WHERE id = %s ", |
544
|
|
|
(new_values['data']['cost_center_id'],)) |
545
|
|
|
row = cursor.fetchone() |
546
|
|
|
if row is None: |
547
|
|
|
cursor.close() |
548
|
|
|
cnx.close() |
549
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
550
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
551
|
|
|
|
552
|
|
|
update_row = (" UPDATE tbl_stores " |
553
|
|
|
" SET name = %s, address = %s, latitude = %s, longitude = %s, area = %s, " |
554
|
|
|
" store_type_id = %s, is_input_counted = %s, " |
555
|
|
|
" contact_id = %s, cost_center_id = %s, " |
556
|
|
|
" description = %s " |
557
|
|
|
" WHERE id = %s ") |
558
|
|
|
cursor.execute(update_row, (name, |
559
|
|
|
address, |
560
|
|
|
latitude, |
561
|
|
|
longitude, |
562
|
|
|
area, |
563
|
|
|
store_type_id, |
564
|
|
|
is_input_counted, |
565
|
|
|
contact_id, |
566
|
|
|
cost_center_id, |
567
|
|
|
description, |
568
|
|
|
id_)) |
569
|
|
|
cnx.commit() |
570
|
|
|
|
571
|
|
|
cursor.close() |
572
|
|
|
cnx.close() |
573
|
|
|
|
574
|
|
|
resp.status = falcon.HTTP_200 |
575
|
|
|
|
576
|
|
|
|
577
|
|
View Code Duplication |
class StoreMeterCollection: |
|
|
|
|
578
|
|
|
def __init__(self): |
579
|
|
|
""""Initializes StoreMeterCollection""" |
580
|
|
|
pass |
581
|
|
|
|
582
|
|
|
@staticmethod |
583
|
|
|
def on_options(req, resp, id_): |
584
|
|
|
resp.status = falcon.HTTP_200 |
585
|
|
|
|
586
|
|
|
@staticmethod |
587
|
|
|
def on_get(req, resp, id_): |
588
|
|
|
if 'API-KEY' not in req.headers or \ |
589
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
590
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
591
|
|
|
access_control(req) |
592
|
|
|
else: |
593
|
|
|
api_key_control(req) |
594
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
595
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
596
|
|
|
description='API.INVALID_STORE_ID') |
597
|
|
|
|
598
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
599
|
|
|
cursor = cnx.cursor() |
600
|
|
|
|
601
|
|
|
cursor.execute(" SELECT name " |
602
|
|
|
" FROM tbl_stores " |
603
|
|
|
" WHERE id = %s ", (id_,)) |
604
|
|
|
if cursor.fetchone() is None: |
605
|
|
|
cursor.close() |
606
|
|
|
cnx.close() |
607
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
608
|
|
|
description='API.STORE_NOT_FOUND') |
609
|
|
|
|
610
|
|
|
query = (" SELECT id, name, uuid " |
611
|
|
|
" FROM tbl_energy_categories ") |
612
|
|
|
cursor.execute(query) |
613
|
|
|
rows_energy_categories = cursor.fetchall() |
614
|
|
|
|
615
|
|
|
energy_category_dict = dict() |
616
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
617
|
|
|
for row in rows_energy_categories: |
618
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
619
|
|
|
"name": row[1], |
620
|
|
|
"uuid": row[2]} |
621
|
|
|
|
622
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
623
|
|
|
" FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " |
624
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
625
|
|
|
" ORDER BY m.id ") |
626
|
|
|
cursor.execute(query, (id_,)) |
627
|
|
|
rows = cursor.fetchall() |
628
|
|
|
|
629
|
|
|
result = list() |
630
|
|
|
if rows is not None and len(rows) > 0: |
631
|
|
|
for row in rows: |
632
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
633
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
634
|
|
|
result.append(meta_result) |
635
|
|
|
|
636
|
|
|
resp.text = json.dumps(result) |
637
|
|
|
|
638
|
|
|
@staticmethod |
639
|
|
|
@user_logger |
640
|
|
|
def on_post(req, resp, id_): |
641
|
|
|
"""Handles POST requests""" |
642
|
|
|
admin_control(req) |
643
|
|
|
try: |
644
|
|
|
raw_json = req.stream.read().decode('utf-8') |
645
|
|
|
except Exception as ex: |
646
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
647
|
|
|
title='API.BAD_REQUEST', |
648
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
649
|
|
|
|
650
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
651
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
652
|
|
|
description='API.INVALID_STORE_ID') |
653
|
|
|
|
654
|
|
|
new_values = json.loads(raw_json) |
655
|
|
|
|
656
|
|
|
if 'meter_id' not in new_values['data'].keys() or \ |
657
|
|
|
not isinstance(new_values['data']['meter_id'], int) or \ |
658
|
|
|
new_values['data']['meter_id'] <= 0: |
659
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
660
|
|
|
description='API.INVALID_METER_ID') |
661
|
|
|
meter_id = new_values['data']['meter_id'] |
662
|
|
|
|
663
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
664
|
|
|
cursor = cnx.cursor() |
665
|
|
|
|
666
|
|
|
cursor.execute(" SELECT name " |
667
|
|
|
" from tbl_stores " |
668
|
|
|
" WHERE id = %s ", (id_,)) |
669
|
|
|
if cursor.fetchone() is None: |
670
|
|
|
cursor.close() |
671
|
|
|
cnx.close() |
672
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
673
|
|
|
description='API.STORE_NOT_FOUND') |
674
|
|
|
|
675
|
|
|
cursor.execute(" SELECT name " |
676
|
|
|
" FROM tbl_meters " |
677
|
|
|
" WHERE id = %s ", (meter_id,)) |
678
|
|
|
if cursor.fetchone() is None: |
679
|
|
|
cursor.close() |
680
|
|
|
cnx.close() |
681
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
682
|
|
|
description='API.METER_NOT_FOUND') |
683
|
|
|
|
684
|
|
|
query = (" SELECT id " |
685
|
|
|
" FROM tbl_stores_meters " |
686
|
|
|
" WHERE store_id = %s AND meter_id = %s") |
687
|
|
|
cursor.execute(query, (id_, meter_id,)) |
688
|
|
|
if cursor.fetchone() is not None: |
689
|
|
|
cursor.close() |
690
|
|
|
cnx.close() |
691
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
692
|
|
|
description='API.STORE_METER_RELATION_EXISTS') |
693
|
|
|
|
694
|
|
|
add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " |
695
|
|
|
" VALUES (%s, %s) ") |
696
|
|
|
cursor.execute(add_row, (id_, meter_id,)) |
697
|
|
|
cnx.commit() |
698
|
|
|
cursor.close() |
699
|
|
|
cnx.close() |
700
|
|
|
|
701
|
|
|
resp.status = falcon.HTTP_201 |
702
|
|
|
resp.location = '/stores/' + str(id_) + '/meters/' + str(meter_id) |
703
|
|
|
|
704
|
|
|
|
705
|
|
View Code Duplication |
class StoreMeterItem: |
|
|
|
|
706
|
|
|
def __init__(self): |
707
|
|
|
"""Initializes Class""" |
708
|
|
|
pass |
709
|
|
|
|
710
|
|
|
@staticmethod |
711
|
|
|
def on_options(req, resp, id_, mid): |
712
|
|
|
resp.status = falcon.HTTP_200 |
713
|
|
|
|
714
|
|
|
@staticmethod |
715
|
|
|
@user_logger |
716
|
|
|
def on_delete(req, resp, id_, mid): |
717
|
|
|
admin_control(req) |
718
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
719
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
720
|
|
|
description='API.INVALID_STORE_ID') |
721
|
|
|
|
722
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
723
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
724
|
|
|
description='API.INVALID_METER_ID') |
725
|
|
|
|
726
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
727
|
|
|
cursor = cnx.cursor() |
728
|
|
|
|
729
|
|
|
cursor.execute(" SELECT name " |
730
|
|
|
" FROM tbl_stores " |
731
|
|
|
" WHERE id = %s ", (id_,)) |
732
|
|
|
if cursor.fetchone() is None: |
733
|
|
|
cursor.close() |
734
|
|
|
cnx.close() |
735
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
736
|
|
|
description='API.STORE_NOT_FOUND') |
737
|
|
|
|
738
|
|
|
cursor.execute(" SELECT name " |
739
|
|
|
" FROM tbl_meters " |
740
|
|
|
" WHERE id = %s ", (mid,)) |
741
|
|
|
if cursor.fetchone() is None: |
742
|
|
|
cursor.close() |
743
|
|
|
cnx.close() |
744
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
745
|
|
|
description='API.METER_NOT_FOUND') |
746
|
|
|
|
747
|
|
|
cursor.execute(" SELECT id " |
748
|
|
|
" FROM tbl_stores_meters " |
749
|
|
|
" WHERE store_id = %s AND meter_id = %s ", (id_, mid)) |
750
|
|
|
if cursor.fetchone() is None: |
751
|
|
|
cursor.close() |
752
|
|
|
cnx.close() |
753
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
754
|
|
|
description='API.STORE_METER_RELATION_NOT_FOUND') |
755
|
|
|
|
756
|
|
|
cursor.execute(" DELETE FROM tbl_stores_meters WHERE store_id = %s AND meter_id = %s ", (id_, mid)) |
757
|
|
|
cnx.commit() |
758
|
|
|
|
759
|
|
|
cursor.close() |
760
|
|
|
cnx.close() |
761
|
|
|
|
762
|
|
|
resp.status = falcon.HTTP_204 |
763
|
|
|
|
764
|
|
|
|
765
|
|
View Code Duplication |
class StoreOfflineMeterCollection: |
|
|
|
|
766
|
|
|
def __init__(self): |
767
|
|
|
"""Initializes Class""" |
768
|
|
|
pass |
769
|
|
|
|
770
|
|
|
@staticmethod |
771
|
|
|
def on_options(req, resp, id_): |
772
|
|
|
resp.status = falcon.HTTP_200 |
773
|
|
|
|
774
|
|
|
@staticmethod |
775
|
|
|
def on_get(req, resp, id_): |
776
|
|
|
if 'API-KEY' not in req.headers or \ |
777
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
778
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
779
|
|
|
access_control(req) |
780
|
|
|
else: |
781
|
|
|
api_key_control(req) |
782
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
783
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
784
|
|
|
description='API.INVALID_STORE_ID') |
785
|
|
|
|
786
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
787
|
|
|
cursor = cnx.cursor() |
788
|
|
|
|
789
|
|
|
cursor.execute(" SELECT name " |
790
|
|
|
" FROM tbl_stores " |
791
|
|
|
" WHERE id = %s ", (id_,)) |
792
|
|
|
if cursor.fetchone() is None: |
793
|
|
|
cursor.close() |
794
|
|
|
cnx.close() |
795
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
796
|
|
|
description='API.STORE_NOT_FOUND') |
797
|
|
|
|
798
|
|
|
query = (" SELECT id, name, uuid " |
799
|
|
|
" FROM tbl_energy_categories ") |
800
|
|
|
cursor.execute(query) |
801
|
|
|
rows_energy_categories = cursor.fetchall() |
802
|
|
|
|
803
|
|
|
energy_category_dict = dict() |
804
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
805
|
|
|
for row in rows_energy_categories: |
806
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
807
|
|
|
"name": row[1], |
808
|
|
|
"uuid": row[2]} |
809
|
|
|
|
810
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
811
|
|
|
" FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " |
812
|
|
|
" WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
813
|
|
|
" ORDER BY m.id ") |
814
|
|
|
cursor.execute(query, (id_,)) |
815
|
|
|
rows = cursor.fetchall() |
816
|
|
|
|
817
|
|
|
result = list() |
818
|
|
|
if rows is not None and len(rows) > 0: |
819
|
|
|
for row in rows: |
820
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
821
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
822
|
|
|
result.append(meta_result) |
823
|
|
|
|
824
|
|
|
resp.text = json.dumps(result) |
825
|
|
|
|
826
|
|
|
@staticmethod |
827
|
|
|
@user_logger |
828
|
|
|
def on_post(req, resp, id_): |
829
|
|
|
"""Handles POST requests""" |
830
|
|
|
admin_control(req) |
831
|
|
|
try: |
832
|
|
|
raw_json = req.stream.read().decode('utf-8') |
833
|
|
|
except Exception as ex: |
834
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
835
|
|
|
title='API.BAD_REQUEST', |
836
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
837
|
|
|
|
838
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
839
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
840
|
|
|
description='API.INVALID_STORE_ID') |
841
|
|
|
|
842
|
|
|
new_values = json.loads(raw_json) |
843
|
|
|
|
844
|
|
|
if 'offline_meter_id' not in new_values['data'].keys() or \ |
845
|
|
|
not isinstance(new_values['data']['offline_meter_id'], int) or \ |
846
|
|
|
new_values['data']['offline_meter_id'] <= 0: |
847
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
848
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
849
|
|
|
offline_meter_id = new_values['data']['offline_meter_id'] |
850
|
|
|
|
851
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
852
|
|
|
cursor = cnx.cursor() |
853
|
|
|
|
854
|
|
|
cursor.execute(" SELECT name " |
855
|
|
|
" from tbl_stores " |
856
|
|
|
" WHERE id = %s ", (id_,)) |
857
|
|
|
if cursor.fetchone() is None: |
858
|
|
|
cursor.close() |
859
|
|
|
cnx.close() |
860
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
861
|
|
|
description='API.STORE_NOT_FOUND') |
862
|
|
|
|
863
|
|
|
cursor.execute(" SELECT name " |
864
|
|
|
" FROM tbl_offline_meters " |
865
|
|
|
" WHERE id = %s ", (offline_meter_id,)) |
866
|
|
|
if cursor.fetchone() is None: |
867
|
|
|
cursor.close() |
868
|
|
|
cnx.close() |
869
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
870
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
871
|
|
|
|
872
|
|
|
query = (" SELECT id " |
873
|
|
|
" FROM tbl_stores_offline_meters " |
874
|
|
|
" WHERE store_id = %s AND offline_meter_id = %s") |
875
|
|
|
cursor.execute(query, (id_, offline_meter_id,)) |
876
|
|
|
if cursor.fetchone() is not None: |
877
|
|
|
cursor.close() |
878
|
|
|
cnx.close() |
879
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
880
|
|
|
description='API.STORE_OFFLINE_METER_RELATION_EXISTS') |
881
|
|
|
|
882
|
|
|
add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " |
883
|
|
|
" VALUES (%s, %s) ") |
884
|
|
|
cursor.execute(add_row, (id_, offline_meter_id,)) |
885
|
|
|
cnx.commit() |
886
|
|
|
cursor.close() |
887
|
|
|
cnx.close() |
888
|
|
|
|
889
|
|
|
resp.status = falcon.HTTP_201 |
890
|
|
|
resp.location = '/stores/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) |
891
|
|
|
|
892
|
|
|
|
893
|
|
View Code Duplication |
class StoreOfflineMeterItem: |
|
|
|
|
894
|
|
|
def __init__(self): |
895
|
|
|
"""Initializes Class""" |
896
|
|
|
pass |
897
|
|
|
|
898
|
|
|
@staticmethod |
899
|
|
|
def on_options(req, resp, id_, mid): |
900
|
|
|
resp.status = falcon.HTTP_200 |
901
|
|
|
|
902
|
|
|
@staticmethod |
903
|
|
|
@user_logger |
904
|
|
|
def on_delete(req, resp, id_, mid): |
905
|
|
|
admin_control(req) |
906
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
907
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
908
|
|
|
description='API.INVALID_STORE_ID') |
909
|
|
|
|
910
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
911
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
912
|
|
|
description='API.INVALID_OFFLINE_METER_ID') |
913
|
|
|
|
914
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
915
|
|
|
cursor = cnx.cursor() |
916
|
|
|
|
917
|
|
|
cursor.execute(" SELECT name " |
918
|
|
|
" FROM tbl_stores " |
919
|
|
|
" WHERE id = %s ", (id_,)) |
920
|
|
|
if cursor.fetchone() is None: |
921
|
|
|
cursor.close() |
922
|
|
|
cnx.close() |
923
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
924
|
|
|
description='API.STORE_NOT_FOUND') |
925
|
|
|
|
926
|
|
|
cursor.execute(" SELECT name " |
927
|
|
|
" FROM tbl_offline_meters " |
928
|
|
|
" WHERE id = %s ", (mid,)) |
929
|
|
|
if cursor.fetchone() is None: |
930
|
|
|
cursor.close() |
931
|
|
|
cnx.close() |
932
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
933
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
934
|
|
|
|
935
|
|
|
cursor.execute(" SELECT id " |
936
|
|
|
" FROM tbl_stores_offline_meters " |
937
|
|
|
" WHERE store_id = %s AND offline_meter_id = %s ", (id_, mid)) |
938
|
|
|
if cursor.fetchone() is None: |
939
|
|
|
cursor.close() |
940
|
|
|
cnx.close() |
941
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
942
|
|
|
description='API.STORE_OFFLINE_METER_RELATION_NOT_FOUND') |
943
|
|
|
|
944
|
|
|
cursor.execute(" DELETE FROM tbl_stores_offline_meters " |
945
|
|
|
" WHERE store_id = %s AND offline_meter_id = %s ", (id_, mid)) |
946
|
|
|
cnx.commit() |
947
|
|
|
|
948
|
|
|
cursor.close() |
949
|
|
|
cnx.close() |
950
|
|
|
|
951
|
|
|
resp.status = falcon.HTTP_204 |
952
|
|
|
|
953
|
|
|
|
954
|
|
View Code Duplication |
class StorePointCollection: |
|
|
|
|
955
|
|
|
def __init__(self): |
956
|
|
|
"""Initializes Class""" |
957
|
|
|
pass |
958
|
|
|
|
959
|
|
|
@staticmethod |
960
|
|
|
def on_options(req, resp, id_): |
961
|
|
|
resp.status = falcon.HTTP_200 |
962
|
|
|
|
963
|
|
|
@staticmethod |
964
|
|
|
def on_get(req, resp, id_): |
965
|
|
|
if 'API-KEY' not in req.headers or \ |
966
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
967
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
968
|
|
|
access_control(req) |
969
|
|
|
else: |
970
|
|
|
api_key_control(req) |
971
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
972
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
973
|
|
|
description='API.INVALID_STORE_ID') |
974
|
|
|
|
975
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
976
|
|
|
cursor = cnx.cursor() |
977
|
|
|
|
978
|
|
|
cursor.execute(" SELECT name " |
979
|
|
|
" FROM tbl_stores " |
980
|
|
|
" WHERE id = %s ", (id_,)) |
981
|
|
|
if cursor.fetchone() is None: |
982
|
|
|
cursor.close() |
983
|
|
|
cnx.close() |
984
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
985
|
|
|
description='API.STORE_NOT_FOUND') |
986
|
|
|
|
987
|
|
|
query = (" SELECT id, name, uuid " |
988
|
|
|
" FROM tbl_data_sources ") |
989
|
|
|
cursor.execute(query) |
990
|
|
|
rows_data_sources = cursor.fetchall() |
991
|
|
|
|
992
|
|
|
data_source_dict = dict() |
993
|
|
|
if rows_data_sources is not None and len(rows_data_sources) > 0: |
994
|
|
|
for row in rows_data_sources: |
995
|
|
|
data_source_dict[row[0]] = {"id": row[0], |
996
|
|
|
"name": row[1], |
997
|
|
|
"uuid": row[2]} |
998
|
|
|
|
999
|
|
|
query = (" SELECT p.id, p.name, p.data_source_id " |
1000
|
|
|
" FROM tbl_stores t, tbl_stores_points tp, tbl_points p " |
1001
|
|
|
" WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " |
1002
|
|
|
" ORDER BY p.id ") |
1003
|
|
|
cursor.execute(query, (id_,)) |
1004
|
|
|
rows = cursor.fetchall() |
1005
|
|
|
|
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], "data_source": data_source_dict.get(row[2], None)} |
1010
|
|
|
result.append(meta_result) |
1011
|
|
|
|
1012
|
|
|
resp.text = json.dumps(result) |
1013
|
|
|
|
1014
|
|
|
@staticmethod |
1015
|
|
|
@user_logger |
1016
|
|
|
def on_post(req, resp, id_): |
1017
|
|
|
"""Handles POST requests""" |
1018
|
|
|
admin_control(req) |
1019
|
|
|
try: |
1020
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1021
|
|
|
except Exception as ex: |
1022
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1023
|
|
|
title='API.BAD_REQUEST', |
1024
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1025
|
|
|
|
1026
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1027
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1028
|
|
|
description='API.INVALID_STORE_ID') |
1029
|
|
|
|
1030
|
|
|
new_values = json.loads(raw_json) |
1031
|
|
|
|
1032
|
|
|
if 'point_id' not in new_values['data'].keys() or \ |
1033
|
|
|
not isinstance(new_values['data']['point_id'], int) or \ |
1034
|
|
|
new_values['data']['point_id'] <= 0: |
1035
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1036
|
|
|
description='API.INVALID_POINT_ID') |
1037
|
|
|
point_id = new_values['data']['point_id'] |
1038
|
|
|
|
1039
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1040
|
|
|
cursor = cnx.cursor() |
1041
|
|
|
|
1042
|
|
|
cursor.execute(" SELECT name " |
1043
|
|
|
" from tbl_stores " |
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.STORE_NOT_FOUND') |
1050
|
|
|
|
1051
|
|
|
cursor.execute(" SELECT name " |
1052
|
|
|
" FROM tbl_points " |
1053
|
|
|
" WHERE id = %s ", (point_id,)) |
1054
|
|
|
if cursor.fetchone() is None: |
1055
|
|
|
cursor.close() |
1056
|
|
|
cnx.close() |
1057
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1058
|
|
|
description='API.POINT_NOT_FOUND') |
1059
|
|
|
|
1060
|
|
|
query = (" SELECT id " |
1061
|
|
|
" FROM tbl_stores_points " |
1062
|
|
|
" WHERE store_id = %s AND point_id = %s") |
1063
|
|
|
cursor.execute(query, (id_, point_id,)) |
1064
|
|
|
if cursor.fetchone() is not None: |
1065
|
|
|
cursor.close() |
1066
|
|
|
cnx.close() |
1067
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1068
|
|
|
description='API.STORE_POINT_RELATION_EXISTS') |
1069
|
|
|
|
1070
|
|
|
add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " |
1071
|
|
|
" VALUES (%s, %s) ") |
1072
|
|
|
cursor.execute(add_row, (id_, point_id,)) |
1073
|
|
|
cnx.commit() |
1074
|
|
|
cursor.close() |
1075
|
|
|
cnx.close() |
1076
|
|
|
|
1077
|
|
|
resp.status = falcon.HTTP_201 |
1078
|
|
|
resp.location = '/stores/' + str(id_) + '/points/' + str(point_id) |
1079
|
|
|
|
1080
|
|
|
|
1081
|
|
View Code Duplication |
class StorePointItem: |
|
|
|
|
1082
|
|
|
def __init__(self): |
1083
|
|
|
"""Initializes Class""" |
1084
|
|
|
pass |
1085
|
|
|
|
1086
|
|
|
@staticmethod |
1087
|
|
|
def on_options(req, resp, id_, pid): |
1088
|
|
|
resp.status = falcon.HTTP_200 |
1089
|
|
|
|
1090
|
|
|
@staticmethod |
1091
|
|
|
@user_logger |
1092
|
|
|
def on_delete(req, resp, id_, pid): |
1093
|
|
|
admin_control(req) |
1094
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1095
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1096
|
|
|
description='API.INVALID_STORE_ID') |
1097
|
|
|
|
1098
|
|
|
if not pid.isdigit() or int(pid) <= 0: |
1099
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1100
|
|
|
description='API.INVALID_POINT_ID') |
1101
|
|
|
|
1102
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1103
|
|
|
cursor = cnx.cursor() |
1104
|
|
|
|
1105
|
|
|
cursor.execute(" SELECT name " |
1106
|
|
|
" FROM tbl_stores " |
1107
|
|
|
" WHERE id = %s ", (id_,)) |
1108
|
|
|
if cursor.fetchone() is None: |
1109
|
|
|
cursor.close() |
1110
|
|
|
cnx.close() |
1111
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1112
|
|
|
description='API.STORE_NOT_FOUND') |
1113
|
|
|
|
1114
|
|
|
cursor.execute(" SELECT name " |
1115
|
|
|
" FROM tbl_points " |
1116
|
|
|
" WHERE id = %s ", (pid,)) |
1117
|
|
|
if cursor.fetchone() is None: |
1118
|
|
|
cursor.close() |
1119
|
|
|
cnx.close() |
1120
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1121
|
|
|
description='API.POINT_NOT_FOUND') |
1122
|
|
|
|
1123
|
|
|
cursor.execute(" SELECT id " |
1124
|
|
|
" FROM tbl_stores_points " |
1125
|
|
|
" WHERE store_id = %s AND point_id = %s ", (id_, pid)) |
1126
|
|
|
if cursor.fetchone() is None: |
1127
|
|
|
cursor.close() |
1128
|
|
|
cnx.close() |
1129
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1130
|
|
|
description='API.STORE_POINT_RELATION_NOT_FOUND') |
1131
|
|
|
|
1132
|
|
|
cursor.execute(" DELETE FROM tbl_stores_points " |
1133
|
|
|
" WHERE store_id = %s AND point_id = %s ", (id_, pid)) |
1134
|
|
|
cnx.commit() |
1135
|
|
|
|
1136
|
|
|
cursor.close() |
1137
|
|
|
cnx.close() |
1138
|
|
|
|
1139
|
|
|
resp.status = falcon.HTTP_204 |
1140
|
|
|
|
1141
|
|
|
|
1142
|
|
View Code Duplication |
class StoreSensorCollection: |
|
|
|
|
1143
|
|
|
def __init__(self): |
1144
|
|
|
"""Initializes Class""" |
1145
|
|
|
pass |
1146
|
|
|
|
1147
|
|
|
@staticmethod |
1148
|
|
|
def on_options(req, resp, id_): |
1149
|
|
|
resp.status = falcon.HTTP_200 |
1150
|
|
|
|
1151
|
|
|
@staticmethod |
1152
|
|
|
def on_get(req, resp, id_): |
1153
|
|
|
if 'API-KEY' not in req.headers or \ |
1154
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1155
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1156
|
|
|
access_control(req) |
1157
|
|
|
else: |
1158
|
|
|
api_key_control(req) |
1159
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1160
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1161
|
|
|
description='API.INVALID_STORE_ID') |
1162
|
|
|
|
1163
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1164
|
|
|
cursor = cnx.cursor() |
1165
|
|
|
|
1166
|
|
|
cursor.execute(" SELECT name " |
1167
|
|
|
" FROM tbl_stores " |
1168
|
|
|
" WHERE id = %s ", (id_,)) |
1169
|
|
|
if cursor.fetchone() is None: |
1170
|
|
|
cursor.close() |
1171
|
|
|
cnx.close() |
1172
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1173
|
|
|
description='API.STORE_NOT_FOUND') |
1174
|
|
|
|
1175
|
|
|
query = (" SELECT s.id, s.name, s.uuid " |
1176
|
|
|
" FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
1177
|
|
|
" WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
1178
|
|
|
" ORDER BY s.id ") |
1179
|
|
|
cursor.execute(query, (id_,)) |
1180
|
|
|
rows = cursor.fetchall() |
1181
|
|
|
|
1182
|
|
|
result = list() |
1183
|
|
|
if rows is not None and len(rows) > 0: |
1184
|
|
|
for row in rows: |
1185
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
1186
|
|
|
result.append(meta_result) |
1187
|
|
|
|
1188
|
|
|
resp.text = json.dumps(result) |
1189
|
|
|
|
1190
|
|
|
@staticmethod |
1191
|
|
|
@user_logger |
1192
|
|
|
def on_post(req, resp, id_): |
1193
|
|
|
"""Handles POST requests""" |
1194
|
|
|
admin_control(req) |
1195
|
|
|
try: |
1196
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1197
|
|
|
except Exception as ex: |
1198
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1199
|
|
|
title='API.BAD_REQUEST', |
1200
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1201
|
|
|
|
1202
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1203
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1204
|
|
|
description='API.INVALID_STORE_ID') |
1205
|
|
|
|
1206
|
|
|
new_values = json.loads(raw_json) |
1207
|
|
|
|
1208
|
|
|
if 'sensor_id' not in new_values['data'].keys() or \ |
1209
|
|
|
not isinstance(new_values['data']['sensor_id'], int) or \ |
1210
|
|
|
new_values['data']['sensor_id'] <= 0: |
1211
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1212
|
|
|
description='API.INVALID_SENSOR_ID') |
1213
|
|
|
sensor_id = new_values['data']['sensor_id'] |
1214
|
|
|
|
1215
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1216
|
|
|
cursor = cnx.cursor() |
1217
|
|
|
|
1218
|
|
|
cursor.execute(" SELECT name " |
1219
|
|
|
" from tbl_stores " |
1220
|
|
|
" WHERE id = %s ", (id_,)) |
1221
|
|
|
if cursor.fetchone() is None: |
1222
|
|
|
cursor.close() |
1223
|
|
|
cnx.close() |
1224
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1225
|
|
|
description='API.STORE_NOT_FOUND') |
1226
|
|
|
|
1227
|
|
|
cursor.execute(" SELECT name " |
1228
|
|
|
" FROM tbl_sensors " |
1229
|
|
|
" WHERE id = %s ", (sensor_id,)) |
1230
|
|
|
if cursor.fetchone() is None: |
1231
|
|
|
cursor.close() |
1232
|
|
|
cnx.close() |
1233
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1234
|
|
|
description='API.SENSOR_NOT_FOUND') |
1235
|
|
|
|
1236
|
|
|
query = (" SELECT id " |
1237
|
|
|
" FROM tbl_stores_sensors " |
1238
|
|
|
" WHERE store_id = %s AND sensor_id = %s") |
1239
|
|
|
cursor.execute(query, (id_, sensor_id,)) |
1240
|
|
|
if cursor.fetchone() is not None: |
1241
|
|
|
cursor.close() |
1242
|
|
|
cnx.close() |
1243
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1244
|
|
|
description='API.STORE_SENSOR_RELATION_EXISTS') |
1245
|
|
|
|
1246
|
|
|
add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
1247
|
|
|
" VALUES (%s, %s) ") |
1248
|
|
|
cursor.execute(add_row, (id_, sensor_id,)) |
1249
|
|
|
cnx.commit() |
1250
|
|
|
cursor.close() |
1251
|
|
|
cnx.close() |
1252
|
|
|
|
1253
|
|
|
resp.status = falcon.HTTP_201 |
1254
|
|
|
resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id) |
1255
|
|
|
|
1256
|
|
|
|
1257
|
|
View Code Duplication |
class StoreSensorItem: |
|
|
|
|
1258
|
|
|
def __init__(self): |
1259
|
|
|
"""Initializes Class""" |
1260
|
|
|
pass |
1261
|
|
|
|
1262
|
|
|
@staticmethod |
1263
|
|
|
def on_options(req, resp, id_, sid): |
1264
|
|
|
resp.status = falcon.HTTP_200 |
1265
|
|
|
|
1266
|
|
|
@staticmethod |
1267
|
|
|
@user_logger |
1268
|
|
|
def on_delete(req, resp, id_, sid): |
1269
|
|
|
admin_control(req) |
1270
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1271
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1272
|
|
|
description='API.INVALID_STORE_ID') |
1273
|
|
|
|
1274
|
|
|
if not sid.isdigit() or int(sid) <= 0: |
1275
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1276
|
|
|
description='API.INVALID_SENSOR_ID') |
1277
|
|
|
|
1278
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1279
|
|
|
cursor = cnx.cursor() |
1280
|
|
|
|
1281
|
|
|
cursor.execute(" SELECT name " |
1282
|
|
|
" FROM tbl_stores " |
1283
|
|
|
" WHERE id = %s ", (id_,)) |
1284
|
|
|
if cursor.fetchone() is None: |
1285
|
|
|
cursor.close() |
1286
|
|
|
cnx.close() |
1287
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1288
|
|
|
description='API.STORE_NOT_FOUND') |
1289
|
|
|
|
1290
|
|
|
cursor.execute(" SELECT name " |
1291
|
|
|
" FROM tbl_sensors " |
1292
|
|
|
" WHERE id = %s ", (sid,)) |
1293
|
|
|
if cursor.fetchone() is None: |
1294
|
|
|
cursor.close() |
1295
|
|
|
cnx.close() |
1296
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1297
|
|
|
description='API.SENSOR_NOT_FOUND') |
1298
|
|
|
|
1299
|
|
|
cursor.execute(" SELECT id " |
1300
|
|
|
" FROM tbl_stores_sensors " |
1301
|
|
|
" WHERE store_id = %s AND sensor_id = %s ", (id_, sid)) |
1302
|
|
|
if cursor.fetchone() is None: |
1303
|
|
|
cursor.close() |
1304
|
|
|
cnx.close() |
1305
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1306
|
|
|
description='API.STORE_SENSOR_RELATION_NOT_FOUND') |
1307
|
|
|
|
1308
|
|
|
cursor.execute(" DELETE FROM tbl_stores_sensors WHERE store_id = %s AND sensor_id = %s ", (id_, sid)) |
1309
|
|
|
cnx.commit() |
1310
|
|
|
|
1311
|
|
|
cursor.close() |
1312
|
|
|
cnx.close() |
1313
|
|
|
|
1314
|
|
|
resp.status = falcon.HTTP_204 |
1315
|
|
|
|
1316
|
|
|
|
1317
|
|
View Code Duplication |
class StoreVirtualMeterCollection: |
|
|
|
|
1318
|
|
|
def __init__(self): |
1319
|
|
|
"""Initializes Class""" |
1320
|
|
|
pass |
1321
|
|
|
|
1322
|
|
|
@staticmethod |
1323
|
|
|
def on_options(req, resp, id_): |
1324
|
|
|
resp.status = falcon.HTTP_200 |
1325
|
|
|
|
1326
|
|
|
@staticmethod |
1327
|
|
|
def on_get(req, resp, id_): |
1328
|
|
|
if 'API-KEY' not in req.headers or \ |
1329
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1330
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1331
|
|
|
access_control(req) |
1332
|
|
|
else: |
1333
|
|
|
api_key_control(req) |
1334
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1335
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1336
|
|
|
description='API.INVALID_STORE_ID') |
1337
|
|
|
|
1338
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1339
|
|
|
cursor = cnx.cursor() |
1340
|
|
|
|
1341
|
|
|
cursor.execute(" SELECT name " |
1342
|
|
|
" FROM tbl_stores " |
1343
|
|
|
" WHERE id = %s ", (id_,)) |
1344
|
|
|
if cursor.fetchone() is None: |
1345
|
|
|
cursor.close() |
1346
|
|
|
cnx.close() |
1347
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1348
|
|
|
description='API.STORE_NOT_FOUND') |
1349
|
|
|
|
1350
|
|
|
query = (" SELECT id, name, uuid " |
1351
|
|
|
" FROM tbl_energy_categories ") |
1352
|
|
|
cursor.execute(query) |
1353
|
|
|
rows_energy_categories = cursor.fetchall() |
1354
|
|
|
|
1355
|
|
|
energy_category_dict = dict() |
1356
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1357
|
|
|
for row in rows_energy_categories: |
1358
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
1359
|
|
|
"name": row[1], |
1360
|
|
|
"uuid": row[2]} |
1361
|
|
|
|
1362
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
1363
|
|
|
" FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " |
1364
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
1365
|
|
|
" ORDER BY m.id ") |
1366
|
|
|
cursor.execute(query, (id_,)) |
1367
|
|
|
rows = cursor.fetchall() |
1368
|
|
|
|
1369
|
|
|
result = list() |
1370
|
|
|
if rows is not None and len(rows) > 0: |
1371
|
|
|
for row in rows: |
1372
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
1373
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
1374
|
|
|
result.append(meta_result) |
1375
|
|
|
|
1376
|
|
|
resp.text = json.dumps(result) |
1377
|
|
|
|
1378
|
|
|
@staticmethod |
1379
|
|
|
@user_logger |
1380
|
|
|
def on_post(req, resp, id_): |
1381
|
|
|
"""Handles POST requests""" |
1382
|
|
|
admin_control(req) |
1383
|
|
|
try: |
1384
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1385
|
|
|
except Exception as ex: |
1386
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1387
|
|
|
title='API.BAD_REQUEST', |
1388
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1389
|
|
|
|
1390
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1391
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1392
|
|
|
description='API.INVALID_STORE_ID') |
1393
|
|
|
|
1394
|
|
|
new_values = json.loads(raw_json) |
1395
|
|
|
|
1396
|
|
|
if 'virtual_meter_id' not in new_values['data'].keys() or \ |
1397
|
|
|
not isinstance(new_values['data']['virtual_meter_id'], int) or \ |
1398
|
|
|
new_values['data']['virtual_meter_id'] <= 0: |
1399
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1400
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1401
|
|
|
virtual_meter_id = new_values['data']['virtual_meter_id'] |
1402
|
|
|
|
1403
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1404
|
|
|
cursor = cnx.cursor() |
1405
|
|
|
|
1406
|
|
|
cursor.execute(" SELECT name " |
1407
|
|
|
" from tbl_stores " |
1408
|
|
|
" WHERE id = %s ", (id_,)) |
1409
|
|
|
if cursor.fetchone() is None: |
1410
|
|
|
cursor.close() |
1411
|
|
|
cnx.close() |
1412
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1413
|
|
|
description='API.STORE_NOT_FOUND') |
1414
|
|
|
|
1415
|
|
|
cursor.execute(" SELECT name " |
1416
|
|
|
" FROM tbl_virtual_meters " |
1417
|
|
|
" WHERE id = %s ", (virtual_meter_id,)) |
1418
|
|
|
if cursor.fetchone() is None: |
1419
|
|
|
cursor.close() |
1420
|
|
|
cnx.close() |
1421
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1422
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1423
|
|
|
|
1424
|
|
|
query = (" SELECT id " |
1425
|
|
|
" FROM tbl_stores_virtual_meters " |
1426
|
|
|
" WHERE store_id = %s AND virtual_meter_id = %s") |
1427
|
|
|
cursor.execute(query, (id_, virtual_meter_id,)) |
1428
|
|
|
if cursor.fetchone() is not None: |
1429
|
|
|
cursor.close() |
1430
|
|
|
cnx.close() |
1431
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1432
|
|
|
description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') |
1433
|
|
|
|
1434
|
|
|
add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " |
1435
|
|
|
" VALUES (%s, %s) ") |
1436
|
|
|
cursor.execute(add_row, (id_, virtual_meter_id,)) |
1437
|
|
|
cnx.commit() |
1438
|
|
|
cursor.close() |
1439
|
|
|
cnx.close() |
1440
|
|
|
|
1441
|
|
|
resp.status = falcon.HTTP_201 |
1442
|
|
|
resp.location = '/stores/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) |
1443
|
|
|
|
1444
|
|
|
|
1445
|
|
View Code Duplication |
class StoreVirtualMeterItem: |
|
|
|
|
1446
|
|
|
def __init__(self): |
1447
|
|
|
"""Initializes Class""" |
1448
|
|
|
pass |
1449
|
|
|
|
1450
|
|
|
@staticmethod |
1451
|
|
|
def on_options(req, resp, id_, mid): |
1452
|
|
|
resp.status = falcon.HTTP_200 |
1453
|
|
|
|
1454
|
|
|
@staticmethod |
1455
|
|
|
@user_logger |
1456
|
|
|
def on_delete(req, resp, id_, mid): |
1457
|
|
|
admin_control(req) |
1458
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1459
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1460
|
|
|
description='API.INVALID_STORE_ID') |
1461
|
|
|
|
1462
|
|
|
if not mid.isdigit() or int(mid) <= 0: |
1463
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1464
|
|
|
description='API.INVALID_VIRTUAL_METER_ID') |
1465
|
|
|
|
1466
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1467
|
|
|
cursor = cnx.cursor() |
1468
|
|
|
|
1469
|
|
|
cursor.execute(" SELECT name " |
1470
|
|
|
" FROM tbl_stores " |
1471
|
|
|
" WHERE id = %s ", (id_,)) |
1472
|
|
|
if cursor.fetchone() is None: |
1473
|
|
|
cursor.close() |
1474
|
|
|
cnx.close() |
1475
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1476
|
|
|
description='API.STORE_NOT_FOUND') |
1477
|
|
|
|
1478
|
|
|
cursor.execute(" SELECT name " |
1479
|
|
|
" FROM tbl_virtual_meters " |
1480
|
|
|
" WHERE id = %s ", (mid,)) |
1481
|
|
|
if cursor.fetchone() is None: |
1482
|
|
|
cursor.close() |
1483
|
|
|
cnx.close() |
1484
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1485
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
1486
|
|
|
|
1487
|
|
|
cursor.execute(" SELECT id " |
1488
|
|
|
" FROM tbl_stores_virtual_meters " |
1489
|
|
|
" WHERE store_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1490
|
|
|
if cursor.fetchone() is None: |
1491
|
|
|
cursor.close() |
1492
|
|
|
cnx.close() |
1493
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1494
|
|
|
description='API.STORE_VIRTUAL_METER_RELATION_NOT_FOUND') |
1495
|
|
|
|
1496
|
|
|
cursor.execute(" DELETE FROM tbl_stores_virtual_meters " |
1497
|
|
|
" WHERE store_id = %s AND virtual_meter_id = %s ", (id_, mid)) |
1498
|
|
|
cnx.commit() |
1499
|
|
|
|
1500
|
|
|
cursor.close() |
1501
|
|
|
cnx.close() |
1502
|
|
|
|
1503
|
|
|
resp.status = falcon.HTTP_204 |
1504
|
|
|
|
1505
|
|
|
|
1506
|
|
View Code Duplication |
class StoreWorkingCalendarCollection: |
|
|
|
|
1507
|
|
|
def __init__(self): |
1508
|
|
|
"""Initializes StoreWorkingCalendarCollection Class""" |
1509
|
|
|
pass |
1510
|
|
|
|
1511
|
|
|
@staticmethod |
1512
|
|
|
def on_options(req, resp, id_): |
1513
|
|
|
resp.status = falcon.HTTP_200 |
1514
|
|
|
|
1515
|
|
|
@staticmethod |
1516
|
|
|
def on_get(req, resp, id_): |
1517
|
|
|
if 'API-KEY' not in req.headers or \ |
1518
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1519
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1520
|
|
|
access_control(req) |
1521
|
|
|
else: |
1522
|
|
|
api_key_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_STORE_ID') |
1526
|
|
|
|
1527
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1528
|
|
|
cursor = cnx.cursor() |
1529
|
|
|
|
1530
|
|
|
cursor.execute(" SELECT name " |
1531
|
|
|
" FROM tbl_stores " |
1532
|
|
|
" WHERE id = %s ", (id_,)) |
1533
|
|
|
if cursor.fetchone() is None: |
1534
|
|
|
cursor.close() |
1535
|
|
|
cnx.close() |
1536
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1537
|
|
|
description='API.STORE_NOT_FOUND') |
1538
|
|
|
|
1539
|
|
|
query = (" SELECT wc.id, wc.name, wc.description " |
1540
|
|
|
" FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
1541
|
|
|
" WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
1542
|
|
|
" ORDER BY wc.id ") |
1543
|
|
|
cursor.execute(query, (id_,)) |
1544
|
|
|
rows = cursor.fetchall() |
1545
|
|
|
|
1546
|
|
|
result = list() |
1547
|
|
|
if rows is not None and len(rows) > 0: |
1548
|
|
|
for row in rows: |
1549
|
|
|
meta_result = {"id": row[0], "name": row[1], "description": row[2]} |
1550
|
|
|
result.append(meta_result) |
1551
|
|
|
|
1552
|
|
|
resp.text = json.dumps(result) |
1553
|
|
|
|
1554
|
|
|
@staticmethod |
1555
|
|
|
@user_logger |
1556
|
|
|
def on_post(req, resp, id_): |
1557
|
|
|
"""Handles POST requests""" |
1558
|
|
|
admin_control(req) |
1559
|
|
|
try: |
1560
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1561
|
|
|
except Exception as ex: |
1562
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1563
|
|
|
title='API.BAD_REQUEST', |
1564
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1565
|
|
|
|
1566
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1567
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1568
|
|
|
description='API.INVALID_STORE_ID') |
1569
|
|
|
|
1570
|
|
|
new_values = json.loads(raw_json) |
1571
|
|
|
|
1572
|
|
|
if 'working_calendar_id' not in new_values['data'].keys() or \ |
1573
|
|
|
not isinstance(new_values['data']['working_calendar_id'], int) or \ |
1574
|
|
|
new_values['data']['working_calendar_id'] <= 0: |
1575
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1576
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
1577
|
|
|
working_calendar_id = new_values['data']['working_calendar_id'] |
1578
|
|
|
|
1579
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1580
|
|
|
cursor = cnx.cursor() |
1581
|
|
|
|
1582
|
|
|
cursor.execute(" SELECT name " |
1583
|
|
|
" from tbl_stores " |
1584
|
|
|
" WHERE id = %s ", (id_,)) |
1585
|
|
|
if cursor.fetchone() is None: |
1586
|
|
|
cursor.close() |
1587
|
|
|
cnx.close() |
1588
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1589
|
|
|
description='API.STORE_NOT_FOUND') |
1590
|
|
|
|
1591
|
|
|
cursor.execute(" SELECT name " |
1592
|
|
|
" FROM tbl_working_calendars " |
1593
|
|
|
" WHERE id = %s ", (working_calendar_id,)) |
1594
|
|
|
if cursor.fetchone() is None: |
1595
|
|
|
cursor.close() |
1596
|
|
|
cnx.close() |
1597
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1598
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
1599
|
|
|
|
1600
|
|
|
query = (" SELECT id " |
1601
|
|
|
" FROM tbl_stores_working_calendars " |
1602
|
|
|
" WHERE store_id = %s AND working_calendar_id = %s") |
1603
|
|
|
cursor.execute(query, (id_, working_calendar_id,)) |
1604
|
|
|
if cursor.fetchone() is not None: |
1605
|
|
|
cursor.close() |
1606
|
|
|
cnx.close() |
1607
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1608
|
|
|
description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
1609
|
|
|
|
1610
|
|
|
add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
1611
|
|
|
" VALUES (%s, %s) ") |
1612
|
|
|
cursor.execute(add_row, (id_, working_calendar_id,)) |
1613
|
|
|
cnx.commit() |
1614
|
|
|
cursor.close() |
1615
|
|
|
cnx.close() |
1616
|
|
|
|
1617
|
|
|
resp.status = falcon.HTTP_201 |
1618
|
|
|
resp.location = '/stores/' + str(id_) + '/workingcalendars/' + str(working_calendar_id) |
1619
|
|
|
|
1620
|
|
|
|
1621
|
|
View Code Duplication |
class StoreWorkingCalendarItem: |
|
|
|
|
1622
|
|
|
def __init__(self): |
1623
|
|
|
"""Initializes StoreWorkingCalendarItem Class""" |
1624
|
|
|
pass |
1625
|
|
|
|
1626
|
|
|
@staticmethod |
1627
|
|
|
def on_options(req, resp, id_, wcid): |
1628
|
|
|
resp.status = falcon.HTTP_200 |
1629
|
|
|
|
1630
|
|
|
@staticmethod |
1631
|
|
|
@user_logger |
1632
|
|
|
def on_delete(req, resp, id_, wcid): |
1633
|
|
|
admin_control(req) |
1634
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1635
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1636
|
|
|
description='API.INVALID_STORE_ID') |
1637
|
|
|
|
1638
|
|
|
if not wcid.isdigit() or int(wcid) <= 0: |
1639
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1640
|
|
|
description='API.INVALID_WORKING_CALENDAR_ID') |
1641
|
|
|
|
1642
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1643
|
|
|
cursor = cnx.cursor() |
1644
|
|
|
|
1645
|
|
|
cursor.execute(" SELECT name " |
1646
|
|
|
" FROM tbl_stores " |
1647
|
|
|
" WHERE id = %s ", (id_,)) |
1648
|
|
|
if cursor.fetchone() is None: |
1649
|
|
|
cursor.close() |
1650
|
|
|
cnx.close() |
1651
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1652
|
|
|
description='API.STORE_NOT_FOUND') |
1653
|
|
|
|
1654
|
|
|
cursor.execute(" SELECT name " |
1655
|
|
|
" FROM tbl_working_calendars " |
1656
|
|
|
" WHERE id = %s ", (wcid,)) |
1657
|
|
|
if cursor.fetchone() is None: |
1658
|
|
|
cursor.close() |
1659
|
|
|
cnx.close() |
1660
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1661
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
1662
|
|
|
|
1663
|
|
|
cursor.execute(" SELECT id " |
1664
|
|
|
" FROM tbl_stores_working_calendars " |
1665
|
|
|
" WHERE store_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
1666
|
|
|
if cursor.fetchone() is None: |
1667
|
|
|
cursor.close() |
1668
|
|
|
cnx.close() |
1669
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1670
|
|
|
description='API.STORE_WORKING_CALENDAR_RELATION_NOT_FOUND') |
1671
|
|
|
|
1672
|
|
|
cursor.execute(" DELETE FROM tbl_stores_working_calendars " |
1673
|
|
|
" WHERE store_id = %s AND working_calendar_id = %s ", (id_, wcid)) |
1674
|
|
|
cnx.commit() |
1675
|
|
|
|
1676
|
|
|
cursor.close() |
1677
|
|
|
cnx.close() |
1678
|
|
|
|
1679
|
|
|
resp.status = falcon.HTTP_204 |
1680
|
|
|
|
1681
|
|
|
|
1682
|
|
View Code Duplication |
class StoreCommandCollection: |
|
|
|
|
1683
|
|
|
def __init__(self): |
1684
|
|
|
"""Initializes Class""" |
1685
|
|
|
pass |
1686
|
|
|
|
1687
|
|
|
@staticmethod |
1688
|
|
|
def on_options(req, resp, id_): |
1689
|
|
|
resp.status = falcon.HTTP_200 |
1690
|
|
|
|
1691
|
|
|
@staticmethod |
1692
|
|
|
def on_get(req, resp, id_): |
1693
|
|
|
if 'API-KEY' not in req.headers or \ |
1694
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1695
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1696
|
|
|
access_control(req) |
1697
|
|
|
else: |
1698
|
|
|
api_key_control(req) |
1699
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1700
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1701
|
|
|
description='API.INVALID_STORE_ID') |
1702
|
|
|
|
1703
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1704
|
|
|
cursor = cnx.cursor() |
1705
|
|
|
|
1706
|
|
|
cursor.execute(" SELECT name " |
1707
|
|
|
" FROM tbl_stores " |
1708
|
|
|
" WHERE id = %s ", (id_,)) |
1709
|
|
|
if cursor.fetchone() is None: |
1710
|
|
|
cursor.close() |
1711
|
|
|
cnx.close() |
1712
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1713
|
|
|
description='API.STORE_NOT_FOUND') |
1714
|
|
|
|
1715
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
1716
|
|
|
" FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
1717
|
|
|
" WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
1718
|
|
|
" ORDER BY c.id ") |
1719
|
|
|
cursor.execute(query, (id_,)) |
1720
|
|
|
rows = cursor.fetchall() |
1721
|
|
|
|
1722
|
|
|
result = list() |
1723
|
|
|
if rows is not None and len(rows) > 0: |
1724
|
|
|
for row in rows: |
1725
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
1726
|
|
|
result.append(meta_result) |
1727
|
|
|
|
1728
|
|
|
resp.text = json.dumps(result) |
1729
|
|
|
|
1730
|
|
|
@staticmethod |
1731
|
|
|
@user_logger |
1732
|
|
|
def on_post(req, resp, id_): |
1733
|
|
|
"""Handles POST requests""" |
1734
|
|
|
admin_control(req) |
1735
|
|
|
try: |
1736
|
|
|
raw_json = req.stream.read().decode('utf-8') |
1737
|
|
|
except Exception as ex: |
1738
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
1739
|
|
|
title='API.BAD_REQUEST', |
1740
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
1741
|
|
|
|
1742
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1743
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1744
|
|
|
description='API.INVALID_STORE_ID') |
1745
|
|
|
|
1746
|
|
|
new_values = json.loads(raw_json) |
1747
|
|
|
|
1748
|
|
|
if 'command_id' not in new_values['data'].keys() or \ |
1749
|
|
|
not isinstance(new_values['data']['command_id'], int) or \ |
1750
|
|
|
new_values['data']['command_id'] <= 0: |
1751
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1752
|
|
|
description='API.INVALID_COMMAND_ID') |
1753
|
|
|
command_id = new_values['data']['command_id'] |
1754
|
|
|
|
1755
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1756
|
|
|
cursor = cnx.cursor() |
1757
|
|
|
|
1758
|
|
|
cursor.execute(" SELECT name " |
1759
|
|
|
" from tbl_stores " |
1760
|
|
|
" WHERE id = %s ", (id_,)) |
1761
|
|
|
if cursor.fetchone() is None: |
1762
|
|
|
cursor.close() |
1763
|
|
|
cnx.close() |
1764
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1765
|
|
|
description='API.STORE_NOT_FOUND') |
1766
|
|
|
|
1767
|
|
|
cursor.execute(" SELECT name " |
1768
|
|
|
" FROM tbl_commands " |
1769
|
|
|
" WHERE id = %s ", (command_id,)) |
1770
|
|
|
if cursor.fetchone() is None: |
1771
|
|
|
cursor.close() |
1772
|
|
|
cnx.close() |
1773
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1774
|
|
|
description='API.COMMAND_NOT_FOUND') |
1775
|
|
|
|
1776
|
|
|
query = (" SELECT id " |
1777
|
|
|
" FROM tbl_stores_commands " |
1778
|
|
|
" WHERE store_id = %s AND command_id = %s") |
1779
|
|
|
cursor.execute(query, (id_, command_id,)) |
1780
|
|
|
if cursor.fetchone() is not None: |
1781
|
|
|
cursor.close() |
1782
|
|
|
cnx.close() |
1783
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
1784
|
|
|
description='API.STORE_COMMAND_RELATION_EXISTS') |
1785
|
|
|
|
1786
|
|
|
add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
1787
|
|
|
" VALUES (%s, %s) ") |
1788
|
|
|
cursor.execute(add_row, (id_, command_id,)) |
1789
|
|
|
cnx.commit() |
1790
|
|
|
cursor.close() |
1791
|
|
|
cnx.close() |
1792
|
|
|
|
1793
|
|
|
resp.status = falcon.HTTP_201 |
1794
|
|
|
resp.location = '/stores/' + str(id_) + '/commands/' + str(command_id) |
1795
|
|
|
|
1796
|
|
|
|
1797
|
|
View Code Duplication |
class StoreCommandItem: |
|
|
|
|
1798
|
|
|
def __init__(self): |
1799
|
|
|
"""Initializes Class""" |
1800
|
|
|
pass |
1801
|
|
|
|
1802
|
|
|
@staticmethod |
1803
|
|
|
def on_options(req, resp, id_, cid): |
1804
|
|
|
resp.status = falcon.HTTP_200 |
1805
|
|
|
|
1806
|
|
|
@staticmethod |
1807
|
|
|
@user_logger |
1808
|
|
|
def on_delete(req, resp, id_, cid): |
1809
|
|
|
admin_control(req) |
1810
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1811
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1812
|
|
|
description='API.INVALID_STORE_ID') |
1813
|
|
|
|
1814
|
|
|
if not cid.isdigit() or int(cid) <= 0: |
1815
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1816
|
|
|
description='API.INVALID_COMMAND_ID') |
1817
|
|
|
|
1818
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1819
|
|
|
cursor = cnx.cursor() |
1820
|
|
|
|
1821
|
|
|
cursor.execute(" SELECT name " |
1822
|
|
|
" FROM tbl_stores " |
1823
|
|
|
" WHERE id = %s ", (id_,)) |
1824
|
|
|
if cursor.fetchone() is None: |
1825
|
|
|
cursor.close() |
1826
|
|
|
cnx.close() |
1827
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1828
|
|
|
description='API.STORE_NOT_FOUND') |
1829
|
|
|
|
1830
|
|
|
cursor.execute(" SELECT name " |
1831
|
|
|
" FROM tbl_commands " |
1832
|
|
|
" WHERE id = %s ", (cid,)) |
1833
|
|
|
if cursor.fetchone() is None: |
1834
|
|
|
cursor.close() |
1835
|
|
|
cnx.close() |
1836
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1837
|
|
|
description='API.COMMAND_NOT_FOUND') |
1838
|
|
|
|
1839
|
|
|
cursor.execute(" SELECT id " |
1840
|
|
|
" FROM tbl_stores_commands " |
1841
|
|
|
" WHERE store_id = %s AND command_id = %s ", (id_, cid)) |
1842
|
|
|
if cursor.fetchone() is None: |
1843
|
|
|
cursor.close() |
1844
|
|
|
cnx.close() |
1845
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1846
|
|
|
description='API.STORE_COMMAND_RELATION_NOT_FOUND') |
1847
|
|
|
|
1848
|
|
|
cursor.execute(" DELETE FROM tbl_stores_commands WHERE store_id = %s AND command_id = %s ", (id_, cid)) |
1849
|
|
|
cnx.commit() |
1850
|
|
|
|
1851
|
|
|
cursor.close() |
1852
|
|
|
cnx.close() |
1853
|
|
|
|
1854
|
|
|
resp.status = falcon.HTTP_204 |
1855
|
|
|
|
1856
|
|
|
|
1857
|
|
|
class StoreExport: |
1858
|
|
|
def __init__(self): |
1859
|
|
|
""""Initializes StoreExport""" |
1860
|
|
|
pass |
1861
|
|
|
|
1862
|
|
|
@staticmethod |
1863
|
|
|
def on_options(req, resp, id_): |
1864
|
|
|
resp.status = falcon.HTTP_200 |
1865
|
|
|
|
1866
|
|
|
@staticmethod |
1867
|
|
|
def on_get(req, resp, id_): |
1868
|
|
|
if 'API-KEY' not in req.headers or \ |
1869
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
1870
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
1871
|
|
|
access_control(req) |
1872
|
|
|
else: |
1873
|
|
|
api_key_control(req) |
1874
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
1875
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
1876
|
|
|
description='API.INVALID_STORE_ID') |
1877
|
|
|
|
1878
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
1879
|
|
|
cursor = cnx.cursor() |
1880
|
|
|
|
1881
|
|
|
query = (" SELECT id, name, uuid " |
1882
|
|
|
" FROM tbl_store_types ") |
1883
|
|
|
cursor.execute(query) |
1884
|
|
|
rows_store_types = cursor.fetchall() |
1885
|
|
|
|
1886
|
|
|
store_type_dict = dict() |
1887
|
|
|
if rows_store_types is not None and len(rows_store_types) > 0: |
1888
|
|
|
for row in rows_store_types: |
1889
|
|
|
store_type_dict[row[0]] = {"id": row[0], |
1890
|
|
|
"name": row[1], |
1891
|
|
|
"uuid": row[2]} |
1892
|
|
|
|
1893
|
|
|
query = (" SELECT id, name, uuid " |
1894
|
|
|
" FROM tbl_contacts ") |
1895
|
|
|
cursor.execute(query) |
1896
|
|
|
rows_contacts = cursor.fetchall() |
1897
|
|
|
|
1898
|
|
|
contact_dict = dict() |
1899
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
1900
|
|
|
for row in rows_contacts: |
1901
|
|
|
contact_dict[row[0]] = {"id": row[0], |
1902
|
|
|
"name": row[1], |
1903
|
|
|
"uuid": row[2]} |
1904
|
|
|
|
1905
|
|
|
query = (" SELECT id, name, uuid " |
1906
|
|
|
" FROM tbl_cost_centers ") |
1907
|
|
|
cursor.execute(query) |
1908
|
|
|
rows_cost_centers = cursor.fetchall() |
1909
|
|
|
|
1910
|
|
|
cost_center_dict = dict() |
1911
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
1912
|
|
|
for row in rows_cost_centers: |
1913
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
1914
|
|
|
"name": row[1], |
1915
|
|
|
"uuid": row[2]} |
1916
|
|
|
|
1917
|
|
|
query = (" SELECT id, name, uuid, " |
1918
|
|
|
" address, latitude, longitude, area, store_type_id," |
1919
|
|
|
" is_input_counted, " |
1920
|
|
|
" contact_id, cost_center_id, description " |
1921
|
|
|
" FROM tbl_stores " |
1922
|
|
|
" WHERE id = %s ") |
1923
|
|
|
cursor.execute(query, (id_,)) |
1924
|
|
|
row = cursor.fetchone() |
1925
|
|
|
|
1926
|
|
|
if row is None: |
1927
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
1928
|
|
|
description='API.STORE_NOT_FOUND') |
1929
|
|
|
else: |
1930
|
|
|
meta_result = {"id": row[0], |
1931
|
|
|
"name": row[1], |
1932
|
|
|
"uuid": row[2], |
1933
|
|
|
"address": row[3], |
1934
|
|
|
"latitude": row[4], |
1935
|
|
|
"longitude": row[5], |
1936
|
|
|
"area": row[6], |
1937
|
|
|
"store_type": store_type_dict.get(row[7], None), |
1938
|
|
|
"is_input_counted": bool(row[8]), |
1939
|
|
|
"contact": contact_dict.get(row[9], None), |
1940
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
1941
|
|
|
"description": row[11], |
1942
|
|
|
"commands": None, |
1943
|
|
|
"meters": None, |
1944
|
|
|
"offline_meters": None, |
1945
|
|
|
"virtual_meters": None, |
1946
|
|
|
"points": None, |
1947
|
|
|
"sensors": None, |
1948
|
|
|
"working_calendars": None |
1949
|
|
|
} |
1950
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
1951
|
|
|
" FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
1952
|
|
|
" WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
1953
|
|
|
" ORDER BY c.id ") |
1954
|
|
|
cursor.execute(query, (id_,)) |
1955
|
|
|
rows = cursor.fetchall() |
1956
|
|
|
|
1957
|
|
|
command_result = list() |
1958
|
|
|
if rows is not None and len(rows) > 0: |
1959
|
|
|
for row in rows: |
1960
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
1961
|
|
|
command_result.append(result) |
1962
|
|
|
meta_result['commands'] = command_result |
1963
|
|
|
|
1964
|
|
|
query = (" SELECT id, name, uuid " |
1965
|
|
|
" FROM tbl_energy_categories ") |
1966
|
|
|
cursor.execute(query) |
1967
|
|
|
rows_energy_categories = cursor.fetchall() |
1968
|
|
|
|
1969
|
|
|
energy_category_dict = dict() |
1970
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1971
|
|
|
for row in rows_energy_categories: |
1972
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
1973
|
|
|
"name": row[1], |
1974
|
|
|
"uuid": row[2]} |
1975
|
|
|
|
1976
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
1977
|
|
|
" FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " |
1978
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
1979
|
|
|
" ORDER BY m.id ") |
1980
|
|
|
cursor.execute(query, (id_,)) |
1981
|
|
|
rows = cursor.fetchall() |
1982
|
|
|
|
1983
|
|
|
meter_result = list() |
1984
|
|
|
if rows is not None and len(rows) > 0: |
1985
|
|
|
for row in rows: |
1986
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], |
1987
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
1988
|
|
|
meter_result.append(result) |
1989
|
|
|
meta_result['meters'] = meter_result |
1990
|
|
|
|
1991
|
|
|
query = (" SELECT id, name, uuid " |
1992
|
|
|
" FROM tbl_energy_categories ") |
1993
|
|
|
cursor.execute(query) |
1994
|
|
|
rows_energy_categories = cursor.fetchall() |
1995
|
|
|
|
1996
|
|
|
energy_category_dict = dict() |
1997
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
1998
|
|
|
for row in rows_energy_categories: |
1999
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
2000
|
|
|
"name": row[1], |
2001
|
|
|
"uuid": row[2]} |
2002
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
2003
|
|
|
" FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " |
2004
|
|
|
" WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
2005
|
|
|
" ORDER BY m.id ") |
2006
|
|
|
cursor.execute(query, (id_,)) |
2007
|
|
|
rows = cursor.fetchall() |
2008
|
|
|
|
2009
|
|
|
offlinemeter_result = list() |
2010
|
|
|
if rows is not None and len(rows) > 0: |
2011
|
|
|
for row in rows: |
2012
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], |
2013
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
2014
|
|
|
offlinemeter_result.append(result) |
2015
|
|
|
meta_result['offline_meters'] = offlinemeter_result |
2016
|
|
|
query = (" SELECT id, name, uuid " |
2017
|
|
|
" FROM tbl_energy_categories ") |
2018
|
|
|
cursor.execute(query) |
2019
|
|
|
rows_energy_categories = cursor.fetchall() |
2020
|
|
|
|
2021
|
|
|
energy_category_dict = dict() |
2022
|
|
|
if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
2023
|
|
|
for row in rows_energy_categories: |
2024
|
|
|
energy_category_dict[row[0]] = {"id": row[0], |
2025
|
|
|
"name": row[1], |
2026
|
|
|
"uuid": row[2]} |
2027
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
2028
|
|
|
" FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " |
2029
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
2030
|
|
|
" ORDER BY m.id ") |
2031
|
|
|
cursor.execute(query, (id_,)) |
2032
|
|
|
rows = cursor.fetchall() |
2033
|
|
|
|
2034
|
|
|
virtualmeter_result = list() |
2035
|
|
|
if rows is not None and len(rows) > 0: |
2036
|
|
|
for row in rows: |
2037
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], |
2038
|
|
|
"energy_category": energy_category_dict.get(row[3], None)} |
2039
|
|
|
virtualmeter_result.append(result) |
2040
|
|
|
meta_result['virtual_meters'] = virtualmeter_result |
2041
|
|
|
query = (" SELECT id, name, uuid " |
2042
|
|
|
" FROM tbl_data_sources ") |
2043
|
|
|
cursor.execute(query) |
2044
|
|
|
rows_data_sources = cursor.fetchall() |
2045
|
|
|
|
2046
|
|
|
data_source_dict = dict() |
2047
|
|
|
if rows_data_sources is not None and len(rows_data_sources) > 0: |
2048
|
|
|
for row in rows_data_sources: |
2049
|
|
|
data_source_dict[row[0]] = {"id": row[0], |
2050
|
|
|
"name": row[1], |
2051
|
|
|
"uuid": row[2]} |
2052
|
|
|
|
2053
|
|
|
query = (" SELECT p.id, p.name, p.data_source_id " |
2054
|
|
|
" FROM tbl_stores t, tbl_stores_points tp, tbl_points p " |
2055
|
|
|
" WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " |
2056
|
|
|
" ORDER BY p.id ") |
2057
|
|
|
cursor.execute(query, (id_,)) |
2058
|
|
|
rows = cursor.fetchall() |
2059
|
|
|
|
2060
|
|
|
point_result = list() |
2061
|
|
|
if rows is not None and len(rows) > 0: |
2062
|
|
|
for row in rows: |
2063
|
|
|
result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
2064
|
|
|
point_result.append(result) |
2065
|
|
|
meta_result['points'] = point_result |
2066
|
|
|
query = (" SELECT s.id, s.name, s.uuid " |
2067
|
|
|
" FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
2068
|
|
|
" WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
2069
|
|
|
" ORDER BY s.id ") |
2070
|
|
|
cursor.execute(query, (id_,)) |
2071
|
|
|
rows = cursor.fetchall() |
2072
|
|
|
|
2073
|
|
|
sensor_result = list() |
2074
|
|
|
if rows is not None and len(rows) > 0: |
2075
|
|
|
for row in rows: |
2076
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2077
|
|
|
sensor_result.append(result) |
2078
|
|
|
meta_result['sensors'] = sensor_result |
2079
|
|
|
query = (" SELECT wc.id, wc.name, wc.description " |
2080
|
|
|
" FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
2081
|
|
|
" WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
2082
|
|
|
" ORDER BY wc.id ") |
2083
|
|
|
cursor.execute(query, (id_,)) |
2084
|
|
|
rows = cursor.fetchall() |
2085
|
|
|
|
2086
|
|
|
workingcalendar_result = list() |
2087
|
|
|
if rows is not None and len(rows) > 0: |
2088
|
|
|
for row in rows: |
2089
|
|
|
result = {"id": row[0], "name": row[1], "description": row[2]} |
2090
|
|
|
workingcalendar_result.append(result) |
2091
|
|
|
meta_result['working_calendar'] = workingcalendar_result |
2092
|
|
|
|
2093
|
|
|
cursor.close() |
2094
|
|
|
cnx.close() |
2095
|
|
|
resp.text = json.dumps(meta_result) |
2096
|
|
|
|
2097
|
|
|
|
2098
|
|
|
class StoreImport: |
2099
|
|
|
def __init__(self): |
2100
|
|
|
""""Initializes StoreImport""" |
2101
|
|
|
pass |
2102
|
|
|
|
2103
|
|
|
@staticmethod |
2104
|
|
|
def on_options(req, resp): |
2105
|
|
|
resp.status = falcon.HTTP_200 |
2106
|
|
|
|
2107
|
|
|
@staticmethod |
2108
|
|
|
@user_logger |
2109
|
|
|
def on_post(req, resp): |
2110
|
|
|
"""Handles POST requests""" |
2111
|
|
|
admin_control(req) |
2112
|
|
|
try: |
2113
|
|
|
raw_json = req.stream.read().decode('utf-8') |
2114
|
|
|
except Exception as ex: |
2115
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
2116
|
|
|
title='API.BAD_REQUEST', |
2117
|
|
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
2118
|
|
|
|
2119
|
|
|
new_values = json.loads(raw_json) |
2120
|
|
|
|
2121
|
|
|
if 'name' not in new_values.keys() or \ |
2122
|
|
|
not isinstance(new_values['name'], str) or \ |
2123
|
|
|
len(str.strip(new_values['name'])) == 0: |
2124
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2125
|
|
|
description='API.INVALID_STORE_NAME') |
2126
|
|
|
name = str.strip(new_values['name']) |
2127
|
|
|
|
2128
|
|
|
if 'address' not in new_values.keys() or \ |
2129
|
|
|
not isinstance(new_values['address'], str) or \ |
2130
|
|
|
len(str.strip(new_values['address'])) == 0: |
2131
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2132
|
|
|
description='API.INVALID_ADDRESS_VALUE') |
2133
|
|
|
address = str.strip(new_values['address']) |
2134
|
|
|
|
2135
|
|
|
if 'latitude' not in new_values.keys() or \ |
2136
|
|
|
not (isinstance(new_values['latitude'], float) or |
2137
|
|
|
isinstance(new_values['latitude'], int)) or \ |
2138
|
|
|
new_values['latitude'] < -90.0 or \ |
2139
|
|
|
new_values['latitude'] > 90.0: |
2140
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2141
|
|
|
description='API.INVALID_LATITUDE_VALUE') |
2142
|
|
|
latitude = new_values['latitude'] |
2143
|
|
|
|
2144
|
|
|
if 'longitude' not in new_values.keys() or \ |
2145
|
|
|
not (isinstance(new_values['longitude'], float) or |
2146
|
|
|
isinstance(new_values['longitude'], int)) or \ |
2147
|
|
|
new_values['longitude'] < -180.0 or \ |
2148
|
|
|
new_values['longitude'] > 180.0: |
2149
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2150
|
|
|
description='API.INVALID_LONGITUDE_VALUE') |
2151
|
|
|
longitude = new_values['longitude'] |
2152
|
|
|
|
2153
|
|
|
if 'area' not in new_values.keys() or \ |
2154
|
|
|
not (isinstance(new_values['area'], float) or |
2155
|
|
|
isinstance(new_values['area'], int)) or \ |
2156
|
|
|
new_values['area'] <= 0.0: |
2157
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2158
|
|
|
description='API.INVALID_AREA_VALUE') |
2159
|
|
|
area = new_values['area'] |
2160
|
|
|
|
2161
|
|
|
if 'id' not in new_values['store_type'].keys() or \ |
2162
|
|
|
not isinstance(new_values['store_type']['id'], int) or \ |
2163
|
|
|
new_values['store_type']['id'] <= 0: |
2164
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2165
|
|
|
description='API.INVALID_STORE_TYPE_ID') |
2166
|
|
|
store_type_id = new_values['store_type']['id'] |
2167
|
|
|
|
2168
|
|
|
if 'is_input_counted' not in new_values.keys() or \ |
2169
|
|
|
not isinstance(new_values['is_input_counted'], bool): |
2170
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2171
|
|
|
description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
2172
|
|
|
is_input_counted = new_values['is_input_counted'] |
2173
|
|
|
|
2174
|
|
|
if 'id' not in new_values['contact'].keys() or \ |
2175
|
|
|
not isinstance(new_values['contact']['id'], int) or \ |
2176
|
|
|
new_values['contact']['id'] <= 0: |
2177
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2178
|
|
|
description='API.INVALID_CONTACT_ID') |
2179
|
|
|
contact_id = new_values['contact']['id'] |
2180
|
|
|
|
2181
|
|
|
if 'id' not in new_values['cost_center'].keys() or \ |
2182
|
|
|
not isinstance(new_values['cost_center']['id'], int) or \ |
2183
|
|
|
new_values['cost_center']['id'] <= 0: |
2184
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2185
|
|
|
description='API.INVALID_COST_CENTER_ID') |
2186
|
|
|
cost_center_id = new_values['cost_center']['id'] |
2187
|
|
|
|
2188
|
|
|
if 'description' in new_values.keys() and \ |
2189
|
|
|
new_values['description'] is not None and \ |
2190
|
|
|
len(str(new_values['description'])) > 0: |
2191
|
|
|
description = str.strip(new_values['description']) |
2192
|
|
|
else: |
2193
|
|
|
description = None |
2194
|
|
|
|
2195
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2196
|
|
|
cursor = cnx.cursor() |
2197
|
|
|
|
2198
|
|
|
cursor.execute(" SELECT name " |
2199
|
|
|
" FROM tbl_stores " |
2200
|
|
|
" WHERE name = %s ", (name,)) |
2201
|
|
|
if cursor.fetchone() is not None: |
2202
|
|
|
cursor.close() |
2203
|
|
|
cnx.close() |
2204
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2205
|
|
|
description='API.STORE_NAME_IS_ALREADY_IN_USE') |
2206
|
|
|
|
2207
|
|
|
cursor.execute(" SELECT name " |
2208
|
|
|
" FROM tbl_store_types " |
2209
|
|
|
" WHERE id = %s ", |
2210
|
|
|
(store_type_id,)) |
2211
|
|
|
if cursor.fetchone() is None: |
2212
|
|
|
cursor.close() |
2213
|
|
|
cnx.close() |
2214
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2215
|
|
|
description='API.STORE_TYPE_NOT_FOUND') |
2216
|
|
|
|
2217
|
|
|
cursor.execute(" SELECT name " |
2218
|
|
|
" FROM tbl_contacts " |
2219
|
|
|
" WHERE id = %s ", |
2220
|
|
|
(new_values['contact']['id'],)) |
2221
|
|
|
row = cursor.fetchone() |
2222
|
|
|
if row is None: |
2223
|
|
|
cursor.close() |
2224
|
|
|
cnx.close() |
2225
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2226
|
|
|
description='API.CONTACT_NOT_FOUND') |
2227
|
|
|
|
2228
|
|
|
cursor.execute(" SELECT name " |
2229
|
|
|
" FROM tbl_cost_centers " |
2230
|
|
|
" WHERE id = %s ", |
2231
|
|
|
(new_values['cost_center']['id'],)) |
2232
|
|
|
row = cursor.fetchone() |
2233
|
|
|
if row is None: |
2234
|
|
|
cursor.close() |
2235
|
|
|
cnx.close() |
2236
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2237
|
|
|
description='API.COST_CENTER_NOT_FOUND') |
2238
|
|
|
|
2239
|
|
|
add_values = (" INSERT INTO tbl_stores " |
2240
|
|
|
" (name, uuid, address, latitude, longitude, area, store_type_id, " |
2241
|
|
|
" is_input_counted, " |
2242
|
|
|
" contact_id, cost_center_id, description) " |
2243
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
2244
|
|
|
cursor.execute(add_values, (name, |
2245
|
|
|
str(uuid.uuid4()), |
2246
|
|
|
address, |
2247
|
|
|
latitude, |
2248
|
|
|
longitude, |
2249
|
|
|
area, |
2250
|
|
|
store_type_id, |
2251
|
|
|
is_input_counted, |
2252
|
|
|
contact_id, |
2253
|
|
|
cost_center_id, |
2254
|
|
|
description)) |
2255
|
|
|
new_id = cursor.lastrowid |
2256
|
|
|
if new_values['commands'] is not None and len(new_values['commands']) > 0: |
2257
|
|
|
for command in new_values['commands']: |
2258
|
|
|
cursor.execute(" SELECT name " |
2259
|
|
|
" FROM tbl_commands " |
2260
|
|
|
" WHERE id = %s ", (command['id'],)) |
2261
|
|
|
if cursor.fetchone() is None: |
2262
|
|
|
cursor.close() |
2263
|
|
|
cnx.close() |
2264
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2265
|
|
|
description='API.COMMAND_NOT_FOUND') |
2266
|
|
|
|
2267
|
|
|
query = (" SELECT id " |
2268
|
|
|
" FROM tbl_stores_commands " |
2269
|
|
|
" WHERE store_id = %s AND command_id = %s") |
2270
|
|
|
cursor.execute(query, (new_id, command['id'],)) |
2271
|
|
|
if cursor.fetchone() is not None: |
2272
|
|
|
cursor.close() |
2273
|
|
|
cnx.close() |
2274
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2275
|
|
|
description='API.STORE_COMMAND_RELATION_EXISTS') |
2276
|
|
|
|
2277
|
|
|
add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
2278
|
|
|
" VALUES (%s, %s) ") |
2279
|
|
|
cursor.execute(add_row, (new_id, command['id'],)) |
2280
|
|
|
if new_values['meters'] is not None and len(new_values['meters']) > 0: |
2281
|
|
|
for meter in new_values['meters']: |
2282
|
|
|
cursor.execute(" SELECT name " |
2283
|
|
|
" FROM tbl_meters " |
2284
|
|
|
" WHERE id = %s ", (meter['id'],)) |
2285
|
|
|
if cursor.fetchone() is None: |
2286
|
|
|
cursor.close() |
2287
|
|
|
cnx.close() |
2288
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2289
|
|
|
description='API.METER_NOT_FOUND') |
2290
|
|
|
|
2291
|
|
|
query = (" SELECT id " |
2292
|
|
|
" FROM tbl_stores_meters " |
2293
|
|
|
" WHERE store_id = %s AND meter_id = %s") |
2294
|
|
|
cursor.execute(query, (new_id, meter['id'],)) |
2295
|
|
|
if cursor.fetchone() is not None: |
2296
|
|
|
cursor.close() |
2297
|
|
|
cnx.close() |
2298
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2299
|
|
|
description='API.STORE_METER_RELATION_EXISTS') |
2300
|
|
|
|
2301
|
|
|
add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " |
2302
|
|
|
" VALUES (%s, %s) ") |
2303
|
|
|
cursor.execute(add_row, (new_id, meter['id'],)) |
2304
|
|
|
if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: |
2305
|
|
|
for offline_meter in new_values['offline_meters']: |
2306
|
|
|
cursor.execute(" SELECT name " |
2307
|
|
|
" FROM tbl_offline_meters " |
2308
|
|
|
" WHERE id = %s ", (offline_meter['id'],)) |
2309
|
|
|
if cursor.fetchone() is None: |
2310
|
|
|
cursor.close() |
2311
|
|
|
cnx.close() |
2312
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2313
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
2314
|
|
|
|
2315
|
|
|
query = (" SELECT id " |
2316
|
|
|
" FROM tbl_stores_offline_meters " |
2317
|
|
|
" WHERE store_id = %s AND offline_meter_id = %s") |
2318
|
|
|
cursor.execute(query, (new_id, offline_meter['id'],)) |
2319
|
|
|
if cursor.fetchone() is not None: |
2320
|
|
|
cursor.close() |
2321
|
|
|
cnx.close() |
2322
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2323
|
|
|
description='API.STORE_OFFLINE_METER_RELATION_EXISTS') |
2324
|
|
|
|
2325
|
|
|
add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " |
2326
|
|
|
" VALUES (%s, %s) ") |
2327
|
|
|
cursor.execute(add_row, (new_id, offline_meter['id'],)) |
2328
|
|
|
if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: |
2329
|
|
|
for virtual_meter in new_values['virtual_meters']: |
2330
|
|
|
cursor.execute(" SELECT name " |
2331
|
|
|
" FROM tbl_virtual_meters " |
2332
|
|
|
" WHERE id = %s ", (virtual_meter['id'],)) |
2333
|
|
|
if cursor.fetchone() is None: |
2334
|
|
|
cursor.close() |
2335
|
|
|
cnx.close() |
2336
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2337
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
2338
|
|
|
|
2339
|
|
|
query = (" SELECT id " |
2340
|
|
|
" FROM tbl_stores_virtual_meters " |
2341
|
|
|
" WHERE store_id = %s AND virtual_meter_id = %s") |
2342
|
|
|
cursor.execute(query, (new_id, virtual_meter['id'],)) |
2343
|
|
|
if cursor.fetchone() is not None: |
2344
|
|
|
cursor.close() |
2345
|
|
|
cnx.close() |
2346
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2347
|
|
|
description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') |
2348
|
|
|
|
2349
|
|
|
add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " |
2350
|
|
|
" VALUES (%s, %s) ") |
2351
|
|
|
cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
2352
|
|
|
if new_values['points'] is not None and len(new_values['points']) > 0: |
2353
|
|
|
for point in new_values['points']: |
2354
|
|
|
cursor.execute(" SELECT name " |
2355
|
|
|
" FROM tbl_points " |
2356
|
|
|
" WHERE id = %s ", (point['id'],)) |
2357
|
|
|
if cursor.fetchone() is None: |
2358
|
|
|
cursor.close() |
2359
|
|
|
cnx.close() |
2360
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2361
|
|
|
description='API.POINT_NOT_FOUND') |
2362
|
|
|
|
2363
|
|
|
query = (" SELECT id " |
2364
|
|
|
" FROM tbl_stores_points " |
2365
|
|
|
" WHERE store_id = %s AND point_id = %s") |
2366
|
|
|
cursor.execute(query, (new_id, point['id'],)) |
2367
|
|
|
if cursor.fetchone() is not None: |
2368
|
|
|
cursor.close() |
2369
|
|
|
cnx.close() |
2370
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2371
|
|
|
description='API.STORE_POINT_RELATION_EXISTS') |
2372
|
|
|
|
2373
|
|
|
add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " |
2374
|
|
|
" VALUES (%s, %s) ") |
2375
|
|
|
cursor.execute(add_row, (new_id, point['id'],)) |
2376
|
|
|
if new_values['sensors'] is not None and len(new_values['sensors']) > 0: |
2377
|
|
|
for sensor in new_values['sensors']: |
2378
|
|
|
cursor.execute(" SELECT name " |
2379
|
|
|
" FROM tbl_sensors " |
2380
|
|
|
" WHERE id = %s ", (sensor['id'],)) |
2381
|
|
|
if cursor.fetchone() is None: |
2382
|
|
|
cursor.close() |
2383
|
|
|
cnx.close() |
2384
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2385
|
|
|
description='API.SENSOR_NOT_FOUND') |
2386
|
|
|
|
2387
|
|
|
query = (" SELECT id " |
2388
|
|
|
" FROM tbl_stores_sensors " |
2389
|
|
|
" WHERE store_id = %s AND sensor_id = %s") |
2390
|
|
|
cursor.execute(query, (new_id, sensor['id'],)) |
2391
|
|
|
if cursor.fetchone() is not None: |
2392
|
|
|
cursor.close() |
2393
|
|
|
cnx.close() |
2394
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2395
|
|
|
description='API.STORE_SENSOR_RELATION_EXISTS') |
2396
|
|
|
|
2397
|
|
|
add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
2398
|
|
|
" VALUES (%s, %s) ") |
2399
|
|
|
cursor.execute(add_row, (new_id, sensor['id'],)) |
2400
|
|
|
if new_values['working_calendars'] is not None and len(new_values['working_calendars']) > 0: |
2401
|
|
|
for working_calendar in new_values['working_calendars']: |
2402
|
|
|
cursor.execute(" SELECT name " |
2403
|
|
|
" FROM tbl_working_calendars " |
2404
|
|
|
" WHERE id = %s ", (working_calendar['id'],)) |
2405
|
|
|
if cursor.fetchone() is None: |
2406
|
|
|
cursor.close() |
2407
|
|
|
cnx.close() |
2408
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2409
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
2410
|
|
|
|
2411
|
|
|
query = (" SELECT id " |
2412
|
|
|
" FROM tbl_stores_working_calendars " |
2413
|
|
|
" WHERE store_id = %s AND working_calendar_id = %s") |
2414
|
|
|
cursor.execute(query, (new_id, working_calendar['id'],)) |
2415
|
|
|
if cursor.fetchone() is not None: |
2416
|
|
|
cursor.close() |
2417
|
|
|
cnx.close() |
2418
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2419
|
|
|
description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
2420
|
|
|
|
2421
|
|
|
add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
2422
|
|
|
" VALUES (%s, %s) ") |
2423
|
|
|
cursor.execute(add_row, (new_id, working_calendar['id'],)) |
2424
|
|
|
cnx.commit() |
2425
|
|
|
cursor.close() |
2426
|
|
|
cnx.close() |
2427
|
|
|
|
2428
|
|
|
resp.status = falcon.HTTP_201 |
2429
|
|
|
resp.location = '/stores/' + str(new_id) |
2430
|
|
|
|
2431
|
|
|
|
2432
|
|
|
class StoreClone: |
2433
|
|
|
def __init__(self): |
2434
|
|
|
""""Initializes StoreClone""" |
2435
|
|
|
pass |
2436
|
|
|
|
2437
|
|
|
@staticmethod |
2438
|
|
|
def on_options(req, resp, id_): |
2439
|
|
|
resp.status = falcon.HTTP_200 |
2440
|
|
|
|
2441
|
|
|
@staticmethod |
2442
|
|
|
@user_logger |
2443
|
|
|
def on_post(req, resp, id_): |
2444
|
|
|
if 'API-KEY' not in req.headers or \ |
2445
|
|
|
not isinstance(req.headers['API-KEY'], str) or \ |
2446
|
|
|
len(str.strip(req.headers['API-KEY'])) == 0: |
2447
|
|
|
access_control(req) |
2448
|
|
|
else: |
2449
|
|
|
api_key_control(req) |
2450
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
2451
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
2452
|
|
|
description='API.INVALID_STORE_ID') |
2453
|
|
|
|
2454
|
|
|
cnx = mysql.connector.connect(**config.myems_system_db) |
2455
|
|
|
cursor = cnx.cursor() |
2456
|
|
|
|
2457
|
|
|
query = (" SELECT id, name, uuid " |
2458
|
|
|
" FROM tbl_store_types ") |
2459
|
|
|
cursor.execute(query) |
2460
|
|
|
rows_store_types = cursor.fetchall() |
2461
|
|
|
|
2462
|
|
|
store_type_dict = dict() |
2463
|
|
|
if rows_store_types is not None and len(rows_store_types) > 0: |
2464
|
|
|
for row in rows_store_types: |
2465
|
|
|
store_type_dict[row[0]] = {"id": row[0], |
2466
|
|
|
"name": row[1], |
2467
|
|
|
"uuid": row[2]} |
2468
|
|
|
|
2469
|
|
|
query = (" SELECT id, name, uuid " |
2470
|
|
|
" FROM tbl_contacts ") |
2471
|
|
|
cursor.execute(query) |
2472
|
|
|
rows_contacts = cursor.fetchall() |
2473
|
|
|
|
2474
|
|
|
contact_dict = dict() |
2475
|
|
|
if rows_contacts is not None and len(rows_contacts) > 0: |
2476
|
|
|
for row in rows_contacts: |
2477
|
|
|
contact_dict[row[0]] = {"id": row[0], |
2478
|
|
|
"name": row[1], |
2479
|
|
|
"uuid": row[2]} |
2480
|
|
|
|
2481
|
|
|
query = (" SELECT id, name, uuid " |
2482
|
|
|
" FROM tbl_cost_centers ") |
2483
|
|
|
cursor.execute(query) |
2484
|
|
|
rows_cost_centers = cursor.fetchall() |
2485
|
|
|
|
2486
|
|
|
cost_center_dict = dict() |
2487
|
|
|
if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
2488
|
|
|
for row in rows_cost_centers: |
2489
|
|
|
cost_center_dict[row[0]] = {"id": row[0], |
2490
|
|
|
"name": row[1], |
2491
|
|
|
"uuid": row[2]} |
2492
|
|
|
|
2493
|
|
|
query = (" SELECT id, name, uuid, " |
2494
|
|
|
" address, latitude, longitude, area, store_type_id," |
2495
|
|
|
" is_input_counted, " |
2496
|
|
|
" contact_id, cost_center_id, description " |
2497
|
|
|
" FROM tbl_stores " |
2498
|
|
|
" WHERE id = %s ") |
2499
|
|
|
cursor.execute(query, (id_,)) |
2500
|
|
|
row = cursor.fetchone() |
2501
|
|
|
|
2502
|
|
|
if row is None: |
2503
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2504
|
|
|
description='API.STORE_NOT_FOUND') |
2505
|
|
|
else: |
2506
|
|
|
meta_result = {"id": row[0], |
2507
|
|
|
"name": row[1], |
2508
|
|
|
"uuid": row[2], |
2509
|
|
|
"address": row[3], |
2510
|
|
|
"latitude": row[4], |
2511
|
|
|
"longitude": row[5], |
2512
|
|
|
"area": row[6], |
2513
|
|
|
"store_type": store_type_dict.get(row[7], None), |
2514
|
|
|
"is_input_counted": bool(row[8]), |
2515
|
|
|
"contact": contact_dict.get(row[9], None), |
2516
|
|
|
"cost_center": cost_center_dict.get(row[10], None), |
2517
|
|
|
"description": row[11], |
2518
|
|
|
"commands": None, |
2519
|
|
|
"meters": None, |
2520
|
|
|
"offline_meters": None, |
2521
|
|
|
"virtual_meters": None, |
2522
|
|
|
"points": None, |
2523
|
|
|
"sensors": None, |
2524
|
|
|
"working_calendars": None |
2525
|
|
|
} |
2526
|
|
|
query = (" SELECT c.id, c.name, c.uuid " |
2527
|
|
|
" FROM tbl_stores s, tbl_stores_commands sc, tbl_commands c " |
2528
|
|
|
" WHERE sc.store_id = s.id AND c.id = sc.command_id AND s.id = %s " |
2529
|
|
|
" ORDER BY c.id ") |
2530
|
|
|
cursor.execute(query, (id_,)) |
2531
|
|
|
rows = cursor.fetchall() |
2532
|
|
|
|
2533
|
|
|
command_result = list() |
2534
|
|
|
if rows is not None and len(rows) > 0: |
2535
|
|
|
for row in rows: |
2536
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2537
|
|
|
command_result.append(result) |
2538
|
|
|
meta_result['commands'] = command_result |
2539
|
|
|
|
2540
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
2541
|
|
|
" FROM tbl_stores t, tbl_stores_meters tm, tbl_meters m " |
2542
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.meter_id AND t.id = %s " |
2543
|
|
|
" ORDER BY m.id ") |
2544
|
|
|
cursor.execute(query, (id_,)) |
2545
|
|
|
rows = cursor.fetchall() |
2546
|
|
|
|
2547
|
|
|
meter_result = list() |
2548
|
|
|
if rows is not None and len(rows) > 0: |
2549
|
|
|
for row in rows: |
2550
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2551
|
|
|
meter_result.append(result) |
2552
|
|
|
meta_result['meters'] = meter_result |
2553
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
2554
|
|
|
" FROM tbl_stores s, tbl_stores_offline_meters sm, tbl_offline_meters m " |
2555
|
|
|
" WHERE sm.store_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " |
2556
|
|
|
" ORDER BY m.id ") |
2557
|
|
|
cursor.execute(query, (id_,)) |
2558
|
|
|
rows = cursor.fetchall() |
2559
|
|
|
|
2560
|
|
|
offlinemeter_result = list() |
2561
|
|
|
if rows is not None and len(rows) > 0: |
2562
|
|
|
for row in rows: |
2563
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2564
|
|
|
offlinemeter_result.append(result) |
2565
|
|
|
meta_result['offline_meters'] = offlinemeter_result |
2566
|
|
|
query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " |
2567
|
|
|
" FROM tbl_stores t, tbl_stores_virtual_meters tm, tbl_virtual_meters m " |
2568
|
|
|
" WHERE tm.store_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " |
2569
|
|
|
" ORDER BY m.id ") |
2570
|
|
|
cursor.execute(query, (id_,)) |
2571
|
|
|
rows = cursor.fetchall() |
2572
|
|
|
|
2573
|
|
|
virtualmeter_result = list() |
2574
|
|
|
if rows is not None and len(rows) > 0: |
2575
|
|
|
for row in rows: |
2576
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2577
|
|
|
virtualmeter_result.append(result) |
2578
|
|
|
meta_result['virtual_meters'] = virtualmeter_result |
2579
|
|
|
query = (" SELECT id, name, uuid " |
2580
|
|
|
" FROM tbl_data_sources ") |
2581
|
|
|
cursor.execute(query) |
2582
|
|
|
rows_data_sources = cursor.fetchall() |
2583
|
|
|
|
2584
|
|
|
data_source_dict = dict() |
2585
|
|
|
if rows_data_sources is not None and len(rows_data_sources) > 0: |
2586
|
|
|
for row in rows_data_sources: |
2587
|
|
|
data_source_dict[row[0]] = {"id": row[0], |
2588
|
|
|
"name": row[1], |
2589
|
|
|
"uuid": row[2]} |
2590
|
|
|
|
2591
|
|
|
query = (" SELECT p.id, p.name, p.data_source_id " |
2592
|
|
|
" FROM tbl_stores t, tbl_stores_points tp, tbl_points p " |
2593
|
|
|
" WHERE tp.store_id = t.id AND p.id = tp.point_id AND t.id = %s " |
2594
|
|
|
" ORDER BY p.id ") |
2595
|
|
|
cursor.execute(query, (id_,)) |
2596
|
|
|
rows = cursor.fetchall() |
2597
|
|
|
|
2598
|
|
|
point_result = list() |
2599
|
|
|
if rows is not None and len(rows) > 0: |
2600
|
|
|
for row in rows: |
2601
|
|
|
result = {"id": row[0], "name": row[1], "data_source": data_source_dict.get(row[2], None)} |
2602
|
|
|
point_result.append(result) |
2603
|
|
|
meta_result['points'] = point_result |
2604
|
|
|
query = (" SELECT s.id, s.name, s.uuid " |
2605
|
|
|
" FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s " |
2606
|
|
|
" WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s " |
2607
|
|
|
" ORDER BY s.id ") |
2608
|
|
|
cursor.execute(query, (id_,)) |
2609
|
|
|
rows = cursor.fetchall() |
2610
|
|
|
|
2611
|
|
|
sensor_result = list() |
2612
|
|
|
if rows is not None and len(rows) > 0: |
2613
|
|
|
for row in rows: |
2614
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2]} |
2615
|
|
|
sensor_result.append(result) |
2616
|
|
|
meta_result['sensors'] = sensor_result |
2617
|
|
|
query = (" SELECT wc.id, wc.name, wc.description " |
2618
|
|
|
" FROM tbl_stores s, tbl_stores_working_calendars swc, tbl_working_calendars wc " |
2619
|
|
|
" WHERE swc.store_id = s.id AND wc.id = swc.working_calendar_id AND s.id = %s " |
2620
|
|
|
" ORDER BY wc.id ") |
2621
|
|
|
cursor.execute(query, (id_,)) |
2622
|
|
|
rows = cursor.fetchall() |
2623
|
|
|
|
2624
|
|
|
workingcalendar_result = list() |
2625
|
|
|
if rows is not None and len(rows) > 0: |
2626
|
|
|
for row in rows: |
2627
|
|
|
result = {"id": row[0], "name": row[1], "description": row[2]} |
2628
|
|
|
workingcalendar_result.append(result) |
2629
|
|
|
meta_result['working_calendar'] = workingcalendar_result |
2630
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
2631
|
|
|
if config.utc_offset[0] == '-': |
2632
|
|
|
timezone_offset = -timezone_offset |
2633
|
|
|
new_name = (str.strip(meta_result['name']) + |
2634
|
|
|
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
2635
|
|
|
add_values = (" INSERT INTO tbl_stores " |
2636
|
|
|
" (name, uuid, address, latitude, longitude, area, store_type_id, " |
2637
|
|
|
" is_input_counted, " |
2638
|
|
|
" contact_id, cost_center_id, description) " |
2639
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
2640
|
|
|
cursor.execute(add_values, (new_name, |
2641
|
|
|
str(uuid.uuid4()), |
2642
|
|
|
meta_result['address'], |
2643
|
|
|
meta_result['latitude'], |
2644
|
|
|
meta_result['longitude'], |
2645
|
|
|
meta_result['area'], |
2646
|
|
|
meta_result['store_type']['id'], |
2647
|
|
|
meta_result['is_input_counted'], |
2648
|
|
|
meta_result['contact']['id'], |
2649
|
|
|
meta_result['cost_center']['id'], |
2650
|
|
|
meta_result['description'])) |
2651
|
|
|
new_id = cursor.lastrowid |
2652
|
|
|
if meta_result['commands'] is not None and len(meta_result['commands']) > 0: |
2653
|
|
|
for command in meta_result['commands']: |
2654
|
|
|
cursor.execute(" SELECT name " |
2655
|
|
|
" FROM tbl_commands " |
2656
|
|
|
" WHERE id = %s ", (command['id'],)) |
2657
|
|
|
if cursor.fetchone() is None: |
2658
|
|
|
cursor.close() |
2659
|
|
|
cnx.close() |
2660
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2661
|
|
|
description='API.COMMAND_NOT_FOUND') |
2662
|
|
|
|
2663
|
|
|
query = (" SELECT id " |
2664
|
|
|
" FROM tbl_stores_commands " |
2665
|
|
|
" WHERE store_id = %s AND command_id = %s") |
2666
|
|
|
cursor.execute(query, (new_id, command['id'],)) |
2667
|
|
|
if cursor.fetchone() is not None: |
2668
|
|
|
cursor.close() |
2669
|
|
|
cnx.close() |
2670
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2671
|
|
|
description='API.STORE_COMMAND_RELATION_EXISTS') |
2672
|
|
|
|
2673
|
|
|
add_row = (" INSERT INTO tbl_stores_commands (store_id, command_id) " |
2674
|
|
|
" VALUES (%s, %s) ") |
2675
|
|
|
cursor.execute(add_row, (new_id, command['id'],)) |
2676
|
|
|
if meta_result['meters'] is not None and len(meta_result['meters']) > 0: |
2677
|
|
|
for meter in meta_result['meters']: |
2678
|
|
|
cursor.execute(" SELECT name " |
2679
|
|
|
" FROM tbl_meters " |
2680
|
|
|
" WHERE id = %s ", (meter['id'],)) |
2681
|
|
|
if cursor.fetchone() is None: |
2682
|
|
|
cursor.close() |
2683
|
|
|
cnx.close() |
2684
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2685
|
|
|
description='API.METER_NOT_FOUND') |
2686
|
|
|
|
2687
|
|
|
query = (" SELECT id " |
2688
|
|
|
" FROM tbl_stores_meters " |
2689
|
|
|
" WHERE store_id = %s AND meter_id = %s") |
2690
|
|
|
cursor.execute(query, (new_id, meter['id'],)) |
2691
|
|
|
if cursor.fetchone() is not None: |
2692
|
|
|
cursor.close() |
2693
|
|
|
cnx.close() |
2694
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2695
|
|
|
description='API.STORE_METER_RELATION_EXISTS') |
2696
|
|
|
|
2697
|
|
|
add_row = (" INSERT INTO tbl_stores_meters (store_id, meter_id) " |
2698
|
|
|
" VALUES (%s, %s) ") |
2699
|
|
|
cursor.execute(add_row, (new_id, meter['id'],)) |
2700
|
|
|
if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: |
2701
|
|
|
for offline_meter in meta_result['offline_meters']: |
2702
|
|
|
cursor.execute(" SELECT name " |
2703
|
|
|
" FROM tbl_offline_meters " |
2704
|
|
|
" WHERE id = %s ", (offline_meter['id'],)) |
2705
|
|
|
if cursor.fetchone() is None: |
2706
|
|
|
cursor.close() |
2707
|
|
|
cnx.close() |
2708
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2709
|
|
|
description='API.OFFLINE_METER_NOT_FOUND') |
2710
|
|
|
|
2711
|
|
|
query = (" SELECT id " |
2712
|
|
|
" FROM tbl_stores_offline_meters " |
2713
|
|
|
" WHERE store_id = %s AND offline_meter_id = %s") |
2714
|
|
|
cursor.execute(query, (new_id, offline_meter['id'],)) |
2715
|
|
|
if cursor.fetchone() is not None: |
2716
|
|
|
cursor.close() |
2717
|
|
|
cnx.close() |
2718
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2719
|
|
|
description='API.STORE_OFFLINE_METER_RELATION_EXISTS') |
2720
|
|
|
|
2721
|
|
|
add_row = (" INSERT INTO tbl_stores_offline_meters (store_id, offline_meter_id) " |
2722
|
|
|
" VALUES (%s, %s) ") |
2723
|
|
|
cursor.execute(add_row, (new_id, offline_meter['id'],)) |
2724
|
|
|
if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: |
2725
|
|
|
for virtual_meter in meta_result['virtual_meters']: |
2726
|
|
|
cursor.execute(" SELECT name " |
2727
|
|
|
" FROM tbl_virtual_meters " |
2728
|
|
|
" WHERE id = %s ", (virtual_meter['id'],)) |
2729
|
|
|
if cursor.fetchone() is None: |
2730
|
|
|
cursor.close() |
2731
|
|
|
cnx.close() |
2732
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2733
|
|
|
description='API.VIRTUAL_METER_NOT_FOUND') |
2734
|
|
|
|
2735
|
|
|
query = (" SELECT id " |
2736
|
|
|
" FROM tbl_stores_virtual_meters " |
2737
|
|
|
" WHERE store_id = %s AND virtual_meter_id = %s") |
2738
|
|
|
cursor.execute(query, (new_id, virtual_meter['id'],)) |
2739
|
|
|
if cursor.fetchone() is not None: |
2740
|
|
|
cursor.close() |
2741
|
|
|
cnx.close() |
2742
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2743
|
|
|
description='API.STORE_VIRTUAL_METER_RELATION_EXISTS') |
2744
|
|
|
|
2745
|
|
|
add_row = (" INSERT INTO tbl_stores_virtual_meters (store_id, virtual_meter_id) " |
2746
|
|
|
" VALUES (%s, %s) ") |
2747
|
|
|
cursor.execute(add_row, (new_id, virtual_meter['id'],)) |
2748
|
|
|
if meta_result['points'] is not None and len(meta_result['points']) > 0: |
2749
|
|
|
for point in meta_result['points']: |
2750
|
|
|
cursor.execute(" SELECT name " |
2751
|
|
|
" FROM tbl_points " |
2752
|
|
|
" WHERE id = %s ", (point['id'],)) |
2753
|
|
|
if cursor.fetchone() is None: |
2754
|
|
|
cursor.close() |
2755
|
|
|
cnx.close() |
2756
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2757
|
|
|
description='API.POINT_NOT_FOUND') |
2758
|
|
|
|
2759
|
|
|
query = (" SELECT id " |
2760
|
|
|
" FROM tbl_stores_points " |
2761
|
|
|
" WHERE store_id = %s AND point_id = %s") |
2762
|
|
|
cursor.execute(query, (new_id, point['id'],)) |
2763
|
|
|
if cursor.fetchone() is not None: |
2764
|
|
|
cursor.close() |
2765
|
|
|
cnx.close() |
2766
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2767
|
|
|
description='API.STORE_POINT_RELATION_EXISTS') |
2768
|
|
|
|
2769
|
|
|
add_row = (" INSERT INTO tbl_stores_points (store_id, point_id) " |
2770
|
|
|
" VALUES (%s, %s) ") |
2771
|
|
|
cursor.execute(add_row, (new_id, point['id'],)) |
2772
|
|
|
if meta_result['sensors'] is not None and len(meta_result['sensors']) > 0: |
2773
|
|
|
for sensor in meta_result['sensors']: |
2774
|
|
|
cursor.execute(" SELECT name " |
2775
|
|
|
" FROM tbl_sensors " |
2776
|
|
|
" WHERE id = %s ", (sensor['id'],)) |
2777
|
|
|
if cursor.fetchone() is None: |
2778
|
|
|
cursor.close() |
2779
|
|
|
cnx.close() |
2780
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2781
|
|
|
description='API.SENSOR_NOT_FOUND') |
2782
|
|
|
|
2783
|
|
|
query = (" SELECT id " |
2784
|
|
|
" FROM tbl_stores_sensors " |
2785
|
|
|
" WHERE store_id = %s AND sensor_id = %s") |
2786
|
|
|
cursor.execute(query, (new_id, sensor['id'],)) |
2787
|
|
|
if cursor.fetchone() is not None: |
2788
|
|
|
cursor.close() |
2789
|
|
|
cnx.close() |
2790
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2791
|
|
|
description='API.STORE_SENSOR_RELATION_EXISTS') |
2792
|
|
|
|
2793
|
|
|
add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) " |
2794
|
|
|
" VALUES (%s, %s) ") |
2795
|
|
|
cursor.execute(add_row, (new_id, sensor['id'],)) |
2796
|
|
|
if meta_result['working_calendars'] is not None and len(meta_result['working_calendars']) > 0: |
2797
|
|
|
for working_calendar in meta_result['working_calendars']: |
2798
|
|
|
cursor.execute(" SELECT name " |
2799
|
|
|
" FROM tbl_working_calendars " |
2800
|
|
|
" WHERE id = %s ", (working_calendar['id'],)) |
2801
|
|
|
if cursor.fetchone() is None: |
2802
|
|
|
cursor.close() |
2803
|
|
|
cnx.close() |
2804
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
2805
|
|
|
description='API.WORKING_CALENDAR_NOT_FOUND') |
2806
|
|
|
|
2807
|
|
|
query = (" SELECT id " |
2808
|
|
|
" FROM tbl_stores_working_calendars " |
2809
|
|
|
" WHERE store_id = %s AND working_calendar_id = %s") |
2810
|
|
|
cursor.execute(query, (new_id, working_calendar['id'],)) |
2811
|
|
|
if cursor.fetchone() is not None: |
2812
|
|
|
cursor.close() |
2813
|
|
|
cnx.close() |
2814
|
|
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
2815
|
|
|
description='API.STORE_WORKING_CALENDAR_RELATION_EXISTS') |
2816
|
|
|
|
2817
|
|
|
add_row = (" INSERT INTO tbl_stores_working_calendars (store_id, working_calendar_id) " |
2818
|
|
|
" VALUES (%s, %s) ") |
2819
|
|
|
cursor.execute(add_row, (new_id, working_calendar['id'],)) |
2820
|
|
|
cnx.commit() |
2821
|
|
|
cursor.close() |
2822
|
|
|
cnx.close() |
2823
|
|
|
|
2824
|
|
|
resp.status = falcon.HTTP_201 |
2825
|
|
|
resp.location = '/stores/' + str(new_id) |
2826
|
|
|
|