Passed
Push — master ( 42322a...183c79 )
by
unknown
10:22 queued 16s
created

PhotovoltaicPowerStationItem.on_delete()   B

Complexity

Conditions 6

Size

Total Lines 40
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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