PhotovoltaicPowerStationCollection.on_options()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import uuid
2
from datetime import datetime, timedelta
3
from decimal import Decimal
4
import falcon
5
import mysql.connector
6
import simplejson as json
7
from core.useractivity import user_logger, admin_control, access_control, api_key_control
8
import config
9
10
11
class PhotovoltaicPowerStationCollection:
12
    def __init__(self):
13
        """"Initializes Class"""
14
        pass
15
16
    @staticmethod
17
    def on_options(req, resp):
18
        _ = req
19
        resp.status = falcon.HTTP_200
20
21 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
    def on_get(req, resp):
23
        access_control(req)
24
        cnx = mysql.connector.connect(**config.myems_system_db)
25
        cursor = cnx.cursor()
26
27
        query = (" SELECT id, name, uuid "
28
                 " FROM tbl_contacts ")
29
        cursor.execute(query)
30
        rows_contacts = cursor.fetchall()
31
32
        contact_dict = dict()
33
        if rows_contacts is not None and len(rows_contacts) > 0:
34
            for row in rows_contacts:
35
                contact_dict[row[0]] = {"id": row[0],
36
                                        "name": row[1],
37
                                        "uuid": row[2]}
38
39
        query = (" SELECT id, name, uuid "
40
                 " FROM tbl_cost_centers ")
41
        cursor.execute(query)
42
        rows_cost_centers = cursor.fetchall()
43
44
        cost_center_dict = dict()
45
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
46
            for row in rows_cost_centers:
47
                cost_center_dict[row[0]] = {"id": row[0],
48
                                            "name": row[1],
49
                                            "uuid": row[2]}
50
51
        svg_dict = dict()
52
53
        query = (" SELECT id, name, uuid "
54
                 " FROM tbl_svgs ")
55
        cursor.execute(query)
56
        rows_svgs = cursor.fetchall()
57
        if rows_svgs is not None and len(rows_svgs) > 0:
58
            for row in rows_svgs:
59
                svg_dict[row[0]] = {"id": row[0],
60
                                    "name": row[1],
61
                                    "uuid": row[2]}
62
63
        query = (" SELECT id, name, uuid, "
64
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
65
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
66
                 " FROM tbl_photovoltaic_power_stations "
67
                 " ORDER BY id ")
68
        cursor.execute(query)
69
        rows_photovoltaic_power_stations = cursor.fetchall()
70
71
        result = list()
72
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
73
            for row in rows_photovoltaic_power_stations:
74
                meta_result = {"id": row[0],
75
                               "name": row[1],
76
                               "uuid": row[2],
77
                               "station_code": row[3],
78
                               "address": row[4],
79
                               "latitude": row[5],
80
                               "longitude": row[6],
81
                               "rated_capacity": row[7],
82
                               "rated_power": row[8],
83
                               "contact": contact_dict.get(row[9], None),
84
                               "cost_center": cost_center_dict.get(row[10], None),
85
                               "svg": svg_dict.get(row[11], None),
86
                               "is_cost_data_displayed": bool(row[12]),
87
                               "phase_of_lifecycle": row[13],
88
                               "description": row[14],
89
                               "qrcode": 'photovoltaicpowerstation:' + row[2]}
90
                result.append(meta_result)
91
92
        cursor.close()
93
        cnx.close()
94
        resp.text = json.dumps(result)
95
96
    @staticmethod
97
    @user_logger
98
    def on_post(req, resp):
99
        """Handles POST requests"""
100
        admin_control(req)
101
        try:
102
            raw_json = req.stream.read().decode('utf-8')
103
        except Exception as ex:
104
            print(str(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_PHOTOVOLTAIC_POWER_STATION_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 'station_code' not in new_values['data'].keys() or \
126
                not isinstance(new_values['data']['station_code'], str) or \
127
                len(str.strip(new_values['data']['station_code'])) == 0:
128
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
129
                                   description='API.INVALID_STATION_CODE')
130
        station_code = str.strip(new_values['data']['station_code'])
131
132
        if 'latitude' not in new_values['data'].keys() or \
133
                not (isinstance(new_values['data']['latitude'], float) or
134
                     isinstance(new_values['data']['latitude'], int)) or \
135
                new_values['data']['latitude'] < -90.0 or \
136
                new_values['data']['latitude'] > 90.0:
137
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
138
                                   description='API.INVALID_LATITUDE_VALUE')
139
        latitude = new_values['data']['latitude']
140
141
        if 'longitude' not in new_values['data'].keys() or \
142
                not (isinstance(new_values['data']['longitude'], float) or
143
                     isinstance(new_values['data']['longitude'], int)) or \
144
                new_values['data']['longitude'] < -180.0 or \
145
                new_values['data']['longitude'] > 180.0:
146
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
147
                                   description='API.INVALID_LONGITUDE_VALUE')
148
        longitude = new_values['data']['longitude']
149
150
        if 'rated_capacity' not in new_values['data'].keys() or \
151
                not (isinstance(new_values['data']['rated_capacity'], float) or
152
                     isinstance(new_values['data']['rated_capacity'], int)) or \
153
                new_values['data']['rated_capacity'] <= 0.0:
154
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
155
                                   description='API.INVALID_RATED_CAPACITY')
156
        rated_capacity = new_values['data']['rated_capacity']
157
158
        if 'rated_power' not in new_values['data'].keys() or \
159
                not (isinstance(new_values['data']['rated_power'], float) or
160
                     isinstance(new_values['data']['rated_power'], int)) or \
161
                new_values['data']['rated_power'] <= 0.0:
162
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
163
                                   description='API.INVALID_RATED_POWER')
164
        rated_power = new_values['data']['rated_power']
165
166
        if 'contact_id' not in new_values['data'].keys() or \
167
                not isinstance(new_values['data']['contact_id'], int) or \
168
                new_values['data']['contact_id'] <= 0:
169
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
170
                                   description='API.INVALID_CONTACT_ID')
171
        contact_id = new_values['data']['contact_id']
172
173
        if 'cost_center_id' not in new_values['data'].keys() or \
174
                not isinstance(new_values['data']['cost_center_id'], int) or \
175
                new_values['data']['cost_center_id'] <= 0:
176
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
177
                                   description='API.INVALID_COST_CENTER_ID')
178
        cost_center_id = new_values['data']['cost_center_id']
179
180
        if 'svg_id' not in new_values['data'].keys() or \
181
                not isinstance(new_values['data']['svg_id'], int) or \
182
                new_values['data']['svg_id'] <= 0:
183
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
184
                                   description='API.INVALID_SVG_ID')
185
        svg_id = new_values['data']['svg_id']
186
187
        if 'is_cost_data_displayed' not in new_values['data'].keys() or \
188
                not isinstance(new_values['data']['is_cost_data_displayed'], bool):
189
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
190
                                   description='API.INVALID_IS_COST_DATA_DISPLAYED')
191
        is_cost_data_displayed = new_values['data']['is_cost_data_displayed']
192
193
        if 'phase_of_lifecycle' not in new_values['data'].keys() or \
194
                not isinstance(new_values['data']['phase_of_lifecycle'], str) or \
195
                len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0:
196
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
197
                                   description='API.INVALID_PHASE_OF_LIFECYCLE')
198
        phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle'])
199
200
        if 'description' in new_values['data'].keys() and \
201
                new_values['data']['description'] is not None and \
202
                len(str(new_values['data']['description'])) > 0:
203
            description = str.strip(new_values['data']['description'])
204
        else:
205
            description = None
206
207
        cnx = mysql.connector.connect(**config.myems_system_db)
208
        cursor = cnx.cursor()
209
210
        cursor.execute(" SELECT name "
211
                       " FROM tbl_photovoltaic_power_stations "
212
                       " WHERE name = %s ", (name,))
213
        if cursor.fetchone() is not None:
214
            cursor.close()
215
            cnx.close()
216
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
217
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NAME_IS_ALREADY_IN_USE')
218
219
        cursor.execute(" SELECT name "
220
                       " FROM tbl_contacts "
221
                       " WHERE id = %s ",
222
                       (contact_id,))
223
        row = cursor.fetchone()
224
        if row is None:
225
            cursor.close()
226
            cnx.close()
227
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
228
                                   description='API.CONTACT_NOT_FOUND')
229
230
        cursor.execute(" SELECT name "
231
                       " FROM tbl_cost_centers "
232
                       " WHERE id = %s ",
233
                       (cost_center_id,))
234
        row = cursor.fetchone()
235
        if row is None:
236
            cursor.close()
237
            cnx.close()
238
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
239
                                   description='API.COST_CENTER_NOT_FOUND')
240
241
        cursor.execute(" SELECT name "
242
                       " FROM tbl_svgs "
243
                       " WHERE id = %s ",
244
                       (svg_id,))
245
        row = cursor.fetchone()
246
        if row is None:
247
            cursor.close()
248
            cnx.close()
249
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
250
                                   description='API.SVG_NOT_FOUND')
251
252
        add_values = (" INSERT INTO tbl_photovoltaic_power_stations "
253
                      " (name, uuid, station_code, address, latitude, longitude, "
254
                      "  rated_capacity, rated_power, contact_id, cost_center_id, svg_id, is_cost_data_displayed, "
255
                      "  phase_of_lifecycle, description) "
256
                      " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
257
        cursor.execute(add_values, (name,
258
                                    str(uuid.uuid4()),
259
                                    station_code,
260
                                    address,
261
                                    latitude,
262
                                    longitude,
263
                                    rated_capacity,
264
                                    rated_power,
265
                                    contact_id,
266
                                    cost_center_id,
267
                                    svg_id,
268
                                    is_cost_data_displayed,
269
                                    phase_of_lifecycle,
270
                                    description))
271
        new_id = cursor.lastrowid
272
        cnx.commit()
273
        cursor.close()
274
        cnx.close()
275
276
        resp.status = falcon.HTTP_201
277
        resp.location = '/photovoltaicpowerstations/' + str(new_id)
278
279
280
class PhotovoltaicPowerStationItem:
281
    def __init__(self):
282
        """"Initializes Class"""
283
        pass
284
285
    @staticmethod
286
    def on_options(req, resp, id_):
287
        _ = req
288
        resp.status = falcon.HTTP_200
289
        _ = id_
290
291 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
292
    def on_get(req, resp, id_):
293
        access_control(req)
294
        if not id_.isdigit() or int(id_) <= 0:
295
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
296
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
297
298
        cnx = mysql.connector.connect(**config.myems_system_db)
299
        cursor = cnx.cursor()
300
301
        query = (" SELECT id, name, uuid "
302
                 " FROM tbl_contacts ")
303
        cursor.execute(query)
304
        rows_contacts = cursor.fetchall()
305
306
        contact_dict = dict()
307
        if rows_contacts is not None and len(rows_contacts) > 0:
308
            for row in rows_contacts:
309
                contact_dict[row[0]] = {"id": row[0],
310
                                        "name": row[1],
311
                                        "uuid": row[2]}
312
313
        query = (" SELECT id, name, uuid "
314
                 " FROM tbl_cost_centers ")
315
        cursor.execute(query)
316
        rows_cost_centers = cursor.fetchall()
317
318
        cost_center_dict = dict()
319
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
320
            for row in rows_cost_centers:
321
                cost_center_dict[row[0]] = {"id": row[0],
322
                                            "name": row[1],
323
                                            "uuid": row[2]}
324
325
        svg_dict = dict()
326
        query = (" SELECT id, name, uuid "
327
                 " FROM tbl_svgs ")
328
        cursor.execute(query)
329
        rows_svgs = cursor.fetchall()
330
        if rows_svgs is not None and len(rows_svgs) > 0:
331
            for row in rows_svgs:
332
                svg_dict[row[0]] = {"id": row[0],
333
                                    "name": row[1],
334
                                    "uuid": row[2]}
335
336
        query = (" SELECT id, name, uuid, "
337
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
338
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
339
                 " FROM tbl_photovoltaic_power_stations "
340
                 " WHERE id = %s ")
341
        cursor.execute(query, (id_,))
342
        row = cursor.fetchone()
343
        cursor.close()
344
        cnx.close()
345
346
        if row is None:
347
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
348
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
349
        else:
350
            meta_result = {"id": row[0],
351
                           "name": row[1],
352
                           "uuid": row[2],
353
                           "station_code": row[3],
354
                           "address": row[4],
355
                           "latitude": row[5],
356
                           "longitude": row[6],
357
                           "rated_capacity": row[7],
358
                           "rated_power": row[8],
359
                           "contact": contact_dict.get(row[9], None),
360
                           "cost_center": cost_center_dict.get(row[10], None),
361
                           "svg": svg_dict.get(row[11], None),
362
                           "is_cost_data_displayed": bool(row[12]),
363
                           "phase_of_lifecycle": row[13],
364
                           "description": row[14],
365
                           "qrcode": 'photovoltaicpowerstation:' + row[2]}
366
367
        resp.text = json.dumps(meta_result)
368
369
    @staticmethod
370
    @user_logger
371
    def on_delete(req, resp, id_):
372
        admin_control(req)
373
        if not id_.isdigit() or int(id_) <= 0:
374
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
375
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
376
377
        cnx = mysql.connector.connect(**config.myems_system_db)
378
        cursor = cnx.cursor()
379
380
        cursor.execute(" SELECT name "
381
                       " FROM tbl_photovoltaic_power_stations "
382
                       " WHERE id = %s ", (id_,))
383
        if cursor.fetchone() is None:
384
            cursor.close()
385
            cnx.close()
386
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
387
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
388
389
        # check relation with spaces
390
        cursor.execute(" SELECT id "
391
                       " FROM  tbl_spaces_photovoltaic_power_stations "
392
                       " WHERE photovoltaic_power_station_id = %s ", (id_,))
393
        rows_spaces = cursor.fetchall()
394
        if rows_spaces is not None and len(rows_spaces) > 0:
395
            cursor.close()
396
            cnx.close()
397
            raise falcon.HTTPError(status=falcon.HTTP_400,
398
                                   title='API.BAD_REQUEST',
399
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
400
401
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations "
402
                       " WHERE id = %s ", (id_,))
403
        cnx.commit()
404
405
        cursor.close()
406
        cnx.close()
407
408
        resp.status = falcon.HTTP_204
409
410
    @staticmethod
411
    @user_logger
412
    def on_put(req, resp, id_):
413
        """Handles PUT requests"""
414
        admin_control(req)
415
        try:
416
            raw_json = req.stream.read().decode('utf-8')
417
        except Exception as ex:
418
            print(str(ex))
419
            raise falcon.HTTPError(status=falcon.HTTP_400,
420
                                   title='API.BAD_REQUEST',
421
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
422
423
        if not id_.isdigit() or int(id_) <= 0:
424
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
425
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
426
427
        new_values = json.loads(raw_json)
428
429
        if 'name' not in new_values['data'].keys() or \
430
                not isinstance(new_values['data']['name'], str) or \
431
                len(str.strip(new_values['data']['name'])) == 0:
432
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
433
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_NAME')
434
        name = str.strip(new_values['data']['name'])
435
436
        if 'station_code' not in new_values['data'].keys() or \
437
                not isinstance(new_values['data']['station_code'], str) or \
438
                len(str.strip(new_values['data']['station_code'])) == 0:
439
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
440
                                   description='API.INVALID_STATION_CODE')
441
        station_code = str.strip(new_values['data']['station_code'])
442
443
        if 'address' not in new_values['data'].keys() or \
444
                not isinstance(new_values['data']['address'], str) or \
445
                len(str.strip(new_values['data']['address'])) == 0:
446
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
447
                                   description='API.INVALID_ADDRESS_VALUE')
448
        address = str.strip(new_values['data']['address'])
449
450
        if 'latitude' not in new_values['data'].keys() or \
451
                not (isinstance(new_values['data']['latitude'], float) or
452
                     isinstance(new_values['data']['latitude'], int)) or \
453
                new_values['data']['latitude'] < -90.0 or \
454
                new_values['data']['latitude'] > 90.0:
455
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
456
                                   description='API.INVALID_LATITUDE_VALUE')
457
        latitude = new_values['data']['latitude']
458
459
        if 'longitude' not in new_values['data'].keys() or \
460
                not (isinstance(new_values['data']['longitude'], float) or
461
                     isinstance(new_values['data']['longitude'], int)) or \
462
                new_values['data']['longitude'] < -180.0 or \
463
                new_values['data']['longitude'] > 180.0:
464
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
465
                                   description='API.INVALID_LONGITUDE_VALUE')
466
        longitude = new_values['data']['longitude']
467
468
        if 'rated_capacity' not in new_values['data'].keys() or \
469
                not (isinstance(new_values['data']['rated_capacity'], float) or
470
                     isinstance(new_values['data']['rated_capacity'], int)) or \
471
                new_values['data']['rated_capacity'] <= 0.0:
472
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
473
                                   description='API.INVALID_RATED_CAPACITY')
474
        rated_capacity = new_values['data']['rated_capacity']
475
476
        if 'rated_power' not in new_values['data'].keys() or \
477
                not (isinstance(new_values['data']['rated_power'], float) or
478
                     isinstance(new_values['data']['rated_power'], int)) or \
479
                new_values['data']['rated_power'] <= 0.0:
480
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
481
                                   description='API.INVALID_RATED_POWER')
482
        rated_power = new_values['data']['rated_power']
483
484
        if 'contact_id' not in new_values['data'].keys() or \
485
                not isinstance(new_values['data']['contact_id'], int) or \
486
                new_values['data']['contact_id'] <= 0:
487
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
488
                                   description='API.INVALID_CONTACT_ID')
489
        contact_id = new_values['data']['contact_id']
490
491
        if 'cost_center_id' not in new_values['data'].keys() or \
492
                not isinstance(new_values['data']['cost_center_id'], int) or \
493
                new_values['data']['cost_center_id'] <= 0:
494
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
495
                                   description='API.INVALID_COST_CENTER_ID')
496
        cost_center_id = new_values['data']['cost_center_id']
497
498
        if 'svg_id' not in new_values['data'].keys() or \
499
                not isinstance(new_values['data']['svg_id'], int) or \
500
                new_values['data']['svg_id'] <= 0:
501
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
502
                                   description='API.INVALID_SVG_ID')
503
        svg_id = new_values['data']['svg_id']
504
505
        if 'is_cost_data_displayed' not in new_values['data'].keys() or \
506
                not isinstance(new_values['data']['is_cost_data_displayed'], bool):
507
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
508
                                   description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE')
509
        is_cost_data_displayed = new_values['data']['is_cost_data_displayed']
510
511
        if 'phase_of_lifecycle' not in new_values['data'].keys() or \
512
                not isinstance(new_values['data']['phase_of_lifecycle'], str) or \
513
                len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0:
514
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
515
                                   description='API.INVALID_PHASE_OF_LIFECYCLE')
516
        phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle'])
517
518
        if 'description' in new_values['data'].keys() and \
519
                new_values['data']['description'] is not None and \
520
                len(str(new_values['data']['description'])) > 0:
521
            description = str.strip(new_values['data']['description'])
522
        else:
523
            description = None
524
525
        cnx = mysql.connector.connect(**config.myems_system_db)
526
        cursor = cnx.cursor()
527
528
        cursor.execute(" SELECT name "
529
                       " FROM tbl_photovoltaic_power_stations "
530
                       " WHERE id = %s ", (id_,))
531
        if cursor.fetchone() is None:
532
            cursor.close()
533
            cnx.close()
534
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
535
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
536
537
        cursor.execute(" SELECT name "
538
                       " FROM tbl_photovoltaic_power_stations "
539
                       " WHERE name = %s AND id != %s ", (name, id_))
540
        if cursor.fetchone() is not None:
541
            cursor.close()
542
            cnx.close()
543
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
544
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NAME_IS_ALREADY_IN_USE')
545
546
        cursor.execute(" SELECT name "
547
                       " FROM tbl_contacts "
548
                       " WHERE id = %s ",
549
                       (new_values['data']['contact_id'],))
550
        row = cursor.fetchone()
551
        if row is None:
552
            cursor.close()
553
            cnx.close()
554
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
555
                                   description='API.CONTACT_NOT_FOUND')
556
557
        cursor.execute(" SELECT name "
558
                       " FROM tbl_cost_centers "
559
                       " WHERE id = %s ",
560
                       (new_values['data']['cost_center_id'],))
561
        row = cursor.fetchone()
562
        if row is None:
563
            cursor.close()
564
            cnx.close()
565
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
566
                                   description='API.COST_CENTER_NOT_FOUND')
567
568
        cursor.execute(" SELECT name "
569
                       " FROM tbl_svgs "
570
                       " WHERE id = %s ",
571
                       (new_values['data']['svg_id'],))
572
        row = cursor.fetchone()
573
        if row is None:
574
            cursor.close()
575
            cnx.close()
576
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
577
                                   description='API.SVG_NOT_FOUND')
578
579
        update_row = (" UPDATE tbl_photovoltaic_power_stations "
580
                      " SET name = %s, station_code = %s, address = %s, "
581
                      "     latitude = %s, longitude = %s, "
582
                      "     rated_capacity = %s, rated_power = %s, "
583
                      "     contact_id = %s, cost_center_id = %s, "
584
                      "     svg_id = %s, is_cost_data_displayed = %s, phase_of_lifecycle = %s, description = %s "
585
                      " WHERE id = %s ")
586
        cursor.execute(update_row, (name,
587
                                    station_code,
588
                                    address,
589
                                    latitude,
590
                                    longitude,
591
                                    rated_capacity,
592
                                    rated_power,
593
                                    contact_id,
594
                                    cost_center_id,
595
                                    svg_id,
596
                                    is_cost_data_displayed,
597
                                    phase_of_lifecycle,
598
                                    description,
599
                                    id_))
600
        cnx.commit()
601
602
        cursor.close()
603
        cnx.close()
604
605
        resp.status = falcon.HTTP_200
606
607
608 View Code Duplication
class PhotovoltaicPowerStationDataSourcePointCollection:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
609
    def __init__(self):
610
        """Initializes"""
611
        pass
612
613
    @staticmethod
614
    def on_options(req, resp, id_):
615
        _ = req
616
        resp.status = falcon.HTTP_200
617
        _ = id_
618
619
    @staticmethod
620
    def on_get(req, resp, id_):
621
        if 'API-KEY' not in req.headers or \
622
                not isinstance(req.headers['API-KEY'], str) or \
623
                len(str.strip(req.headers['API-KEY'])) == 0:
624
            access_control(req)
625
        else:
626
            api_key_control(req)
627
        if not id_.isdigit() or int(id_) <= 0:
628
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
629
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
630
631
        cnx = mysql.connector.connect(** config.myems_system_db)
632
        cursor = cnx.cursor()
633
634
        query = (" SELECT p.id, p.name "
635
                 " FROM tbl_points p, tbl_photovoltaic_power_stations_data_sources ppds, tbl_data_sources ds "
636
                 " WHERE ppds.photovoltaic_power_station_id = %s "  
637
                 "       AND ppds.data_source_id = ds.id "          
638
                 "       AND p.data_source_id = ds.id "             
639
                 " ORDER BY p.id ")
640
        cursor.execute(query, (id_,))
641
        rows = cursor.fetchall()
642
643
        result = list()
644
        if rows is not None and len(rows) > 0:
645
            for row in rows:
646
                meta_result = {"id": row[0], "name": row[1]}
647
                result.append(meta_result)
648
649
        cursor.close()
650
        cnx.close()
651
652
        resp.text = json.dumps(result)
653
654
class PhotovoltaicPowerStationExport:
655
    def __init__(self):
656
        """"Initializes Class"""
657
        pass
658
659
    @staticmethod
660
    def on_options(req, resp, id_):
661
        _ = req
662
        resp.status = falcon.HTTP_200
663
        _ = id_
664
665
    @staticmethod
666
    def on_get(req, resp, id_):
667
        access_control(req)
668
        if not id_.isdigit() or int(id_) <= 0:
669
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
670
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
671
672
        cnx = mysql.connector.connect(**config.myems_system_db)
673
        cursor = cnx.cursor()
674
675
        query = (" SELECT id, name, uuid "
676
                 " FROM tbl_contacts ")
677
        cursor.execute(query)
678
        rows_contacts = cursor.fetchall()
679
680
        contact_dict = dict()
681
        if rows_contacts is not None and len(rows_contacts) > 0:
682
            for row in rows_contacts:
683
                contact_dict[row[0]] = {"id": row[0],
684
                                        "name": row[1],
685
                                        "uuid": row[2]}
686
687
        query = (" SELECT id, name, uuid "
688
                 " FROM tbl_cost_centers ")
689
        cursor.execute(query)
690
        rows_cost_centers = cursor.fetchall()
691
692
        cost_center_dict = dict()
693
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
694
            for row in rows_cost_centers:
695
                cost_center_dict[row[0]] = {"id": row[0],
696
                                            "name": row[1],
697
                                            "uuid": row[2]}
698
699
        query = (" SELECT id, name, uuid "
700
                 " FROM tbl_svgs ")
701
        cursor.execute(query)
702
        rows_svgs = cursor.fetchall()
703
704
        svg_dict = dict()
705
        if rows_svgs is not None and len(rows_svgs) > 0:
706
            for row in rows_svgs:
707
                svg_dict[row[0]] = {"id": row[0],
708
                                    "name": row[1],
709
                                    "uuid": row[2]}
710
711
        query = (" SELECT id, name, uuid, "
712
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
713
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
714
                 " FROM tbl_photovoltaic_power_stations "
715
                 " WHERE id = %s ")
716
        cursor.execute(query, (id_,))
717
        row = cursor.fetchone()
718
        cursor.close()
719
        cnx.close()
720
721 View Code Duplication
        if row is None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
722
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
723
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
724
        else:
725
            meta_result = {"name": row[1],
726
                           "uuid": row[2],
727
                           "station_code": row[3],
728
                           "address": row[4],
729
                           "latitude": row[5],
730
                           "longitude": row[6],
731
                           "rated_capacity": row[7],
732
                           "rated_power": row[8],
733
                           "contact": contact_dict.get(row[9], None),
734
                           "cost_center": cost_center_dict.get(row[10], None),
735
                           "svg": svg_dict.get(row[11], None),
736
                           "is_cost_data_displayed": bool(row[12]),
737
                           "phase_of_lifecycle": row[13],
738
                           "description": row[14]}
739
740
        resp.text = json.dumps(meta_result)
741
742
743
class PhotovoltaicPowerStationImport:
744
    def __init__(self):
745
        """"Initializes PhotovoltaicPowerStationImport"""
746
        pass
747
748
    @staticmethod
749
    def on_options(req, resp):
750
        _ = req
751
        resp.status = falcon.HTTP_200
752
753
    @staticmethod
754
    @user_logger
755
    def on_post(req, resp):
756
        """Handles POST requests"""
757
        admin_control(req)
758
        try:
759
            raw_json = req.stream.read().decode('utf-8')
760
        except Exception as ex:
761
            print(str(ex))
762
            raise falcon.HTTPError(status=falcon.HTTP_400,
763
                                   title='API.BAD_REQUEST',
764
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
765
766
        new_values = json.loads(raw_json)
767
768
        if 'name' not in new_values.keys() or \
769
                not isinstance(new_values['name'], str) or \
770
                len(str.strip(new_values['name'])) == 0:
771
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
772
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_NAME')
773
        name = str.strip(new_values['name'])
774
775
        if 'station_code' not in new_values.keys() or \
776
                not isinstance(new_values['station_code'], str) or \
777
                len(str.strip(new_values['station_code'])) == 0:
778
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
779
                                   description='API.INVALID_STATION_CODE')
780
        station_code = str.strip(new_values['station_code'])
781
782
        if 'address' not in new_values.keys() or \
783
                not isinstance(new_values['address'], str) or \
784
                len(str.strip(new_values['address'])) == 0:
785
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
786
                                   description='API.INVALID_ADDRESS_VALUE')
787
        address = str.strip(new_values['address'])
788
789
        if 'latitude' not in new_values.keys() or \
790
                not (isinstance(new_values['latitude'], float) or
791
                     isinstance(new_values['latitude'], int)) or \
792
                new_values['latitude'] < -90.0 or \
793
                new_values['latitude'] > 90.0:
794
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
795
                                   description='API.INVALID_LATITUDE_VALUE')
796
        latitude = new_values['latitude']
797
798
        if 'longitude' not in new_values.keys() or \
799
                not (isinstance(new_values['longitude'], float) or
800
                     isinstance(new_values['longitude'], int)) or \
801
                new_values['longitude'] < -180.0 or \
802
                new_values['longitude'] > 180.0:
803
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
804
                                   description='API.INVALID_LONGITUDE_VALUE')
805
        longitude = new_values['longitude']
806
807
        if 'rated_capacity' not in new_values.keys() or \
808
                not (isinstance(new_values['rated_capacity'], float) or
809
                     isinstance(new_values['rated_capacity'], int)) or \
810
                new_values['rated_capacity'] <= 0.0:
811
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
812
                                   description='API.INVALID_RATED_CAPACITY')
813
        rated_capacity = new_values['rated_capacity']
814
815
        if 'rated_power' not in new_values.keys() or \
816
                not (isinstance(new_values['rated_power'], float) or
817
                     isinstance(new_values['rated_power'], int)) or \
818
                new_values['rated_power'] <= 0.0:
819
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
820
                                   description='API.INVALID_RATED_POWER')
821
        rated_power = new_values['rated_power']
822
823
        if 'contact' not in new_values.keys() or \
824
                'id' not in new_values['contact'].keys() or \
825
                not isinstance(new_values['contact']['id'], int) or \
826
                new_values['contact']['id'] <= 0:
827
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
828
                                   description='API.INVALID_CONTACT_ID')
829
        contact_id = new_values['contact']['id']
830
831
        if 'cost_center' not in new_values.keys() or \
832
                'id' not in new_values['cost_center'].keys() or \
833
                not isinstance(new_values['cost_center']['id'], int) or \
834
                new_values['cost_center']['id'] <= 0:
835
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
836
                                   description='API.INVALID_COST_CENTER_ID')
837
        cost_center_id = new_values['cost_center']['id']
838
839
        if 'svg' not in new_values.keys() or \
840
                'id' not in new_values['svg'].keys() or \
841
                not isinstance(new_values['svg']['id'], int) or \
842
                new_values['svg']['id'] <= 0:
843
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
844
                                   description='API.INVALID_SVG_ID')
