Passed
Push — master ( 844492...7c39ee )
by
unknown
09:45 queued 11s
created

PhotovoltaicPowerStationCollection.on_get()   F

Complexity

Conditions 16

Size

Total Lines 98
Code Lines 67

Duplication

Lines 98
Ratio 100 %

Importance

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