PhotovoltaicPowerStationCollection.on_get()   D
last analyzed

Complexity

Conditions 13

Size

Total Lines 74
Code Lines 58

Duplication

Lines 74
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 58
dl 74
loc 74
rs 4.2
c 0
b 0
f 0
cc 13
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like core.photovoltaicpowerstation.PhotovoltaicPowerStationCollection.on_get() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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