845
        svg_id = new_values['svg']['id']
846
847
        if 'is_cost_data_displayed' not in new_values.keys() or \
848
                not isinstance(new_values['is_cost_data_displayed'], bool):
849
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
850
                                   description='API.INVALID_IS_COST_DATA_DISPLAYED')
851
        is_cost_data_displayed = new_values['is_cost_data_displayed']
852
853
        if 'phase_of_lifecycle' not in new_values.keys() or \
854
                not isinstance(new_values['phase_of_lifecycle'], str) or \
855
                len(str.strip(new_values['phase_of_lifecycle'])) == 0:
856
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
857
                                   description='API.INVALID_PHASE_OF_LIFECYCLE')
858
        phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle'])
859
860
        if 'description' in new_values.keys() and \
861
                new_values['description'] is not None and \
862
                len(str(new_values['description'])) > 0:
863
            description = str.strip(new_values['description'])
864
        else:
865
            description = None
866
867
        cnx = mysql.connector.connect(**config.myems_system_db)
868
        cursor = cnx.cursor()
869
870
        cursor.execute(" SELECT name "
871
                       " FROM tbl_photovoltaic_power_stations "
872
                       " WHERE name = %s ", (name,))
873
        if cursor.fetchone() is not None:
874
            cursor.close()
875
            cnx.close()
876
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
877
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NAME_IS_ALREADY_IN_USE')
878
879
        cursor.execute(" SELECT name "
880
                       " FROM tbl_contacts "
881
                       " WHERE id = %s ",
882
                       (new_values['contact']['id'],))
883
        row = cursor.fetchone()
884
        if row is None:
885
            cursor.close()
886
            cnx.close()
887
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
888
                                   description='API.CONTACT_NOT_FOUND')
889
890
        cursor.execute(" SELECT name "
891
                       " FROM tbl_cost_centers "
892
                       " WHERE id = %s ",
893
                       (new_values['cost_center']['id'],))
894
        row = cursor.fetchone()
895
        if row is None:
896
            cursor.close()
897
            cnx.close()
898
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
899
                                   description='API.COST_CENTER_NOT_FOUND')
900
901
        cursor.execute(" SELECT name "
902
                       " FROM tbl_svgs "
903
                       " WHERE id = %s ",
904
                       (svg_id,))
905
        row = cursor.fetchone()
906
        if row is None:
907
            cursor.close()
908
            cnx.close()
909
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
910
                                   description='API.SVG_NOT_FOUND')
911
912
        add_values = (" INSERT INTO tbl_photovoltaic_power_stations "
913
                      "    (name, uuid, station_code, address, latitude, longitude, "
914
                      "     rated_capacity, rated_power, "
915
                      "     contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle,"
916
                      "     description) "
917
                      " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
918
        cursor.execute(add_values, (name,
919
                                    str(uuid.uuid4()),
920
                                    station_code,
921
                                    address,
922
                                    latitude,
923
                                    longitude,
924
                                    rated_capacity,
925
                                    rated_power,
926
                                    contact_id,
927
                                    cost_center_id,
928
                                    svg_id,
929
                                    is_cost_data_displayed,
930
                                    phase_of_lifecycle,
931
                                    description))
932
        new_id = cursor.lastrowid
933
        cnx.commit()
934
        cursor.close()
935
        cnx.close()
936
937
        resp.status = falcon.HTTP_201
938
        resp.location = '/photovoltaicpowerstations/' + str(new_id)
939
940
941
class PhotovoltaicPowerStationClone:
942
    def __init__(self):
943
        """"Initializes PhotovoltaicPowerStationClone"""
944
        pass
945
946
    @staticmethod
947
    def on_options(req, resp, id_):
948
        _ = req
949
        resp.status = falcon.HTTP_200
950
        _ = id_
951
952
    @staticmethod
953
    @user_logger
954
    def on_post(req, resp, id_):
955
        admin_control(req)
956
        if not id_.isdigit() or int(id_) <= 0:
957
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
958
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
959
960
        cnx = mysql.connector.connect(**config.myems_system_db)
961
        cursor = cnx.cursor()
962
963
        query = (" SELECT id, name, uuid, "
964
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
965
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
966
                 " FROM tbl_photovoltaic_power_stations "
967
                 " WHERE id = %s ")
968
        cursor.execute(query, (id_,))
969
        row = cursor.fetchone()
970
971
        if row is None:
972
            cursor.close()
973
            cnx.close()
974
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
975
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
976
        else:
977
            meta_result = {"name": row[1],
978
                           "uuid": row[2],
979
                           "station_code": row[3],
980
                           "address": row[4],
981
                           "latitude": row[5],
982
                           "longitude": row[6],
983
                           "rated_capacity": row[7],
984
                           "rated_power": row[8],
985
                           "contact_id": row[9],
986
                           "cost_center_id": row[10],
987
                           "svg_id": row[11],
988
                           "is_cost_data_displayed": row[12],
989
                           "phase_of_lifecycle": row[13],
990
                           "description": row[14]}
991
992
            timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
993
            if config.utc_offset[0] == '-':
994
                timezone_offset = -timezone_offset
995
            new_name = str.strip(meta_result['name']) + \
996
                (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')
997
998
            add_values = (" INSERT INTO tbl_photovoltaic_power_stations "
999
                          "    (name, uuid, station_code, address, latitude, longitude, "
1000
                          "     rated_capacity, rated_power, "
1001
                          "     contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, "
1002
                          "     description) "
1003
                          " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ")
1004
            cursor.execute(add_values, (new_name,
1005
                                        str(uuid.uuid4()),
1006
                                        meta_result['station_code'],
1007
                                        meta_result['address'],
1008
                                        meta_result['latitude'],
1009
                                        meta_result['longitude'],
1010
                                        meta_result['rated_capacity'],
1011
                                        meta_result['rated_power'],
1012
                                        meta_result['contact_id'],
1013
                                        meta_result['cost_center_id'],
1014
                                        meta_result['svg_id'],
1015
                                        meta_result['is_cost_data_displayed'],
1016
                                        meta_result['phase_of_lifecycle'],
1017
                                        meta_result['description']))
1018
            new_id = cursor.lastrowid
1019
            cnx.commit()
1020
            cursor.close()
1021
            cnx.close()
1022
1023
            resp.status = falcon.HTTP_201
1024
            resp.location = '/photovoltaicpowerstations/' + str(new_id)
1025
1026
1027
class PhotovoltaicPowerStationGridCollection:
1028
    def __init__(self):
1029
        """Initializes Class"""
1030
        pass
1031
1032
    @staticmethod
1033
    def on_options(req, resp, id_):
1034
        _ = req
1035
        resp.status = falcon.HTTP_200
1036
        _ = id_
1037
1038 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1039
    def on_get(req, resp, id_):
1040
        access_control(req)
1041
        if not id_.isdigit() or int(id_) <= 0:
1042
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1043
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1044
1045
        cnx = mysql.connector.connect(**config.myems_system_db)
1046
        cursor = cnx.cursor()
1047
1048
        cursor.execute(" SELECT name "
1049
                       " FROM tbl_photovoltaic_power_stations "
1050
                       " WHERE id = %s ", (id_,))
1051
        if cursor.fetchone() is None:
1052
            cursor.close()
1053
            cnx.close()
1054
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1055
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1056
1057
        # query meter dict
1058
        query = (" SELECT id, name, uuid "
1059
                 " FROM tbl_meters ")
1060
        cursor.execute(query)
1061
        rows_meters = cursor.fetchall()
1062
1063
        meter_dict = dict()
1064
        if rows_meters is not None and len(rows_meters) > 0:
1065
            for row in rows_meters:
1066
                meter_dict[row[0]] = {"id": row[0],
1067
                                      "name": row[1],
1068
                                      "uuid": row[2]}
1069
        # query point dict
1070
        query = (" SELECT id, name "
1071
                 " FROM tbl_points ")
1072
        cursor.execute(query)
1073
        rows_points = cursor.fetchall()
1074
1075
        point_dict = dict()
1076
        if rows_points is not None and len(rows_points) > 0:
1077
            for row in rows_points:
1078
                point_dict[row[0]] = {"id": row[0],
1079
                                      "name": row[1]}
1080
1081
        query = (" SELECT id, name, uuid, "
1082
                 "        power_point_id, buy_meter_id, sell_meter_id, capacity, "
1083
                 "        total_active_power_point_id, "
1084
                 "        active_power_a_point_id, "
1085
                 "        active_power_b_point_id, "
1086
                 "        active_power_c_point_id, "
1087
                 "        total_reactive_power_point_id, "
1088
                 "        reactive_power_a_point_id, "
1089
                 "        reactive_power_b_point_id, "
1090
                 "        reactive_power_c_point_id, "
1091
                 "        total_apparent_power_point_id, "
1092
                 "        apparent_power_a_point_id, "
1093
                 "        apparent_power_b_point_id, "
1094
                 "        apparent_power_c_point_id, "
1095
                 "        total_power_factor_point_id, "
1096
                 "        active_energy_import_point_id, "
1097
                 "        active_energy_export_point_id, "
1098
                 "        active_energy_net_point_id "
1099
                 " FROM tbl_photovoltaic_power_stations_grids "
1100
                 " WHERE photovoltaic_power_station_id = %s "
1101
                 " ORDER BY name ")
1102
        cursor.execute(query, (id_,))
1103
        rows = cursor.fetchall()
1104
1105
        result = list()
1106
        if rows is not None and len(rows) > 0:
1107
            for row in rows:
1108
                meta_result = {"id": row[0],
1109
                               "name": row[1],
1110
                               "uuid": row[2],
1111
                               "power_point": point_dict.get(row[3]),
1112
                               "buy_meter": meter_dict.get(row[4]),
1113
                               "sell_meter": meter_dict.get(row[5]),
1114
                               "capacity": row[6],
1115
                               "total_active_power_point": point_dict.get(row[7]),
1116
                               "active_power_a_point": point_dict.get(row[8]),
1117
                               "active_power_b_point": point_dict.get(row[9]),
1118
                               "active_power_c_point": point_dict.get(row[10]),
1119
                               "total_reactive_power_point": point_dict.get(row[11]),
1120
                               "reactive_power_a_point": point_dict.get(row[12]),
1121
                               "reactive_power_b_point": point_dict.get(row[13]),
1122
                               "reactive_power_c_point": point_dict.get(row[14]),
1123
                               "total_apparent_power_point": point_dict.get(row[15]),
1124
                               "apparent_power_a_point": point_dict.get(row[16]),
1125
                               "apparent_power_b_point": point_dict.get(row[17]),
1126
                               "apparent_power_c_point": point_dict.get(row[19]),
1127
                               "total_power_factor_point": point_dict.get(row[19]),
1128
                               "active_energy_import_point": point_dict.get(row[20]),
1129
                               "active_energy_export_point": point_dict.get(row[21]),
1130
                               "active_energy_net_point_id": point_dict.get(row[22]),
1131
                               }
1132
                result.append(meta_result)
1133
1134
        resp.text = json.dumps(result)
1135
1136
    @staticmethod
1137
    @user_logger
1138
    def on_post(req, resp, id_):
1139
        """Handles POST requests"""
1140
        admin_control(req)
1141
        try:
1142
            raw_json = req.stream.read().decode('utf-8')
1143
        except Exception as ex:
1144
            print(str(ex))
1145
            raise falcon.HTTPError(status=falcon.HTTP_400,
1146
                                   title='API.BAD_REQUEST',
1147
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1148
        if not id_.isdigit() or int(id_) <= 0:
1149
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1150
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1151
1152
        cnx = mysql.connector.connect(**config.myems_system_db)
1153
        cursor = cnx.cursor()
1154
1155
        cursor.execute(" SELECT name "
1156
                       " FROM tbl_photovoltaic_power_stations "
1157
                       " WHERE id = %s ", (id_,))
1158
        if cursor.fetchone() is None:
1159
            cursor.close()
1160
            cnx.close()
1161
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1162
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1163
1164
        new_values = json.loads(raw_json)
1165
1166
        if 'name' not in new_values['data'].keys() or \
1167
                not isinstance(new_values['data']['name'], str) or \
1168
                len(str.strip(new_values['data']['name'])) == 0:
1169
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1170
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_NAME')
1171
        name = str.strip(new_values['data']['name'])
1172
1173
        if 'power_point_id' not in new_values['data'].keys() or \
1174
                not isinstance(new_values['data']['power_point_id'], int) or \
1175
                new_values['data']['power_point_id'] <= 0:
1176
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1177
                                   description='API.INVALID_POWER_POINT_ID')
1178
        power_point_id = new_values['data']['power_point_id']
1179
1180
        if 'buy_meter_id' not in new_values['data'].keys() or \
1181
                not isinstance(new_values['data']['buy_meter_id'], int) or \
1182
                new_values['data']['buy_meter_id'] <= 0:
1183
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1184
                                   description='API.INVALID_BUY_METER_ID')
1185
        buy_meter_id = new_values['data']['buy_meter_id']
1186
1187
        if 'sell_meter_id' not in new_values['data'].keys() or \
1188
                not isinstance(new_values['data']['sell_meter_id'], int) or \
1189
                new_values['data']['sell_meter_id'] <= 0:
1190
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1191
                                   description='API.INVALID_SELL_METER_ID')
1192
        sell_meter_id = new_values['data']['sell_meter_id']
1193
1194
        if 'capacity' not in new_values['data'].keys() or \
1195
                not (isinstance(new_values['data']['capacity'], float) or
1196
                     isinstance(new_values['data']['capacity'], int)):
1197
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1198
                                   description='API.INVALID_CAPACITY')
1199
        capacity = Decimal(new_values['data']['capacity'])
1200
1201
        total_active_power_point_id = None
1202
        active_power_a_point_id = None
1203
        active_power_b_point_id = None
1204
        active_power_c_point_id = None
1205
        total_reactive_power_point_id = None
1206
        reactive_power_a_point_id = None
1207
        reactive_power_b_point_id = None
1208
        reactive_power_c_point_id = None
1209
        total_apparent_power_point_id = None
1210
        apparent_power_a_point_id = None
1211
        apparent_power_b_point_id = None
1212
        apparent_power_c_point_id = None
1213
        total_power_factor_point_id = None
1214
        active_energy_import_point_id = None
1215
        active_energy_export_point_id = None
1216
        active_energy_net_point_id = None
1217
1218
        if 'total_active_power_point_id' in new_values['data'].keys() and \
1219
                isinstance(new_values['data']['total_active_power_point_id'], int) and \
1220
                new_values['data']['total_active_power_point_id'] > 0:
1221
            total_active_power_point_id = new_values['data']['total_active_power_point_id']
1222
1223
        if 'active_power_a_point_id' in new_values['data'].keys() and \
1224
                isinstance(new_values['data']['active_power_a_point_id'], int) and \
1225
                new_values['data']['active_power_a_point_id'] > 0:
1226
            active_power_a_point_id = new_values['data']['active_power_a_point_id']
1227
1228
        if 'active_power_b_point_id' in new_values['data'].keys() and \
1229
                isinstance(new_values['data']['active_power_b_point_id'], int) and \
1230
                new_values['data']['active_power_b_point_id'] > 0:
1231
            active_power_b_point_id = new_values['data']['active_power_b_point_id']
1232
1233
        if 'active_power_c_point_id' in new_values['data'].keys() and \
1234
                isinstance(new_values['data']['active_power_c_point_id'], int) and \
1235
                new_values['data']['active_power_c_point_id'] > 0:
1236
            active_power_c_point_id = new_values['data']['active_power_c_point_id']
1237
1238
        if 'total_reactive_power_point_id' in new_values['data'].keys() and \
1239
                isinstance(new_values['data']['total_reactive_power_point_id'], int) and \
1240
                new_values['data']['total_reactive_power_point_id'] > 0:
1241
            total_reactive_power_point_id = new_values['data']['total_reactive_power_point_id']
1242
1243
        if 'reactive_power_a_point_id' in new_values['data'].keys() and \
1244
                isinstance(new_values['data']['reactive_power_a_point_id'], int) and \
1245
                new_values['data']['reactive_power_a_point_id'] > 0:
1246
            reactive_power_a_point_id = new_values['data']['reactive_power_a_point_id']
1247
1248
        if 'reactive_power_b_point_id' in new_values['data'].keys() and \
1249
                isinstance(new_values['data']['reactive_power_b_point_id'], int) and \
1250
                new_values['data']['reactive_power_b_point_id'] > 0:
1251
            reactive_power_b_point_id = new_values['data']['reactive_power_b_point_id']
1252
1253
        if 'reactive_power_c_point_id' in new_values['data'].keys() and \
1254
                isinstance(new_values['data']['reactive_power_c_point_id'], int) and \
1255
                new_values['data']['reactive_power_c_point_id'] > 0:
1256
            reactive_power_c_point_id = new_values['data']['reactive_power_c_point_id']
1257
1258
        if 'total_apparent_power_point_id' in new_values['data'].keys() and \
1259
                isinstance(new_values['data']['total_apparent_power_point_id'], int) and \
1260
                new_values['data']['total_apparent_power_point_id'] > 0:
1261
            total_apparent_power_point_id = new_values['data']['total_apparent_power_point_id']
1262
1263
        if 'apparent_power_a_point_id' in new_values['data'].keys() and \
1264
                isinstance(new_values['data']['apparent_power_a_point_id'], int) and \
1265
                new_values['data']['apparent_power_a_point_id'] > 0:
1266
            apparent_power_a_point_id = new_values['data']['apparent_power_a_point_id']
1267
1268
        if 'apparent_power_b_point_id' in new_values['data'].keys() and \
1269
                isinstance(new_values['data']['apparent_power_b_point_id'], int) and \
1270
                new_values['data']['apparent_power_b_point_id'] > 0:
1271
            apparent_power_b_point_id = new_values['data']['apparent_power_b_point_id']
1272
1273
        if 'apparent_power_c_point_id' in new_values['data'].keys() and \
1274
                isinstance(new_values['data']['apparent_power_c_point_id'], int) and \
1275
                new_values['data']['apparent_power_c_point_id'] > 0:
1276
            apparent_power_c_point_id = new_values['data']['apparent_power_c_point_id']
1277
1278
        if 'total_power_factor_point_id' in new_values['data'].keys() and \
1279
                isinstance(new_values['data']['total_power_factor_point_id'], int) and \
1280
                new_values['data']['total_power_factor_point_id'] > 0:
1281
            total_power_factor_point_id = new_values['data']['total_power_factor_point_id']
1282
1283
        if 'active_energy_import_point_id' in new_values['data'].keys() and \
1284
                isinstance(new_values['data']['active_energy_import_point_id'], int) and \
1285
                new_values['data']['active_energy_import_point_id'] > 0:
1286
            active_energy_import_point_id = new_values['data']['active_energy_import_point_id']
1287
1288
        if 'active_energy_export_point_id' in new_values['data'].keys() and \
1289
                isinstance(new_values['data']['active_energy_export_point_id'], int) and \
1290
                new_values['data']['active_energy_export_point_id'] > 0:
1291
            active_energy_export_point_id = new_values['data']['active_energy_export_point_id']
1292
1293
        if 'active_energy_net_point_id' in new_values['data'].keys() and \
1294
                isinstance(new_values['data']['active_energy_net_point_id'], int) and \
1295
                new_values['data']['active_energy_net_point_id'] > 0:
1296
            active_energy_net_point_id = new_values['data']['active_energy_net_point_id']
1297
1298
        cnx = mysql.connector.connect(**config.myems_system_db)
1299
        cursor = cnx.cursor()
1300
1301
        cursor.execute(" SELECT name "
1302
                       " FROM tbl_photovoltaic_power_stations "
1303
                       " WHERE id = %s ",
1304
                       (id_,))
1305
        if cursor.fetchone() is None:
1306
            cursor.close()
1307
            cnx.close()
1308
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1309
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1310
1311
        cursor.execute(" SELECT name "
1312
                       " FROM tbl_photovoltaic_power_stations_grids "
1313
                       " WHERE photovoltaic_power_station_id = %s AND name = %s ",
1314
                       (id_, name,))
1315
        if cursor.fetchone() is not None:
1316
            cursor.close()
1317
            cnx.close()
1318
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1319
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NAME_IS_ALREADY_IN_USE')
1320
1321
        cursor.execute(" SELECT name "
1322
                       " FROM tbl_points "
1323
                       " WHERE id = %s ",
1324
                       (power_point_id,))
1325
        if cursor.fetchone() is None:
1326
            cursor.close()
1327
            cnx.close()
1328
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1329
                                   description='API.POWER_POINT_NOT_FOUND')
1330
1331
        cursor.execute(" SELECT name "
1332
                       " FROM tbl_meters "
1333
                       " WHERE id = %s ",
1334
                       (buy_meter_id,))
1335
        if cursor.fetchone() is None:
1336
            cursor.close()
1337
            cnx.close()
1338
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1339
                                   description='API.BUY_METER_NOT_FOUND')
1340
1341
        cursor.execute(" SELECT name "
1342
                       " FROM tbl_meters "
1343
                       " WHERE id = %s ",
1344
                       (sell_meter_id,))
1345
        if cursor.fetchone() is None:
1346
            cursor.close()
1347
            cnx.close()
1348
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1349
                                   description='API.SELL_METER_NOT_FOUND')
1350
1351
        add_values = (" INSERT INTO tbl_photovoltaic_power_stations_grids "
1352
                      "    (name, uuid, photovoltaic_power_station_id, power_point_id, "
1353
                      "     buy_meter_id, sell_meter_id, capacity, "
1354
                      "     total_active_power_point_id, "
1355
                      "     active_power_a_point_id, "
1356
                      "     active_power_b_point_id, "
1357
                      "     active_power_c_point_id, "
1358
                      "     total_reactive_power_point_id, "
1359
                      "     reactive_power_a_point_id, "
1360
                      "     reactive_power_b_point_id, "
1361
                      "     reactive_power_c_point_id, "
1362
                      "     total_apparent_power_point_id, "
1363
                      "     apparent_power_a_point_id, "
1364
                      "     apparent_power_b_point_id, "
1365
                      "     apparent_power_c_point_id, "
1366
                      "     total_power_factor_point_id, "
1367
                      "     active_energy_import_point_id, "
1368
                      "     active_energy_export_point_id, "
1369
                      "     active_energy_net_point_id) "
1370
                      " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
1371
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
1372
                      "         %s, %s, %s) ")
1373
        cursor.execute(add_values, (name,
1374
                                    str(uuid.uuid4()),
1375
                                    id_,
1376
                                    power_point_id,
1377
                                    buy_meter_id,
1378
                                    sell_meter_id,
1379
                                    capacity,
1380
                                    total_active_power_point_id,
1381
                                    active_power_a_point_id,
1382
                                    active_power_b_point_id,
1383
                                    active_power_c_point_id,
1384
                                    total_reactive_power_point_id,
1385
                                    reactive_power_a_point_id,
1386
                                    reactive_power_b_point_id,
1387
                                    reactive_power_c_point_id,
1388
                                    total_apparent_power_point_id,
1389
                                    apparent_power_a_point_id,
1390
                                    apparent_power_b_point_id,
1391
                                    apparent_power_c_point_id,
1392
                                    total_power_factor_point_id,
1393
                                    active_energy_import_point_id,
1394
                                    active_energy_export_point_id,
1395
                                    active_energy_net_point_id
1396
                                    ))
1397
        new_id = cursor.lastrowid
1398
        cnx.commit()
1399
        cursor.close()
1400
        cnx.close()
1401
1402
        resp.status = falcon.HTTP_201
1403
        resp.location = '/photovoltaics/' + str(id_) + '/grids/' + str(new_id)
1404
1405
1406
class PhotovoltaicPowerStationGridItem:
1407
    def __init__(self):
1408
        """Initializes Class"""
1409
        pass
1410
1411
    @staticmethod
1412
    def on_options(req, resp, id_, gid):
1413
        _ = req
1414
        resp.status = falcon.HTTP_200
1415
        _ = id_
1416
1417 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1418
    def on_get(req, resp, id_, gid):
1419
        access_control(req)
1420
        if not id_.isdigit() or int(id_) <= 0:
1421
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1422
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1423
        if not gid.isdigit() or int(gid) <= 0:
1424
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1425
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_ID')
1426
1427
        cnx = mysql.connector.connect(**config.myems_system_db)
1428
        cursor = cnx.cursor()
1429
1430
        cursor.execute(" SELECT name "
1431
                       " FROM tbl_photovoltaic_power_stations "
1432
                       " WHERE id = %s ", (id_,))
1433
        if cursor.fetchone() is None:
1434
            cursor.close()
1435
            cnx.close()
1436
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1437
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1438
1439
        query = (" SELECT id, name, uuid "
1440
                 " FROM tbl_photovoltaic_power_stations ")
1441
        cursor.execute(query)
1442
        rows_photovoltaic_power_stations = cursor.fetchall()
1443
1444
        photovoltaic_power_station_dict = dict()
1445
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
1446
            for row in rows_photovoltaic_power_stations:
1447
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
1448
                                                           "name": row[1],
1449
                                                           "uuid": row[2]}
1450
        # query meter dict
1451
        query = (" SELECT id, name, uuid "
1452
                 " FROM tbl_meters ")
1453
        cursor.execute(query)
1454
        rows_meters = cursor.fetchall()
1455
1456
        meter_dict = dict()
1457
        if rows_meters is not None and len(rows_meters) > 0:
1458
            for row in rows_meters:
1459
                meter_dict[row[0]] = {"id": row[0],
1460
                                      "name": row[1],
1461
                                      "uuid": row[2]}
1462
        # query point dict
1463
        query = (" SELECT id, name "
1464
                 " FROM tbl_points ")
1465
        cursor.execute(query)
1466
        rows_points = cursor.fetchall()
1467
1468
        point_dict = dict()
1469
        if rows_points is not None and len(rows_points) > 0:
1470
            for row in rows_points:
1471
                point_dict[row[0]] = {"id": row[0],
1472
                                      "name": row[1]}
1473
1474
        query = (" SELECT id, name, uuid, "
1475
                 "        photovoltaic_power_station_id, "
1476
                 "        power_point_id, "
1477
                 "        buy_meter_id, "
1478
                 "        sell_meter_id, "
1479
                 "        capacity, "
1480
                 "        total_active_power_point_id, "
1481
                 "        active_power_a_point_id, "
1482
                 "        active_power_b_point_id, "
1483
                 "        active_power_c_point_id, "
1484
                 "        total_reactive_power_point_id, "
1485
                 "        reactive_power_a_point_id, "
1486
                 "        reactive_power_b_point_id, "
1487
                 "        reactive_power_c_point_id, "
1488
                 "        total_apparent_power_point_id, "
1489
                 "        apparent_power_a_point_id, "
1490
                 "        apparent_power_b_point_id, "
1491
                 "        apparent_power_c_point_id, "
1492
                 "        total_power_factor_point_id, "
1493
                 "        active_energy_import_point_id, "
1494
                 "        active_energy_export_point_id, "
1495
                 "        active_energy_net_point_id "
1496
                 " FROM tbl_photovoltaic_power_stations_grids "
1497
                 " WHERE id = %s ")
1498
        cursor.execute(query, (gid,))
1499
        row = cursor.fetchone()
1500
        cursor.close()
1501
        cnx.close()
1502
1503
        if row is None:
1504
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1505
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NOT_FOUND')
1506
        else:
1507
            meta_result = {"id": row[0],
1508
                           "name": row[1],
1509
                           "uuid": row[2],
1510
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
1511
                           "power_point": point_dict.get(row[4]),
1512
                           "buy_meter": meter_dict.get(row[5]),
1513
                           "sell_meter": meter_dict.get(row[6]),
1514
                           "capacity": row[7],
1515
                           "total_active_power_point": point_dict.get(row[8]),
1516
                           "active_power_a_point": point_dict.get(row[9]),
1517
                           "active_power_b_point": point_dict.get(row[10]),
1518
                           "active_power_c_point": point_dict.get(row[11]),
1519
                           "total_reactive_power_point": point_dict.get(row[12]),
1520
                           "reactive_power_a_point": point_dict.get(row[13]),
1521
                           "reactive_power_b_point": point_dict.get(row[14]),
1522
                           "reactive_power_c_point": point_dict.get(row[15]),
1523
                           "total_apparent_power_point": point_dict.get(row[16]),
1524
                           "apparent_power_a_point": point_dict.get(row[17]),
1525
                           "apparent_power_b_point": point_dict.get(row[18]),
1526
                           "apparent_power_c_point": point_dict.get(row[19]),
1527
                           "total_power_factor_point": point_dict.get(row[20]),
1528
                           "active_energy_import_point": point_dict.get(row[21]),
1529
                           "active_energy_export_point": point_dict.get(row[22]),
1530
                           "active_energy_net_point_id": point_dict.get(row[23]),
1531
                           }
1532
1533
        resp.text = json.dumps(meta_result)
1534
1535
    @staticmethod
1536
    @user_logger
1537
    def on_delete(req, resp, id_, gid):
1538
        admin_control(req)
1539
        if not id_.isdigit() or int(id_) <= 0:
1540
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1541
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1542
        if not gid.isdigit() or int(gid) <= 0:
1543
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1544
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_ID')
1545
1546
        cnx = mysql.connector.connect(**config.myems_system_db)
1547
        cursor = cnx.cursor()
1548
1549
        cursor.execute(" SELECT name "
1550
                       " FROM tbl_photovoltaic_power_stations "
1551
                       " WHERE id = %s ", (id_,))
1552
        if cursor.fetchone() is None:
1553
            cursor.close()
1554
            cnx.close()
1555
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1556
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1557
1558
        cursor.execute(" SELECT name "
1559
                       " FROM tbl_photovoltaic_power_stations_grids "
1560
                       " WHERE id = %s ", (gid,))
1561
        if cursor.fetchone() is None:
1562
            cursor.close()
1563
            cnx.close()
1564
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1565
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NOT_FOUND')
1566
1567
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_grids "
1568
                       " WHERE id = %s ", (gid,))
1569
        cnx.commit()
1570
1571
        cursor.close()
1572
        cnx.close()
1573
1574
        resp.status = falcon.HTTP_204
1575
1576
    @staticmethod
1577
    @user_logger
1578
    def on_put(req, resp, id_, gid):
1579
        """Handles PUT requests"""
1580
        admin_control(req)
