PhotovoltaicPowerStationDataSourceItem.on_delete()   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 50
Code Lines 39

Duplication

Lines 50
Ratio 100 %

Importance

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