1581
        try:
1582
            raw_json = req.stream.read().decode('utf-8')
1583
        except Exception as ex:
1584
            print(str(ex))
1585
            raise falcon.HTTPError(status=falcon.HTTP_400,
1586
                                   title='API.BAD_REQUEST',
1587
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1588
        if not id_.isdigit() or int(id_) <= 0:
1589
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1590
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1591
1592
        if not gid.isdigit() or int(gid) <= 0:
1593
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1594
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_ID')
1595
1596
        new_values = json.loads(raw_json)
1597
1598
        if 'name' not in new_values['data'].keys() or \
1599
                not isinstance(new_values['data']['name'], str) or \
1600
                len(str.strip(new_values['data']['name'])) == 0:
1601
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1602
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_GRID_NAME')
1603
        name = str.strip(new_values['data']['name'])
1604
1605
        if 'power_point_id' not in new_values['data'].keys() or \
1606
                not isinstance(new_values['data']['power_point_id'], int) or \
1607
                new_values['data']['power_point_id'] <= 0:
1608
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1609
                                   description='API.INVALID_POWER_POINT_ID')
1610
        power_point_id = new_values['data']['power_point_id']
1611
1612
        if 'buy_meter_id' not in new_values['data'].keys() or \
1613
                not isinstance(new_values['data']['buy_meter_id'], int) or \
1614
                new_values['data']['buy_meter_id'] <= 0:
1615
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1616
                                   description='API.INVALID_BUY_METER_ID')
1617
        buy_meter_id = new_values['data']['buy_meter_id']
1618
1619
        if 'sell_meter_id' not in new_values['data'].keys() or \
1620
                not isinstance(new_values['data']['sell_meter_id'], int) or \
1621
                new_values['data']['sell_meter_id'] <= 0:
1622
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1623
                                   description='API.INVALID_METER_ID')
1624
        sell_meter_id = new_values['data']['sell_meter_id']
1625
1626
        if 'capacity' not in new_values['data'].keys() or \
1627
                not (isinstance(new_values['data']['capacity'], float) or
1628
                     isinstance(new_values['data']['capacity'], int)):
1629
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1630
                                   description='API.INVALID_CAPACITY')
1631
        capacity = Decimal(new_values['data']['capacity'])
1632
        total_active_power_point_id = None
1633
        active_power_a_point_id = None
1634
        active_power_b_point_id = None
1635
        active_power_c_point_id = None
1636
        total_reactive_power_point_id = None
1637
        reactive_power_a_point_id = None
1638
        reactive_power_b_point_id = None
1639
        reactive_power_c_point_id = None
1640
        total_apparent_power_point_id = None
1641
        apparent_power_a_point_id = None
1642
        apparent_power_b_point_id = None
1643
        apparent_power_c_point_id = None
1644
        total_power_factor_point_id = None
1645
        active_energy_import_point_id = None
1646
        active_energy_export_point_id = None
1647
        active_energy_net_point_id = None
1648
1649
        if 'total_active_power_point_id' in new_values['data'].keys() and \
1650
                isinstance(new_values['data']['total_active_power_point_id'], int) and \
1651
                new_values['data']['total_active_power_point_id'] > 0:
1652
            total_active_power_point_id = new_values['data']['total_active_power_point_id']
1653
1654
        if 'active_power_a_point_id' in new_values['data'].keys() and \
1655
                isinstance(new_values['data']['active_power_a_point_id'], int) and \
1656
                new_values['data']['active_power_a_point_id'] > 0:
1657
            active_power_a_point_id = new_values['data']['active_power_a_point_id']
1658
1659
        if 'active_power_b_point_id' in new_values['data'].keys() and \
1660
                isinstance(new_values['data']['active_power_b_point_id'], int) and \
1661
                new_values['data']['active_power_b_point_id'] > 0:
1662
            active_power_b_point_id = new_values['data']['active_power_b_point_id']
1663
1664
        if 'active_power_c_point_id' in new_values['data'].keys() and \
1665
                isinstance(new_values['data']['active_power_c_point_id'], int) and \
1666
                new_values['data']['active_power_c_point_id'] > 0:
1667
            active_power_c_point_id = new_values['data']['active_power_c_point_id']
1668
1669
        if 'total_reactive_power_point_id' in new_values['data'].keys() and \
1670
                isinstance(new_values['data']['total_reactive_power_point_id'], int) and \
1671
                new_values['data']['total_reactive_power_point_id'] > 0:
1672
            total_reactive_power_point_id = new_values['data']['total_reactive_power_point_id']
1673
1674
        if 'reactive_power_a_point_id' in new_values['data'].keys() and \
1675
                isinstance(new_values['data']['reactive_power_a_point_id'], int) and \
1676
                new_values['data']['reactive_power_a_point_id'] > 0:
1677
            reactive_power_a_point_id = new_values['data']['reactive_power_a_point_id']
1678
1679
        if 'reactive_power_b_point_id' in new_values['data'].keys() and \
1680
                isinstance(new_values['data']['reactive_power_b_point_id'], int) and \
1681
                new_values['data']['reactive_power_b_point_id'] > 0:
1682
            reactive_power_b_point_id = new_values['data']['reactive_power_b_point_id']
1683
1684
        if 'reactive_power_c_point_id' in new_values['data'].keys() and \
1685
                isinstance(new_values['data']['reactive_power_c_point_id'], int) and \
1686
                new_values['data']['reactive_power_c_point_id'] > 0:
1687
            reactive_power_c_point_id = new_values['data']['reactive_power_c_point_id']
1688
1689
        if 'total_apparent_power_point_id' in new_values['data'].keys() and \
1690
                isinstance(new_values['data']['total_apparent_power_point_id'], int) and \
1691
                new_values['data']['total_apparent_power_point_id'] > 0:
1692
            total_apparent_power_point_id = new_values['data']['total_apparent_power_point_id']
1693
1694
        if 'apparent_power_a_point_id' in new_values['data'].keys() and \
1695
                isinstance(new_values['data']['apparent_power_a_point_id'], int) and \
1696
                new_values['data']['apparent_power_a_point_id'] > 0:
1697
            apparent_power_a_point_id = new_values['data']['apparent_power_a_point_id']
1698
1699
        if 'apparent_power_b_point_id' in new_values['data'].keys() and \
1700
                isinstance(new_values['data']['apparent_power_b_point_id'], int) and \
1701
                new_values['data']['apparent_power_b_point_id'] > 0:
1702
            apparent_power_b_point_id = new_values['data']['apparent_power_b_point_id']
1703
1704
        if 'apparent_power_c_point_id' in new_values['data'].keys() and \
1705
                isinstance(new_values['data']['apparent_power_c_point_id'], int) and \
1706
                new_values['data']['apparent_power_c_point_id'] > 0:
1707
            apparent_power_c_point_id = new_values['data']['apparent_power_c_point_id']
1708
1709
        if 'total_power_factor_point_id' in new_values['data'].keys() and \
1710
                isinstance(new_values['data']['total_power_factor_point_id'], int) and \
1711
                new_values['data']['total_power_factor_point_id'] > 0:
1712
            total_power_factor_point_id = new_values['data']['total_power_factor_point_id']
1713
1714
        if 'active_energy_import_point_id' in new_values['data'].keys() and \
1715
                isinstance(new_values['data']['active_energy_import_point_id'], int) and \
1716
                new_values['data']['active_energy_import_point_id'] > 0:
1717
            active_energy_import_point_id = new_values['data']['active_energy_import_point_id']
1718
1719
        if 'active_energy_export_point_id' in new_values['data'].keys() and \
1720
                isinstance(new_values['data']['active_energy_export_point_id'], int) and \
1721
                new_values['data']['active_energy_export_point_id'] > 0:
1722
            active_energy_export_point_id = new_values['data']['active_energy_export_point_id']
1723
1724
        if 'active_energy_net_point_id' in new_values['data'].keys() and \
1725
                isinstance(new_values['data']['active_energy_net_point_id'], int) and \
1726
                new_values['data']['active_energy_net_point_id'] > 0:
1727
            active_energy_net_point_id = new_values['data']['active_energy_net_point_id']
1728
1729
        cnx = mysql.connector.connect(**config.myems_system_db)
1730
        cursor = cnx.cursor()
1731
1732
        cursor.execute(" SELECT name "
1733
                       " FROM tbl_photovoltaic_power_stations "
1734
                       " WHERE id = %s ", (id_,))
1735
        if cursor.fetchone() is None:
1736
            cursor.close()
1737
            cnx.close()
1738
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1739
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1740
1741
        cursor.execute(" SELECT name "
1742
                       " FROM tbl_photovoltaic_power_stations_grids "
1743
                       " WHERE id = %s ", (gid,))
1744
        if cursor.fetchone() is None:
1745
            cursor.close()
1746
            cnx.close()
1747
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1748
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NOT_FOUND')
1749
1750
        cursor.execute(" SELECT name "
1751
                       " FROM tbl_photovoltaic_power_stations_grids "
1752
                       " WHERE photovoltaic_power_station_id = %s AND name = %s AND id != %s ",
1753
                       (id_, name, gid))
1754
        if cursor.fetchone() is not None:
1755
            cursor.close()
1756
            cnx.close()
1757
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1758
                                   description='API.PHOTOVOLTAIC_POWER_STATION_GRID_NAME_IS_ALREADY_IN_USE')
1759
1760
        cursor.execute(" SELECT name "
1761
                       " FROM tbl_points "
1762
                       " WHERE id = %s ",
1763
                       (power_point_id,))
1764
        if cursor.fetchone() is None:
1765
            cursor.close()
1766
            cnx.close()
1767
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1768
                                   description='API.POWER_POINT_NOT_FOUND')
1769
1770
        cursor.execute(" SELECT name "
1771
                       " FROM tbl_meters "
1772
                       " WHERE id = %s ",
1773
                       (buy_meter_id,))
1774
        if cursor.fetchone() is None:
1775
            cursor.close()
1776
            cnx.close()
1777
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1778
                                   description='API.BUY_METER_NOT_FOUND')
1779
1780
        cursor.execute(" SELECT name "
1781
                       " FROM tbl_meters "
1782
                       " WHERE id = %s ",
1783
                       (sell_meter_id,))
1784
        if cursor.fetchone() is None:
1785
            cursor.close()
1786
            cnx.close()
1787
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1788
                                   description='API.SELL_METER_NOT_FOUND')
1789
1790
        update_row = (" UPDATE tbl_photovoltaic_power_stations_grids "
1791
                      " SET name = %s, photovoltaic_power_station_id = %s, "
1792
                      "     power_point_id = %s, buy_meter_id = %s, sell_meter_id = %s, capacity = %s, "
1793
                      "     total_active_power_point_id = %s, "
1794
                      "     active_power_a_point_id = %s, "
1795
                      "     active_power_b_point_id = %s, "
1796
                      "     active_power_c_point_id = %s, "
1797
                      "     total_reactive_power_point_id = %s, "
1798
                      "     reactive_power_a_point_id = %s, "
1799
                      "     reactive_power_b_point_id = %s, "
1800
                      "     reactive_power_c_point_id = %s, "
1801
                      "     total_apparent_power_point_id = %s, "
1802
                      "     apparent_power_a_point_id = %s, "
1803
                      "     apparent_power_b_point_id = %s, "
1804
                      "     apparent_power_c_point_id = %s, "
1805
                      "     total_power_factor_point_id = %s, "
1806
                      "     active_energy_import_point_id = %s, "
1807
                      "     active_energy_export_point_id = %s, "
1808
                      "     active_energy_net_point_id = %s "
1809
                      "     WHERE id = %s ")
1810
        cursor.execute(update_row, (name,
1811
                                    id_,
1812
                                    power_point_id,
1813
                                    buy_meter_id,
1814
                                    sell_meter_id,
1815
                                    capacity,
1816
                                    total_active_power_point_id,
1817
                                    active_power_a_point_id,
1818
                                    active_power_b_point_id,
1819
                                    active_power_c_point_id,
1820
                                    total_reactive_power_point_id,
1821
                                    reactive_power_a_point_id,
1822
                                    reactive_power_b_point_id,
1823
                                    reactive_power_c_point_id,
1824
                                    total_apparent_power_point_id,
1825
                                    apparent_power_a_point_id,
1826
                                    apparent_power_b_point_id,
1827
                                    apparent_power_c_point_id,
1828
                                    total_power_factor_point_id,
1829
                                    active_energy_import_point_id,
1830
                                    active_energy_export_point_id,
1831
                                    active_energy_net_point_id,
1832
                                    gid))
1833
        cnx.commit()
1834
1835
        cursor.close()
1836
        cnx.close()
1837
1838
        resp.status = falcon.HTTP_200
1839
1840
1841
class PhotovoltaicPowerStationInvertorCollection:
1842
    def __init__(self):
1843
        """Initializes Class"""
1844
        pass
1845
1846
    @staticmethod
1847
    def on_options(req, resp, id_):
1848
        _ = req
1849
        resp.status = falcon.HTTP_200
1850
        _ = id_
1851
1852
    @staticmethod
1853
    def on_get(req, resp, id_):
1854
        access_control(req)
1855
        if not id_.isdigit() or int(id_) <= 0:
1856
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1857
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
1858
1859
        cnx = mysql.connector.connect(**config.myems_system_db)
1860
        cursor = cnx.cursor()
1861
1862
        cursor.execute(" SELECT name "
1863
                       " FROM tbl_photovoltaic_power_stations "
1864
                       " WHERE id = %s ", (id_,))
1865
        if cursor.fetchone() is None:
1866
            cursor.close()
1867
            cnx.close()
1868
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1869
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
1870
1871
        # query point dict
1872
        query = (" SELECT id, name "
1873
                 " FROM tbl_points ")
1874
        cursor.execute(query)
1875
        rows_points = cursor.fetchall()
1876
1877
        point_dict = dict()
1878
        if rows_points is not None and len(rows_points) > 0:
1879
            for row in rows_points:
1880
                point_dict[row[0]] = {"id": row[0],
1881
                                      "name": row[1]}
1882
1883
        # query meter dict
1884
        query = (" SELECT id, name "
1885
                 " FROM tbl_meters ")
1886
        cursor.execute(query)
1887
        rows_meters = cursor.fetchall()
1888
1889
        meter_dict = dict()
1890
        if rows_meters is not None and len(rows_meters) > 0:
1891
            for row in rows_meters:
1892
                meter_dict[row[0]] = {"id": row[0],
1893
                                      "name": row[1]}
1894
1895
        query = (" SELECT id, name, uuid, "
1896
                 "        model, "
1897
                 "        serial_number, "
1898
                 "        invertor_state_point_id, "
1899
                 "        communication_state_point_id, "
1900
                 "        total_energy_point_id, "
1901
                 "        generation_meter_id, "
1902
                 "        today_energy_point_id, "
1903
                 "        efficiency_point_id, "
1904
                 "        temperature_point_id, "
1905
                 "        power_factor_point_id, "
1906
                 "        active_power_point_id, "
1907
                 "        reactive_power_point_id, "
1908
                 "        frequency_point_id, "
1909
                 "        uab_point_id, "
1910
                 "        ubc_point_id, "
1911
                 "        uca_point_id, "
1912
                 "        ua_point_id, "
1913
                 "        ub_point_id, "
1914
                 "        uc_point_id, "
1915
                 "        ia_point_id, "
1916
                 "        ib_point_id, "
1917
                 "        ic_point_id, "
1918
                 "        pv1_u_point_id, "
1919
                 "        pv1_i_point_id, "
1920
                 "        pv2_u_point_id, "
1921
                 "        pv2_i_point_id, "
1922
                 "        pv3_u_point_id, "
1923
                 "        pv3_i_point_id, "
1924
                 "        pv4_u_point_id, "
1925
                 "        pv4_i_point_id, "
1926
                 "        pv5_u_point_id, "
1927
                 "        pv5_i_point_id, "
1928
                 "        pv6_u_point_id, "
1929
                 "        pv6_i_point_id, "
1930
                 "        pv7_u_point_id, "
1931
                 "        pv7_i_point_id, "
1932
                 "        pv8_u_point_id, "
1933
                 "        pv8_i_point_id, "
1934
                 "        pv9_u_point_id, "
1935
                 "        pv9_i_point_id, "
1936
                 "        pv10_u_point_id, "
1937
                 "        pv10_i_point_id, "
1938
                 "        pv11_u_point_id, "
1939
                 "        pv11_i_point_id, "
1940
                 "        pv12_u_point_id, "
1941
                 "        pv12_i_point_id, "
1942
                 "        pv13_u_point_id, "
1943
                 "        pv13_i_point_id, "
1944
                 "        pv14_u_point_id, "
1945
                 "        pv14_i_point_id, "
1946
                 "        pv15_u_point_id, "
1947
                 "        pv15_i_point_id, "
1948
                 "        pv16_u_point_id, "
1949
                 "        pv16_i_point_id, "
1950
                 "        pv17_u_point_id, "
1951
                 "        pv17_i_point_id, "
1952
                 "        pv18_u_point_id, "
1953
                 "        pv18_i_point_id, "
1954
                 "        pv19_u_point_id, "
1955
                 "        pv19_i_point_id, "
1956
                 "        pv20_u_point_id, "
1957
                 "        pv20_i_point_id, "
1958
                 "        pv21_u_point_id, "
1959
                 "        pv21_i_point_id, "
1960
                 "        pv22_u_point_id, "
1961
                 "        pv22_i_point_id, "
1962
                 "        pv23_u_point_id, "
1963
                 "        pv23_i_point_id, "
1964
                 "        pv24_u_point_id, "
1965
                 "        pv24_i_point_id, "
1966
                 "        pv25_u_point_id, "
1967
                 "        pv25_i_point_id, "
1968
                 "        pv26_u_point_id, "
1969
                 "        pv26_i_point_id, "
1970
                 "        pv27_u_point_id, "
1971
                 "        pv27_i_point_id, "
1972
                 "        pv28_u_point_id, "
1973
                 "        pv28_i_point_id, "
1974
                 "        mppt_total_energy_point_id, "
1975
                 "        mppt_power_point_id, "
1976
                 "        mppt_1_energy_point_id, "
1977
                 "        mppt_2_energy_point_id, "
1978
                 "        mppt_3_energy_point_id, "
1979
                 "        mppt_4_energy_point_id, "
1980
                 "        mppt_5_energy_point_id, "
1981
                 "        mppt_6_energy_point_id, "
1982
                 "        mppt_7_energy_point_id, "
1983
                 "        mppt_8_energy_point_id, "
1984
                 "        mppt_9_energy_point_id, "
1985
                 "        mppt_10_energy_point_id "
1986
                 " FROM tbl_photovoltaic_power_stations_invertors "
1987
                 " WHERE photovoltaic_power_station_id = %s "
1988
                 " ORDER BY name ")
1989
        cursor.execute(query, (id_,))
1990
        rows = cursor.fetchall()
1991
1992
        result = list()
1993
        if rows is not None and len(rows) > 0:
1994
            for row in rows:
1995
                meta_result = {"id": row[0],
1996
                               "name": row[1],
1997
                               "uuid": row[2],
1998
                               "model": row[3],
1999
                               "serial_number": row[4],
2000
                               "invertor_state_point": point_dict.get(row[5]),
2001
                               "communication_state_point": point_dict.get(row[6]),
2002
                               "total_energy_point": point_dict.get(row[7]),
2003
                               "generation_meter": meter_dict.get(row[8]),
2004
                               "today_energy_point": point_dict.get(row[9]),
2005
                               "efficiency_point": point_dict.get(row[10]),
2006
                               "temperature_point": point_dict.get(row[11]),
2007
                               "power_factor_point": point_dict.get(row[12]),
2008
                               "active_power_point": point_dict.get(row[13]),
2009
                               "reactive_power_point": point_dict.get(row[14]),
2010
                               "frequency_point": point_dict.get(row[15]),
2011
                               "uab_point": point_dict.get(row[16]),
2012
                               "ubc_point": point_dict.get(row[17]),
2013
                               "uca_point": point_dict.get(row[18]),
2014
                               "ua_point": point_dict.get(row[19]),
2015
                               "ub_point": point_dict.get(row[20]),
2016
                               "uc_point": point_dict.get(row[21]),
2017
                               "ia_point": point_dict.get(row[22]),
2018
                               "ib_point": point_dict.get(row[23]),
2019
                               "ic_point": point_dict.get(row[24]),
2020
                               "pv1_u_point": point_dict.get(row[25]),
2021
                               "pv1_i_point": point_dict.get(row[26]),
2022
                               "pv2_u_point": point_dict.get(row[27]),
2023
                               "pv2_i_point": point_dict.get(row[28]),
2024
                               "pv3_u_point": point_dict.get(row[29]),
2025
                               "pv3_i_point": point_dict.get(row[30]),
2026
                               "pv4_u_point": point_dict.get(row[31]),
2027
                               "pv4_i_point": point_dict.get(row[32]),
2028
                               "pv5_u_point": point_dict.get(row[33]),
2029
                               "pv5_i_point": point_dict.get(row[34]),
2030
                               "pv6_u_point": point_dict.get(row[35]),
2031
                               "pv6_i_point": point_dict.get(row[36]),
2032
                               "pv7_u_point": point_dict.get(row[37]),
2033
                               "pv7_i_point": point_dict.get(row[38]),
2034
                               "pv8_u_point": point_dict.get(row[39]),
2035
                               "pv8_i_point": point_dict.get(row[40]),
2036
                               "pv9_u_point": point_dict.get(row[41]),
2037
                               "pv9_i_point": point_dict.get(row[42]),
2038
                               "pv10_u_point": point_dict.get(row[43]),
2039
                               "pv10_i_point": point_dict.get(row[44]),
2040
                               "pv11_u_point": point_dict.get(row[45]),
2041
                               "pv11_i_point": point_dict.get(row[46]),
2042
                               "pv12_u_point": point_dict.get(row[47]),
2043
                               "pv12_i_point": point_dict.get(row[48]),
2044
                               "pv13_u_point": point_dict.get(row[49]),
2045
                               "pv13_i_point": point_dict.get(row[50]),
2046
                               "pv14_u_point": point_dict.get(row[51]),
2047
                               "pv14_i_point": point_dict.get(row[52]),
2048
                               "pv15_u_point": point_dict.get(row[53]),
2049
                               "pv15_i_point": point_dict.get(row[54]),
2050
                               "pv16_u_point": point_dict.get(row[55]),
2051
                               "pv16_i_point": point_dict.get(row[56]),
2052
                               "pv17_u_point": point_dict.get(row[57]),
2053
                               "pv17_i_point": point_dict.get(row[58]),
2054
                               "pv18_u_point": point_dict.get(row[59]),
2055
                               "pv18_i_point": point_dict.get(row[60]),
2056
                               "pv19_u_point": point_dict.get(row[61]),
2057
                               "pv19_i_point": point_dict.get(row[62]),
2058
                               "pv20_u_point": point_dict.get(row[63]),
2059
                               "pv20_i_point": point_dict.get(row[64]),
2060
                               "pv21_u_point": point_dict.get(row[65]),
2061
                               "pv21_i_point": point_dict.get(row[66]),
2062
                               "pv22_u_point": point_dict.get(row[67]),
2063
                               "pv22_i_point": point_dict.get(row[68]),
2064
                               "pv23_u_point": point_dict.get(row[69]),
2065
                               "pv23_i_point": point_dict.get(row[70]),
2066
                               "pv24_u_point": point_dict.get(row[71]),
2067
                               "pv24_i_point": point_dict.get(row[72]),
2068
                               "pv25_u_point": point_dict.get(row[73]),
2069
                               "pv25_i_point": point_dict.get(row[74]),
2070
                               "pv26_u_point": point_dict.get(row[75]),
2071
                               "pv26_i_point": point_dict.get(row[76]),
2072
                               "pv27_u_point": point_dict.get(row[77]),
2073
                               "pv27_i_point": point_dict.get(row[78]),
2074
                               "pv28_u_point": point_dict.get(row[79]),
2075
                               "pv28_i_point": point_dict.get(row[80]),
2076
                               "mppt_total_energy_point": point_dict.get(row[81]),
2077
                               "mppt_power_point": point_dict.get(row[82]),
2078
                               "mppt_1_energy_point": point_dict.get(row[83]),
2079
                               "mppt_2_energy_point": point_dict.get(row[84]),
2080
                               "mppt_3_energy_point": point_dict.get(row[85]),
2081
                               "mppt_4_energy_point": point_dict.get(row[85]),
2082
                               "mppt_5_energy_point": point_dict.get(row[87]),
2083
                               "mppt_6_energy_point": point_dict.get(row[88]),
2084
                               "mppt_7_energy_point": point_dict.get(row[89]),
2085
                               "mppt_8_energy_point": point_dict.get(row[90]),
2086
                               "mppt_9_energy_point": point_dict.get(row[91]),
2087
                               "mppt_10_energy_point": point_dict.get(row[92]),
2088
                               }
2089
                result.append(meta_result)
2090
2091
        resp.text = json.dumps(result)
2092
2093
    @staticmethod
2094
    @user_logger
2095
    def on_post(req, resp, id_):
2096
        """Handles POST requests"""
2097
        admin_control(req)
2098
        try:
2099
            raw_json = req.stream.read().decode('utf-8')
2100
        except Exception as ex:
2101
            print(str(ex))
2102
            raise falcon.HTTPError(status=falcon.HTTP_400,
2103
                                   title='API.BAD_REQUEST',
2104
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2105
        if not id_.isdigit() or int(id_) <= 0:
2106
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2107
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
2108
2109
        cnx = mysql.connector.connect(**config.myems_system_db)
2110
        cursor = cnx.cursor()
2111
2112
        cursor.execute(" SELECT name "
2113
                       " FROM tbl_photovoltaic_power_stations "
2114
                       " WHERE id = %s ", (id_,))
2115
        if cursor.fetchone() is None:
2116
            cursor.close()
2117
            cnx.close()
2118
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2119
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
2120
2121
        new_values = json.loads(raw_json)
2122
2123
        if 'name' not in new_values['data'].keys() or \
2124
                not isinstance(new_values['data']['name'], str) or \
2125
                len(str.strip(new_values['data']['name'])) == 0:
2126
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2127
                                   description='API.INVALID_NAME')
2128
        name = str.strip(new_values['data']['name'])
2129
2130
        if 'model' not in new_values['data'].keys() or \
2131
                not isinstance(new_values['data']['model'], str) or \
2132
                len(str.strip(new_values['data']['model'])) == 0:
2133
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2134
                                   description='API.INVALID_MODEL')
2135
        model = str.strip(new_values['data']['model'])
2136
2137
        if 'serial_number' not in new_values['data'].keys() or \
2138
                not isinstance(new_values['data']['serial_number'], str) or \
2139
                len(str.strip(new_values['data']['serial_number'])) == 0:
2140
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2141
                                   description='API.INVALID_SERIAL_NUMBER')
2142
        serial_number = str.strip(new_values['data']['serial_number'])
2143
2144
        if 'invertor_state_point_id' not in new_values['data'].keys() or \
2145
                not isinstance(new_values['data']['invertor_state_point_id'], int) or \
2146
                new_values['data']['invertor_state_point_id'] <= 0:
2147
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2148
                                   description='API.INVALID_INVERTOR_STATE_POINT_ID')
2149
        invertor_state_point_id = new_values['data']['invertor_state_point_id']
2150
2151
        if 'communication_state_point_id' not in new_values['data'].keys() or \
2152
                not isinstance(new_values['data']['communication_state_point_id'], int) or \
2153
                new_values['data']['communication_state_point_id'] <= 0:
2154
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2155
                                   description='API.INVALID_COMMUNICATION_STATE_POINT_ID')
2156
        communication_state_point_id = new_values['data']['communication_state_point_id']
2157
2158
        if 'total_energy_point_id' not in new_values['data'].keys() or \
2159
                not isinstance(new_values['data']['total_energy_point_id'], int) or \
2160
                new_values['data']['total_energy_point_id'] <= 0:
2161
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2162
                                   description='API.INVALID_TOTAL_ENERGY_POINT_ID')
2163
        total_energy_point_id = new_values['data']['total_energy_point_id']
2164
2165
        if 'generation_meter_id' not in new_values['data'].keys() or \
2166
                not isinstance(new_values['data']['generation_meter_id'], int) or \
2167
                new_values['data']['generation_meter_id'] <= 0:
2168
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2169
                                   description='API.INVALID_GENERATION_METER_ID')
2170
        generation_meter_id = new_values['data']['generation_meter_id']
2171
2172
        today_energy_point_id = None
2173
        efficiency_point_id = None
2174
        temperature_point_id = None
2175
        power_factor_point_id = None
2176
        active_power_point_id = None
2177
        reactive_power_point_id = None
2178
        frequency_point_id = None
2179
        uab_point_id = None
2180
        ubc_point_id = None
2181
        uca_point_id = None
2182
        ua_point_id = None
2183
        ub_point_id = None
2184
        uc_point_id = None
2185
        ia_point_id = None
2186
        ib_point_id = None
2187
        ic_point_id = None
2188
        pv1_u_point_id = None
2189
        pv1_i_point_id = None
2190
        pv2_u_point_id = None
2191
        pv2_i_point_id = None
2192
        pv3_u_point_id = None
2193
        pv3_i_point_id = None
2194
        pv4_u_point_id = None
2195
        pv4_i_point_id = None
2196
        pv5_u_point_id = None
2197
        pv5_i_point_id = None
2198
        pv6_u_point_id = None
2199
        pv6_i_point_id = None
2200
        pv7_u_point_id = None
2201
        pv7_i_point_id = None
2202
        pv8_u_point_id = None
2203
        pv8_i_point_id = None
2204
        pv9_u_point_id = None
2205
        pv9_i_point_id = None
2206
        pv10_u_point_id = None
2207
        pv10_i_point_id = None
2208
        pv11_u_point_id = None
2209
        pv11_i_point_id = None
2210
        pv12_u_point_id = None
2211
        pv12_i_point_id = None
2212
        pv13_u_point_id = None
2213
        pv13_i_point_id = None
2214
        pv14_u_point_id = None
2215
        pv14_i_point_id = None
2216
        pv15_u_point_id = None
2217
        pv15_i_point_id = None
2218
        pv16_u_point_id = None
2219
        pv16_i_point_id = None
2220
        pv17_u_point_id = None
2221
        pv17_i_point_id = None
2222
        pv18_u_point_id = None
2223
        pv18_i_point_id = None
2224
        pv19_u_point_id = None
2225
        pv19_i_point_id = None
2226
        pv20_u_point_id = None
2227
        pv20_i_point_id = None
2228
        pv21_u_point_id = None
2229
        pv21_i_point_id = None
2230
        pv22_u_point_id = None
2231
        pv22_i_point_id = None
2232
        pv23_u_point_id = None
2233
        pv23_i_point_id = None
2234
        pv24_u_point_id = None
2235
        pv24_i_point_id = None
2236
        pv25_u_point_id = None
2237
        pv25_i_point_id = None
2238
        pv26_u_point_id = None
2239
        pv26_i_point_id = None
2240
        pv27_u_point_id = None
2241
        pv27_i_point_id = None
2242
        pv28_u_point_id = None
2243
        pv28_i_point_id = None
2244
        mppt_total_energy_point_id = None
2245
        mppt_power_point_id = None
2246
        mppt_1_energy_point_id = None
2247
        mppt_2_energy_point_id = None
2248
        mppt_3_energy_point_id = None
2249
        mppt_4_energy_point_id = None
2250
        mppt_5_energy_point_id = None
2251
        mppt_6_energy_point_id = None
2252
        mppt_7_energy_point_id = None
2253
        mppt_8_energy_point_id = None
2254
        mppt_9_energy_point_id = None
2255
        mppt_10_energy_point_id = None
2256
2257
        if 'today_energy_point_id' in new_values['data'].keys() and \
2258
                isinstance(new_values['data']['today_energy_point_id'], int) and \
2259
                new_values['data']['today_energy_point_id'] > 0:
2260
            today_energy_point_id = new_values['data']['today_energy_point_id']
2261
2262
        if 'efficiency_point_id' in new_values['data'].keys() and \
2263
                isinstance(new_values['data']['efficiency_point_id'], int) and \
2264
                new_values['data']['efficiency_point_id'] > 0:
2265
            efficiency_point_id = new_values['data']['efficiency_point_id']
2266
2267
        if 'temperature_point_id' in new_values['data'].keys() and \
2268
                isinstance(new_values['data']['temperature_point_id'], int) and \
2269
                new_values['data']['temperature_point_id'] > 0:
2270
            temperature_point_id = new_values['data']['temperature_point_id']
2271
2272
        if 'power_factor_point_id' in new_values['data'].keys() and \
2273
                isinstance(new_values['data']['power_factor_point_id'], int) and \
2274
                new_values['data']['power_factor_point_id'] > 0:
2275
            power_factor_point_id = new_values['data']['power_factor_point_id']
2276
2277
        if 'active_power_point_id' in new_values['data'].keys() and \
2278
                isinstance(new_values['data']['active_power_point_id'], int) and \
2279
                new_values['data']['active_power_point_id'] > 0:
2280
            active_power_point_id = new_values['data']['active_power_point_id']
2281
2282
        if 'reactive_power_point_id' in new_values['data'].keys() and \
2283
                isinstance(new_values['data']['reactive_power_point_id'], int) and \
2284
                new_values['data']['reactive_power_point_id'] > 0:
2285
            reactive_power_point_id = new_values['data']['reactive_power_point_id']
2286
2287
        if 'frequency_point_id' in new_values['data'].keys() and \
2288
                isinstance(new_values['data']['frequency_point_id'], int) and \
2289
                new_values['data']['frequency_point_id'] > 0:
2290
            frequency_point_id = new_values['data']['frequency_point_id']
2291
2292
        if 'uab_point_id' in new_values['data'].keys() and \
2293
                isinstance(new_values['data']['uab_point_id'], int) and \
2294
                new_values['data']['uab_point_id'] > 0:
2295
            uab_point_id = new_values['data']['uab_point_id']
2296
2297
        if 'ubc_point_id' in new_values['data'].keys() and \
2298
                isinstance(new_values['data']['ubc_point_id'], int) and \
2299
                new_values['data']['ubc_point_id'] > 0:
2300
            ubc_point_id = new_values['data']['ubc_point_id']
2301
2302
        if 'uca_point_id' in new_values['data'].keys() and \
2303
                isinstance(new_values['data']['uca_point_id'], int) and \
2304
                new_values['data']['uca_point_id'] > 0:
2305
            uca_point_id = new_values['data']['uca_point_id']
2306
2307
        if 'ua_point_id' in new_values['data'].keys() and \
2308
                isinstance(new_values['data']['ua_point_id'], int) and \
2309
                new_values['data']['ua_point_id'] > 0:
2310
            ua_point_id = new_values['data']['ua_point_id']
2311
2312
        if 'ub_point_id' in new_values['data'].keys() and \
2313
                isinstance(new_values['data']['ub_point_id'], int) and \
2314
                new_values['data']['ub_point_id'] > 0:
2315
            ub_point_id = new_values['data']['ub_point_id']
2316
2317
        if 'uc_point_id' in new_values['data'].keys() and \
2318
                isinstance(new_values['data']['uc_point_id'], int) and \
2319
                new_values['data']['uc_point_id'] > 0:
2320
            uc_point_id = new_values['data']['uc_point_id']
2321
2322
        if 'ia_point_id' in new_values['data'].keys() and \
2323
                isinstance(new_values['data']['ia_point_id'], int) and \
2324
                new_values['data']['ia_point_id'] > 0:
2325
            ia_point_id = new_values['data']['ia_point_id']
2326
2327
        if 'ib_point_id' in new_values['data'].keys() and \
2328
                isinstance(new_values['data']['ib_point_id'], int) and \
2329
                new_values['data']['ib_point_id'] > 0:
2330
            ib_point_id = new_values['data']['ib_point_id']
2331
2332
        if 'ic_point_id' in new_values['data'].keys() and \
2333
                isinstance(new_values['data']['ic_point_id'], int) and \
2334
                new_values['data']['ic_point_id'] > 0:
2335
            ic_point_id = new_values['data']['ic_point_id']
2336
2337
        if 'pv1_u_point_id' in new_values['data'].keys() and \
2338
                isinstance(new_values['data']['pv1_u_point_id'], int) and \
2339
                new_values['data']['pv1_u_point_id'] > 0:
2340
            pv1_u_point_id = new_values['data']['pv1_u_point_id']
2341
2342
        if 'pv1_i_point_id' in new_values['data'].keys() and \
2343
                isinstance(new_values['data']['pv1_i_point_id'], int) and \
2344
                new_values['data']['pv1_i_point_id'] > 0:
2345
            pv1_i_point_id = new_values['data']['pv1_i_point_id']
2346
2347
        if 'pv2_u_point_id' in new_values['data'].keys() and \
2348
                isinstance(new_values['data']['pv2_u_point_id'], int) and \
2349
                new_values['data']['pv2_u_point_id'] > 0:
2350
            pv2_u_point_id = new_values['data']['pv2_u_point_id']
2351
2352
        if 'pv2_i_point_id' in new_values['data'].keys() and \
2353
                isinstance(new_values['data']['pv2_i_point_id'], int) and \
2354
                new_values['data']['pv2_i_point_id'] > 0:
2355
            pv2_i_point_id = new_values['data']['pv2_i_point_id']
2356
2357
        if 'pv3_u_point_id' in new_values['data'].keys() and \
2358
                isinstance(new_values['data']['pv3_u_point_id'], int) and \
2359
                new_values['data']['pv3_u_point_id'] > 0:
2360
            pv3_u_point_id = new_values['data']['pv3_u_point_id']
2361
2362
        if 'pv3_i_point_id' in new_values['data'].keys() and \
2363
                isinstance(new_values['data']['pv3_i_point_id'], int) and \
2364
                new_values['data']['pv3_i_point_id'] > 0:
2365
            pv3_i_point_id = new_values['data']['pv3_i_point_id']
2366
2367
        if 'pv4_u_point_id' in new_values['data'].keys() and \
2368
                isinstance(new_values['data']['pv4_u_point_id'], int) and \
2369
                new_values['data']['pv4_u_point_id'] > 0:
2370
            pv4_u_point_id = new_values['data']['pv4_u_point_id']
2371
2372
        if 'pv4_i_point_id' in new_values['data'].keys() and \
2373
                isinstance(new_values['data']['pv4_i_point_id'], int) and \
2374
                new_values['data']['pv4_i_point_id'] > 0:
2375
            pv4_i_point_id = new_values['data']['pv4_i_point_id']
2376
2377
        if 'pv5_u_point_id' in new_values['data'].keys() and \
2378
                isinstance(new_values['data']['pv5_u_point_id'], int) and \
2379
                new_values['data']['pv5_u_point_id'] > 0:
2380
            pv5_u_point_id = new_values['data']['pv5_u_point_id']
2381
2382
        if 'pv5_i_point_id' in new_values['data'].keys() and \
2383
                isinstance(new_values['data']['pv5_i_point_id'], int) and \
2384
                new_values['data']['pv5_i_point_id'] > 0:
2385
            pv5_i_point_id = new_values['data']['pv5_i_point_id']
2386
2387
        if 'pv6_u_point_id' in new_values['data'].keys() and \
2388
                isinstance(new_values['data']['pv6_u_point_id'], int) and \
2389
                new_values['data']['pv6_u_point_id'] > 0:
2390
            pv6_u_point_id = new_values['data']['pv6_u_point_id']
2391
2392
        if 'pv6_i_point_id' in new_values['data'].keys() and \
2393
                isinstance(new_values['data']['pv6_i_point_id'], int) and \
2394
                new_values['data']['pv6_i_point_id'] > 0:
2395
            pv6_i_point_id = new_values['data']['pv6_i_point_id']
2396
2397
        if 'pv7_u_point_id' in new_values['data'].keys() and \
2398
                isinstance(new_values['data']['pv7_u_point_id'], int) and \
2399
                new_values['data']['pv7_u_point_id'] > 0:
2400
            pv7_u_point_id = new_values['data']['pv7_u_point_id']
2401
2402
        if 'pv7_i_point_id' in new_values['data'].keys() and \
2403
                isinstance(new_values['data']['pv7_i_point_id'], int) and \
2404
                new_values['data']['pv7_i_point_id'] > 0:
2405
            pv7_i_point_id = new_values['data']['pv7_i_point_id']
2406
2407
        if 'pv8_u_point_id' in new_values['data'].keys() and \
2408
                isinstance(new_values['data']['pv8_u_point_id'], int) and \
2409
                new_values['data']['pv8_u_point_id'] > 0:
2410
            pv8_u_point_id = new_values['data']['pv8_u_point_id']
2411
2412
        if 'pv8_i_point_id' in new_values['data'].keys() and \
2413
                isinstance(new_values['data']['pv8_i_point_id'], int) and \
2414
                new_values['data']['pv8_i_point_id'] > 0:
2415
            pv8_i_point_id = new_values['data']['pv8_i_point_id']
2416
2417
        if 'pv9_u_point_id' in new_values['data'].keys() and \
2418
                isinstance(new_values['data']['pv9_u_point_id'], int) and \
2419
                new_values['data']['pv9_u_point_id'] > 0:
2420
            pv9_u_point_id = new_values['data']['pv9_u_point_id']
2421
2422
        if 'pv9_i_point_id' in new_values['data'].keys() and \
2423
                isinstance(new_values['data']['pv9_i_point_id'], int) and \
2424
                new_values['data']['pv9_i_point_id'] > 0:
2425
            pv9_i_point_id = new_values['data']['pv9_i_point_id']
2426
2427
        if 'pv10_u_point_id' in new_values['data'].keys() and \
2428
                isinstance(new_values['data']['pv10_u_point_id'], int) and \
2429
                new_values['data']['pv10_u_point_id'] > 0:
2430
            pv10_u_point_id = new_values['data']['pv10_u_point_id']
2431
2432
        if 'pv10_i_point_id' in new_values['data'].keys() and \
2433
                isinstance(new_values['data']['pv10_i_point_id'], int) and \
2434
                new_values['data']['pv10_i_point_id'] > 0:
2435
            pv10_i_point_id = new_values['data']['pv10_i_point_id']
2436
2437
        if 'pv11_u_point_id' in new_values['data'].keys() and \
2438
                isinstance(new_values['data']['pv11_u_point_id'], int) and \
2439
                new_values['data']['pv11_u_point_id'] > 0:
2440
            pv11_u_point_id = new_values['data']['pv11_u_point_id']
2441
2442
        if 'pv11_i_point_id' in new_values['data'].keys() and \
2443
                isinstance(new_values['data']['pv11_i_point_id'], int) and \
2444
                new_values['data']['pv11_i_point_id'] > 0:
2445
            pv11_i_point_id = new_values['data']['pv11_i_point_id']
2446
2447
        if 'pv12_u_point_id' in new_values['data'].keys() and \
2448
                isinstance(new_values['data']['pv12_u_point_id'], int) and \
2449
                new_values['data']['pv12_u_point_id'] > 0:
2450
            pv12_u_point_id = new_values['data']['pv12_u_point_id']
2451
2452
        if 'pv12_i_point_id' in new_values['data'].keys() and \
2453
                isinstance(new_values['data']['pv12_i_point_id'], int) and \
2454
                new_values['data']['pv12_i_point_id'] > 0:
2455
            pv12_i_point_id = new_values['data']['pv12_i_point_id']
2456
2457
        if 'pv13_u_point_id' in new_values['data'].keys() and \
2458
                isinstance(new_values['data']['pv13_u_point_id'], int) and \
2459
                new_values['data']['pv13_u_point_id'] > 0:
2460
            pv13_u_point_id = new_values['data']['pv13_u_point_id']
2461
2462
        if 'pv13_i_point_id' in new_values['data'].keys() and \
2463
                isinstance(new_values['data']['pv13_i_point_id'], int) and \
2464
                new_values['data']['pv13_i_point_id'] > 0:
2465
            pv13_i_point_id = new_values['data']['pv13_i_point_id']
2466
2467
        if 'pv14_u_point_id' in new_values['data'].keys() and \
2468
                isinstance(new_values['data']['pv14_u_point_id'], int) and \
2469
                new_values['data']['pv14_u_point_id'] > 0:
2470
            pv14_u_point_id = new_values['data']['pv14_u_point_id']
2471
2472
        if 'pv14_i_point_id' in new_values['data'].keys() and \
2473
                isinstance(new_values['data']['pv14_i_point_id'], int) and \
2474
                new_values['data']['pv14_i_point_id'] > 0:
2475
            pv14_i_point_id = new_values['data']['pv14_i_point_id']
2476
2477
        if 'pv15_u_point_id' in new_values['data'].keys() and \
2478
                isinstance(new_values['data']['pv15_u_point_id'], int) and \
2479
                new_values['data']['pv15_u_point_id'] > 0:
2480
            pv15_u_point_id = new_values['data']['pv15_u_point_id']
2481
2482
        if 'pv15_i_point_id' in new_values['data'].keys() and \
2483
                isinstance(new_values['data']['pv15_i_point_id'], int) and \
2484
                new_values['data']['pv15_i_point_id'] > 0:
2485
            pv15_i_point_id = new_values['data']['pv15_i_point_id']
2486
2487
        if 'pv16_u_point_id' in new_values['data'].keys() and \
2488
                isinstance(new_values['data']['pv16_u_point_id'], int) and \
2489
                new_values['data']['pv16_u_point_id'] > 0:
2490
            pv16_u_point_id = new_values['data']['pv16_u_point_id']
2491
2492
        if 'pv16_i_point_id' in new_values['data'].keys() and \
2493
                isinstance(new_values['data']['pv16_i_point_id'], int) and \
2494
                new_values['data']['pv16_i_point_id'] > 0:
2495
            pv16_i_point_id = new_values['data']['pv16_i_point_id']
2496
2497
        if 'pv17_u_point_id' in new_values['data'].keys() and \
2498
                isinstance(new_values['data']['pv17_u_point_id'], int) and \
2499
                new_values['data']['pv17_u_point_id'] > 0:
2500
            pv17_u_point_id = new_values['data']['pv17_u_point_id']
2501
2502
        if 'pv17_i_point_id' in new_values['data'].keys() and \
2503
                isinstance(new_values['data']['pv17_i_point_id'], int) and \
2504
                new_values['data']['pv17_i_point_id'] > 0:
2505
            pv17_i_point_id = new_values['data']['pv17_i_point_id']
2506
2507
        if 'pv18_u_point_id' in new_values['data'].keys() and \
2508
                isinstance(new_values['data']['pv18_u_point_id'], int) and \
2509
                new_values['data']['pv18_u_point_id'] > 0:
2510
            pv18_u_point_id = new_values['data']['pv18_u_point_id']
2511
2512
        if 'pv18_i_point_id' in new_values['data'].keys() and \
2513
                isinstance(new_values['data']['pv18_i_point_id'], int) and \
2514
                new_values['data']['pv18_i_point_id'] > 0:
2515
            pv18_i_point_id = new_values['data']['pv18_i_point_id']
2516
2517
        if 'pv19_u_point_id' in new_values['data'].keys() and \
2518
                isinstance(new_values['data']['pv19_u_point_id'], int) and \
2519
                new_values['data']['pv19_u_point_id'] > 0:
2520
            pv19_u_point_id = new_values['data']['pv19_u_point_id']
2521
2522
        if 'pv19_i_point_id' in new_values['data'].keys() and \
2523
                isinstance(new_values['data']['pv19_i_point_id'], int) and \
2524
                new_values['data']['pv19_i_point_id'] > 0:
2525
            pv19_i_point_id = new_values['data']['pv19_i_point_id']
2526
2527
        if 'pv20_u_point_id' in new_values['data'].keys() and \
2528
                isinstance(new_values['data']['pv20_u_point_id'], int) and \
2529
                new_values['data']['pv20_u_point_id'] > 0:
2530
            pv20_u_point_id = new_values['data']['pv20_u_point_id']
2531
2532
        if 'pv20_i_point_id' in new_values['data'].keys() and \
2533
                isinstance(new_values['data']['pv20_i_point_id'], int) and \
2534
                new_values['data']['pv20_i_point_id'] > 0:
2535
            pv20_i_point_id = new_values['data']['pv20_i_point_id']
2536
2537
        if 'pv21_u_point_id' in new_values['data'].keys() and \
2538
                isinstance(new_values['data']['pv21_u_point_id'], int) and \
2539
                new_values['data']['pv21_u_point_id'] > 0:
2540
            pv21_u_point_id = new_values['data']['pv21_u_point_id']
2541
2542
        if 'pv21_i_point_id' in new_values['data'].keys() and \
2543
                isinstance(new_values['data']['pv21_i_point_id'], int) and \
2544
                new_values['data']['pv21_i_point_id'] > 0:
2545
            pv21_i_point_id = new_values['data']['pv21_i_point_id']
2546
2547
        if 'pv22_u_point_id' in new_values['data'].keys() and \
2548
                isinstance(new_values['data']['pv22_u_point_id'], int) and \
2549
                new_values['data']['pv22_u_point_id'] > 0:
2550
            pv22_u_point_id = new_values['data']['pv22_u_point_id']
2551
2552
        if 'pv22_i_point_id' in new_values['data'].keys() and \
2553
                isinstance(new_values['data']['pv22_i_point_id'], int) and \
2554
                new_values['data']['pv22_i_point_id'] > 0:
2555
            pv22_i_point_id = new_values['data']['pv22_i_point_id']
2556
2557
        if 'pv23_u_point_id' in new_values['data'].keys() and \
2558
                isinstance(new_values['data']['pv23_u_point_id'], int) and \
2559
                new_values['data']['pv23_u_point_id'] > 0:
2560
            pv23_u_point_id = new_values['data']['pv23_u_point_id']
2561
2562
        if 'pv23_i_point_id' in new_values['data'].keys() and \
2563
                isinstance(new_values['data']['pv23_i_point_id'], int) and \
2564
                new_values['data']['pv23_i_point_id'] > 0:
2565
            pv23_i_point_id = new_values['data']['pv23_i_point_id']
2566
2567
        if 'pv24_u_point_id' in new_values['data'].keys() and \
2568
                isinstance(new_values['data']['pv24_u_point_id'], int) and \
2569
                new_values['data']['pv24_u_point_id'] > 0:
2570
            pv24_u_point_id = new_values['data']['pv24_u_point_id']
2571
2572
        if 'pv24_i_point_id' in new_values['data'].keys() and \
2573
                isinstance(new_values['data']['pv24_i_point_id'], int) and \
2574
                new_values['data']['pv24_i_point_id'] > 0:
2575
            pv24_i_point_id = new_values['data']['pv24_i_point_id']
2576
2577
        if 'pv25_u_point_id' in new_values['data'].keys() and \
2578
                isinstance(new_values['data']['pv25_u_point_id'], int) and \
2579
                new_values['data']['pv25_u_point_id'] > 0:
2580
            pv25_u_point_id = new_values['data']['pv25_u_point_id']
2581
2582
        if 'pv25_i_point_id' in new_values['data'].keys() and \
2583
                isinstance(new_values['data']['pv25_i_point_id'], int) and \
2584
                new_values['data']['pv25_i_point_id'] > 0:
2585
            pv25_i_point_id = new_values['data']['pv25_i_point_id']
2586
2587
        if 'pv26_u_point_id' in new_values['data'].keys() and \
2588
                isinstance(new_values['data']['pv26_u_point_id'], int) and \
2589
                new_values['data']['pv26_u_point_id'] > 0:
2590
            pv26_u_point_id = new_values['data']['pv26_u_point_id']
2591
2592
        if 'pv26_i_point_id' in new_values['data'].keys() and \
2593
                isinstance(new_values['data']['pv26_i_point_id'], int) and \
2594
                new_values['data']['pv26_i_point_id'] > 0:
2595
            pv26_i_point_id = new_values['data']['pv26_i_point_id']
2596
2597
        if 'pv27_u_point_id' in new_values['data'].keys() and \
2598
                isinstance(new_values['data']['pv27_u_point_id'], int) and \
2599
                new_values['data']['pv27_u_point_id'] > 0:
2600
            pv27_u_point_id = new_values['data']['pv27_u_point_id']
2601
2602
        if 'pv27_i_point_id' in new_values['data'].keys() and \
2603
                isinstance(new_values['data']['pv27_i_point_id'], int) and \
2604
                new_values['data']['pv27_i_point_id'] > 0:
2605
            pv27_i_point_id = new_values['data']['pv27_i_point_id']
2606
2607
        if 'pv28_u_point_id' in new_values['data'].keys() and \
2608
                isinstance(new_values['data']['pv28_u_point_id'], int) and \
2609
                new_values['data']['pv28_u_point_id'] > 0:
2610
            pv28_u_point_id = new_values['data']['pv28_u_point_id']
2611
2612
        if 'pv28_i_point_id' in new_values['data'].keys() and \
2613
                isinstance(new_values['data']['pv28_i_point_id'], int) and \
2614
                new_values['data']['pv28_i_point_id'] > 0:
2615
            pv28_i_point_id = new_values['data']['pv28_i_point_id']
2616
2617
        if 'mppt_total_energy_point_id' in new_values['data'].keys() and \
2618
                isinstance(new_values['data']['mppt_total_energy_point_id'], int) and \
2619
                new_values['data']['mppt_total_energy_point_id'] > 0:
2620
            mppt_total_energy_point_id = new_values['data']['mppt_total_energy_point_id']
2621
2622
        if 'mppt_power_point_id' in new_values['data'].keys() and \
2623
                isinstance(new_values['data']['mppt_power_point_id'], int) and \
2624
                new_values['data']['mppt_power_point_id'] > 0:
2625
            mppt_power_point_id = new_values['data']['mppt_power_point_id']
2626
2627
        if 'mppt_1_energy_point_id' in new_values['data'].keys() and \
2628
                isinstance(new_values['data']['mppt_1_energy_point_id'], int) and \
2629
                new_values['data']['mppt_1_energy_point_id'] > 0:
2630
            mppt_1_energy_point_id = new_values['data']['mppt_1_energy_point_id']
2631
2632
        if 'mppt_2_energy_point_id' in new_values['data'].keys() and \
2633
                isinstance(new_values['data']['mppt_2_energy_point_id'], int) and \
2634
                new_values['data']['mppt_2_energy_point_id'] > 0:
2635
            mppt_2_energy_point_id = new_values['data']['mppt_2_energy_point_id']
2636
2637
        if 'mppt_3_energy_point_id' in new_values['data'].keys() and \
2638
                isinstance(new_values['data']['mppt_3_energy_point_id'], int) and \
2639
                new_values['data']['mppt_3_energy_point_id'] > 0:
2640
            mppt_3_energy_point_id = new_values['data']['mppt_3_energy_point_id']
2641
2642
        if 'mppt_4_energy_point_id' in new_values['data'].keys() and \
2643
                isinstance(new_values['data']['mppt_4_energy_point_id'], int) and \
2644
                new_values['data']['mppt_4_energy_point_id'] > 0:
2645
            mppt_4_energy_point_id = new_values['data']['mppt_4_energy_point_id']
2646
2647
        if 'mppt_5_energy_point_id' in new_values['data'].keys() and \
2648
                isinstance(new_values['data']['mppt_5_energy_point_id'], int) and \
2649
                new_values['data']['mppt_5_energy_point_id'] > 0:
2650
            mppt_5_energy_point_id = new_values['data']['mppt_5_energy_point_id']
2651
2652
        if 'mppt_6_energy_point_id' in new_values['data'].keys() and \
2653
                isinstance(new_values['data']['mppt_6_energy_point_id'], int) and \
2654
                new_values['data']['mppt_6_energy_point_id'] > 0:
2655
            mppt_6_energy_point_id = new_values['data']['mppt_6_energy_point_id']
2656
2657
        if 'mppt_7_energy_point_id' in new_values['data'].keys() and \
2658
                isinstance(new_values['data']['mppt_7_energy_point_id'], int) and \
2659
                new_values['data']['mppt_7_energy_point_id'] > 0:
2660
            mppt_7_energy_point_id = new_values['data']['mppt_7_energy_point_id']
2661
2662
        if 'mppt_8_energy_point_id' in new_values['data'].keys() and \
2663
                isinstance(new_values['data']['mppt_8_energy_point_id'], int) and \
2664
                new_values['data']['mppt_8_energy_point_id'] > 0:
2665
            mppt_8_energy_point_id = new_values['data']['mppt_8_energy_point_id']
2666
2667
        if 'mppt_9_energy_point_id' in new_values['data'].keys() and \
2668
                isinstance(new_values['data']['mppt_9_energy_point_id'], int) and \
2669
                new_values['data']['mppt_9_energy_point_id'] > 0:
2670
            mppt_9_energy_point_id = new_values['data']['mppt_9_energy_point_id']
2671
2672
        if 'mppt_10_energy_point_id' in new_values['data'].keys() and \
2673
                isinstance(new_values['data']['mppt_10_energy_point_id'], int) and \
2674
                new_values['data']['mppt_10_energy_point_id'] > 0:
2675
            mppt_10_energy_point_id = new_values['data']['mppt_10_energy_point_id']
2676
2677
        cnx = mysql.connector.connect(**config.myems_system_db)
2678
        cursor = cnx.cursor()
2679
2680
        cursor.execute(" SELECT name "
2681
                       " FROM tbl_photovoltaic_power_stations "
2682
                       " WHERE id = %s ",
2683
                       (id_,))
2684
        if cursor.fetchone() is None:
2685
            cursor.close()
2686
            cnx.close()
2687
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2688
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
2689
2690
        cursor.execute(" SELECT name "
2691
                       " FROM tbl_photovoltaic_power_stations_invertors "
2692
                       " WHERE photovoltaic_power_station_id = %s AND name = %s ",
2693
                       (id_, name,))
2694
        if cursor.fetchone() is not None:
2695
            cursor.close()
2696
            cnx.close()
2697
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2698
                                   description='API.PHOTOVOLTAIC_POWER_STATION_INVERTOR_NAME_IS_ALREADY_IN_USE')
2699
2700
        cursor.execute(" SELECT name "
2701
                       " FROM tbl_points "
2702
                       " WHERE id = %s ",
2703
                       (invertor_state_point_id,))
2704
        if cursor.fetchone() is None:
2705
            cursor.close()
2706
            cnx.close()
2707
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2708
                                   description='API.INVERTOR_STATE_POINT_NOT_FOUND')
2709
2710
        cursor.execute(" SELECT name "
2711
                       " FROM tbl_points "
2712
                       " WHERE id = %s ",
2713
                       (communication_state_point_id,))
2714
        if cursor.fetchone() is None:
2715
            cursor.close()
2716
            cnx.close()
2717
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2718
                                   description='API.COMMUNICATION_STATE_POINT_NOT_FOUND')
2719
2720
        cursor.execute(" SELECT name "
2721
                       " FROM tbl_points "
2722
                       " WHERE id = %s ",
2723
                       (total_energy_point_id,))
2724
        if cursor.fetchone() is None:
2725
            cursor.close()
2726
            cnx.close()
2727
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2728
                                   description='API.TOTAL_ENERGY_POINT_NOT_FOUND')
2729
2730
        cursor.execute(" SELECT name "
2731
                       " FROM tbl_meters "
2732
                       " WHERE id = %s ",
2733
                       (generation_meter_id,))
2734
        if cursor.fetchone() is None:
2735
            cursor.close()
2736
            cnx.close()
2737
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2738
                                   description='API.GENERATION_METER_NOT_FOUND')
2739
2740
        add_values = (" INSERT INTO tbl_photovoltaic_power_stations_invertors "
2741
                      "    (name, uuid, "
2742
                      "     photovoltaic_power_station_id, "
2743
                      "     model, "
2744
                      "     serial_number, "
2745
                      "     invertor_state_point_id, "
2746
                      "     communication_state_point_id, "
2747
                      "     total_energy_point_id, "
2748
                      "     generation_meter_id, "
2749
                      "     today_energy_point_id, "
2750
                      "     efficiency_point_id, "
2751
                      "     temperature_point_id, "
2752
                      "     power_factor_point_id, "
2753
                      "     active_power_point_id, "
2754
                      "     reactive_power_point_id, "
2755
                      "     frequency_point_id, "
2756
                      "     uab_point_id, "
2757
                      "     ubc_point_id, "
2758
                      "     uca_point_id, "
2759
                      "     ua_point_id, "
2760
                      "     ub_point_id, "
2761
                      "     uc_point_id, "
2762
                      "     ia_point_id, "
2763
                      "     ib_point_id, "
2764
                      "     ic_point_id, "
2765
                      "     pv1_u_point_id, "
2766
                      "     pv1_i_point_id, "
2767
                      "     pv2_u_point_id, "
2768
                      "     pv2_i_point_id, "
2769
                      "     pv3_u_point_id, "
2770
                      "     pv3_i_point_id, "
2771
                      "     pv4_u_point_id, "
2772
                      "     pv4_i_point_id, "
2773
                      "     pv5_u_point_id, "
2774
                      "     pv5_i_point_id, "
2775
                      "     pv6_u_point_id, "
2776
                      "     pv6_i_point_id, "
2777
                      "     pv7_u_point_id, "
2778
                      "     pv7_i_point_id, "
2779
                      "     pv8_u_point_id, "
2780
                      "     pv8_i_point_id, "
2781
                      "     pv9_u_point_id, "
2782
                      "     pv9_i_point_id, "
2783
                      "     pv10_u_point_id, "
2784
                      "     pv10_i_point_id, "
2785
                      "     pv11_u_point_id, "
2786
                      "     pv11_i_point_id, "
2787
                      "     pv12_u_point_id, "
2788
                      "     pv12_i_point_id, "
2789
                      "     pv13_u_point_id, "
2790
                      "     pv13_i_point_id, "
2791
                      "     pv14_u_point_id, "
2792
                      "     pv14_i_point_id, "
2793
                      "     pv15_u_point_id, "
2794
                      "     pv15_i_point_id, "
2795
                      "     pv16_u_point_id, "
2796
                      "     pv16_i_point_id, "
2797
                      "     pv17_u_point_id, "
2798
                      "     pv17_i_point_id, "
2799
                      "     pv18_u_point_id, "
2800
                      "     pv18_i_point_id, "
2801
                      "     pv19_u_point_id, "
2802
                      "     pv19_i_point_id, "
2803
                      "     pv20_u_point_id, "
2804
                      "     pv20_i_point_id, "
2805
                      "     pv21_u_point_id, "
2806
                      "     pv21_i_point_id, "
2807
                      "     pv22_u_point_id, "
2808
                      "     pv22_i_point_id, "
2809
                      "     pv23_u_point_id, "
2810
                      "     pv23_i_point_id, "
2811
                      "     pv24_u_point_id, "
2812
                      "     pv24_i_point_id, "
2813
                      "     pv25_u_point_id, "
2814
                      "     pv25_i_point_id, "
2815
                      "     pv26_u_point_id, "
2816
                      "     pv26_i_point_id, "
2817
                      "     pv27_u_point_id, "
2818
                      "     pv27_i_point_id, "
2819
                      "     pv28_u_point_id, "
2820
                      "     pv28_i_point_id, "
2821
                      "     mppt_total_energy_point_id, "
2822
                      "     mppt_power_point_id, "
2823
                      "     mppt_1_energy_point_id, "
2824
                      "     mppt_2_energy_point_id, "
2825
                      "     mppt_3_energy_point_id, "
2826
                      "     mppt_4_energy_point_id, "
2827
                      "     mppt_5_energy_point_id, "
2828
                      "     mppt_6_energy_point_id, "
2829
                      "     mppt_7_energy_point_id, "
2830
                      "     mppt_8_energy_point_id, "
2831
                      "     mppt_9_energy_point_id, "
2832
                      "     mppt_10_energy_point_id) "
2833
                      " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2834
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2835
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2836
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2837
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2838
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2839
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2840
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2841
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
2842
                      "         %s, %s, %s) ")
2843
        cursor.execute(add_values, (name,
2844
                                    str(uuid.uuid4()),
2845
                                    id_,
2846
                                    model,
2847
                                    serial_number,
2848
                                    invertor_state_point_id,
2849
                                    communication_state_point_id,
2850
                                    total_energy_point_id,
2851
                                    generation_meter_id,
2852
                                    today_energy_point_id,
2853
                                    efficiency_point_id,
2854
                                    temperature_point_id,
2855
                                    power_factor_point_id,
2856
                                    active_power_point_id,
2857
                                    reactive_power_point_id,
2858
                                    frequency_point_id,
2859
                                    uab_point_id,
2860
                                    ubc_point_id,
2861
                                    uca_point_id,
2862
                                    ua_point_id,
2863
                                    ub_point_id,
2864
                                    uc_point_id,
2865
                                    ia_point_id,
2866
                                    ib_point_id,
2867
                                    ic_point_id,
2868
                                    pv1_u_point_id,
2869
                                    pv1_i_point_id,
2870
                                    pv2_u_point_id,
2871
                                    pv2_i_point_id,
2872
                                    pv3_u_point_id,
2873
                                    pv3_i_point_id,
2874
                                    pv4_u_point_id,
2875
                                    pv4_i_point_id,
2876
                                    pv5_u_point_id,
2877
                                    pv5_i_point_id,
2878
                                    pv6_u_point_id,
2879
                                    pv6_i_point_id,
2880
                                    pv7_u_point_id,
2881
                                    pv7_i_point_id,
2882
                                    pv8_u_point_id,
2883
                                    pv8_i_point_id,
2884
                                    pv9_u_point_id,
2885
                                    pv9_i_point_id,
2886
                                    pv10_u_point_id,
2887
                                    pv10_i_point_id,
2888
                                    pv11_u_point_id,
2889
                                    pv11_i_point_id,
2890
                                    pv12_u_point_id,
2891
                                    pv12_i_point_id,
2892
                                    pv13_u_point_id,
2893
                                    pv13_i_point_id,
2894
                                    pv14_u_point_id,
2895
                                    pv14_i_point_id,
2896
                                    pv15_u_point_id,
2897
                                    pv15_i_point_id,
2898
                                    pv16_u_point_id,
2899
                                    pv16_i_point_id,
2900
                                    pv17_u_point_id,
2901
                                    pv17_i_point_id,
2902
                                    pv18_u_point_id,
2903
                                    pv18_i_point_id,
2904
                                    pv19_u_point_id,
2905
                                    pv19_i_point_id,
2906
                                    pv20_u_point_id,
2907
                                    pv20_i_point_id,
2908
                                    pv21_u_point_id,
2909
                                    pv21_i_point_id,
2910
                                    pv22_u_point_id,
2911
                                    pv22_i_point_id,
2912
                                    pv23_u_point_id,
2913
                                    pv23_i_point_id,
2914
                                    pv24_u_point_id,
2915
                                    pv24_i_point_id,
2916
                                    pv25_u_point_id,
2917
                                    pv25_i_point_id,
2918
                                    pv26_u_point_id,
2919
                                    pv26_i_point_id,
2920
                                    pv27_u_point_id,
2921
                                    pv27_i_point_id,
2922
                                    pv28_u_point_id,
2923
                                    pv28_i_point_id,
2924
                                    mppt_total_energy_point_id,
2925
                                    mppt_power_point_id,
2926
                                    mppt_1_energy_point_id,
2927
                                    mppt_2_energy_point_id,
2928
                                    mppt_3_energy_point_id,
2929
                                    mppt_4_energy_point_id,
2930
                                    mppt_5_energy_point_id,
2931
                                    mppt_6_energy_point_id,
2932
                                    mppt_7_energy_point_id,
2933
                                    mppt_8_energy_point_id,
2934
                                    mppt_9_energy_point_id,
2935
                                    mppt_10_energy_point_id
2936
                                    ))
2937
        new_id = cursor.lastrowid
2938
        cnx.commit()
2939
        cursor.close()
2940
        cnx.close()
2941
2942
        resp.status = falcon.HTTP_201
2943
        resp.location = '/photovoltaicpowerstations/' + str(id_) + '/invertors/' + str(new_id)
2944
2945
2946
class PhotovoltaicPowerStationInvertorItem:
2947
    def __init__(self):
2948
        """Initializes Class"""
2949
        pass
2950
2951
    @staticmethod
2952
    def on_options(req, resp, id_, iid):
2953
        _ = req
2954
        resp.status = falcon.HTTP_200
2955
        _ = id_
2956
2957
    @staticmethod
2958
    def on_get(req, resp, id_, iid):
2959
        access_control(req)
2960
        if not id_.isdigit() or int(id_) <= 0:
2961
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2962
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
2963
        if not iid.isdigit() or int(iid) <= 0:
2964
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2965
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_INVERTOR_ID')
2966
2967
        cnx = mysql.connector.connect(**config.myems_system_db)
2968
        cursor = cnx.cursor()
2969
2970
        cursor.execute(" SELECT name "
2971
                       " FROM tbl_photovoltaic_power_stations "
2972
                       " WHERE id = %s ", (id_,))
2973
        if cursor.fetchone() is None:
2974
            cursor.close()
2975
            cnx.close()
2976
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2977
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
2978
2979
        # query point dict
2980
        query = (" SELECT id, name "
2981
                 " FROM tbl_points ")
2982
        cursor.execute(query)
2983
        rows_points = cursor.fetchall()
2984
2985
        point_dict = dict()
2986
        if rows_points is not None and len(rows_points) > 0:
2987
            for row in rows_points:
2988
                point_dict[row[0]] = {"id": row[0],
2989
                                      "name": row[1]}
2990
2991
        # query meter dict
2992
        query = (" SELECT id, name "
2993
                 " FROM tbl_meters ")
2994
        cursor.execute(query)
2995
        rows_meters = cursor.fetchall()
2996
2997
        meter_dict = dict()
2998
        if rows_meters is not None and len(rows_meters) > 0:
2999
            for row in rows_meters:
3000
                meter_dict[row[0]] = {"id": row[0],
3001
                                      "name": row[1]}
3002
3003
        query = (" SELECT id, name, uuid, "
3004
                 "        model, "
3005
                 "        serial_number, "
3006
                 "        invertor_state_point_id, "
3007
                 "        communication_state_point_id, "
3008
                 "        total_energy_point_id, "
3009
                 "        generation_meter_id, "
3010
                 "        today_energy_point_id, "
3011
                 "        efficiency_point_id, "
3012
                 "        temperature_point_id, "
3013
                 "        power_factor_point_id, "
3014
                 "        active_power_point_id, "
3015
                 "        reactive_power_point_id, "
3016
                 "        frequency_point_id, "
3017
                 "        uab_point_id, "
3018
                 "        ubc_point_id, "
3019
                 "        uca_point_id, "
3020
                 "        ua_point_id, "
3021
                 "        ub_point_id, "
3022
                 "        uc_point_id, "
3023
                 "        ia_point_id, "
3024
                 "        ib_point_id, "
3025
                 "        ic_point_id, "
3026
                 "        pv1_u_point_id, "
3027
                 "        pv1_i_point_id, "
3028
                 "        pv2_u_point_id, "
3029
                 "        pv2_i_point_id, "
3030
                 "        pv3_u_point_id, "
3031
                 "        pv3_i_point_id, "
3032
                 "        pv4_u_point_id, "
3033
                 "        pv4_i_point_id, "
3034
                 "        pv5_u_point_id, "
3035
                 "        pv5_i_point_id, "
3036
                 "        pv6_u_point_id, "
3037
                 "        pv6_i_point_id, "
3038
                 "        pv7_u_point_id, "
3039
                 "        pv7_i_point_id, "
3040
                 "        pv8_u_point_id, "
3041
                 "        pv8_i_point_id, "
3042
                 "        pv9_u_point_id, "
3043
                 "        pv9_i_point_id, "
3044
                 "        pv10_u_point_id, "
3045
                 "        pv10_i_point_id, "
3046
                 "        pv11_u_point_id, "
3047
                 "        pv11_i_point_id, "
3048
                 "        pv12_u_point_id, "
3049
                 "        pv12_i_point_id, "
3050
                 "        pv13_u_point_id, "
3051
                 "        pv13_i_point_id, "
3052
                 "        pv14_u_point_id, "
3053
                 "        pv14_i_point_id, "
3054
                 "        pv15_u_point_id, "
3055
                 "        pv15_i_point_id, "
3056
                 "        pv16_u_point_id, "
3057
                 "        pv16_i_point_id, "
3058
                 "        pv17_u_point_id, "
3059
                 "        pv17_i_point_id, "
3060
                 "        pv18_u_point_id, "
3061
                 "        pv18_i_point_id, "
3062
                 "        pv19_u_point_id, "
3063
                 "        pv19_i_point_id, "
3064
                 "        pv20_u_point_id, "
3065
                 "        pv20_i_point_id, "
3066
                 "        pv21_u_point_id, "
3067
                 "        pv21_i_point_id, "
3068
                 "        pv22_u_point_id, "
3069
                 "        pv22_i_point_id, "
3070
                 "        pv23_u_point_id, "
3071
                 "        pv23_i_point_id, "
3072
                 "        pv24_u_point_id, "
3073
                 "        pv24_i_point_id, "
3074
                 "        pv25_u_point_id, "
3075
                 "        pv25_i_point_id, "
3076
                 "        pv26_u_point_id, "
3077
                 "        pv26_i_point_id, "
3078
                 "        pv27_u_point_id, "
3079
                 "        pv27_i_point_id, "
3080
                 "        pv28_u_point_id, "
3081
                 "        pv28_i_point_id, "
3082
                 "        mppt_total_energy_point_id, "
3083
                 "        mppt_power_point_id, "
3084
                 "        mppt_1_energy_point_id, "
3085
                 "        mppt_2_energy_point_id, "
3086
                 "        mppt_3_energy_point_id, "
3087
                 "        mppt_4_energy_point_id, "
3088
                 "        mppt_5_energy_point_id, "
3089
                 "        mppt_6_energy_point_id, "
3090
                 "        mppt_7_energy_point_id, "
3091
                 "        mppt_8_energy_point_id, "
3092
                 "        mppt_9_energy_point_id, "
3093
                 "        mppt_10_energy_point_id "
3094
                 " FROM tbl_photovoltaic_power_stations_invertors "
3095
                 " WHERE id = %s ")
3096
        cursor.execute(query, (iid,))
3097
        row = cursor.fetchone()
3098
        cursor.close()
3099
        cnx.close()
3100
3101
        if row is None:
3102
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3103
                                   description='API.PHOTOVOLTAIC_POWER_STATION_INVERTOR_NOT_FOUND')
3104
        else:
3105
            meta_result = {"id": row[0],
3106
                           "name": row[1],
3107
                           "uuid": row[2],
3108
                           "model": row[3],
3109
                           "serial_number": row[4],
3110
                           "invertor_state_point": point_dict.get(row[5]),
3111
                           "communication_state_point": point_dict.get(row[6]),
3112
                           "total_energy_point": point_dict.get(row[7]),
3113
                           "generation_mter": meter_dict.get(row[8]),
3114
                           "today_energy_point": point_dict.get(row[9]),
3115
                           "efficiency_point": point_dict.get(row[10]),
3116
                           "temperature_point": point_dict.get(row[11]),
3117
                           "power_factor_point": point_dict.get(row[12]),
3118
                           "active_power_point": point_dict.get(row[13]),
3119
                           "reactive_power_point": point_dict.get(row[14]),
3120
                           "frequency_point": point_dict.get(row[15]),
3121
                           "uab_point": point_dict.get(row[16]),
3122
                           "ubc_point": point_dict.get(row[17]),
3123
                           "uca_point": point_dict.get(row[18]),
3124
                           "ua_point": point_dict.get(row[19]),
3125
                           "ub_point": point_dict.get(row[20]),
3126
                           "uc_point": point_dict.get(row[21]),
3127
                           "ia_point": point_dict.get(row[22]),
3128
                           "ib_point": point_dict.get(row[23]),
3129
                           "ic_point": point_dict.get(row[24]),
3130
                           "pv1_u_point": point_dict.get(row[25]),
3131
                           "pv1_i_point": point_dict.get(row[26]),
3132
                           "pv2_u_point": point_dict.get(row[27]),
3133
                           "pv2_i_point": point_dict.get(row[28]),
3134
                           "pv3_u_point": point_dict.get(row[29]),
3135
                           "pv3_i_point": point_dict.get(row[30]),
3136
                           "pv4_u_point": point_dict.get(row[31]),
3137
                           "pv4_i_point": point_dict.get(row[32]),
3138
                           "pv5_u_point": point_dict.get(row[33]),
3139
                           "pv5_i_point": point_dict.get(row[34]),
3140
                           "pv6_u_point": point_dict.get(row[35]),
3141
                           "pv6_i_point": point_dict.get(row[36]),
3142
                           "pv7_u_point": point_dict.get(row[37]),
3143
                           "pv7_i_point": point_dict.get(row[38]),
3144
                           "pv8_u_point": point_dict.get(row[39]),
3145
                           "pv8_i_point": point_dict.get(row[40]),
3146
                           "pv9_u_point": point_dict.get(row[41]),
3147
                           "pv9_i_point": point_dict.get(row[42]),
3148
                           "pv10_u_point": point_dict.get(row[43]),
3149
                           "pv10_i_point": point_dict.get(row[44]),
3150
                           "pv11_u_point": point_dict.get(row[45]),
3151
                           "pv11_i_point": point_dict.get(row[46]),
3152
                           "pv12_u_point": point_dict.get(row[47]),
3153
                           "pv12_i_point": point_dict.get(row[48]),
3154
                           "pv13_u_point": point_dict.get(row[49]),
3155
                           "pv13_i_point": point_dict.get(row[50]),
3156
                           "pv14_u_point": point_dict.get(row[51]),
3157
                           "pv14_i_point": point_dict.get(row[52]),
3158
                           "pv15_u_point": point_dict.get(row[53]),
3159
                           "pv15_i_point": point_dict.get(row[54]),
3160
                           "pv16_u_point": point_dict.get(row[55]),
3161
                           "pv16_i_point": point_dict.get(row[56]),
3162
                           "pv17_u_point": point_dict.get(row[57]),
3163
                           "pv17_i_point": point_dict.get(row[58]),
3164
                           "pv18_u_point": point_dict.get(row[59]),
3165
                           "pv18_i_point": point_dict.get(row[60]),
3166
                           "pv19_u_point": point_dict.get(row[61]),
3167
                           "pv19_i_point": point_dict.get(row[62]),
3168
                           "pv20_u_point": point_dict.get(row[63]),
3169
                           "pv20_i_point": point_dict.get(row[64]),
3170
                           "pv21_u_point": point_dict.get(row[65]),
3171
                           "pv21_i_point": point_dict.get(row[66]),
3172
                           "pv22_u_point": point_dict.get(row[67]),
3173
                           "pv22_i_point": point_dict.get(row[68]),
3174
                           "pv23_u_point": point_dict.get(row[69]),
3175
                           "pv23_i_point": point_dict.get(row[70]),
3176
                           "pv24_u_point": point_dict.get(row[71]),
3177
                           "pv24_i_point": point_dict.get(row[72]),
3178
                           "pv25_u_point": point_dict.get(row[73]),
3179
                           "pv25_i_point": point_dict.get(row[74]),
3180
                           "pv26_u_point": point_dict.get(row[75]),
3181
                           "pv26_i_point": point_dict.get(row[76]),
3182
                           "pv27_u_point": point_dict.get(row[77]),
3183
                           "pv27_i_point": point_dict.get(row[78]),
3184
                           "pv28_u_point": point_dict.get(row[79]),
3185
                           "pv28_i_point": point_dict.get(row[80]),
3186
                           "mppt_total_energy_point": point_dict.get(row[81]),
3187
                           "mppt_power_point": point_dict.get(row[82]),
3188
                           "mppt_1_energy_point": point_dict.get(row[83]),
3189
                           "mppt_2_energy_point": point_dict.get(row[84]),
3190
                           "mppt_3_energy_point": point_dict.get(row[85]),
3191
                           "mppt_4_energy_point": point_dict.get(row[85]),
3192
                           "mppt_5_energy_point": point_dict.get(row[87]),
3193
                           "mppt_6_energy_point": point_dict.get(row[88]),
3194
                           "mppt_7_energy_point": point_dict.get(row[89]),
3195
                           "mppt_8_energy_point": point_dict.get(row[90]),
3196
                           "mppt_9_energy_point": point_dict.get(row[91]),
3197
                           "mppt_10_energy_point": point_dict.get(row[92]),
3198
                           }
3199
3200
        resp.text = json.dumps(meta_result)
3201
3202
    @staticmethod
3203
    @user_logger
3204
    def on_delete(req, resp, id_, iid):
3205
        admin_control(req)
3206
        if not id_.isdigit() or int(id_) <= 0:
3207
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3208
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
3209
        if not iid.isdigit() or int(iid) <= 0:
3210
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3211
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_INVERTOR_ID')
3212
3213
        cnx = mysql.connector.connect(**config.myems_system_db)
3214
        cursor = cnx.cursor()
3215
3216
        cursor.execute(" SELECT name "
3217
                       " FROM tbl_photovoltaic_power_stations "
3218
                       " WHERE id = %s ", (id_,))
3219
        if cursor.fetchone() is None:
3220
            cursor.close()
3221
            cnx.close()
3222
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3223
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
3224
3225
        cursor.execute(" SELECT name "
3226
                       " FROM tbl_photovoltaic_power_stations_invertors "
3227
                       " WHERE id = %s ", (iid,))
3228
        if cursor.fetchone() is None:
3229
            cursor.close()
3230
            cnx.close()
3231
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3232
                                   description='API.PHOTOVOLTAIC_POWER_STATION_INVERTOR_NOT_FOUND')
3233
3234
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_invertors "
3235
                       " WHERE id = %s ", (iid,))
3236
        cnx.commit()
3237
3238
        cursor.close()
3239
        cnx.close()
3240
3241
        resp.status = falcon.HTTP_204
3242
3243
    @staticmethod
3244
    @user_logger
3245
    def on_put(req, resp, id_, iid):
3246
        """Handles PUT requests"""
3247
        admin_control(req)
3248
        try:
3249
            raw_json = req.stream.read().decode('utf-8')
3250
        except Exception as ex:
3251
            print(str(ex))
3252
            raise falcon.HTTPError(status=falcon.HTTP_400,
3253
                                   title='API.BAD_REQUEST',
3254
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3255
        if not id_.isdigit() or int(id_) <= 0:
3256
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3257
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
3258
3259
        if not iid.isdigit() or int(iid) <= 0:
3260
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3261
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_INVERTOR_ID')
3262
3263
        new_values = json.loads(raw_json)
3264
3265
        if 'name' not in new_values['data'].keys() or \
3266
                not isinstance(new_values['data']['name'], str) or \
3267
                len(str.strip(new_values['data']['name'])) == 0:
3268
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3269
                                   description='API.INVALID_NAME')
3270
        name = str.strip(new_values['data']['name'])
3271
3272
        if 'model' not in new_values['data'].keys() or \
3273
                not isinstance(new_values['data']['model'], str) or \
3274
                len(str.strip(new_values['data']['model'])) == 0:
3275
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3276
                                   description='API.INVALID_MODEL')
3277
        model = str.strip(new_values['data']['model'])
3278
3279
        if 'serial_number' not in new_values['data'].keys() or \
3280
                not isinstance(new_values['data']['serial_number'], str) or \
3281
                len(str.strip(new_values['data']['serial_number'])) == 0:
3282
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3283
                                   description='API.INVALID_SERIAL_NUMBER')
3284
        serial_number = str.strip(new_values['data']['serial_number'])
3285
3286
        if 'invertor_state_point_id' not in new_values['data'].keys() or \
3287
                not isinstance(new_values['data']['invertor_state_point_id'], int) or \
3288
                new_values['data']['invertor_state_point_id'] <= 0:
3289
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3290
                                   description='API.INVALID_INVERTOR_STATE_POINT_ID')
3291
        invertor_state_point_id = new_values['data']['invertor_state_point_id']
3292
3293
        if 'communication_state_point_id' not in new_values['data'].keys() or \
3294
                not isinstance(new_values['data']['communication_state_point_id'], int) or \
3295
                new_values['data']['communication_state_point_id'] <= 0:
3296
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3297
                                   description='API.INVALID_COMMUNICATION_STATE_POINT_ID')
3298
        communication_state_point_id = new_values['data']['communication_state_point_id']
3299
3300
        if 'total_energy_point_id' not in new_values['data'].keys() or \
3301
                not isinstance(new_values['data']['total_energy_point_id'], int) or \
3302
                new_values['data']['total_energy_point_id'] <= 0:
3303
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3304
                                   description='API.INVALID_TOTAL_ENERGY_POINT_ID')
3305
        total_energy_point_id = new_values['data']['total_energy_point_id']
3306
3307
        if 'generation_meter_id' not in new_values['data'].keys() or \
3308
                not isinstance(new_values['data']['generation_meter_id'], int) or \
3309
                new_values['data']['generation_meter_id'] <= 0:
3310
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3311
                                   description='API.INVALID_GENERATION_METER_ID')
3312
        generation_meter_id = new_values['data']['generation_meter_id']
3313
3314
        today_energy_point_id = None
3315
        efficiency_point_id = None
3316
        temperature_point_id = None
3317
        power_factor_point_id = None
3318
        active_power_point_id = None
3319
        reactive_power_point_id = None
3320
        frequency_point_id = None
3321
        uab_point_id = None
3322
        ubc_point_id = None
3323
        uca_point_id = None
3324
        ua_point_id = None
3325
        ub_point_id = None
3326
        uc_point_id = None
3327
        ia_point_id = None
3328
        ib_point_id = None
3329
        ic_point_id = None
3330
        pv1_u_point_id = None
3331
        pv1_i_point_id = None
3332
        pv2_u_point_id = None
3333
        pv2_i_point_id = None
3334
        pv3_u_point_id = None
3335
        pv3_i_point_id = None
3336
        pv4_u_point_id = None
3337
        pv4_i_point_id = None
3338
        pv5_u_point_id = None
3339
        pv5_i_point_id = None
3340
        pv6_u_point_id = None
3341
        pv6_i_point_id = None
3342
        pv7_u_point_id = None
3343
        pv7_i_point_id = None
3344
        pv8_u_point_id = None
3345
        pv8_i_point_id = None
3346
        pv9_u_point_id = None
3347
        pv9_i_point_id = None
3348
        pv10_u_point_id = None
3349
        pv10_i_point_id = None
3350
        pv11_u_point_id = None
3351
        pv11_i_point_id = None
3352
        pv12_u_point_id = None
3353
        pv12_i_point_id = None
3354
        pv13_u_point_id = None
3355
        pv13_i_point_id = None
3356
        pv14_u_point_id = None
3357
        pv14_i_point_id = None
3358
        pv15_u_point_id = None
3359
        pv15_i_point_id = None
3360
        pv16_u_point_id = None
3361
        pv16_i_point_id = None
3362
        pv17_u_point_id = None
3363
        pv17_i_point_id = None
3364
        pv18_u_point_id = None
3365
        pv18_i_point_id = None
3366
        pv19_u_point_id = None
3367
        pv19_i_point_id = None
3368
        pv20_u_point_id = None
3369
        pv20_i_point_id = None
3370
        pv21_u_point_id = None
3371
        pv21_i_point_id = None
3372
        pv22_u_point_id = None
3373
        pv22_i_point_id = None
3374
        pv23_u_point_id = None
3375
        pv23_i_point_id = None
3376
        pv24_u_point_id = None
3377
        pv24_i_point_id = None
3378
        pv25_u_point_id = None
3379
        pv25_i_point_id = None
3380
        pv26_u_point_id = None
3381
        pv26_i_point_id = None
3382
        pv27_u_point_id = None
3383
        pv27_i_point_id = None
3384
        pv28_u_point_id = None
3385
        pv28_i_point_id = None
3386
        mppt_total_energy_point_id = None
3387
        mppt_power_point_id = None
3388
        mppt_1_energy_point_id = None
3389
        mppt_2_energy_point_id = None
3390
        mppt_3_energy_point_id = None
3391
        mppt_4_energy_point_id = None
3392
        mppt_5_energy_point_id = None
3393
        mppt_6_energy_point_id = None
3394
        mppt_7_energy_point_id = None
3395
        mppt_8_energy_point_id = None
3396
        mppt_9_energy_point_id = None
3397
        mppt_10_energy_point_id = None
3398
3399
        if 'today_energy_point_id' in new_values['data'].keys() and \
3400
                isinstance(new_values['data']['today_energy_point_id'], int) and \
3401
                new_values['data']['today_energy_point_id'] > 0:
3402
            today_energy_point_id = new_values['data']['today_energy_point_id']
3403
3404
        if 'efficiency_point_id' in new_values['data'].keys() and \
3405
                isinstance(new_values['data']['efficiency_point_id'], int) and \
3406
                new_values['data']['efficiency_point_id'] > 0:
3407
            efficiency_point_id = new_values['data']['efficiency_point_id']
3408
3409
        if 'temperature_point_id' in new_values['data'].keys() and \
3410
                isinstance(new_values['data']['temperature_point_id'], int) and \
3411
                new_values['data']['temperature_point_id'] > 0:
3412
            temperature_point_id = new_values['data']['temperature_point_id']
3413
3414
        if 'power_factor_point_id' in new_values['data'].keys() and \
3415
                isinstance(new_values['data']['power_factor_point_id'], int) and \
3416
                new_values['data']['power_factor_point_id'] > 0:
3417
            power_factor_point_id = new_values['data']['power_factor_point_id']
3418
3419
        if 'active_power_point_id' in new_values['data'].keys() and \
3420
                isinstance(new_values['data']['active_power_point_id'], int) and \
3421
                new_values['data']['active_power_point_id'] > 0:
3422
            active_power_point_id = new_values['data']['active_power_point_id']
3423
3424
        if 'reactive_power_point_id' in new_values['data'].keys() and \
3425
                isinstance(new_values['data']['reactive_power_point_id'], int) and \
3426
                new_values['data']['reactive_power_point_id'] > 0:
3427
            reactive_power_point_id = new_values['data']['reactive_power_point_id']
3428
3429
        if 'frequency_point_id' in new_values['data'].keys() and \
3430
                isinstance(new_values['data']['frequency_point_id'], int) and \
3431
                new_values['data']['frequency_point_id'] > 0:
3432
            frequency_point_id = new_values['data']['frequency_point_id']
3433
3434
        if 'uab_point_id' in new_values['data'].keys() and \
3435
                isinstance(new_values['data']['uab_point_id'], int) and \
3436
                new_values['data']['uab_point_id'] > 0:
3437
            uab_point_id = new_values['data']['uab_point_id']
3438
3439
        if 'ubc_point_id' in new_values['data'].keys() and \
3440
                isinstance(new_values['data']['ubc_point_id'], int) and \
3441
                new_values['data']['ubc_point_id'] > 0:
3442
            ubc_point_id = new_values['data']['ubc_point_id']
3443
3444
        if 'uca_point_id' in new_values['data'].keys() and \
3445
                isinstance(new_values['data']['uca_point_id'], int) and \
3446
                new_values['data']['uca_point_id'] > 0:
3447
            uca_point_id = new_values['data']['uca_point_id']
3448
3449
        if 'ua_point_id' in new_values['data'].keys() and \
3450
                isinstance(new_values['data']['ua_point_id'], int) and \
3451
                new_values['data']['ua_point_id'] > 0:
3452
            ua_point_id = new_values['data']['ua_point_id']
3453
3454
        if 'ub_point_id' in new_values['data'].keys() and \
3455
                isinstance(new_values['data']['ub_point_id'], int) and \
3456
                new_values['data']['ub_point_id'] > 0:
3457
            ub_point_id = new_values['data']['ub_point_id']
3458
3459
        if 'uc_point_id' in new_values['data'].keys() and \
3460
                isinstance(new_values['data']['uc_point_id'], int) and \
3461
                new_values['data']['uc_point_id'] > 0:
3462
            uc_point_id = new_values['data']['uc_point_id']
3463
3464
        if 'ia_point_id' in new_values['data'].keys() and \
3465
                isinstance(new_values['data']['ia_point_id'], int) and \
3466
                new_values['data']['ia_point_id'] > 0:
3467
            ia_point_id = new_values['data']['ia_point_id']
3468
3469
        if 'ib_point_id' in new_values['data'].keys() and \
3470
                isinstance(new_values['data']['ib_point_id'], int) and \
3471
                new_values['data']['ib_point_id'] > 0:
3472
            ib_point_id = new_values['data']['ib_point_id']
3473
3474
        if 'ic_point_id' in new_values['data'].keys() and \
3475
                isinstance(new_values['data']['ic_point_id'], int) and \
3476
                new_values['data']['ic_point_id'] > 0:
3477
            ic_point_id = new_values['data']['ic_point_id']
3478
3479
        if 'pv1_u_point_id' in new_values['data'].keys() and \
3480
                isinstance(new_values['data']['pv1_u_point_id'], int) and \
3481
                new_values['data']['pv1_u_point_id'] > 0:
3482
            pv1_u_point_id = new_values['data']['pv1_u_point_id']
3483
3484
        if 'pv1_i_point_id' in new_values['data'].keys() and \
3485
                isinstance(new_values['data']['pv1_i_point_id'], int) and \
3486
                new_values['data']['pv1_i_point_id'] > 0:
3487
            pv1_i_point_id = new_values['data']['pv1_i_point_id']
3488
3489
        if 'pv2_u_point_id' in new_values['data'].keys() and \
3490
                isinstance(new_values['data']['pv2_u_point_id'], int) and \
3491
                new_values['data']['pv2_u_point_id'] > 0:
3492
            pv2_u_point_id = new_values['data']['pv2_u_point_id']
3493
3494
        if 'pv2_i_point_id' in new_values['data'].keys() and \
3495
                isinstance(new_values['data']['pv2_i_point_id'], int) and \
3496
                new_values['data']['pv2_i_point_id'] > 0:
3497
            pv2_i_point_id = new_values['data']['pv2_i_point_id']
3498
3499
        if 'pv3_u_point_id' in new_values['data'].keys() and \
3500
                isinstance(new_values['data']['pv3_u_point_id'], int) and \
3501
                new_values['data']['pv3_u_point_id'] > 0:
3502
            pv3_u_point_id = new_values['data']['pv3_u_point_id']
3503
3504
        if 'pv3_i_point_id' in new_values['data'].keys() and \
3505
                isinstance(new_values['data']['pv3_i_point_id'], int) and \
3506
                new_values['data']['pv3_i_point_id'] > 0:
3507
            pv3_i_point_id = new_values['data']['pv3_i_point_id']
3508
3509
        if 'pv4_u_point_id' in new_values['data'].keys() and \
3510
                isinstance(new_values['data']['pv4_u_point_id'], int) and \
3511
                new_values['data']['pv4_u_point_id'] > 0:
3512
            pv4_u_point_id = new_values['data']['pv4_u_point_id']
3513
3514
        if 'pv4_i_point_id' in new_values['data'].keys() and \
3515
                isinstance(new_values['data']['pv4_i_point_id'], int) and \
3516
                new_values['data']['pv4_i_point_id'] > 0:
3517
            pv4_i_point_id = new_values['data']['pv4_i_point_id']
3518
3519
        if 'pv5_u_point_id' in new_values['data'].keys() and \
3520
                isinstance(new_values['data']['pv5_u_point_id'], int) and \
3521
                new_values['data']['pv5_u_point_id'] > 0:
3522
            pv5_u_point_id = new_values['data']['pv5_u_point_id']
3523
3524
        if 'pv5_i_point_id' in new_values['data'].keys() and \
3525
                isinstance(new_values['data']['pv5_i_point_id'], int) and \
3526
                new_values['data']['pv5_i_point_id'] > 0:
3527
            pv5_i_point_id = new_values['data']['pv5_i_point_id']
3528
3529
        if 'pv6_u_point_id' in new_values['data'].keys() and \
3530
                isinstance(new_values['data']['pv6_u_point_id'], int) and \
3531
                new_values['data']['pv6_u_point_id'] > 0:
3532
            pv6_u_point_id = new_values['data']['pv6_u_point_id']
3533
3534
        if 'pv6_i_point_id' in new_values['data'].keys() and \
3535
                isinstance(new_values['data']['pv6_i_point_id'], int) and \
3536
                new_values['data']['pv6_i_point_id'] > 0:
3537
            pv6_i_point_id = new_values['data']['pv6_i_point_id']
3538
3539
        if 'pv7_u_point_id' in new_values['data'].keys() and \
3540
                isinstance(new_values['data']['pv7_u_point_id'], int) and \
3541
                new_values['data']['pv7_u_point_id'] > 0:
3542
            pv7_u_point_id = new_values['data']['pv7_u_point_id']
3543
3544
        if 'pv7_i_point_id' in new_values['data'].keys() and \
3545
                isinstance(new_values['data']['pv7_i_point_id'], int) and \
3546
                new_values['data']['pv7_i_point_id'] > 0:
3547
            pv7_i_point_id = new_values['data']['pv7_i_point_id']
3548
3549
        if 'pv8_u_point_id' in new_values['data'].keys() and \
3550
                isinstance(new_values['data']['pv8_u_point_id'], int) and \
3551
                new_values['data']['pv8_u_point_id'] > 0:
3552
            pv8_u_point_id = new_values['data']['pv8_u_point_id']
3553
3554
        if 'pv8_i_point_id' in new_values['data'].keys() and \
3555
                isinstance(new_values['data']['pv8_i_point_id'], int) and \
3556
                new_values['data']['pv8_i_point_id'] > 0:
3557
            pv8_i_point_id = new_values['data']['pv8_i_point_id']
3558
3559
        if 'pv9_u_point_id' in new_values['data'].keys() and \
3560
                isinstance(new_values['data']['pv9_u_point_id'], int) and \
3561
                new_values['data']['pv9_u_point_id'] > 0:
3562
            pv9_u_point_id = new_values['data']['pv9_u_point_id']
3563
3564
        if 'pv9_i_point_id' in new_values['data'].keys() and \
3565
                isinstance(new_values['data']['pv9_i_point_id'], int) and \
3566
                new_values['data']['pv9_i_point_id'] > 0:
3567
            pv9_i_point_id = new_values['data']['pv9_i_point_id']
3568
3569
        if 'pv10_u_point_id' in new_values['data'].keys() and \
3570
                isinstance(new_values['data']['pv10_u_point_id'], int) and \
3571
                new_values['data']['pv10_u_point_id'] > 0:
3572
            pv10_u_point_id = new_values['data']['pv10_u_point_id']
3573
3574
        if 'pv10_i_point_id' in new_values['data'].keys() and \
3575
                isinstance(new_values['data']['pv10_i_point_id'], int) and \
3576
                new_values['data']['pv10_i_point_id'] > 0:
3577
            pv10_i_point_id = new_values['data']['pv10_i_point_id']
3578
3579
        if 'pv11_u_point_id' in new_values['data'].keys() and \
3580
                isinstance(new_values['data']['pv11_u_point_id'], int) and \
3581
                new_values['data']['pv11_u_point_id'] > 0:
3582
            pv11_u_point_id = new_values['data']['pv11_u_point_id']
3583
3584
        if 'pv11_i_point_id' in new_values['data'].keys() and \
3585
                isinstance(new_values['data']['pv11_i_point_id'], int) and \
3586
                new_values['data']['pv11_i_point_id'] > 0:
3587
            pv11_i_point_id = new_values['data']['pv11_i_point_id']
3588
3589
        if 'pv12_u_point_id' in new_values['data'].keys() and \
3590
                isinstance(new_values['data']['pv12_u_point_id'], int) and \
3591
                new_values['data']['pv12_u_point_id'] > 0:
3592
            pv12_u_point_id = new_values['data']['pv12_u_point_id']
3593
3594
        if 'pv12_i_point_id' in new_values['data'].keys() and \
3595
                isinstance(new_values['data']['pv12_i_point_id'], int) and \
3596
                new_values['data']['pv12_i_point_id'] > 0:
3597
            pv12_i_point_id = new_values['data']['pv12_i_point_id']
3598
3599
        if 'pv13_u_point_id' in new_values['data'].keys() and \
3600
                isinstance(new_values['data']['pv13_u_point_id'], int) and \
3601
                new_values['data']['pv13_u_point_id'] > 0:
3602
            pv13_u_point_id = new_values['data']['pv13_u_point_id']
3603
3604
        if 'pv13_i_point_id' in new_values['data'].keys() and \
3605
                isinstance(new_values['data']['pv13_i_point_id'], int) and \
3606
                new_values['data']['pv13_i_point_id'] > 0:
3607
            pv13_i_point_id = new_values['data']['pv13_i_point_id']
3608
3609
        if 'pv14_u_point_id' in new_values['data'].keys() and \
3610
                isinstance(new_values['data']['pv14_u_point_id'], int) and \
3611
                new_values['data']['pv14_u_point_id'] > 0:
3612
            pv14_u_point_id = new_values['data']['pv14_u_point_id']
3613
3614
        if 'pv14_i_point_id' in new_values['data'].keys() and \
3615
                isinstance(new_values['data']['pv14_i_point_id'], int) and \
3616
                new_values['data']['pv14_i_point_id'] > 0:
3617
            pv14_i_point_id = new_values['data']['pv14_i_point_id']
3618
3619
        if 'pv15_u_point_id' in new_values['data'].keys() and \
3620
                isinstance(new_values['data']['pv15_u_point_id'], int) and \
3621
                new_values['data']['pv15_u_point_id'] > 0:
3622
            pv15_u_point_id = new_values['data']['pv15_u_point_id']
3623
3624
        if 'pv15_i_point_id' in new_values['data'].keys() and \
3625
                isinstance(new_values['data']['pv15_i_point_id'], int) and \
3626
                new_values['data']['pv15_i_point_id'] > 0:
3627
            pv15_i_point_id = new_values['data']['pv15_i_point_id']
3628
3629
        if 'pv16_u_point_id' in new_values['data'].keys() and \
3630
                isinstance(new_values['data']['pv16_u_point_id'], int) and \
3631
                new_values['data']['pv16_u_point_id'] > 0:
3632
            pv16_u_point_id = new_values['data']['pv16_u_point_id']
3633
3634
        if 'pv16_i_point_id' in new_values['data'].keys() and \
3635
                isinstance(new_values['data']['pv16_i_point_id'], int) and \
3636
                new_values['data']['pv16_i_point_id'] > 0:
3637
            pv16_i_point_id = new_values['data']['pv16_i_point_id']
3638
3639
        if 'pv17_u_point_id' in new_values['data'].keys() and \
3640
                isinstance(new_values['data']['pv17_u_point_id'], int) and \
3641
                new_values['data']['pv17_u_point_id'] > 0:
3642
            pv17_u_point_id = new_values['data']['pv17_u_point_id']
3643
3644
        if 'pv17_i_point_id' in new_values['data'].keys() and \
3645
                isinstance(new_values['data']['pv17_i_point_id'], int) and \
3646
                new_values['data']['pv17_i_point_id'] > 0:
3647
            pv17_i_point_id = new_values['data']['pv17_i_point_id']
3648
3649
        if 'pv18_u_point_id' in new_values['data'].keys() and \
3650
                isinstance(new_values['data']['pv18_u_point_id'], int) and \
3651
                new_values['data']['pv18_u_point_id'] > 0:
3652
            pv18_u_point_id = new_values['data']['pv18_u_point_id']
3653
3654
        if 'pv18_i_point_id' in new_values['data'].keys() and \
3655
                isinstance(new_values['data']['pv18_i_point_id'], int) and \
3656
                new_values['data']['pv18_i_point_id'] > 0:
3657
            pv18_i_point_id = new_values['data']['pv18_i_point_id']
3658
3659
        if 'pv19_u_point_id' in new_values['data'].keys() and \
3660
                isinstance(new_values['data']['pv19_u_point_id'], int) and \
3661
                new_values['data']['pv19_u_point_id'] > 0:
3662
            pv19_u_point_id = new_values['data']['pv19_u_point_id']
3663
3664
        if 'pv19_i_point_id' in new_values['data'].keys() and \
3665
                isinstance(new_values['data']['pv19_i_point_id'], int) and \
3666
                new_values['data']['pv19_i_point_id'] > 0:
3667
            pv19_i_point_id = new_values['data']['pv19_i_point_id']
3668
3669
        if 'pv20_u_point_id' in new_values['data'].keys() and \
3670
                isinstance(new_values['data']['pv20_u_point_id'], int) and \
3671
                new_values['data']['pv20_u_point_id'] > 0:
3672
            pv20_u_point_id = new_values['data']['pv20_u_point_id']
3673
3674
        if 'pv20_i_point_id' in new_values['data'].keys() and \
3675
                isinstance(new_values['data']['pv20_i_point_id'], int) and \
3676
                new_values['data']['pv20_i_point_id'] > 0:
3677
            pv20_i_point_id = new_values['data']['pv20_i_point_id']
3678
3679
        if 'pv21_u_point_id' in new_values['data'].keys() and \
3680
                isinstance(new_values['data']['pv21_u_point_id'], int) and \
3681
                new_values['data']['pv21_u_point_id'] > 0:
3682
            pv21_u_point_id = new_values['data']['pv21_u_point_id']
3683
3684
        if 'pv21_i_point_id' in new_values['data'].keys() and \
3685
                isinstance(new_values['data']['pv21_i_point_id'], int) and \
3686
                new_values['data']['pv21_i_point_id'] > 0:
3687
            pv21_i_point_id = new_values['data']['pv21_i_point_id']
3688
3689
        if 'pv22_u_point_id' in new_values['data'].keys() and \
3690
                isinstance(new_values['data']['pv22_u_point_id'], int) and \
3691
                new_values['data']['pv22_u_point_id'] > 0:
3692
            pv22_u_point_id = new_values['data']['pv22_u_point_id']
3693
3694
        if 'pv22_i_point_id' in new_values['data'].keys() and \
3695
                isinstance(new_values['data']['pv22_i_point_id'], int) and \
3696
                new_values['data']['pv22_i_point_id'] > 0:
3697
            pv22_i_point_id = new_values['data']['pv22_i_point_id']
3698
3699
        if 'pv23_u_point_id' in new_values['data'].keys() and \
3700
                isinstance(new_values['data']['pv23_u_point_id'], int) and \
3701
                new_values['data']['pv23_u_point_id'] > 0:
3702
            pv23_u_point_id = new_values['data']['pv23_u_point_id']
3703
3704
        if 'pv23_i_point_id' in new_values['data'].keys() and \
3705
                isinstance(new_values['data']['pv23_i_point_id'], int) and \
3706
                new_values['data']['pv23_i_point_id'] > 0:
3707
            pv23_i_point_id = new_values['data']['pv23_i_point_id']
3708
3709
        if 'pv24_u_point_id' in new_values['data'].keys() and \
3710
                isinstance(new_values['data']['pv24_u_point_id'], int) and \
3711
                new_values['data']['pv24_u_point_id'] > 0:
3712
            pv24_u_point_id = new_values['data']['pv24_u_point_id']
3713
3714
        if 'pv24_i_point_id' in new_values['data'].keys() and \
3715
                isinstance(new_values['data']['pv24_i_point_id'], int) and \
3716
                new_values['data']['pv24_i_point_id'] > 0:
3717
            pv24_i_point_id = new_values['data']['pv24_i_point_id']
3718
3719
        if 'pv25_u_point_id' in new_values['data'].keys() and \
3720
                isinstance(new_values['data']['pv25_u_point_id'], int) and \
3721
                new_values['data']['pv25_u_point_id'] > 0:
3722
            pv25_u_point_id = new_values['data']['pv25_u_point_id']
3723
3724
        if 'pv25_i_point_id' in new_values['data'].keys() and \
3725
                isinstance(new_values['data']['pv25_i_point_id'], int) and \
3726
                new_values['data']['pv25_i_point_id'] > 0:
3727
            pv25_i_point_id = new_values['data']['pv25_i_point_id']
3728
3729
        if 'pv26_u_point_id' in new_values['data'].keys() and \
3730
                isinstance(new_values['data']['pv26_u_point_id'], int) and \
3731
                new_values['data']['pv26_u_point_id'] > 0:
3732
            pv26_u_point_id = new_values['data']['pv26_u_point_id']
3733
3734
        if 'pv26_i_point_id' in new_values['data'].keys() and \
3735
                isinstance(new_values['data']['pv26_i_point_id'], int) and \
3736
                new_values['data']['pv26_i_point_id'] > 0:
3737
            pv26_i_point_id = new_values['data']['pv26_i_point_id']
3738
3739
        if 'pv27_u_point_id' in new_values['data'].keys() and \
3740
                isinstance(new_values['data']['pv27_u_point_id'], int) and \
3741
                new_values['data']['pv27_u_point_id'] > 0:
3742
            pv27_u_point_id = new_values['data']['pv27_u_point_id']
3743
3744
        if 'pv27_i_point_id' in new_values['data'].keys() and \
3745
                isinstance(new_values['data']['pv27_i_point_id'], int) and \
3746
                new_values['data']['pv27_i_point_id'] > 0:
3747
            pv27_i_point_id = new_values['data']['pv27_i_point_id']
3748
3749
        if 'pv28_u_point_id' in new_values['data'].keys() and \
3750
                isinstance(new_values['data']['pv28_u_point_id'], int) and \
3751
                new_values['data']['pv28_u_point_id'] > 0:
3752
            pv28_u_point_id = new_values['data']['pv28_u_point_id']
3753
3754
        if 'pv28_i_point_id' in new_values['data'].keys() and \
3755
                isinstance(new_values['data']['pv28_i_point_id'], int) and \
3756
                new_values['data']['pv28_i_point_id'] > 0:
3757
            pv28_i_point_id = new_values['data']['pv28_i_point_id']
3758
3759
        if 'mppt_total_energy_point_id' in new_values['data'].keys() and \
3760
                isinstance(new_values['data']['mppt_total_energy_point_id'], int) and \
3761
                new_values['data']['mppt_total_energy_point_id'] > 0:
3762
            mppt_total_energy_point_id = new_values['data']['mppt_total_energy_point_id']
3763
3764
        if 'mppt_power_point_id' in new_values['data'].keys() and \
3765
                isinstance(new_values['data']['mppt_power_point_id'], int) and \
3766
                new_values['data']['mppt_power_point_id'] > 0:
3767
            mppt_power_point_id = new_values['data']['mppt_power_point_id']
3768
3769
        if 'mppt_1_energy_point_id' in new_values['data'].keys() and \
3770
                isinstance(new_values['data']['mppt_1_energy_point_id'], int) and \
3771
                new_values['data']['mppt_1_energy_point_id'] > 0:
3772
            mppt_1_energy_point_id = new_values['data']['mppt_1_energy_point_id']
3773
3774
        if 'mppt_2_energy_point_id' in new_values['data'].keys() and \
3775
                isinstance(new_values['data']['mppt_2_energy_point_id'], int) and \
3776
                new_values['data']['mppt_2_energy_point_id'] > 0:
3777
            mppt_2_energy_point_id = new_values['data']['mppt_2_energy_point_id']
3778
3779
        if 'mppt_3_energy_point_id' in new_values['data'].keys() and \
3780
                isinstance(new_values['data']['mppt_3_energy_point_id'], int) and \
3781
                new_values['data']['mppt_3_energy_point_id'] > 0:
3782
            mppt_3_energy_point_id = new_values['data']['mppt_3_energy_point_id']
3783
3784
        if 'mppt_4_energy_point_id' in new_values['data'].keys() and \
3785
                isinstance(new_values['data']['mppt_4_energy_point_id'], int) and \
3786
                new_values['data']['mppt_4_energy_point_id'] > 0:
3787
            mppt_4_energy_point_id = new_values['data']['mppt_4_energy_point_id']
3788
3789
        if 'mppt_5_energy_point_id' in new_values['data'].keys() and \
3790
                isinstance(new_values['data']['mppt_5_energy_point_id'], int) and \
3791
                new_values['data']['mppt_5_energy_point_id'] > 0:
3792
            mppt_5_energy_point_id = new_values['data']['mppt_5_energy_point_id']
3793
3794
        if 'mppt_6_energy_point_id' in new_values['data'].keys() and \
3795
                isinstance(new_values['data']['mppt_6_energy_point_id'], int) and \
3796
                new_values['data']['mppt_6_energy_point_id'] > 0:
3797
            mppt_6_energy_point_id = new_values['data']['mppt_6_energy_point_id']
3798
3799
        if 'mppt_7_energy_point_id' in new_values['data'].keys() and \
3800
                isinstance(new_values['data']['mppt_7_energy_point_id'], int) and \
3801
                new_values['data']['mppt_7_energy_point_id'] > 0:
3802
            mppt_7_energy_point_id = new_values['data']['mppt_7_energy_point_id']
3803
3804
        if 'mppt_8_energy_point_id' in new_values['data'].keys() and \
3805
                isinstance(new_values['data']['mppt_8_energy_point_id'], int) and \
3806
                new_values['data']['mppt_8_energy_point_id'] > 0:
3807
            mppt_8_energy_point_id = new_values['data']['mppt_8_energy_point_id']
3808
3809
        if 'mppt_9_energy_point_id' in new_values['data'].keys() and \
3810
                isinstance(new_values['data']['mppt_9_energy_point_id'], int) and \
3811
                new_values['data']['mppt_9_energy_point_id'] > 0:
3812
            mppt_9_energy_point_id = new_values['data']['mppt_9_energy_point_id']
3813
3814
        if 'mppt_10_energy_point_id' in new_values['data'].keys() and \
3815
                isinstance(new_values['data']['mppt_10_energy_point_id'], int) and \
3816
                new_values['data']['mppt_10_energy_point_id'] > 0:
3817
            mppt_10_energy_point_id = new_values['data']['mppt_10_energy_point_id']
3818
3819
        cnx = mysql.connector.connect(**config.myems_system_db)
3820
        cursor = cnx.cursor()
3821
3822
        cursor.execute(" SELECT name "
3823
                       " FROM tbl_photovoltaic_power_stations "
3824
                       " WHERE id = %s ",
3825
                       (id_,))
3826
        if cursor.fetchone() is None:
3827
            cursor.close()
3828
            cnx.close()
3829
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3830
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
3831
3832
        cursor.execute(" SELECT name "
3833
                       " FROM tbl_photovoltaic_power_stations_invertors "
3834
                       " WHERE id = %s ",
3835
                       (iid,))
3836
        if cursor.fetchone() is None:
3837
            cursor.close()
3838
            cnx.close()
3839
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3840
                                   description='API.PHOTOVOLTAIC_POWER_STATION_INVERTOR_NOT_FOUND')
3841
3842
        cursor.execute(" SELECT name "
3843
                       " FROM tbl_photovoltaic_power_stations_invertors "
3844
                       " WHERE photovoltaic_power_station_id = %s AND id != %s AND name = %s ",
3845
                       (id_, iid, name,))
3846
        if cursor.fetchone() is not None:
3847
            cursor.close()
3848
            cnx.close()
3849
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3850
                                   description='API.PHOTOVOLTAIC_POWER_STATION_INVERTOR_NAME_IS_ALREADY_IN_USE')
3851
3852
        cursor.execute(" SELECT name "
3853
                       " FROM tbl_points "
3854
                       " WHERE id = %s ",
3855
                       (invertor_state_point_id,))
3856
        if cursor.fetchone() is None:
3857
            cursor.close()
3858
            cnx.close()
3859
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3860
                                   description='API.INVERTOR_STATE_POINT_NOT_FOUND')
3861
3862
        cursor.execute(" SELECT name "
3863
                       " FROM tbl_points "
3864
                       " WHERE id = %s ",
3865
                       (communication_state_point_id,))
3866
        if cursor.fetchone() is None:
3867
            cursor.close()
3868
            cnx.close()
3869
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3870
                                   description='API.COMMUNICATION_STATE_POINT_NOT_FOUND')
3871
3872
        cursor.execute(" SELECT name "
3873
                       " FROM tbl_points "
3874
                       " WHERE id = %s ",
3875
                       (total_energy_point_id,))
3876
        if cursor.fetchone() is None:
3877
            cursor.close()
3878
            cnx.close()
3879
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3880
                                   description='API.TOTAL_ENERGY_POINT_NOT_FOUND')
3881
3882
        cursor.execute(" SELECT name "
3883
                       " FROM tbl_meters "
3884
                       " WHERE id = %s ",
3885
                       (generation_meter_id,))
3886
        if cursor.fetchone() is None:
3887
            cursor.close()
3888
            cnx.close()
3889
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3890
                                   description='API.GENERATION_METER_NOT_FOUND')
3891
3892
        update_row = (" UPDATE tbl_photovoltaic_power_stations_invertors "
3893
                      " SET name = %s, "
3894
                      "     model = %s, "
3895
                      "     serial_number = %s, "
3896
                      "     invertor_state_point_id = %s, "
3897
                      "     communication_state_point_id = %s, "
3898
                      "     total_energy_point_id = %s, "
3899
                      "     generation_meter_id = %s, "
3900
                      "     today_energy_point_id = %s, "
3901
                      "     efficiency_point_id = %s, "
3902
                      "     temperature_point_id = %s, "
3903
                      "     power_factor_point_id = %s, "
3904
                      "     active_power_point_id = %s, "
3905
                      "     reactive_power_point_id = %s, "
3906
                      "     frequency_point_id = %s, "
3907
                      "     uab_point_id = %s, "
3908
                      "     ubc_point_id = %s, "
3909
                      "     uca_point_id = %s, "
3910
                      "     ua_point_id = %s, "
3911
                      "     ub_point_id = %s, "
3912
                      "     uc_point_id = %s, "
3913
                      "     ia_point_id = %s, "
3914
                      "     ib_point_id = %s, "
3915
                      "     ic_point_id = %s, "
3916
                      "     pv1_u_point_id = %s, "
3917
                      "     pv1_i_point_id = %s, "
3918
                      "     pv2_u_point_id = %s, "
3919
                      "     pv2_i_point_id = %s, "
3920
                      "     pv3_u_point_id = %s, "
3921
                      "     pv3_i_point_id = %s, "
3922
                      "     pv4_u_point_id = %s, "
3923
                      "     pv4_i_point_id = %s, "
3924
                      "     pv5_u_point_id = %s, "
3925
                      "     pv5_i_point_id = %s, "
3926
                      "     pv6_u_point_id = %s, "
3927
                      "     pv6_i_point_id = %s, "
3928
                      "     pv7_u_point_id = %s, "
3929
                      "     pv7_i_point_id = %s, "
3930
                      "     pv8_u_point_id = %s, "
3931
                      "     pv8_i_point_id = %s, "
3932
                      "     pv9_u_point_id = %s, "
3933
                      "     pv9_i_point_id = %s, "
3934
                      "     pv10_u_point_id = %s, "
3935
                      "     pv10_i_point_id = %s, "
3936
                      "     pv11_u_point_id = %s, "
3937
                      "     pv11_i_point_id = %s, "
3938
                      "     pv12_u_point_id = %s, "
3939
                      "     pv12_i_point_id = %s, "
3940
                      "     pv13_u_point_id = %s, "
3941
                      "     pv13_i_point_id = %s, "
3942
                      "     pv14_u_point_id = %s, "
3943
                      "     pv14_i_point_id = %s, "
3944
                      "     pv15_u_point_id = %s, "
3945
                      "     pv15_i_point_id = %s, "
3946
                      "     pv16_u_point_id = %s, "
3947
                      "     pv16_i_point_id = %s, "
3948
                      "     pv17_u_point_id = %s, "
3949
                      "     pv17_i_point_id = %s, "
3950
                      "     pv18_u_point_id = %s, "
3951
                      "     pv18_i_point_id = %s, "
3952
                      "     pv19_u_point_id = %s, "
3953
                      "     pv19_i_point_id = %s, "
3954
                      "     pv20_u_point_id = %s, "
3955
                      "     pv20_i_point_id = %s, "
3956
                      "     pv21_u_point_id = %s, "
3957
                      "     pv21_i_point_id = %s, "
3958
                      "     pv22_u_point_id = %s, "
3959
                      "     pv22_i_point_id = %s, "
3960
                      "     pv23_u_point_id = %s, "
3961
                      "     pv23_i_point_id = %s, "
3962
                      "     pv24_u_point_id = %s, "
3963
                      "     pv24_i_point_id = %s, "
3964
                      "     pv25_u_point_id = %s, "
3965
                      "     pv25_i_point_id = %s, "
3966
                      "     pv26_u_point_id = %s, "
3967
                      "     pv26_i_point_id = %s, "
3968
                      "     pv27_u_point_id = %s, "
3969
                      "     pv27_i_point_id = %s, "
3970
                      "     pv28_u_point_id = %s, "
3971
                      "     pv28_i_point_id = %s, "
3972
                      "     mppt_total_energy_point_id = %s, "
3973
                      "     mppt_power_point_id = %s, "
3974
                      "     mppt_1_energy_point_id = %s, "
3975
                      "     mppt_2_energy_point_id = %s, "
3976
                      "     mppt_3_energy_point_id = %s, "
3977
                      "     mppt_4_energy_point_id = %s, "
3978
                      "     mppt_5_energy_point_id = %s, "
3979
                      "     mppt_6_energy_point_id = %s, "
3980
                      "     mppt_7_energy_point_id = %s, "
3981
                      "     mppt_8_energy_point_id = %s, "
3982
                      "     mppt_9_energy_point_id = %s, "
3983
                      "     mppt_10_energy_point_id = %s "
3984
                      "     WHERE id = %s ")
3985
        cursor.execute(update_row, (name,
3986
                                    model,
3987
                                    serial_number,
3988
                                    invertor_state_point_id,
3989
                                    communication_state_point_id,
3990
                                    total_energy_point_id,
3991
                                    generation_meter_id,
3992
                                    today_energy_point_id,
3993
                                    efficiency_point_id,
3994
                                    temperature_point_id,
3995
                                    power_factor_point_id,
3996
                                    active_power_point_id,
3997
                                    reactive_power_point_id,
3998
                                    frequency_point_id,
3999
                                    uab_point_id,
4000
                                    ubc_point_id,
4001
                                    uca_point_id,
4002
                                    ua_point_id,
4003
                                    ub_point_id,
4004
                                    uc_point_id,
4005
                                    ia_point_id,
4006
                                    ib_point_id,
4007
                                    ic_point_id,
4008
                                    pv1_u_point_id,
4009
                                    pv1_i_point_id,
4010
                                    pv2_u_point_id,
4011
                                    pv2_i_point_id,
4012
                                    pv3_u_point_id,
4013
                                    pv3_i_point_id,
4014
                                    pv4_u_point_id,
4015
                                    pv4_i_point_id,
4016
                                    pv5_u_point_id,
4017
                                    pv5_i_point_id,
4018
                                    pv6_u_point_id,
4019
                                    pv6_i_point_id,
4020
                                    pv7_u_point_id,
4021
                                    pv7_i_point_id,
4022
                                    pv8_u_point_id,
4023
                                    pv8_i_point_id,
4024
                                    pv9_u_point_id,
4025
                                    pv9_i_point_id,
4026
                                    pv10_u_point_id,
4027
                                    pv10_i_point_id,
4028
                                    pv11_u_point_id,
4029
                                    pv11_i_point_id,
4030
                                    pv12_u_point_id,
4031
                                    pv12_i_point_id,
4032
                                    pv13_u_point_id,
4033
                                    pv13_i_point_id,
4034
                                    pv14_u_point_id,
4035
                                    pv14_i_point_id,
4036
                                    pv15_u_point_id,
4037
                                    pv15_i_point_id,
4038
                                    pv16_u_point_id,
4039
                                    pv16_i_point_id,
4040
                                    pv17_u_point_id,
4041
                                    pv17_i_point_id,
4042
                                    pv18_u_point_id,
4043
                                    pv18_i_point_id,
4044
                                    pv19_u_point_id,
4045
                                    pv19_i_point_id,
4046
                                    pv20_u_point_id,
4047
                                    pv20_i_point_id,
4048
                                    pv21_u_point_id,
4049
                                    pv21_i_point_id,
4050
                                    pv22_u_point_id,
4051
                                    pv22_i_point_id,
4052
                                    pv23_u_point_id,
4053
                                    pv23_i_point_id,
4054
                                    pv24_u_point_id,
4055
                                    pv24_i_point_id,
4056
                                    pv25_u_point_id,
4057
                                    pv25_i_point_id,
4058
                                    pv26_u_point_id,
4059
                                    pv26_i_point_id,
4060
                                    pv27_u_point_id,
4061
                                    pv27_i_point_id,
4062
                                    pv28_u_point_id,
4063
                                    pv28_i_point_id,
4064
                                    mppt_total_energy_point_id,
4065
                                    mppt_power_point_id,
4066
                                    mppt_1_energy_point_id,
4067
                                    mppt_2_energy_point_id,
4068
                                    mppt_3_energy_point_id,
4069
                                    mppt_4_energy_point_id,
4070
                                    mppt_5_energy_point_id,
4071
                                    mppt_6_energy_point_id,
4072
                                    mppt_7_energy_point_id,
4073
                                    mppt_8_energy_point_id,
4074
                                    mppt_9_energy_point_id,
4075
                                    mppt_10_energy_point_id,
4076
                                    iid))
4077
        cnx.commit()
4078
4079
        cursor.close()
4080
        cnx.close()
4081
4082
        resp.status = falcon.HTTP_200
4083
4084
4085
class PhotovoltaicPowerStationLoadCollection:
4086
    def __init__(self):
4087
        """Initializes Class"""
4088
        pass
4089
4090
    @staticmethod
4091
    def on_options(req, resp, id_):
4092
        _ = req
4093
        resp.status = falcon.HTTP_200
4094
        _ = id_
4095
4096 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4097
    def on_get(req, resp, id_):
4098
        access_control(req)
4099
        if not id_.isdigit() or int(id_) <= 0:
4100
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4101
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4102
4103
        cnx = mysql.connector.connect(**config.myems_system_db)
4104
        cursor = cnx.cursor()
4105
4106
        cursor.execute(" SELECT name "
4107
                       " FROM tbl_photovoltaic_power_stations "
4108
                       " WHERE id = %s ", (id_,))
4109
        if cursor.fetchone() is None:
4110
            cursor.close()
4111
            cnx.close()
4112
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4113
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4114
4115
        # query meter dict
4116
        query = (" SELECT id, name, uuid "
4117
                 " FROM tbl_meters ")
4118
        cursor.execute(query)
4119
        rows_meters = cursor.fetchall()
4120
4121
        meter_dict = dict()
4122
        if rows_meters is not None and len(rows_meters) > 0:
4123
            for row in rows_meters:
4124
                meter_dict[row[0]] = {"id": row[0],
4125
                                      "name": row[1],
4126
                                      "uuid": row[2]}
4127
        # query point dict
4128
        query = (" SELECT id, name "
4129
                 " FROM tbl_points ")
4130
        cursor.execute(query)
4131
        rows_points = cursor.fetchall()
4132
4133
        point_dict = dict()
4134
        if rows_points is not None and len(rows_points) > 0:
4135
            for row in rows_points:
4136
                point_dict[row[0]] = {"id": row[0],
4137
                                      "name": row[1]}
4138
4139
        query = (" SELECT id, name, uuid, "
4140
                 "        power_point_id, meter_id, rated_input_power, "
4141
                 "        total_active_power_point_id, "
4142
                 "        active_power_a_point_id, "
4143
                 "        active_power_b_point_id, "
4144
                 "        active_power_c_point_id, "
4145
                 "        total_reactive_power_point_id, "
4146
                 "        reactive_power_a_point_id, "
4147
                 "        reactive_power_b_point_id, "
4148
                 "        reactive_power_c_point_id, "
4149
                 "        total_apparent_power_point_id, "
4150
                 "        apparent_power_a_point_id, "
4151
                 "        apparent_power_b_point_id, "
4152
                 "        apparent_power_c_point_id, "
4153
                 "        total_power_factor_point_id, "
4154
                 "        active_energy_import_point_id, "
4155
                 "        active_energy_export_point_id, "
4156
                 "        active_energy_net_point_id "
4157
                 " FROM tbl_photovoltaic_power_stations_loads "
4158
                 " WHERE photovoltaic_power_station_id = %s "
4159
                 " ORDER BY name ")
4160
        cursor.execute(query, (id_,))
4161
        rows = cursor.fetchall()
4162
4163
        result = list()
4164
        if rows is not None and len(rows) > 0:
4165
            for row in rows:
4166
                meta_result = {"id": row[0],
4167
                               "name": row[1],
4168
                               "uuid": row[2],
4169
                               "power_point": point_dict.get(row[3]),
4170
                               "meter": meter_dict.get(row[4]),
4171
                               "rated_input_power": row[5],
4172
                               "total_active_power_point": point_dict.get(row[6]),
4173
                               "active_power_a_point": point_dict.get(row[7]),
4174
                               "active_power_b_point": point_dict.get(row[8]),
4175
                               "active_power_c_point": point_dict.get(row[9]),
4176
                               "total_reactive_power_point": point_dict.get(row[10]),
4177
                               "reactive_power_a_point": point_dict.get(row[11]),
4178
                               "reactive_power_b_point": point_dict.get(row[12]),
4179
                               "reactive_power_c_point": point_dict.get(row[13]),
4180
                               "total_apparent_power_point": point_dict.get(row[14]),
4181
                               "apparent_power_a_point": point_dict.get(row[15]),
4182
                               "apparent_power_b_point": point_dict.get(row[16]),
4183
                               "apparent_power_c_point": point_dict.get(row[17]),
4184
                               "total_power_factor_point": point_dict.get(row[18]),
4185
                               "active_energy_import_point": point_dict.get(row[19]),
4186
                               "active_energy_export_point": point_dict.get(row[20]),
4187
                               "active_energy_net_point": point_dict.get(row[21])}
4188
                result.append(meta_result)
4189
4190
        resp.text = json.dumps(result)
4191
4192
    @staticmethod
4193
    @user_logger
4194
    def on_post(req, resp, id_):
4195
        """Handles POST requests"""
4196
        admin_control(req)
4197
        try:
4198
            raw_json = req.stream.read().decode('utf-8')
4199
        except Exception as ex:
4200
            print(str(ex))
4201
            raise falcon.HTTPError(status=falcon.HTTP_400,
4202
                                   title='API.BAD_REQUEST',
4203
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4204
        if not id_.isdigit() or int(id_) <= 0:
4205
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4206
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4207
4208
        cnx = mysql.connector.connect(**config.myems_system_db)
4209
        cursor = cnx.cursor()
4210
4211
        cursor.execute(" SELECT name "
4212
                       " FROM tbl_photovoltaic_power_stations "
4213
                       " WHERE id = %s ", (id_,))
4214
        if cursor.fetchone() is None:
4215
            cursor.close()
4216
            cnx.close()
4217
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4218
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4219
4220
        new_values = json.loads(raw_json)
4221
4222
        if 'name' not in new_values['data'].keys() or \
4223
                not isinstance(new_values['data']['name'], str) or \
4224
                len(str.strip(new_values['data']['name'])) == 0:
4225
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4226
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_NAME')
4227
        name = str.strip(new_values['data']['name'])
4228
4229
        if 'power_point_id' not in new_values['data'].keys() or \
4230
                not isinstance(new_values['data']['power_point_id'], int) or \
4231
                new_values['data']['power_point_id'] <= 0:
4232
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4233
                                   description='API.INVALID_POWER_POINT_ID')
4234
        power_point_id = new_values['data']['power_point_id']
4235
4236
        if 'meter_id' not in new_values['data'].keys() or \
4237
                not isinstance(new_values['data']['meter_id'], int) or \
4238
                new_values['data']['meter_id'] <= 0:
4239
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4240
                                   description='API.INVALID_METER_ID')
4241
        meter_id = new_values['data']['meter_id']
4242
4243
        if 'rated_input_power' not in new_values['data'].keys() or \
4244
                not (isinstance(new_values['data']['rated_input_power'], float) or
4245
                     isinstance(new_values['data']['rated_input_power'], int)):
4246
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4247
                                   description='API.INVALID_RATED_INPUT_POWER')
4248
        rated_input_power = Decimal(new_values['data']['rated_input_power'])
4249
4250
        total_active_power_point_id = None
4251
        active_power_a_point_id = None
4252
        active_power_b_point_id = None
4253
        active_power_c_point_id = None
4254
        total_reactive_power_point_id = None
4255
        reactive_power_a_point_id = None
4256
        reactive_power_b_point_id = None
4257
        reactive_power_c_point_id = None
4258
        total_apparent_power_point_id = None
4259
        apparent_power_a_point_id = None
4260
        apparent_power_b_point_id = None
4261
        apparent_power_c_point_id = None
4262
        total_power_factor_point_id = None
4263
        active_energy_import_point_id = None
4264
        active_energy_export_point_id = None
4265
        active_energy_net_point_id = None
4266
4267
        if 'total_active_power_point_id' in new_values['data'].keys() and \
4268
                isinstance(new_values['data']['total_active_power_point_id'], int) and \
4269
                new_values['data']['total_active_power_point_id'] > 0:
4270
            total_active_power_point_id = new_values['data']['total_active_power_point_id']
4271
4272
        if 'active_power_a_point_id' in new_values['data'].keys() and \
4273
                isinstance(new_values['data']['active_power_a_point_id'], int) and \
4274
                new_values['data']['active_power_a_point_id'] > 0:
4275
            active_power_a_point_id = new_values['data']['active_power_a_point_id']
4276
4277
        if 'active_power_b_point_id' in new_values['data'].keys() and \
4278
                isinstance(new_values['data']['active_power_b_point_id'], int) and \
4279
                new_values['data']['active_power_b_point_id'] > 0:
4280
            active_power_b_point_id = new_values['data']['active_power_b_point_id']
4281
4282
        if 'active_power_c_point_id' in new_values['data'].keys() and \
4283
                isinstance(new_values['data']['active_power_c_point_id'], int) and \
4284
                new_values['data']['active_power_c_point_id'] > 0:
4285
            active_power_c_point_id = new_values['data']['active_power_c_point_id']
4286
4287
        if 'total_reactive_power_point_id' in new_values['data'].keys() and \
4288
                isinstance(new_values['data']['total_reactive_power_point_id'], int) and \
4289
                new_values['data']['total_reactive_power_point_id'] > 0:
4290
            total_reactive_power_point_id = new_values['data']['total_reactive_power_point_id']
4291
4292
        if 'reactive_power_a_point_id' in new_values['data'].keys() and \
4293
                isinstance(new_values['data']['reactive_power_a_point_id'], int) and \
4294
                new_values['data']['reactive_power_a_point_id'] > 0:
4295
            reactive_power_a_point_id = new_values['data']['reactive_power_a_point_id']
4296
4297
        if 'reactive_power_b_point_id' in new_values['data'].keys() and \
4298
                isinstance(new_values['data']['reactive_power_b_point_id'], int) and \
4299
                new_values['data']['reactive_power_b_point_id'] > 0:
4300
            reactive_power_b_point_id = new_values['data']['reactive_power_b_point_id']
4301
4302
        if 'reactive_power_c_point_id' in new_values['data'].keys() and \
4303
                isinstance(new_values['data']['reactive_power_c_point_id'], int) and \
4304
                new_values['data']['reactive_power_c_point_id'] > 0:
4305
            reactive_power_c_point_id = new_values['data']['reactive_power_c_point_id']
4306
4307
        if 'total_apparent_power_point_id' in new_values['data'].keys() and \
4308
                isinstance(new_values['data']['total_apparent_power_point_id'], int) and \
4309
                new_values['data']['total_apparent_power_point_id'] > 0:
4310
            total_apparent_power_point_id = new_values['data']['total_apparent_power_point_id']
4311
4312
        if 'apparent_power_a_point_id' in new_values['data'].keys() and \
4313
                isinstance(new_values['data']['apparent_power_a_point_id'], int) and \
4314
                new_values['data']['apparent_power_a_point_id'] > 0:
4315
            apparent_power_a_point_id = new_values['data']['apparent_power_a_point_id']
4316
4317
        if 'apparent_power_b_point_id' in new_values['data'].keys() and \
4318
                isinstance(new_values['data']['apparent_power_b_point_id'], int) and \
4319
                new_values['data']['apparent_power_b_point_id'] > 0:
4320
            apparent_power_b_point_id = new_values['data']['apparent_power_b_point_id']
4321
4322
        if 'apparent_power_c_point_id' in new_values['data'].keys() and \
4323
                isinstance(new_values['data']['apparent_power_c_point_id'], int) and \
4324
                new_values['data']['apparent_power_c_point_id'] > 0:
4325
            apparent_power_c_point_id = new_values['data']['apparent_power_c_point_id']
4326
4327
        if 'total_power_factor_point_id' in new_values['data'].keys() and \
4328
                isinstance(new_values['data']['total_power_factor_point_id'], int) and \
4329
                new_values['data']['total_power_factor_point_id'] > 0:
4330
            total_power_factor_point_id = new_values['data']['total_power_factor_point_id']
4331
4332
        if 'active_energy_import_point_id' in new_values['data'].keys() and \
4333
                isinstance(new_values['data']['active_energy_import_point_id'], int) and \
4334
                new_values['data']['active_energy_import_point_id'] > 0:
4335
            active_energy_import_point_id = new_values['data']['active_energy_import_point_id']
4336
4337
        if 'active_energy_export_point_id' in new_values['data'].keys() and \
4338
                isinstance(new_values['data']['active_energy_export_point_id'], int) and \
4339
                new_values['data']['active_energy_export_point_id'] > 0:
4340
            active_energy_export_point_id = new_values['data']['active_energy_export_point_id']
4341
4342
        if 'active_energy_net_point_id' in new_values['data'].keys() and \
4343
                isinstance(new_values['data']['active_energy_net_point_id'], int) and \
4344
                new_values['data']['active_energy_net_point_id'] > 0:
4345
            active_energy_net_point_id = new_values['data']['active_energy_net_point_id']
4346
4347
        cnx = mysql.connector.connect(**config.myems_system_db)
4348
        cursor = cnx.cursor()
4349
4350
        cursor.execute(" SELECT name "
4351
                       " FROM tbl_photovoltaic_power_stations "
4352
                       " WHERE id = %s ",
4353
                       (id_,))
4354
        if cursor.fetchone() is None:
4355
            cursor.close()
4356
            cnx.close()
4357
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4358
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4359
4360
        cursor.execute(" SELECT name "
4361
                       " FROM tbl_photovoltaic_power_stations_loads "
4362
                       " WHERE photovoltaic_power_station_id = %s AND name = %s ",
4363
                       (id_, name,))
4364
        if cursor.fetchone() is not None:
4365
            cursor.close()
4366
            cnx.close()
4367
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4368
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NAME_IS_ALREADY_IN_USE')
4369
4370
        cursor.execute(" SELECT name "
4371
                       " FROM tbl_points "
4372
                       " WHERE id = %s ",
4373
                       (power_point_id,))
4374
        if cursor.fetchone() is None:
4375
            cursor.close()
4376
            cnx.close()
4377
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4378
                                   description='API.POWER_POINT_NOT_FOUND')
4379
4380
        cursor.execute(" SELECT name "
4381
                       " FROM tbl_meters "
4382
                       " WHERE id = %s ",
4383
                       (meter_id,))
4384
        if cursor.fetchone() is None:
4385
            cursor.close()
4386
            cnx.close()
4387
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4388
                                   description='API.METER_NOT_FOUND')
4389
4390
        add_values = (" INSERT INTO tbl_photovoltaic_power_stations_loads "
4391
                      "    (name, uuid, photovoltaic_power_station_id, power_point_id, meter_id, rated_input_power,"
4392
                      "     total_active_power_point_id,"
4393
                      "     active_power_a_point_id,"
4394
                      "     active_power_b_point_id,"
4395
                      "     active_power_c_point_id,"
4396
                      "     total_reactive_power_point_id,"
4397
                      "     reactive_power_a_point_id,"
4398
                      "     reactive_power_b_point_id,"
4399
                      "     reactive_power_c_point_id,"
4400
                      "     total_apparent_power_point_id,"
4401
                      "     apparent_power_a_point_id,"
4402
                      "     apparent_power_b_point_id,"
4403
                      "     apparent_power_c_point_id,"
4404
                      "     total_power_factor_point_id,"
4405
                      "     active_energy_import_point_id,"
4406
                      "     active_energy_export_point_id,"
4407
                      "     active_energy_net_point_id) "
4408
                      " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,"
4409
                      "         %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,"
4410
                      "         %s, %s) ")
4411
        cursor.execute(add_values, (name,
4412
                                    str(uuid.uuid4()),
4413
                                    id_,
4414
                                    power_point_id,
4415
                                    meter_id,
4416
                                    rated_input_power,
4417
                                    total_active_power_point_id,
4418
                                    active_power_a_point_id,
4419
                                    active_power_b_point_id,
4420
                                    active_power_c_point_id,
4421
                                    total_reactive_power_point_id,
4422
                                    reactive_power_a_point_id,
4423
                                    reactive_power_b_point_id,
4424
                                    reactive_power_c_point_id,
4425
                                    total_apparent_power_point_id,
4426
                                    apparent_power_a_point_id,
4427
                                    apparent_power_b_point_id,
4428
                                    apparent_power_c_point_id,
4429
                                    total_power_factor_point_id,
4430
                                    active_energy_import_point_id,
4431
                                    active_energy_export_point_id,
4432
                                    active_energy_net_point_id
4433
                                    ))
4434
        new_id = cursor.lastrowid
4435
        cnx.commit()
4436
        cursor.close()
4437
        cnx.close()
4438
4439
        resp.status = falcon.HTTP_201
4440
        resp.location = '/photovoltaicpowerstations/' + str(id_) + '/loads/' + str(new_id)
4441
4442
4443
class PhotovoltaicPowerStationLoadItem:
4444
    def __init__(self):
4445
        """Initializes Class"""
4446
        pass
4447
4448
    @staticmethod
4449
    def on_options(req, resp, id_, lid):
4450
        _ = req
4451
        resp.status = falcon.HTTP_200
4452
        _ = id_
4453
4454 View Code Duplication
    @staticmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4455
    def on_get(req, resp, id_, lid):
4456
        access_control(req)
4457
        if not id_.isdigit() or int(id_) <= 0:
4458
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4459
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4460
        if not lid.isdigit() or int(lid) <= 0:
4461
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4462
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_ID')
4463
4464
        cnx = mysql.connector.connect(**config.myems_system_db)
4465
        cursor = cnx.cursor()
4466
4467
        cursor.execute(" SELECT name "
4468
                       " FROM tbl_photovoltaic_power_stations "
4469
                       " WHERE id = %s ", (id_,))
4470
        if cursor.fetchone() is None:
4471
            cursor.close()
4472
            cnx.close()
4473
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4474
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4475
4476
        query = (" SELECT id, name, uuid "
4477
                 " FROM tbl_photovoltaic_power_stations ")
4478
        cursor.execute(query)
4479
        rows_photovoltaic_power_stations = cursor.fetchall()
4480
4481
        photovoltaic_power_station_dict = dict()
4482
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
4483
            for row in rows_photovoltaic_power_stations:
4484
                photovoltaic_power_station_dict[row[0]] = {"id": row[0],
4485
                                                           "name": row[1],
4486
                                                           "uuid": row[2]}
4487
        # query meter dict
4488
        query = (" SELECT id, name, uuid "
4489
                 " FROM tbl_meters ")
4490
        cursor.execute(query)
4491
        rows_meters = cursor.fetchall()
4492
4493
        meter_dict = dict()
4494
        if rows_meters is not None and len(rows_meters) > 0:
4495
            for row in rows_meters:
4496
                meter_dict[row[0]] = {"id": row[0],
4497
                                      "name": row[1],
4498
                                      "uuid": row[2]}
4499
        # query point dict
4500
        query = (" SELECT id, name "
4501
                 " FROM tbl_points ")
4502
        cursor.execute(query)
4503
        rows_points = cursor.fetchall()
4504
4505
        point_dict = dict()
4506
        if rows_points is not None and len(rows_points) > 0:
4507
            for row in rows_points:
4508
                point_dict[row[0]] = {"id": row[0],
4509
                                      "name": row[1]}
4510
4511
        query = (" SELECT id, name, uuid, "
4512
                 "        photovoltaic_power_station_id, "
4513
                 "        power_point_id, meter_id, "
4514
                 "        rated_input_power, "
4515
                 "        total_active_power_point_id, "
4516
                 "        active_power_a_point_id, "
4517
                 "        active_power_b_point_id, "
4518
                 "        active_power_c_point_id, "
4519
                 "        total_reactive_power_point_id, "
4520
                 "        reactive_power_a_point_id, "
4521
                 "        reactive_power_b_point_id, "
4522
                 "        reactive_power_c_point_id, "
4523
                 "        total_apparent_power_point_id, "
4524
                 "        apparent_power_a_point_id, "
4525
                 "        apparent_power_b_point_id, "
4526
                 "        apparent_power_c_point_id, "
4527
                 "        total_power_factor_point_id, "
4528
                 "        active_energy_import_point_id, "
4529
                 "        active_energy_export_point_id, "
4530
                 "        active_energy_net_point_id "
4531
                 " FROM tbl_photovoltaic_power_stations_loads "
4532
                 " WHERE id = %s ")
4533
        cursor.execute(query, (lid,))
4534
        row = cursor.fetchone()
4535
        cursor.close()
4536
        cnx.close()
4537
4538
        if row is None:
4539
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4540
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NOT_FOUND')
4541
        else:
4542
            meta_result = {"id": row[0],
4543
                           "name": row[1],
4544
                           "uuid": row[2],
4545
                           "photovoltaic_power_station": photovoltaic_power_station_dict.get(row[3]),
4546
                           "power_point": point_dict.get(row[4]),
4547
                           "meter": meter_dict.get(row[5]),
4548
                           "rated_input_power": row[6],
4549
                           "total_active_power_point": point_dict.get(row[7]),
4550
                           "active_power_a_point": point_dict.get(row[8]),
4551
                           "active_power_b_point": point_dict.get(row[9]),
4552
                           "active_power_c_point": point_dict.get(row[10]),
4553
                           "total_reactive_power_point": point_dict.get(row[11]),
4554
                           "reactive_power_a_point": point_dict.get(row[12]),
4555
                           "reactive_power_b_point": point_dict.get(row[13]),
4556
                           "reactive_power_c_point": point_dict.get(row[14]),
4557
                           "total_apparent_power_point": point_dict.get(row[15]),
4558
                           "apparent_power_a_point": point_dict.get(row[16]),
4559
                           "apparent_power_b_point": point_dict.get(row[17]),
4560
                           "apparent_power_c_point": point_dict.get(row[18]),
4561
                           "total_power_factor_point": point_dict.get(row[19]),
4562
                           "active_energy_import_point": point_dict.get(row[20]),
4563
                           "active_energy_export_point": point_dict.get(row[21]),
4564
                           "active_energy_net_point": point_dict.get(row[22])}
4565
4566
        resp.text = json.dumps(meta_result)
4567
4568
    @staticmethod
4569
    @user_logger
4570
    def on_delete(req, resp, id_, lid):
4571
        admin_control(req)
4572
        if not id_.isdigit() or int(id_) <= 0:
4573
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4574
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4575
        if not lid.isdigit() or int(lid) <= 0:
4576
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4577
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_ID')
4578
4579
        cnx = mysql.connector.connect(**config.myems_system_db)
4580
        cursor = cnx.cursor()
4581
4582
        cursor.execute(" SELECT name "
4583
                       " FROM tbl_photovoltaic_power_stations "
4584
                       " WHERE id = %s ", (id_,))
4585
        if cursor.fetchone() is None:
4586
            cursor.close()
4587
            cnx.close()
4588
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4589
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4590
4591
        cursor.execute(" SELECT name "
4592
                       " FROM tbl_photovoltaic_power_stations_loads "
4593
                       " WHERE id = %s ", (lid,))
4594
        if cursor.fetchone() is None:
4595
            cursor.close()
4596
            cnx.close()
4597
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4598
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NOT_FOUND')
4599
4600
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_loads "
4601
                       " WHERE id = %s ", (lid,))
4602
        cnx.commit()
4603
4604
        cursor.close()
4605
        cnx.close()
4606
4607
        resp.status = falcon.HTTP_204
4608
4609
    @staticmethod
4610
    @user_logger
4611
    def on_put(req, resp, id_, lid):
4612
        """Handles PUT requests"""
4613
        admin_control(req)
4614
        try:
4615
            raw_json = req.stream.read().decode('utf-8')
4616
        except Exception as ex:
4617
            print(str(ex))
4618
            raise falcon.HTTPError(status=falcon.HTTP_400,
4619
                                   title='API.BAD_REQUEST',
4620
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4621
        if not id_.isdigit() or int(id_) <= 0:
4622
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4623
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4624
        if not lid.isdigit() or int(lid) <= 0:
4625
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4626
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_ID')
4627
4628
        new_values = json.loads(raw_json)
4629
4630
        if 'name' not in new_values['data'].keys() or \
4631
                not isinstance(new_values['data']['name'], str) or \
4632
                len(str.strip(new_values['data']['name'])) == 0:
4633
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4634
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_LOAD_NAME')
4635
        name = str.strip(new_values['data']['name'])
4636
4637
        if 'power_point_id' not in new_values['data'].keys() or \
4638
                not isinstance(new_values['data']['power_point_id'], int) or \
4639
                new_values['data']['power_point_id'] <= 0:
4640
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4641
                                   description='API.INVALID_POWER_POINT_ID')
4642
        power_point_id = new_values['data']['power_point_id']
4643
4644
        if 'meter_id' not in new_values['data'].keys() or \
4645
                not isinstance(new_values['data']['meter_id'], int) or \
4646
                new_values['data']['meter_id'] <= 0:
4647
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4648
                                   description='API.INVALID_METER_ID')
4649
        meter_id = new_values['data']['meter_id']
4650
4651
        if 'rated_input_power' not in new_values['data'].keys() or \
4652
                not (isinstance(new_values['data']['rated_input_power'], float) or
4653
                     isinstance(new_values['data']['rated_input_power'], int)):
4654
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4655
                                   description='API.INVALID_RATED_INPUT_POWER')
4656
        rated_input_power = Decimal(new_values['data']['rated_input_power'])
4657
4658
        total_active_power_point_id = None
4659
        active_power_a_point_id = None
4660
        active_power_b_point_id = None
4661
        active_power_c_point_id = None
4662
        total_reactive_power_point_id = None
4663
        reactive_power_a_point_id = None
4664
        reactive_power_b_point_id = None
4665
        reactive_power_c_point_id = None
4666
        total_apparent_power_point_id = None
4667
        apparent_power_a_point_id = None
4668
        apparent_power_b_point_id = None
4669
        apparent_power_c_point_id = None
4670
        total_power_factor_point_id = None
4671
        active_energy_import_point_id = None
4672
        active_energy_export_point_id = None
4673
        active_energy_net_point_id = None
4674
4675
        if 'total_active_power_point_id' in new_values['data'].keys() and \
4676
                isinstance(new_values['data']['total_active_power_point_id'], int) and \
4677
                new_values['data']['total_active_power_point_id'] > 0:
4678
            total_active_power_point_id = new_values['data']['total_active_power_point_id']
4679
4680
        if 'active_power_a_point_id' in new_values['data'].keys() and \
4681
                isinstance(new_values['data']['active_power_a_point_id'], int) and \
4682
                new_values['data']['active_power_a_point_id'] > 0:
4683
            active_power_a_point_id = new_values['data']['active_power_a_point_id']
4684
4685
        if 'active_power_b_point_id' in new_values['data'].keys() and \
4686
                isinstance(new_values['data']['active_power_b_point_id'], int) and \
4687
                new_values['data']['active_power_b_point_id'] > 0:
4688
            active_power_b_point_id = new_values['data']['active_power_b_point_id']
4689
4690
        if 'active_power_c_point_id' in new_values['data'].keys() and \
4691
                isinstance(new_values['data']['active_power_c_point_id'], int) and \
4692
                new_values['data']['active_power_c_point_id'] > 0:
4693
            active_power_c_point_id = new_values['data']['active_power_c_point_id']
4694
4695
        if 'total_reactive_power_point_id' in new_values['data'].keys() and \
4696
                isinstance(new_values['data']['total_reactive_power_point_id'], int) and \
4697
                new_values['data']['total_reactive_power_point_id'] > 0:
4698
            total_reactive_power_point_id = new_values['data']['total_reactive_power_point_id']
4699
4700
        if 'reactive_power_a_point_id' in new_values['data'].keys() and \
4701
                isinstance(new_values['data']['reactive_power_a_point_id'], int) and \
4702
                new_values['data']['reactive_power_a_point_id'] > 0:
4703
            reactive_power_a_point_id = new_values['data']['reactive_power_a_point_id']
4704
4705
        if 'reactive_power_b_point_id' in new_values['data'].keys() and \
4706
                isinstance(new_values['data']['reactive_power_b_point_id'], int) and \
4707
                new_values['data']['reactive_power_b_point_id'] > 0:
4708
            reactive_power_b_point_id = new_values['data']['reactive_power_b_point_id']
4709
4710
        if 'reactive_power_c_point_id' in new_values['data'].keys() and \
4711
                isinstance(new_values['data']['reactive_power_c_point_id'], int) and \
4712
                new_values['data']['reactive_power_c_point_id'] > 0:
4713
            reactive_power_c_point_id = new_values['data']['reactive_power_c_point_id']
4714
4715
        if 'total_apparent_power_point_id' in new_values['data'].keys() and \
4716
                isinstance(new_values['data']['total_apparent_power_point_id'], int) and \
4717
                new_values['data']['total_apparent_power_point_id'] > 0:
4718
            total_apparent_power_point_id = new_values['data']['total_apparent_power_point_id']
4719
4720
        if 'apparent_power_a_point_id' in new_values['data'].keys() and \
4721
                isinstance(new_values['data']['apparent_power_a_point_id'], int) and \
4722
                new_values['data']['apparent_power_a_point_id'] > 0:
4723
            apparent_power_a_point_id = new_values['data']['apparent_power_a_point_id']
4724
4725
        if 'apparent_power_b_point_id' in new_values['data'].keys() and \
4726
                isinstance(new_values['data']['apparent_power_b_point_id'], int) and \
4727
                new_values['data']['apparent_power_b_point_id'] > 0:
4728
            apparent_power_b_point_id = new_values['data']['apparent_power_b_point_id']
4729
4730
        if 'apparent_power_c_point_id' in new_values['data'].keys() and \
4731
                isinstance(new_values['data']['apparent_power_c_point_id'], int) and \
4732
                new_values['data']['apparent_power_c_point_id'] > 0:
4733
            apparent_power_c_point_id = new_values['data']['apparent_power_c_point_id']
4734
4735
        if 'total_power_factor_point_id' in new_values['data'].keys() and \
4736
                isinstance(new_values['data']['total_power_factor_point_id'], int) and \
4737
                new_values['data']['total_power_factor_point_id'] > 0:
4738
            total_power_factor_point_id = new_values['data']['total_power_factor_point_id']
4739
4740
        if 'active_energy_import_point_id' in new_values['data'].keys() and \
4741
                isinstance(new_values['data']['active_energy_import_point_id'], int) and \
4742
                new_values['data']['active_energy_import_point_id'] > 0:
4743
            active_energy_import_point_id = new_values['data']['active_energy_import_point_id']
4744
4745
        if 'active_energy_export_point_id' in new_values['data'].keys() and \
4746
                isinstance(new_values['data']['active_energy_export_point_id'], int) and \
4747
                new_values['data']['active_energy_export_point_id'] > 0:
4748
            active_energy_export_point_id = new_values['data']['active_energy_export_point_id']
4749
4750
        if 'active_energy_net_point_id' in new_values['data'].keys() and \
4751
                isinstance(new_values['data']['active_energy_net_point_id'], int) and \
4752
                new_values['data']['active_energy_net_point_id'] > 0:
4753
            active_energy_net_point_id = new_values['data']['active_energy_net_point_id']
4754
4755
        cnx = mysql.connector.connect(**config.myems_system_db)
4756
        cursor = cnx.cursor()
4757
4758
        cursor.execute(" SELECT name "
4759
                       " FROM tbl_photovoltaic_power_stations "
4760
                       " WHERE id = %s ", (id_,))
4761
        if cursor.fetchone() is None:
4762
            cursor.close()
4763
            cnx.close()
4764
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4765
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4766
4767
        cursor.execute(" SELECT name "
4768
                       " FROM tbl_photovoltaic_power_stations_loads "
4769
                       " WHERE id = %s ", (lid,))
4770
        if cursor.fetchone() is None:
4771
            cursor.close()
4772
            cnx.close()
4773
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4774
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NOT_FOUND')
4775
4776
        cursor.execute(" SELECT name "
4777
                       " FROM tbl_photovoltaic_power_stations_loads "
4778
                       " WHERE photovoltaic_power_station_id = %s AND name = %s AND id != %s ",
4779
                       (id_, name, lid))
4780
        if cursor.fetchone() is not None:
4781
            cursor.close()
4782
            cnx.close()
4783
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4784
                                   description='API.PHOTOVOLTAIC_POWER_STATION_LOAD_NAME_IS_ALREADY_IN_USE')
4785
4786
        cursor.execute(" SELECT name "
4787
                       " FROM tbl_points "
4788
                       " WHERE id = %s ",
4789
                       (power_point_id,))
4790
        if cursor.fetchone() is None:
4791
            cursor.close()
4792
            cnx.close()
4793
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4794
                                   description='API.POWER_POINT_NOT_FOUND')
4795
4796
        cursor.execute(" SELECT name "
4797
                       " FROM tbl_meters "
4798
                       " WHERE id = %s ",
4799
                       (meter_id,))
4800
        if cursor.fetchone() is None:
4801
            cursor.close()
4802
            cnx.close()
4803
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4804
                                   description='API.METER_NOT_FOUND')
4805
4806
        update_row = (" UPDATE tbl_photovoltaic_power_stations_loads "
4807
                      " SET name = %s, photovoltaic_power_station_id = %s, power_point_id = %s, "
4808
                      "     meter_id = %s, rated_input_power = %s, "
4809
                      "     total_active_power_point_id = %s, "
4810
                      "     active_power_a_point_id = %s, "
4811
                      "     active_power_b_point_id = %s, "
4812
                      "     active_power_c_point_id = %s, "
4813
                      "     total_reactive_power_point_id = %s, "
4814
                      "     reactive_power_a_point_id = %s, "
4815
                      "     reactive_power_b_point_id = %s, "
4816
                      "     reactive_power_c_point_id = %s, "
4817
                      "     total_apparent_power_point_id = %s, "
4818
                      "     apparent_power_a_point_id = %s, "
4819
                      "     apparent_power_b_point_id = %s, "
4820
                      "     apparent_power_c_point_id = %s, "
4821
                      "     total_power_factor_point_id = %s, "
4822
                      "     active_energy_import_point_id = %s, "
4823
                      "     active_energy_export_point_id = %s, "
4824
                      "     active_energy_net_point_id = %s "
4825
                      " WHERE id = %s ")
4826
        cursor.execute(update_row, (name,
4827
                                    id_,
4828
                                    power_point_id,
4829
                                    meter_id,
4830
                                    rated_input_power,
4831
                                    total_active_power_point_id,
4832
                                    active_power_a_point_id,
4833
                                    active_power_b_point_id,
4834
                                    active_power_c_point_id,
4835
                                    total_reactive_power_point_id,
4836
                                    reactive_power_a_point_id,
4837
                                    reactive_power_b_point_id,
4838
                                    reactive_power_c_point_id,
4839
                                    total_apparent_power_point_id,
4840
                                    apparent_power_a_point_id,
4841
                                    apparent_power_b_point_id,
4842
                                    apparent_power_c_point_id,
4843
                                    total_power_factor_point_id,
4844
                                    active_energy_import_point_id,
4845
                                    active_energy_export_point_id,
4846
                                    active_energy_net_point_id,
4847
                                    lid))
4848
        cnx.commit()
4849
4850
        cursor.close()
4851
        cnx.close()
4852
4853
        resp.status = falcon.HTTP_200
4854
4855
4856 View Code Duplication
class PhotovoltaicPowerStationUserCollection:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4857
    def __init__(self):
4858
        """Initializes Class"""
4859
        pass
4860
4861
    @staticmethod
4862
    def on_options(req, resp, id_):
4863
        _ = req
4864
        resp.status = falcon.HTTP_200
4865
        _ = id_
4866
4867
    @staticmethod
4868
    def on_get(req, resp, id_):
4869
        access_control(req)
4870
        if not id_.isdigit() or int(id_) <= 0:
4871
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4872
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4873
4874
        cnx = mysql.connector.connect(**config.myems_system_db)
4875
        cursor = cnx.cursor()
4876
        cursor.execute(" SELECT name "
4877
                       " FROM tbl_photovoltaic_power_stations "
4878
                       " WHERE id = %s ", (id_,))
4879
        if cursor.fetchone() is None:
4880
            cursor.close()
4881
            cnx.close()
4882
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4883
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4884
4885
        query = (" SELECT u.id, u.name, u.uuid "
4886
                 " FROM tbl_photovoltaic_power_stations m, tbl_photovoltaic_power_stations_users mu, "
4887
                 + config.myems_user_db['database'] + ".tbl_users u "
4888
                 " WHERE mu.photovoltaic_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s "
4889
                 " ORDER BY u.id ")
4890
        cursor.execute(query, (id_,))
4891
        rows = cursor.fetchall()
4892
        result = list()
4893
        if rows is not None and len(rows) > 0:
4894
            for row in rows:
4895
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
4896
                result.append(meta_result)
4897
4898
        cursor.close()
4899
        cnx.close()
4900
        resp.text = json.dumps(result)
4901
4902
    @staticmethod
4903
    @user_logger
4904
    def on_post(req, resp, id_):
4905
        """Handles POST requests"""
4906
        admin_control(req)
4907
        try:
4908
            raw_json = req.stream.read().decode('utf-8')
4909
        except Exception as ex:
4910
            print(str(ex))
4911
            raise falcon.HTTPError(status=falcon.HTTP_400,
4912
                                   title='API.BAD_REQUEST',
4913
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
4914
4915
        if not id_.isdigit() or int(id_) <= 0:
4916
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4917
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4918
4919
        new_values = json.loads(raw_json)
4920
        if 'user_id' not in new_values['data'].keys() or \
4921
                not isinstance(new_values['data']['user_id'], int) or \
4922
                new_values['data']['user_id'] <= 0:
4923
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4924
                                   description='API.INVALID_USER_ID')
4925
        user_id = new_values['data']['user_id']
4926
        cnx = mysql.connector.connect(**config.myems_system_db)
4927
        cursor = cnx.cursor()
4928
        cursor.execute(" SELECT name "
4929
                       " FROM tbl_photovoltaic_power_stations "
4930
                       " WHERE id = %s ", (id_,))
4931
        if cursor.fetchone() is None:
4932
            cursor.close()
4933
            cnx.close()
4934
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4935
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
4936
4937
        cnx_user = mysql.connector.connect(**config.myems_user_db)
4938
        cursor_user = cnx_user.cursor()
4939
        cursor_user.execute(" SELECT name"
4940
                            " FROM tbl_users "
4941
                            " WHERE id = %s ", (user_id,))
4942
        if cursor_user.fetchone() is None:
4943
            cursor_user.close()
4944
            cnx_user.close()
4945
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4946
                                   description='API.USER_NOT_FOUND')
4947
        query = (" SELECT id "
4948
                 " FROM tbl_photovoltaic_power_stations_users "
4949
                 " WHERE photovoltaic_power_station_id = %s AND user_id = %s")
4950
        cursor.execute(query, (id_, user_id,))
4951
        if cursor.fetchone() is not None:
4952
            cursor.close()
4953
            cnx.close()
4954
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
4955
                                   description='API.PHOTOVOLTAIC_POWER_STATION_USER_RELATION_EXISTS')
4956
        add_row = (" INSERT INTO tbl_photovoltaic_power_stations_users (photovoltaic_power_station_id, user_id) "
4957
                   " VALUES (%s, %s) ")
4958
        cursor.execute(add_row, (id_, user_id,))
4959
        cnx.commit()
4960
        cursor.close()
4961
        cnx.close()
4962
        cursor_user.close()
4963
        cnx_user.close()
4964
4965
        resp.status = falcon.HTTP_201
4966
        resp.location = '/photovoltaicpowerstations/' + str(id_) + '/users/' + str(user_id)
4967
4968
4969 View Code Duplication
class PhotovoltaicPowerStationUserItem:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
4970
    def __init__(self):
4971
        """Initializes Class"""
4972
        pass
4973
4974
    @staticmethod
4975
    def on_options(req, resp, id_, uid):
4976
        _ = req
4977
        resp.status = falcon.HTTP_200
4978
        _ = id_
4979
4980
    @staticmethod
4981
    @user_logger
4982
    def on_delete(req, resp, id_, uid):
4983
        # todo Verify if the user is bound when deleting it
4984
        admin_control(req)
4985
        if not id_.isdigit() or int(id_) <= 0:
4986
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4987
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
4988
4989
        if not uid.isdigit() or int(uid) <= 0:
4990
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4991
                                   description='API.INVALID_USER_ID')
4992
4993
        cnx = mysql.connector.connect(**config.myems_system_db)
4994
        cursor = cnx.cursor()
4995
        cursor.execute(" SELECT name "
4996
                       " FROM tbl_photovoltaic_power_stations "
4997
                       " WHERE id = %s ", (id_,))
4998
        if cursor.fetchone() is None:
4999
            cursor.close()
5000
            cnx.close()
5001
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5002
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
5003
5004
        cnx_user = mysql.connector.connect(**config.myems_user_db)
5005
        cursor_user = cnx_user.cursor()
5006
        cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,))
5007
        if cursor_user.fetchone() is None:
5008
            cursor_user.close()
5009
            cnx_user.close()
5010
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5011
                                   description='API.USER_NOT_FOUND')
5012
5013
        cursor.execute(" SELECT id "
5014
                       " FROM tbl_photovoltaic_power_stations_users "
5015
                       " WHERE photovoltaic_power_station_id = %s AND user_id = %s ", (id_, uid))
5016
        if cursor.fetchone() is None:
5017
            cursor.close()
5018
            cnx.close()
5019
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5020
                                   description='API.PHOTOVOLTAIC_POWER_STATION_USER_RELATION_NOT_FOUND')
5021
5022
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_users "
5023
                       " WHERE photovoltaic_power_station_id = %s AND user_id = %s ", (id_, uid))
5024
        cnx.commit()
5025
5026
        cursor.close()
5027
        cnx.close()
5028
        cursor_user.close()
5029
        cnx_user.close()
5030
5031
        resp.status = falcon.HTTP_204
5032
5033
5034 View Code Duplication
class PhotovoltaicPowerStationDataSourceCollection:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5035
    def __init__(self):
5036
        """Initializes Class"""
5037
        pass
5038
5039
    @staticmethod
5040
    def on_options(req, resp, id_):
5041
        _ = req
5042
        resp.status = falcon.HTTP_200
5043
        _ = id_
5044
5045
    @staticmethod
5046
    def on_get(req, resp, id_):
5047
        if 'API-KEY' not in req.headers or \
5048
                not isinstance(req.headers['API-KEY'], str) or \
5049
                len(str.strip(req.headers['API-KEY'])) == 0:
5050
            access_control(req)
5051
        else:
5052
            api_key_control(req)
5053
        if not id_.isdigit() or int(id_) <= 0:
5054
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5055
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5056
5057
        cnx = mysql.connector.connect(**config.myems_system_db)
5058
        cursor = cnx.cursor()
5059
5060
        cursor.execute(" SELECT name "
5061
                       " FROM tbl_photovoltaic_power_stations "
5062
                       " WHERE id = %s ", (id_,))
5063
        if cursor.fetchone() is None:
5064
            cursor.close()
5065
            cnx.close()
5066
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5067
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
5068
5069
        query = (" SELECT ds.id, ds.name, ds.uuid "
5070
                 " FROM tbl_photovoltaic_power_stations psp, tbl_photovoltaic_power_stations_data_sources psds, "
5071
                 "      tbl_data_sources ds "
5072
                 " WHERE psds.photovoltaic_power_station_id = psp.id AND ds.id = psds.data_source_id AND psp.id = %s "
5073
                 " ORDER BY ds.id ")
5074
        cursor.execute(query, (id_,))
5075
        rows = cursor.fetchall()
5076
5077
        result = list()
5078
        if rows is not None and len(rows) > 0:
5079
            for row in rows:
5080
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
5081
                result.append(meta_result)
5082
5083
        resp.text = json.dumps(result)
5084
5085
    @staticmethod
5086
    @user_logger
5087
    def on_post(req, resp, id_):
5088
        """Handles POST requests"""
5089
        admin_control(req)
5090
        try:
5091
            raw_json = req.stream.read().decode('utf-8')
5092
        except Exception as ex:
5093
            print(str(ex))
5094
            raise falcon.HTTPError(status=falcon.HTTP_400,
5095
                                   title='API.BAD_REQUEST',
5096
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5097
5098
        if not id_.isdigit() or int(id_) <= 0:
5099
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5100
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5101
5102
        new_values = json.loads(raw_json)
5103
5104
        if 'data_source_id' not in new_values['data'].keys() or \
5105
                not isinstance(new_values['data']['data_source_id'], int) or \
5106
                new_values['data']['data_source_id'] <= 0:
5107
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5108
                                   description='API.INVALID_DATA_SOURCE_ID')
5109
        data_source_id = new_values['data']['data_source_id']
5110
5111
        cnx = mysql.connector.connect(**config.myems_system_db)
5112
        cursor = cnx.cursor()
5113
5114
        cursor.execute(" SELECT name "
5115
                       " from tbl_photovoltaic_power_stations "
5116
                       " WHERE id = %s ", (id_,))
5117
        if cursor.fetchone() is None:
5118
            cursor.close()
5119
            cnx.close()
5120
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5121
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
5122
5123
        cursor.execute(" SELECT name "
5124
                       " FROM tbl_data_sources "
5125
                       " WHERE id = %s ", (data_source_id,))
5126
        if cursor.fetchone() is None:
5127
            cursor.close()
5128
            cnx.close()
5129
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5130
                                   description='API.DATA_SOURCE_NOT_FOUND')
5131
5132
        query = (" SELECT id " 
5133
                 " FROM tbl_photovoltaic_power_stations_data_sources "
5134
                 " WHERE photovoltaic_power_station_id = %s AND data_source_id = %s")
5135
        cursor.execute(query, (id_, data_source_id,))
5136
        if cursor.fetchone() is not None:
5137
            cursor.close()
5138
            cnx.close()
5139
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
5140
                                   description='API.PHOTOVOLTAIC_POWER_STATION_DATA_SOURCE_RELATION_EXISTS')
5141
5142
        add_row = (" INSERT INTO tbl_photovoltaic_power_stations_data_sources "
5143
                   "             (photovoltaic_power_station_id, data_source_id) "
5144
                   " VALUES (%s, %s) ")
5145
        cursor.execute(add_row, (id_, data_source_id,))
5146
        cnx.commit()
5147
        cursor.close()
5148
        cnx.close()
5149
5150
        resp.status = falcon.HTTP_201
5151
        resp.location = '/photovoltaicpowerstations/' + str(id_) + '/datasources/' + str(data_source_id)
5152
5153
5154 View Code Duplication
class PhotovoltaicPowerStationDataSourceItem:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5155
    def __init__(self):
5156
        """Initializes Class"""
5157
        pass
5158
5159
    @staticmethod
5160
    def on_options(req, resp, id_, dsid):
5161
        _ = req
5162
        resp.status = falcon.HTTP_200
5163
        _ = id_
5164
        _ = dsid
5165
5166
    @staticmethod
5167
    @user_logger
5168
    def on_delete(req, resp, id_, dsid):
5169
        admin_control(req)
5170
        if not id_.isdigit() or int(id_) <= 0:
5171
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5172
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5173
5174
        if not dsid.isdigit() or int(dsid) <= 0:
5175
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5176
                                   description='API.INVALID_DATA_SOURCE_ID')
5177
5178
        cnx = mysql.connector.connect(**config.myems_system_db)
5179
        cursor = cnx.cursor()
5180
5181
        cursor.execute(" SELECT name "
5182
                       " FROM tbl_photovoltaic_power_stations "
5183
                       " WHERE id = %s ", (id_,))
5184
        if cursor.fetchone() is None:
5185
            cursor.close()
5186
            cnx.close()
5187
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5188
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
5189
5190
        cursor.execute(" SELECT name "
5191
                       " FROM tbl_data_sources "
5192
                       " WHERE id = %s ", (dsid,))
5193
        if cursor.fetchone() is None:
5194
            cursor.close()
5195
            cnx.close()
5196
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5197
                                   description='API.DATA_SOURCE_NOT_FOUND')
5198
5199
        cursor.execute(" SELECT id "
5200
                       " FROM tbl_photovoltaic_power_stations_data_sources "
5201
                       " WHERE photovoltaic_power_station_id = %s AND data_source_id = %s ", (id_, dsid))
5202
        if cursor.fetchone() is None:
5203
            cursor.close()
5204
            cnx.close()
5205
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5206
                                   description='API.PHOTOVOLTAIC_POWER_STATION_DATA_SOURCE_RELATION_NOT_FOUND')
5207
5208
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_data_sources "
5209
                       " WHERE photovoltaic_power_station_id = %s AND data_source_id = %s ", (id_, dsid))
5210
        cnx.commit()
5211
5212
        cursor.close()
5213
        cnx.close()
5214
5215
        resp.status = falcon.HTTP_204
5216
5217 View Code Duplication
class PhotovoltaicPowerStationInvertorPointCollection:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5218
    def __init__(self):
5219
        pass
5220
5221
    @staticmethod
5222
    def on_options(req, resp, id_, iid):
5223
        _ = req
5224
        resp.status = falcon.HTTP_200
5225
        _ = id_
5226
        _ = iid
5227
5228
    @staticmethod
5229
    def on_get(req, resp, id_, iid):
5230
        if 'API-KEY' not in req.headers or \
5231
                not isinstance(req.headers['API-KEY'], str) or \
5232
                len(str.strip(req.headers['API-KEY'])) == 0:
5233
            access_control(req)
5234
        else:
5235
            api_key_control(req)
5236
        if not id_.isdigit() or int(id_) <= 0:
5237
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5238
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5239
        if not iid.isdigit() or int(iid) <= 0:
5240
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5241
                                   description='API.INVALID_INVERTOR_ID')
5242
5243
        cnx = mysql.connector.connect(**config.myems_system_db)
5244
        cursor = cnx.cursor()
5245
5246
        cursor.execute(" SELECT name "
5247
                       " FROM tbl_photovoltaic_power_stations_invertors "
5248
                       " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, iid,))
5249
        if cursor.fetchone() is None:
5250
            cursor.close()
5251
            cnx.close()
5252
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5253
                                   description='API.PHOTOVOLTAIC_INVERTOR_NOT_FOUND')
5254
5255
        query = (" SELECT p.id, p.name, "
5256
                 "        ds.id, ds.name, ds.uuid, "
5257
                 "        p.address "
5258
                 " FROM tbl_points p, tbl_photovoltaic_power_stations_invertors_points mp, tbl_data_sources ds "
5259
                 " WHERE mp.invertor_id = %s AND p.id = mp.point_id AND p.data_source_id = ds.id "
5260
                 " ORDER BY p.name ")
5261
        cursor.execute(query, (iid,))
5262
        rows = cursor.fetchall()
5263
5264
        result = list()
5265
        if rows is not None and len(rows) > 0:
5266
            for row in rows:
5267
                meta_result = {"id": row[0], "name": row[1],
5268
                               "data_source": {"id": row[2], "name": row[3], "uuid": row[4]},
5269
                               "address": row[5]}
5270
                result.append(meta_result)
5271
5272
        resp.text = json.dumps(result)
5273
5274
    @staticmethod
5275
    @user_logger
5276
    def on_post(req, resp, id_, iid):
5277
        admin_control(req)
5278
        try:
5279
            raw_json = req.stream.read().decode('utf-8')
5280
        except Exception as ex:
5281
            print(str(ex))
5282
            raise falcon.HTTPError(status=falcon.HTTP_400,
5283
                                   title='API.BAD_REQUEST',
5284
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5285
5286
        if not id_.isdigit() or int(id_) <= 0:
5287
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5288
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5289
        if not iid.isdigit() or int(iid) <= 0:
5290
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5291
                                   description='API.INVALID_INVERTOR_ID')
5292
5293
        new_values = json.loads(raw_json)
5294
        cnx = mysql.connector.connect(**config.myems_system_db)
5295
        cursor = cnx.cursor()
5296
5297
        cursor.execute(" SELECT name "
5298
                       " FROM tbl_photovoltaic_power_stations_invertors "
5299
                       " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, iid,))
5300
        if cursor.fetchone() is None:
5301
            cursor.close()
5302
            cnx.close()
5303
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5304
                                   description='API.PHOTOVOLTAIC_INVERTOR_NOT_FOUND')
5305
5306
        cursor.execute(" SELECT name, object_type "
5307
                       " FROM tbl_points "
5308
                       " WHERE id = %s ", (new_values['data']['point_id'],))
5309
        row = cursor.fetchone()
5310
        if row is None:
5311
            cursor.close()
5312
            cnx.close()
5313
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5314
                                   description='API.POINT_NOT_FOUND')
5315
5316
        query = (" SELECT id "
5317
                 " FROM tbl_photovoltaic_power_stations_invertors_points "
5318
                 " WHERE invertor_id = %s AND point_id = %s")
5319
        cursor.execute(query, (iid, new_values['data']['point_id'],))
5320
        if cursor.fetchone() is not None:
5321
            cursor.close()
5322
            cnx.close()
5323
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR',
5324
                                   description='API.INVERTOR_POINT_RELATION_EXISTS')
5325
5326
        add_row = (" INSERT INTO tbl_photovoltaic_power_stations_invertors_points (invertor_id, point_id) "
5327
                   " VALUES (%s, %s) ")
5328
        cursor.execute(add_row, (iid, new_values['data']['point_id'],))
5329
        cnx.commit()
5330
        cursor.close()
5331
        cnx.close()
5332
5333
        resp.status = falcon.HTTP_201
5334
        resp.location = '/photovoltaicpowerstations/' + str(id_) + '/invertors/' + str(iid) + '/points/' + \
5335
                        str(new_values['data']['point_id'])
5336
5337
5338 View Code Duplication
class PhotovoltaicPowerStationInvertorPointItem:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5339
    def __init__(self):
5340
        pass
5341
5342
    @staticmethod
5343
    def on_options(req, resp, id_, iid, pid):
5344
        _ = req
5345
        resp.status = falcon.HTTP_200
5346
        _ = id_
5347
        _ = iid
5348
        _ = pid
5349
5350
    @staticmethod
5351
    @user_logger
5352
    def on_delete(req, resp, id_, iid, pid):
5353
        admin_control(req)
5354
        if not id_.isdigit() or int(id_) <= 0:
5355
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5356
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
5357
        if not iid.isdigit() or int(iid) <= 0:
5358
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5359
                                   description='API.INVALID_INVERTOR_ID')
5360
        if not pid.isdigit() or int(pid) <= 0:
5361
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5362
                                   description='API.INVALID_POINT_ID')
5363
5364
        cnx = mysql.connector.connect(**config.myems_system_db)
5365
        cursor = cnx.cursor()
5366
5367
        cursor.execute(" SELECT name "
5368
                       " FROM tbl_photovoltaic_power_stations_invertors "
5369
                       " WHERE photovoltaic_power_station_id = %s AND id = %s ", (id_, iid,))
5370
        if cursor.fetchone() is None:
5371
            cursor.close()
5372
            cnx.close()
5373
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5374
                                   description='API.PHOTOVOLTAIC_INVERTOR_NOT_FOUND')
5375
5376
        cursor.execute(" SELECT name "
5377
                       " FROM tbl_points "
5378
                       " WHERE id = %s ", (pid,))
5379
        if cursor.fetchone() is None:
5380
            cursor.close()
5381
            cnx.close()
5382
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5383
                                   description='API.POINT_NOT_FOUND')
5384
5385
        cursor.execute(" SELECT id "
5386
                       " FROM tbl_photovoltaic_power_stations_invertors_points "
5387
                       " WHERE invertor_id = %s AND point_id = %s ", (iid, pid))
5388
        if cursor.fetchone() is None:
5389
            cursor.close()
5390
            cnx.close()
5391
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5392
                                   description='API.INVERTOR_POINT_RELATION_NOT_FOUND')
5393
5394
        cursor.execute(" DELETE FROM tbl_photovoltaic_power_stations_invertors_points "
5395
                       " WHERE invertor_id = %s AND point_id = %s ", (iid, pid))
5396
        cnx.commit()
5397
5398
        cursor.close()
5399
        cnx.close()
5400
5401
        resp.status = falcon.HTTP_204
5402
5403
5404