| 1 |  |  | import uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from datetime import datetime, timedelta | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import falcon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import mysql.connector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import simplejson as json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from core.useractivity import user_logger, admin_control, access_control, api_key_control | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class EquipmentCollection: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     Equipment Collection Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     This class handles CRUD operations for equipment collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     It provides endpoints for listing all equipment and creating new equipment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     Equipment represents physical devices and systems in the energy management system. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         """Initialize EquipmentCollection""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     def on_options(req, resp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         """Handle OPTIONS requests for CORS preflight""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def on_get(req, resp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                  " FROM tbl_cost_centers ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         rows_cost_centers = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         cost_center_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if rows_cost_centers is not None and len(rows_cost_centers) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             for row in rows_cost_centers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 cost_center_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                                             "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                                             "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         query_svg = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                      " FROM tbl_svgs ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         cursor.execute(query_svg) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         rows_svgs = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         svg_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if rows_svgs is not None and len(rows_svgs) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             for row in rows_svgs: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 svg_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                                     "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                                     "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         search_query = req.get_param('q') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         query_base = (" SELECT id, name, uuid, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                       "        is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                       "        cost_center_id, svg_id, camera_url, description " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                       " FROM tbl_equipments ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         params = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if search_query and isinstance(search_query, str) and len(str.strip(search_query)) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             query_base += " WHERE name LIKE %s OR description LIKE %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             trimmed_query = str.strip(search_query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             params = [f'%{trimmed_query}%', f'%{trimmed_query}%'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         query_base += " ORDER BY id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         cursor.execute(query_base, params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         rows_equipments = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if rows_equipments is not None and len(rows_equipments) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             for row in rows_equipments: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                                "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                                "is_input_counted": bool(row[3]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                                "is_output_counted": bool(row[4]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                                "cost_center": cost_center_dict.get(row[5], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                "svg": svg_dict.get(row[6], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                                "camera_url": row[7], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                                "description": row[8], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                                "qrcode": 'equipment:' + row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 98 |  | View Code Duplication |     @staticmethod | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     def on_post(req, resp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if 'name' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 not isinstance(new_values['data']['name'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 len(str.strip(new_values['data']['name'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                                    description='API.INVALID_EQUIPMENT_NAME') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         name = str.strip(new_values['data']['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if 'is_input_counted' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 not isinstance(new_values['data']['is_input_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                                    description='API.INVALID_IS_INPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         is_input_counted = new_values['data']['is_input_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if 'is_output_counted' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 not isinstance(new_values['data']['is_output_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                                    description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         is_output_counted = new_values['data']['is_output_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if 'cost_center_id' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 not isinstance(new_values['data']['cost_center_id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 new_values['data']['cost_center_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                                    description='API.INVALID_COST_CENTER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         cost_center_id = new_values['data']['cost_center_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if 'svg_id' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 isinstance(new_values['data']['svg_id'], int) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 new_values['data']['svg_id'] > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             svg_id = new_values['data']['svg_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             svg_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         if 'camera_url' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 new_values['data']['camera_url'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 len(str(new_values['data']['camera_url'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             camera_url = str.strip(new_values['data']['camera_url']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             camera_url = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         if 'description' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 new_values['data']['description'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 len(str(new_values['data']['description'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             description = str.strip(new_values['data']['description']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             description = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                        " WHERE name = %s ", (name,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                                    description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         if cost_center_id is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                            " FROM tbl_cost_centers " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                            " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                            (new_values['data']['cost_center_id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                                        description='API.COST_CENTER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         if svg_id is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                            " FROM tbl_svgs " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                            " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                            (svg_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                                        description='API.SVG_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         add_values = (" INSERT INTO tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                       "    (name, uuid, is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                       "     cost_center_id, svg_id, camera_url, description) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |                       " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         cursor.execute(add_values, (name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                                     str(uuid.uuid4()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                                     is_input_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                                     is_output_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                                     cost_center_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |                                     svg_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |                                     camera_url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |                                     description)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         new_id = cursor.lastrowid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         resp.location = '/equipments/' + str(new_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | class EquipmentItem: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 227 |  | View Code Duplication |     @staticmethod | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |                  " FROM tbl_cost_centers ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         rows_cost_centers = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         cost_center_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         if rows_cost_centers is not None and len(rows_cost_centers) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |             for row in rows_cost_centers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |                 cost_center_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |                                             "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                                             "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         svg_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |                  " FROM tbl_svgs ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         rows_svgs = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         if rows_svgs is not None and len(rows_svgs) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |             for row in rows_svgs: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |                 svg_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |                                     "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |                                     "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         query = (" SELECT id, name, uuid, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |                  "        is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |                  "        cost_center_id, svg_id, camera_url, description " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                  " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |                  " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |             meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |                            "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                            "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |                            "is_input_counted": bool(row[3]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |                            "is_output_counted": bool(row[4]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |                            "cost_center": cost_center_dict.get(row[5], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |                            "svg": svg_dict.get(row[6], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |                            "camera_url": row[7], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |                            "description": row[8], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |                            "qrcode": 'equipment:' + row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         resp.text = json.dumps(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |     def on_delete(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         # check relation with space | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |         cursor.execute(" SELECT space_id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |                        " FROM tbl_spaces_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         rows_equipments = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |         if rows_equipments is not None and len(rows_equipments) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |                                    description='API.THERE_IS_RELATION_WITH_SPACES') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         # check relation with combined equipments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |         cursor.execute(" SELECT combined_equipment_id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |                        " FROM tbl_combined_equipments_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         rows_combined_equipments = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |         if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |                                    description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         # check relation with shopfloor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |         cursor.execute(" SELECT shopfloor_id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |                        " FROM tbl_shopfloors_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |         rows_combined_shopfloor = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         if rows_combined_shopfloor is not None and len(rows_combined_shopfloor) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |                                    description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |         # delete relation with meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_meters WHERE equipment_id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |         # delete relation with offline meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_offline_meters WHERE equipment_id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |         # delete relation with virtual meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_virtual_meters WHERE equipment_id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |         # delete relation with commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_commands WHERE equipment_id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |         # delete all associated parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_parameters WHERE equipment_id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |         cursor.execute(" DELETE FROM tbl_equipments WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 367 |  | View Code Duplication |     @staticmethod | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |     def on_put(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |         """Handles PUT requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |         if 'name' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |                 not isinstance(new_values['data']['name'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |                 len(str.strip(new_values['data']['name'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |                                    description='API.INVALID_EQUIPMENT_NAME') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |         name = str.strip(new_values['data']['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |         if 'is_input_counted' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |                 not isinstance(new_values['data']['is_input_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |                                    description='API.INVALID_IS_INPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |         is_input_counted = new_values['data']['is_input_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |         if 'is_output_counted' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |                 not isinstance(new_values['data']['is_output_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |                                    description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |         is_output_counted = new_values['data']['is_output_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |         if 'cost_center_id' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |                 not isinstance(new_values['data']['cost_center_id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |                 new_values['data']['cost_center_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |                                    description='API.INVALID_COST_CENTER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |         cost_center_id = new_values['data']['cost_center_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |         if 'svg_id' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |                 isinstance(new_values['data']['svg_id'], int) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |                 new_values['data']['svg_id'] > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |             svg_id = new_values['data']['svg_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |             svg_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |         if 'description' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |                 new_values['data']['description'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |                 len(str(new_values['data']['description'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |             description = str.strip(new_values['data']['description']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |             description = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |         if 'camera_url' in new_values['data'].keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |                 new_values['data']['camera_url'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |                 len(str(new_values['data']['camera_url'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |             camera_url = str.strip(new_values['data']['camera_url']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |             camera_url = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |                        " WHERE name = %s AND id != %s ", (name, id_)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |                                    description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |                        " FROM tbl_cost_centers " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |                        " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |                        (new_values['data']['cost_center_id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |                                    description='API.COST_CENTER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |         if svg_id is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |             cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |                            " FROM tbl_svgs " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |                            " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |                            (new_values['data']['svg_id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |                                        description='API.SVG_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |         update_row = (" UPDATE tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |                       " SET name = %s, is_input_counted = %s, is_output_counted = %s, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |                       "     cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |                       " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |         cursor.execute(update_row, (name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |                                     is_input_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |                                     is_output_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |                                     cost_center_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |                                     svg_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |                                     camera_url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |                                     description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |                                     id_)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |     # Clone an Equipment | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 496 |  | View Code Duplication |     @staticmethod | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |         query = (" SELECT name, is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |                  "        cost_center_id, svg_id, camera_url, description " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |                  " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |                  " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |             add_values = (" INSERT INTO tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |                           "    (name, uuid, is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |                           "     cost_center_id, svg_id, camera_url, description) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |                           " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |             cursor.execute(add_values, (row[0] + ' Copy', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  |                                         str(uuid.uuid4()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |                                         row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |                                         row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |                                         row[3], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |                                         row[4], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |                                         row[5], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |                                         row[6])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |             new_id = cursor.lastrowid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |         # clone relation with meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |         cursor.execute(" SELECT meter_id, is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |                        " FROM tbl_equipments_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |         rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |         if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |             add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |                           " VALUES  ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |             for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |                 add_values += " (" + str(new_id) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |                 add_values += str(row[0]) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |                 add_values += str(bool(row[1])) + "), " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |             # trim ", " at the end of string and then execute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |             cursor.execute(add_values[:-2]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |         # clone relation with offline meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |         cursor.execute(" SELECT offline_meter_id, is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |                        " FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 |  |  |         rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 |  |  |         if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 |  |  |             add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |                           " VALUES  ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |             for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |                 add_values += " (" + str(new_id) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |                 add_values += "'" + str(row[0]) + "'," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |                 add_values += str(bool(row[1])) + "), " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |             # trim ", " at the end of string and then execute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |             cursor.execute(add_values[:-2]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |         # clone relation with virtual meter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |         cursor.execute(" SELECT virtual_meter_id, is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |                        " FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |         rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |         if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |             add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |                           " VALUES  ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |             for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |                 add_values += " (" + str(new_id) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |                 add_values += str(row[0]) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |                 add_values += str(bool(row[1])) + "), " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |             # trim ", " at the end of string and then execute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |             cursor.execute(add_values[:-2]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |         # clone parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |         cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |                        " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |                        " WHERE equipment_id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |         rows_parameters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |         if rows_parameters is not None and len(rows_parameters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |             add_values = (" INSERT INTO tbl_equipments_parameters" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |                           "     (equipment_id, name, parameter_type, constant, point_id, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 |  |  |                           "      numerator_meter_uuid, denominator_meter_uuid) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 |  |  |                           " VALUES  ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 |  |  |             for row in rows_parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 |  |  |                 add_values += " (" + str(new_id) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |                 add_values += "'" + str(row[0]) + "'," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |                 add_values += "'" + str(row[1]) + "'," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 |  |  |                 if row[2] is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |                     add_values += "'" + str(row[2]) + "'," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 611 |  |  |                     add_values += "null, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 612 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 613 |  |  |                 if row[3] is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 614 |  |  |                     add_values += str(row[3]) + "," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 615 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 616 |  |  |                     add_values += "null, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 617 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 618 |  |  |                 if row[4] is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 619 |  |  |                     add_values += "'" + row[4] + "'," | 
            
                                                                                                            
                            
            
                                    
            
            
                | 620 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 621 |  |  |                     add_values += "null, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 622 |  |  |                 if row[5] is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 623 |  |  |                     add_values += "'" + row[5] + "'), " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 624 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 625 |  |  |                     add_values += "null), " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 626 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 627 |  |  |             # trim ", " at the end of string and then execute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 628 |  |  |             cursor.execute(add_values[:-2]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 629 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 630 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 631 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 632 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 633 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 634 |  |  |         resp.location = '/equipments/' + str(new_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 635 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 636 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 637 |  | View Code Duplication | class EquipmentParameterCollection: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 638 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 639 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 640 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 641 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 642 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 643 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 644 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 645 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 646 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 647 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 648 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 649 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 650 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 651 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 652 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 653 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 654 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 655 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 656 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 657 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 658 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 659 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 660 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 661 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 662 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 663 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 664 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 665 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 666 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 667 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 668 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 669 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 670 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 671 |  |  |         query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 672 |  |  |                  " FROM tbl_points ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 673 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 674 |  |  |         rows_points = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 675 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 676 |  |  |         point_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 677 |  |  |         if rows_points is not None and len(rows_points) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 678 |  |  |             for row in rows_points: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 679 |  |  |                 point_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 680 |  |  |                                       "name": row[1]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 681 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 682 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 683 |  |  |                  " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 684 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 685 |  |  |         rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 686 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 687 |  |  |         meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 688 |  |  |         if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 689 |  |  |             for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 690 |  |  |                 meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 691 |  |  |                                       "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 692 |  |  |                                       "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 693 |  |  |                                       "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 694 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 695 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 696 |  |  |                  " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 697 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 698 |  |  |         rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 699 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 700 |  |  |         offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 701 |  |  |         if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 702 |  |  |             for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 703 |  |  |                 offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 704 |  |  |                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 705 |  |  |                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 706 |  |  |                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 707 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 708 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 709 |  |  |                  " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 710 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 711 |  |  |         rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 712 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 713 |  |  |         virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 714 |  |  |         if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 715 |  |  |             for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 716 |  |  |                 virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 717 |  |  |                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 718 |  |  |                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 719 |  |  |                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 720 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 721 |  |  |         query = (" SELECT id, name, parameter_type, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 722 |  |  |                  "        constant, point_id, numerator_meter_uuid, denominator_meter_uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 723 |  |  |                  " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 724 |  |  |                  " WHERE equipment_id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 725 |  |  |                  " ORDER BY id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 726 |  |  |         cursor.execute(query, (id_, )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 727 |  |  |         rows_parameters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 728 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 729 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 730 |  |  |         if rows_parameters is not None and len(rows_parameters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 731 |  |  |             for row in rows_parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 732 |  |  |                 constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 733 |  |  |                 point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 734 |  |  |                 numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 735 |  |  |                 denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 736 |  |  |                 if row[2] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 737 |  |  |                     point = point_dict.get(row[4], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 738 |  |  |                     constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 739 |  |  |                     numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 740 |  |  |                     denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 741 |  |  |                 elif row[2] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 742 |  |  |                     constant = row[3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 743 |  |  |                     point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 744 |  |  |                     numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 745 |  |  |                     denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 746 |  |  |                 elif row[2] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 747 |  |  |                     constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 748 |  |  |                     point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 749 |  |  |                     # find numerator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 750 |  |  |                     numerator_meter = meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 751 |  |  |                     if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 752 |  |  |                         numerator_meter = virtual_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 753 |  |  |                     if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 754 |  |  |                         numerator_meter = offline_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 755 |  |  |                     # find denominator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 756 |  |  |                     denominator_meter = meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 757 |  |  |                     if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 758 |  |  |                         denominator_meter = virtual_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 759 |  |  |                     if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 760 |  |  |                         denominator_meter = offline_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 761 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 762 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 763 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 764 |  |  |                                "parameter_type": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 765 |  |  |                                "constant": constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 766 |  |  |                                "point": point, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 767 |  |  |                                "numerator_meter": numerator_meter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 768 |  |  |                                "denominator_meter": denominator_meter} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 769 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 770 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 771 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 772 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 773 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 774 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 775 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 776 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 777 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 778 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 779 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 780 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 781 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 782 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 783 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 784 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 785 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 786 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 787 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 788 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 789 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 790 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 791 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 792 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 793 |  |  |         if 'name' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 794 |  |  |                 not isinstance(new_values['data']['name'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 795 |  |  |                 len(str.strip(new_values['data']['name'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 796 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 797 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_NAME') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 798 |  |  |         name = str.strip(new_values['data']['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 799 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 800 |  |  |         if 'parameter_type' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 801 |  |  |                 not isinstance(new_values['data']['parameter_type'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 802 |  |  |                 len(str.strip(new_values['data']['parameter_type'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 803 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 804 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 805 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 806 |  |  |         parameter_type = str.strip(new_values['data']['parameter_type']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 807 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 808 |  |  |         if parameter_type not in ('constant', 'point', 'fraction'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 809 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 810 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 811 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 812 |  |  |         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 813 |  |  |         if 'constant' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 814 |  |  |             if new_values['data']['constant'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 815 |  |  |                     isinstance(new_values['data']['constant'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 816 |  |  |                     len(str.strip(new_values['data']['constant'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 817 |  |  |                 constant = str.strip(new_values['data']['constant']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 818 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 819 |  |  |         point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 820 |  |  |         if 'point_id' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 821 |  |  |             if new_values['data']['point_id'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 822 |  |  |                     new_values['data']['point_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 823 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 824 |  |  |                                        description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 825 |  |  |             point_id = new_values['data']['point_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 826 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 827 |  |  |         numerator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 828 |  |  |         if 'numerator_meter_uuid' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 829 |  |  |             if new_values['data']['numerator_meter_uuid'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 830 |  |  |                     isinstance(new_values['data']['numerator_meter_uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 831 |  |  |                     len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 832 |  |  |                 numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 833 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 834 |  |  |         denominator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 835 |  |  |         if 'denominator_meter_uuid' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 836 |  |  |             if new_values['data']['denominator_meter_uuid'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 837 |  |  |                     isinstance(new_values['data']['denominator_meter_uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 838 |  |  |                     len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 839 |  |  |                 denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 840 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 841 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 842 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 843 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 844 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 845 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 846 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 847 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 848 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 849 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 850 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 851 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 852 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 853 |  |  |                        " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 854 |  |  |                        " WHERE name = %s AND equipment_id = %s ", (name, id_)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 855 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 856 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 857 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 858 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 859 |  |  |                                    description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 860 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 861 |  |  |         # validate by parameter type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 862 |  |  |         if parameter_type == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 863 |  |  |             if point_id is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 864 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 865 |  |  |                                        description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 866 |  |  |             query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 867 |  |  |                      " FROM tbl_points " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 868 |  |  |                      " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 869 |  |  |             cursor.execute(query, (point_id, )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 870 |  |  |             if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 871 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 872 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 873 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 874 |  |  |                                        description='API.POINT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 875 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 876 |  |  |         elif parameter_type == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 877 |  |  |             if constant is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 878 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 879 |  |  |                                        description='API.INVALID_CONSTANT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 880 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 881 |  |  |         elif parameter_type == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 882 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 883 |  |  |                      " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 884 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 885 |  |  |             rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 886 |  |  |             meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 887 |  |  |             if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 888 |  |  |                 for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 889 |  |  |                     meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 890 |  |  |                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 891 |  |  |                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 892 |  |  |                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 893 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 894 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 895 |  |  |                      " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 896 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 897 |  |  |             rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 898 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 899 |  |  |             offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 900 |  |  |             if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 901 |  |  |                 for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 902 |  |  |                     offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 903 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 904 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 905 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 906 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 907 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 908 |  |  |                      " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 909 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 910 |  |  |             rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 911 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 912 |  |  |             virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 913 |  |  |             if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 914 |  |  |                 for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 915 |  |  |                     virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 916 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 917 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 918 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 919 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 920 |  |  |             # validate numerator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 921 |  |  |             if meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 922 |  |  |                     virtual_meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 923 |  |  |                     offline_meter_dict.get(numerator_meter_uuid) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 924 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 925 |  |  |                                        description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 926 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 927 |  |  |             # validate denominator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 928 |  |  |             if denominator_meter_uuid == numerator_meter_uuid: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 929 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 930 |  |  |                                        description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 931 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 932 |  |  |             if denominator_meter_uuid not in meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 933 |  |  |                     denominator_meter_uuid not in virtual_meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 934 |  |  |                     denominator_meter_uuid not in offline_meter_dict: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 935 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 936 |  |  |                                        description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 937 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 938 |  |  |         add_values = (" INSERT INTO tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 939 |  |  |                       "    (equipment_id, name, parameter_type, constant, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 940 |  |  |                       "     point_id, numerator_meter_uuid, denominator_meter_uuid) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 941 |  |  |                       " VALUES (%s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 942 |  |  |         cursor.execute(add_values, (id_, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 943 |  |  |                                     name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 944 |  |  |                                     parameter_type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 945 |  |  |                                     constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 946 |  |  |                                     point_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 947 |  |  |                                     numerator_meter_uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 948 |  |  |                                     denominator_meter_uuid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 949 |  |  |         new_id = cursor.lastrowid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 950 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 951 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 952 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 953 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 954 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 955 |  |  |         resp.location = '/equipments/' + str(id_) + 'parameters/' + str(new_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 956 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 957 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 958 |  | View Code Duplication | class EquipmentParameterItem: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 959 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 960 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 961 |  |  |     def __init__(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 962 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 963 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 964 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 965 |  |  |     def on_options(req, resp, id_, pid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 966 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 967 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 968 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 969 |  |  |         _ = pid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 970 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 971 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 972 |  |  |     def on_get(req, resp, id_, pid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 973 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 974 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 975 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 976 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 977 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 978 |  |  |             api_key_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_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 982 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 983 |  |  |         if not pid.isdigit() or int(pid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 984 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 985 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 986 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 987 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 988 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 989 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 990 |  |  |         query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 991 |  |  |                  " FROM tbl_points ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 992 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 993 |  |  |         rows_points = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 994 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 995 |  |  |         point_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 996 |  |  |         if rows_points is not None and len(rows_points) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 997 |  |  |             for row in rows_points: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 998 |  |  |                 point_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 999 |  |  |                                       "name": row[1]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1000 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1001 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1002 |  |  |                  " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1003 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1004 |  |  |         rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1005 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1006 |  |  |         meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1007 |  |  |         if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1008 |  |  |             for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1009 |  |  |                 meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1010 |  |  |                                       "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1011 |  |  |                                       "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1012 |  |  |                                       "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1013 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1014 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1015 |  |  |                  " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1016 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1017 |  |  |         rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1018 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1019 |  |  |         offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1020 |  |  |         if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1021 |  |  |             for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1022 |  |  |                 offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1023 |  |  |                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1024 |  |  |                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1025 |  |  |                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1026 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1027 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1028 |  |  |                  " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1029 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1030 |  |  |         rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1031 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1032 |  |  |         virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1033 |  |  |         if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1034 |  |  |             for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1035 |  |  |                 virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1036 |  |  |                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1037 |  |  |                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1038 |  |  |                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1039 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1040 |  |  |         query = (" SELECT id, name, parameter_type, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1041 |  |  |                  "        constant, point_id, numerator_meter_uuid, denominator_meter_uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1042 |  |  |                  " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1043 |  |  |                  " WHERE equipment_id = %s AND id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1044 |  |  |         cursor.execute(query, (id_, pid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1045 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1046 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1047 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1048 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1049 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1050 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1051 |  |  |                                    description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1052 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1053 |  |  |             constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1054 |  |  |             point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1055 |  |  |             numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1056 |  |  |             denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1057 |  |  |             if row[2] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1058 |  |  |                 point = point_dict.get(row[4], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1059 |  |  |                 constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1060 |  |  |                 numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1061 |  |  |                 denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1062 |  |  |             elif row[2] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1063 |  |  |                 constant = row[3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1064 |  |  |                 point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1065 |  |  |                 numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1066 |  |  |                 denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1067 |  |  |             elif row[2] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1068 |  |  |                 constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1069 |  |  |                 point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1070 |  |  |                 # find numerator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1071 |  |  |                 numerator_meter = meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1072 |  |  |                 if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1073 |  |  |                     numerator_meter = virtual_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1074 |  |  |                 if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1075 |  |  |                     numerator_meter = offline_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1076 |  |  |                 # find denominator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1077 |  |  |                 denominator_meter = meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1078 |  |  |                 if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1079 |  |  |                     denominator_meter = virtual_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1080 |  |  |                 if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1081 |  |  |                     denominator_meter = offline_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1082 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1083 |  |  |             meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1084 |  |  |                            "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1085 |  |  |                            "parameter_type": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1086 |  |  |                            "constant": constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1087 |  |  |                            "point": point, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1088 |  |  |                            "numerator_meter": numerator_meter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1089 |  |  |                            "denominator_meter": denominator_meter} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1090 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1091 |  |  |         resp.text = json.dumps(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1092 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1093 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1094 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1095 |  |  |     def on_delete(req, resp, id_, pid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1096 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1097 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1098 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1099 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1101 |  |  |         if not pid.isdigit() or int(pid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1102 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1103 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1105 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1106 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1108 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1109 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1110 |  |  |                        " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1111 |  |  |                        (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1112 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1113 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1114 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1115 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1116 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1117 |  |  |                                    title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1118 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1120 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1121 |  |  |                        " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1122 |  |  |                        " WHERE equipment_id = %s AND id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1123 |  |  |                        (id_, pid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1124 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1125 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1126 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1127 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1128 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1129 |  |  |                                    title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1130 |  |  |                                    description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1132 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1133 |  |  |                        " WHERE id = %s ", (pid, )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1134 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1136 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1137 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1139 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1141 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1142 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1143 |  |  |     def on_put(req, resp, id_, pid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1144 |  |  |         """Handles PUT requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1145 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1146 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1147 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1148 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1150 |  |  |         if not pid.isdigit() or int(pid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1151 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1152 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1154 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1155 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1156 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1157 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1158 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1159 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1160 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1162 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1164 |  |  |         if 'name' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1165 |  |  |                 not isinstance(new_values['data']['name'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1166 |  |  |                 len(str.strip(new_values['data']['name'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1167 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1168 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_NAME') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1169 |  |  |         name = str.strip(new_values['data']['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1171 |  |  |         if 'parameter_type' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1172 |  |  |                 not isinstance(new_values['data']['parameter_type'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1173 |  |  |                 len(str.strip(new_values['data']['parameter_type'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1174 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1175 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1177 |  |  |         parameter_type = str.strip(new_values['data']['parameter_type']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1179 |  |  |         if parameter_type not in ('constant', 'point', 'fraction'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1180 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1181 |  |  |                                    description='API.INVALID_EQUIPMENT_PARAMETER_TYPE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1183 |  |  |         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1184 |  |  |         if 'constant' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1185 |  |  |             if new_values['data']['constant'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1186 |  |  |                     isinstance(new_values['data']['constant'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1187 |  |  |                     len(str.strip(new_values['data']['constant'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1188 |  |  |                 constant = str.strip(new_values['data']['constant']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1190 |  |  |         point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1191 |  |  |         if 'point_id' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1192 |  |  |             if new_values['data']['point_id'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1193 |  |  |                     new_values['data']['point_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1194 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1195 |  |  |                                        description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1196 |  |  |             point_id = new_values['data']['point_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1198 |  |  |         numerator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1199 |  |  |         if 'numerator_meter_uuid' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1200 |  |  |             if new_values['data']['numerator_meter_uuid'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1201 |  |  |                     isinstance(new_values['data']['numerator_meter_uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1202 |  |  |                     len(str.strip(new_values['data']['numerator_meter_uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1203 |  |  |                 numerator_meter_uuid = str.strip(new_values['data']['numerator_meter_uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1205 |  |  |         denominator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1206 |  |  |         if 'denominator_meter_uuid' in new_values['data'].keys(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1207 |  |  |             if new_values['data']['denominator_meter_uuid'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1208 |  |  |                     isinstance(new_values['data']['denominator_meter_uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1209 |  |  |                     len(str.strip(new_values['data']['denominator_meter_uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1210 |  |  |                 denominator_meter_uuid = str.strip(new_values['data']['denominator_meter_uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1212 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1213 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1215 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1216 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1217 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1218 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1219 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1220 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1221 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1222 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1224 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1225 |  |  |                        " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1226 |  |  |                        " WHERE equipment_id = %s AND id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1227 |  |  |                        (id_, pid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1228 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1229 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1230 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1231 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1232 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1233 |  |  |                                    title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1234 |  |  |                                    description='API.EQUIPMENT_PARAMETER_NOT_FOUND_OR_NOT_MATCH') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1236 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1237 |  |  |                        " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1238 |  |  |                        " WHERE name = %s AND equipment_id = %s  AND id != %s ", (name, id_, pid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1239 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1240 |  |  |         if row is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1241 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1242 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1243 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1244 |  |  |                                    description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1245 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1246 |  |  |         # validate by parameter type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1247 |  |  |         if parameter_type == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1248 |  |  |             if point_id is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1249 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1250 |  |  |                                        description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1252 |  |  |             query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1253 |  |  |                      " FROM tbl_points " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1254 |  |  |                      " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1255 |  |  |             cursor.execute(query, (point_id, )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1256 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1257 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1258 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1259 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1260 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1261 |  |  |                                        description='API.POINT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1263 |  |  |         elif parameter_type == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1264 |  |  |             if constant is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1265 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1266 |  |  |                                        description='API.INVALID_CONSTANT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1267 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1268 |  |  |         elif parameter_type == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1270 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1271 |  |  |                      " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1272 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1273 |  |  |             rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1275 |  |  |             meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1276 |  |  |             if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1277 |  |  |                 for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1278 |  |  |                     meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1279 |  |  |                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1280 |  |  |                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1281 |  |  |                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1283 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1284 |  |  |                      " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1285 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1286 |  |  |             rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1288 |  |  |             offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1289 |  |  |             if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1290 |  |  |                 for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1291 |  |  |                     offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1292 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1293 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1294 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1296 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1297 |  |  |                      " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1298 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1299 |  |  |             rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1301 |  |  |             virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1302 |  |  |             if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1303 |  |  |                 for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1304 |  |  |                     virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1305 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1306 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1307 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1309 |  |  |             # validate numerator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1310 |  |  |             if meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1311 |  |  |                     virtual_meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1312 |  |  |                     offline_meter_dict.get(numerator_meter_uuid) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1313 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1314 |  |  |                                        description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1315 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1316 |  |  |             # validate denominator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1317 |  |  |             if denominator_meter_uuid == numerator_meter_uuid: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1318 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1319 |  |  |                                        description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1320 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1321 |  |  |             if denominator_meter_uuid not in meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1322 |  |  |                     denominator_meter_uuid not in virtual_meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1323 |  |  |                     denominator_meter_uuid not in offline_meter_dict: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1324 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1325 |  |  |                                        description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1326 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1327 |  |  |         add_values = (" UPDATE tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1328 |  |  |                       " SET name = %s , parameter_type = %s, constant = %s, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1329 |  |  |                       "     point_id = %s, numerator_meter_uuid = %s, denominator_meter_uuid = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1330 |  |  |                       " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1331 |  |  |         cursor.execute(add_values, (name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1332 |  |  |                                     parameter_type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1333 |  |  |                                     constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1334 |  |  |                                     point_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1335 |  |  |                                     numerator_meter_uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1336 |  |  |                                     denominator_meter_uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1337 |  |  |                                     pid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1338 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1340 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1341 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1343 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1344 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1345 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1346 |  | View Code Duplication | class EquipmentMeterCollection: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1347 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1348 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1349 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1350 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1351 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1352 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1353 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1354 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1355 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1356 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1357 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1358 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1359 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1360 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1361 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1362 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1363 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1364 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1365 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1366 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1367 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1368 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1369 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1370 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1371 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1372 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1373 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1374 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1375 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1376 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1377 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1378 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1379 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1380 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1381 |  |  |                  " FROM tbl_energy_categories ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1382 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1383 |  |  |         rows_energy_categories = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1384 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1385 |  |  |         energy_category_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1386 |  |  |         if rows_energy_categories is not None and len(rows_energy_categories) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1387 |  |  |             for row in rows_energy_categories: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1388 |  |  |                 energy_category_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1389 |  |  |                                                 "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1390 |  |  |                                                 "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1392 |  |  |         query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1393 |  |  |                  " FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1394 |  |  |                  " WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1395 |  |  |                  " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1396 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1397 |  |  |         rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1398 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1399 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1400 |  |  |         if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1401 |  |  |             for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1402 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1403 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1404 |  |  |                                "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1405 |  |  |                                "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1406 |  |  |                                "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1407 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1408 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1409 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1410 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1411 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1412 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1413 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1414 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1415 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1416 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1417 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1418 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1419 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1420 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1421 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1422 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1423 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1424 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1425 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1426 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1427 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1428 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1429 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1430 |  |  |         if 'meter_id' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1431 |  |  |                 not isinstance(new_values['data']['meter_id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1432 |  |  |                 new_values['data']['meter_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1433 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1434 |  |  |                                    description='API.INVALID_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1435 |  |  |         meter_id = new_values['data']['meter_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1436 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1437 |  |  |         if 'is_output' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1438 |  |  |                 not isinstance(new_values['data']['is_output'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1439 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1440 |  |  |                                    description='API.INVALID_IS_OUTPUT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1441 |  |  |         is_output = new_values['data']['is_output'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1442 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1443 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1444 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1446 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1447 |  |  |                        " from tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1448 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1449 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1450 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1451 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1452 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1453 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1454 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1455 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1456 |  |  |                        " FROM tbl_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1457 |  |  |                        " WHERE id = %s ", (meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1458 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1459 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1460 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1461 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1462 |  |  |                                    description='API.METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1463 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1464 |  |  |         query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1465 |  |  |                  " FROM tbl_equipments_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1466 |  |  |                  " WHERE equipment_id = %s AND meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1467 |  |  |         cursor.execute(query, (id_, meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1468 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1469 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1470 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1471 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1472 |  |  |                                    description='API.EQUIPMENT_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1473 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1474 |  |  |         add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1475 |  |  |                    " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1476 |  |  |         cursor.execute(add_row, (id_, meter_id, is_output)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1477 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1478 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1479 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1480 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1481 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1482 |  |  |         resp.location = '/equipments/' + str(id_) + '/meters/' + str(meter_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1483 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1484 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1485 |  | View Code Duplication | class EquipmentMeterItem: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1486 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1487 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1488 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1489 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1490 |  |  |     def on_options(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1491 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1492 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1493 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1494 |  |  |         _ = mid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1495 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1496 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1497 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1498 |  |  |     def on_delete(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1499 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1500 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1501 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1502 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1503 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1504 |  |  |         if not mid.isdigit() or int(mid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1505 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1506 |  |  |                                    description='API.INVALID_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1507 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1508 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1509 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1510 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1511 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1512 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1513 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1514 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1515 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1516 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1517 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1518 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1519 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1520 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1521 |  |  |                        " FROM tbl_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1522 |  |  |                        " WHERE id = %s ", (mid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1523 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1524 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1525 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1526 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1527 |  |  |                                    description='API.METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1528 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1529 |  |  |         cursor.execute(" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1530 |  |  |                        " FROM tbl_equipments_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1531 |  |  |                        " WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1532 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1533 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1534 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1535 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1536 |  |  |                                    description='API.EQUIPMENT_METER_RELATION_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1537 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1538 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_meters WHERE equipment_id = %s AND meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1539 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1540 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1541 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1542 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1543 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1544 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1545 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1546 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1547 |  | View Code Duplication | class EquipmentOfflineMeterCollection: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1548 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1549 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1550 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1551 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1552 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1553 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1554 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1555 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1556 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1557 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1558 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1559 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1560 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1561 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1562 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1563 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1564 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1565 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1566 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1567 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1568 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1569 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1570 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1571 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1572 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1573 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1574 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1575 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1576 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1577 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1578 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1579 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1580 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1581 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1582 |  |  |                  " FROM tbl_energy_categories ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1583 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1584 |  |  |         rows_energy_categories = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1585 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1586 |  |  |         energy_category_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1587 |  |  |         if rows_energy_categories is not None and len(rows_energy_categories) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1588 |  |  |             for row in rows_energy_categories: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1589 |  |  |                 energy_category_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1590 |  |  |                                                 "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1591 |  |  |                                                 "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1592 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1593 |  |  |         query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1594 |  |  |                  " FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1595 |  |  |                  " WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1596 |  |  |                  " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1597 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1598 |  |  |         rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1599 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1600 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1601 |  |  |         if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1602 |  |  |             for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1603 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1604 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1605 |  |  |                                "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1606 |  |  |                                "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1607 |  |  |                                "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1608 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1609 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1610 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1611 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1612 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1613 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1614 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1615 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1616 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1617 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1618 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1619 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1620 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1621 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1622 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1623 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1624 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1625 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1626 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1627 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1628 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1629 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1630 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1631 |  |  |         if 'offline_meter_id' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1632 |  |  |                 not isinstance(new_values['data']['offline_meter_id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1633 |  |  |                 new_values['data']['offline_meter_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1634 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1635 |  |  |                                    description='API.INVALID_OFFLINE_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1636 |  |  |         offline_meter_id = new_values['data']['offline_meter_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1637 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1638 |  |  |         if 'is_output' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1639 |  |  |                 not isinstance(new_values['data']['is_output'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1640 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1641 |  |  |                                    description='API.INVALID_IS_OUTPUT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1642 |  |  |         is_output = new_values['data']['is_output'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1643 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1644 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1645 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1646 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1647 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1648 |  |  |                        " from tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1649 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1650 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1651 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1652 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1653 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1654 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1655 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1656 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1657 |  |  |                        " FROM tbl_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1658 |  |  |                        " WHERE id = %s ", (offline_meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1659 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1660 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1661 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1662 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1663 |  |  |                                    description='API.OFFLINE_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1664 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1665 |  |  |         query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1666 |  |  |                  " FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1667 |  |  |                  " WHERE equipment_id = %s AND offline_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1668 |  |  |         cursor.execute(query, (id_, offline_meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1669 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1670 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1671 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1672 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1673 |  |  |                                    description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1674 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1675 |  |  |         add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1676 |  |  |                    " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1677 |  |  |         cursor.execute(add_row, (id_, offline_meter_id, is_output)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1678 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1679 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1680 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1681 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1682 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1683 |  |  |         resp.location = '/equipments/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1684 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1685 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1686 |  | View Code Duplication | class EquipmentOfflineMeterItem: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1687 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1688 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1689 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1690 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1691 |  |  |     def on_options(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1692 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1693 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1694 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1695 |  |  |         _ = mid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1696 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1697 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1698 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1699 |  |  |     def on_delete(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1700 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1701 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1702 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1703 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1704 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1705 |  |  |         if not mid.isdigit() or int(mid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1706 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1707 |  |  |                                    description='API.INVALID_OFFLINE_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1708 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1709 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1710 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1711 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1712 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1713 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1714 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1715 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1716 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1717 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1718 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1719 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1720 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1721 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1722 |  |  |                        " FROM tbl_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1723 |  |  |                        " WHERE id = %s ", (mid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1724 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1725 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1726 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1727 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1728 |  |  |                                    description='API.OFFLINE_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1729 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1730 |  |  |         cursor.execute(" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1731 |  |  |                        " FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1732 |  |  |                        " WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1733 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1734 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1735 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1736 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1737 |  |  |                                    description='API.EQUIPMENT_OFFLINE_METER_RELATION_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1738 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1739 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1740 |  |  |                        " WHERE equipment_id = %s AND offline_meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1741 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1742 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1743 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1744 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1745 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1746 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1747 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1748 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1749 |  | View Code Duplication | class EquipmentVirtualMeterCollection: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1750 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1751 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1752 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1753 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1754 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1755 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1756 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1757 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1758 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1759 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1760 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1761 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1762 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1763 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1764 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1765 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1766 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1767 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1768 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1769 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1770 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1771 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1772 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1773 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1774 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1775 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1776 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1777 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1778 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1779 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1780 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1781 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1782 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1783 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1784 |  |  |                  " FROM tbl_energy_categories ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1785 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1786 |  |  |         rows_energy_categories = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1787 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1788 |  |  |         energy_category_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1789 |  |  |         if rows_energy_categories is not None and len(rows_energy_categories) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1790 |  |  |             for row in rows_energy_categories: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1791 |  |  |                 energy_category_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1792 |  |  |                                                 "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1793 |  |  |                                                 "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1794 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1795 |  |  |         query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1796 |  |  |                  " FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1797 |  |  |                  " WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1798 |  |  |                  " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1799 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1800 |  |  |         rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1801 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1802 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1803 |  |  |         if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1804 |  |  |             for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1805 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1806 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1807 |  |  |                                "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1808 |  |  |                                "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1809 |  |  |                                "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1810 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1811 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1812 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1813 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1814 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1815 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1816 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1817 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1818 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1819 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1820 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1821 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1822 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1823 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1824 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1825 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1826 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1827 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1828 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1829 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1830 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1831 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1832 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1833 |  |  |         if 'virtual_meter_id' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1834 |  |  |                 not isinstance(new_values['data']['virtual_meter_id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1835 |  |  |                 new_values['data']['virtual_meter_id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1836 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1837 |  |  |                                    description='API.INVALID_VIRTUAL_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1838 |  |  |         virtual_meter_id = new_values['data']['virtual_meter_id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1839 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1840 |  |  |         if 'is_output' not in new_values['data'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1841 |  |  |                 not isinstance(new_values['data']['is_output'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1842 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1843 |  |  |                                    description='API.INVALID_IS_OUTPUT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1844 |  |  |         is_output = new_values['data']['is_output'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1845 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1846 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1847 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1848 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1849 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1850 |  |  |                        " from tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1851 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1852 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1853 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1854 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1855 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1856 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1857 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1858 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1859 |  |  |                        " FROM tbl_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1860 |  |  |                        " WHERE id = %s ", (virtual_meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1861 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1862 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1863 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1864 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1865 |  |  |                                    description='API.VIRTUAL_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1866 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1867 |  |  |         query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1868 |  |  |                  " FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1869 |  |  |                  " WHERE equipment_id = %s AND virtual_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1870 |  |  |         cursor.execute(query, (id_, virtual_meter_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1871 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1872 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1873 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1874 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1875 |  |  |                                    description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1876 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1877 |  |  |         add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1878 |  |  |                    " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1879 |  |  |         cursor.execute(add_row, (id_, virtual_meter_id, is_output)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1880 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1881 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1882 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1883 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1884 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1885 |  |  |         resp.location = '/equipments/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1886 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1887 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1888 |  | View Code Duplication | class EquipmentVirtualMeterItem: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1889 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1890 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1891 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1892 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1893 |  |  |     def on_options(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1894 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1895 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1896 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1897 |  |  |         _ = mid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1898 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1899 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1900 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1901 |  |  |     def on_delete(req, resp, id_, mid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1902 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1903 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1904 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1905 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1906 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1907 |  |  |         if not mid.isdigit() or int(mid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1908 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1909 |  |  |                                    description='API.INVALID_VIRTUAL_METER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1910 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1911 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1912 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1913 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1914 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1915 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1916 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1917 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1918 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1919 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1920 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1921 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1922 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1923 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1924 |  |  |                        " FROM tbl_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1925 |  |  |                        " WHERE id = %s ", (mid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1926 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1927 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1928 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1929 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1930 |  |  |                                    description='API.VIRTUAL_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1931 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1932 |  |  |         cursor.execute(" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1933 |  |  |                        " FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1934 |  |  |                        " WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1935 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1936 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1937 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1938 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1939 |  |  |                                    description='API.EQUIPMENT_VIRTUAL_METER_RELATION_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1940 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1941 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1942 |  |  |                        " WHERE equipment_id = %s AND virtual_meter_id = %s ", (id_, mid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1943 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1944 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1945 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1946 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1947 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1948 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1949 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1950 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 1951 |  | View Code Duplication | class EquipmentCommandCollection: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1952 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1953 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1954 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1955 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1956 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1957 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1958 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1959 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1960 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1961 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1962 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1963 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1964 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1965 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1966 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1967 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1968 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1969 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1970 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1971 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1972 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1973 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1974 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1975 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1976 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1977 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1978 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1979 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1980 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1981 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1982 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1983 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1984 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1985 |  |  |         query = (" SELECT c.id, c.name, c.uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1986 |  |  |                  " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1987 |  |  |                  " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1988 |  |  |                  " ORDER BY c.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1989 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1990 |  |  |         rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1991 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1992 |  |  |         result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1993 |  |  |         if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1994 |  |  |             for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1995 |  |  |                 meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1996 |  |  |                                "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1997 |  |  |                                "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1998 |  |  |                 result.append(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 1999 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2000 |  |  |         resp.text = json.dumps(result) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 2001 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2002 |  |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 2003 |  |  |     @user_logger | 
            
                                                                        
                            
            
                                    
            
            
                | 2004 |  |  |     def on_post(req, resp, id_): | 
            
                                                                        
                            
            
                                    
            
            
                | 2005 |  |  |         """Handles POST requests""" | 
            
                                                                        
                            
            
                                    
            
            
                | 2006 |  |  |         admin_control(req) | 
            
                                                                        
                            
            
                                    
            
            
                | 2007 |  |  |         try: | 
            
                                                                        
                            
            
                                    
            
            
                | 2008 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                        
                            
            
                                    
            
            
                | 2009 |  |  |         except Exception as ex: | 
            
                                                                        
                            
            
                                    
            
            
                | 2010 |  |  |             print(str(ex)) | 
            
                                                                        
                            
            
                                    
            
            
                | 2011 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                        
                            
            
                                    
            
            
                | 2012 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                        
                            
            
                                    
            
            
                | 2013 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                        
                            
            
                                    
            
            
                | 2014 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2015 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                        
                            
            
                                    
            
            
                | 2016 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                        
                            
            
                                    
            
            
                | 2017 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                        
                            
            
                                    
            
            
                | 2018 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2019 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                        
                            
            
                                    
            
            
                | 2020 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2021 |  |  |         if 'command_id' not in new_values['data'].keys() or \ | 
            
                                                                        
                            
            
                                    
            
            
                | 2022 |  |  |                 not isinstance(new_values['data']['command_id'], int) or \ | 
            
                                                                        
                            
            
                                    
            
            
                | 2023 |  |  |                 new_values['data']['command_id'] <= 0: | 
            
                                                                        
                            
            
                                    
            
            
                | 2024 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                        
                            
            
                                    
            
            
                | 2025 |  |  |                                    description='API.INVALID_COMMAND_ID') | 
            
                                                                        
                            
            
                                    
            
            
                | 2026 |  |  |         command_id = new_values['data']['command_id'] | 
            
                                                                        
                            
            
                                    
            
            
                | 2027 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2028 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                        
                            
            
                                    
            
            
                | 2029 |  |  |         cursor = cnx.cursor() | 
            
                                                                        
                            
            
                                    
            
            
                | 2030 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2031 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                        
                            
            
                                    
            
            
                | 2032 |  |  |                        " from tbl_equipments " | 
            
                                                                        
                            
            
                                    
            
            
                | 2033 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                        
                            
            
                                    
            
            
                | 2034 |  |  |         if cursor.fetchone() is None: | 
            
                                                                        
                            
            
                                    
            
            
                | 2035 |  |  |             cursor.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2036 |  |  |             cnx.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2037 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                        
                            
            
                                    
            
            
                | 2038 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                        
                            
            
                                    
            
            
                | 2039 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2040 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                        
                            
            
                                    
            
            
                | 2041 |  |  |                        " FROM tbl_commands " | 
            
                                                                        
                            
            
                                    
            
            
                | 2042 |  |  |                        " WHERE id = %s ", (command_id,)) | 
            
                                                                        
                            
            
                                    
            
            
                | 2043 |  |  |         if cursor.fetchone() is None: | 
            
                                                                        
                            
            
                                    
            
            
                | 2044 |  |  |             cursor.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2045 |  |  |             cnx.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2046 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                        
                            
            
                                    
            
            
                | 2047 |  |  |                                    description='API.COMMAND_NOT_FOUND') | 
            
                                                                        
                            
            
                                    
            
            
                | 2048 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2049 |  |  |         query = (" SELECT id " | 
            
                                                                        
                            
            
                                    
            
            
                | 2050 |  |  |                  " FROM tbl_equipments_commands " | 
            
                                                                        
                            
            
                                    
            
            
                | 2051 |  |  |                  " WHERE equipment_id = %s AND command_id = %s") | 
            
                                                                        
                            
            
                                    
            
            
                | 2052 |  |  |         cursor.execute(query, (id_, command_id,)) | 
            
                                                                        
                            
            
                                    
            
            
                | 2053 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                        
                            
            
                                    
            
            
                | 2054 |  |  |             cursor.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2055 |  |  |             cnx.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2056 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                        
                            
            
                                    
            
            
                | 2057 |  |  |                                    description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') | 
            
                                                                        
                            
            
                                    
            
            
                | 2058 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2059 |  |  |         add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " | 
            
                                                                        
                            
            
                                    
            
            
                | 2060 |  |  |                    " VALUES (%s, %s) ") | 
            
                                                                        
                            
            
                                    
            
            
                | 2061 |  |  |         cursor.execute(add_row, (id_, command_id,)) | 
            
                                                                        
                            
            
                                    
            
            
                | 2062 |  |  |         cnx.commit() | 
            
                                                                        
                            
            
                                    
            
            
                | 2063 |  |  |         cursor.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2064 |  |  |         cnx.close() | 
            
                                                                        
                            
            
                                    
            
            
                | 2065 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 2066 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                        
                            
            
                                    
            
            
                | 2067 |  |  |         resp.location = '/equipments/' + str(id_) + '/commands/' + str(command_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2068 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2069 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 2070 |  | View Code Duplication | class EquipmentCommandItem: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2071 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2072 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2073 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2074 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2075 |  |  |     def on_options(req, resp, id_, cid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2076 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2077 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2078 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2079 |  |  |         _ = cid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2080 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2081 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2082 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2083 |  |  |     def on_delete(req, resp, id_, cid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2084 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2085 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2086 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2087 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2088 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2089 |  |  |         if not cid.isdigit() or int(cid) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2090 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2091 |  |  |                                    description='API.INVALID_COMMAND_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2092 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2093 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2094 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2095 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2096 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2097 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2098 |  |  |                        " WHERE id = %s ", (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2099 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2100 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2101 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2102 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2103 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2105 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2106 |  |  |                        " FROM tbl_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2107 |  |  |                        " WHERE id = %s ", (cid,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2108 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2109 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2110 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2111 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2112 |  |  |                                    description='API.COMMAND_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2114 |  |  |         cursor.execute(" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2115 |  |  |                        " FROM tbl_equipments_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2116 |  |  |                        " WHERE equipment_id = %s AND command_id = %s ", (id_, cid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2117 |  |  |         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2118 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2119 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2120 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2121 |  |  |                                    description='API.EQUIPMENT_COMMAND_RELATION_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2123 |  |  |         cursor.execute(" DELETE FROM tbl_equipments_commands WHERE equipment_id = %s AND command_id = %s ", (id_, cid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2124 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2126 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2127 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2129 |  |  |         resp.status = falcon.HTTP_204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2132 |  |  | class EquipmentExport: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2133 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2134 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2136 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2137 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2138 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2139 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2140 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2142 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2143 |  |  |     def on_get(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2144 |  |  |         if 'API-KEY' not in req.headers or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2145 |  |  |                 not isinstance(req.headers['API-KEY'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2146 |  |  |                 len(str.strip(req.headers['API-KEY'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2147 |  |  |             access_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2148 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2149 |  |  |             api_key_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2150 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2151 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2152 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2154 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2155 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2157 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2158 |  |  |                  " FROM tbl_cost_centers ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2159 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2160 |  |  |         rows_cost_centers = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2162 |  |  |         cost_center_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2163 |  |  |         if rows_cost_centers is not None and len(rows_cost_centers) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2164 |  |  |             for row in rows_cost_centers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2165 |  |  |                 cost_center_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2166 |  |  |                                             "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2167 |  |  |                                             "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2169 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2170 |  |  |                  " FROM tbl_svgs ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2171 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2172 |  |  |         rows_svgs = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2174 |  |  |         svg_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2175 |  |  |         if rows_svgs is not None and len(rows_svgs) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2176 |  |  |             for row in rows_svgs: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2177 |  |  |                 svg_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2178 |  |  |                                     "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2179 |  |  |                                     "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2181 |  |  |         query = (" SELECT id, name, uuid, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2182 |  |  |                  "        is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2183 |  |  |                  "        cost_center_id, svg_id, camera_url, description " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2184 |  |  |                  " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2185 |  |  |                  " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2186 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2187 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2189 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2190 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2191 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2192 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2193 |  |  |             meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2194 |  |  |                            "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2195 |  |  |                            "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2196 |  |  |                            "is_input_counted": bool(row[3]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2197 |  |  |                            "is_output_counted": bool(row[4]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2198 |  |  |                            "cost_center": cost_center_dict.get(row[5], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2199 |  |  |                            "svg": svg_dict.get(row[6], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2200 |  |  |                            "camera_url": row[7], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2201 |  |  |                            "description": row[8], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2202 |  |  |                            "commands": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2203 |  |  |                            "meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2204 |  |  |                            "offline_meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2205 |  |  |                            "virtual_meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2206 |  |  |                            "parameters": None} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2208 |  |  |             query = (" SELECT c.id, c.name, c.uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2209 |  |  |                      " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2210 |  |  |                      " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2211 |  |  |                      " ORDER BY c.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2212 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2213 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2215 |  |  |             command_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2216 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2217 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2218 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2219 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2220 |  |  |                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2221 |  |  |                     command_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2222 |  |  |                 meta_result['commands'] = command_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2224 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2225 |  |  |                      " FROM tbl_energy_categories ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2226 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2227 |  |  |             rows_energy_categories = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2228 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2229 |  |  |             energy_category_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2230 |  |  |             if rows_energy_categories is not None and len(rows_energy_categories) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2231 |  |  |                 for row in rows_energy_categories: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2232 |  |  |                     energy_category_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2233 |  |  |                                                     "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2234 |  |  |                                                     "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2236 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2237 |  |  |                      " FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2238 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2239 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2240 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2241 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2243 |  |  |             meter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2244 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2245 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2246 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2247 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2248 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2249 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2250 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2251 |  |  |                     meter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2252 |  |  |                 meta_result['meters'] = meter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2253 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2254 |  |  |                      " FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2255 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2256 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2257 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2258 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2260 |  |  |             offlinemeter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2261 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2262 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2263 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2264 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2265 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2266 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2267 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2268 |  |  |                     offlinemeter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2269 |  |  |                 meta_result['offline_meters'] = offlinemeter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2270 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2271 |  |  |                      " FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2272 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2273 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2274 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2275 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2276 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2277 |  |  |             virtualmeter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2278 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2279 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2280 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2281 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2282 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2283 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2284 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2285 |  |  |                     virtualmeter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2286 |  |  |                 meta_result['virtual_meters'] = virtualmeter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2287 |  |  |             query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2288 |  |  |                      " FROM tbl_points ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2289 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2290 |  |  |             rows_points = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2292 |  |  |             point_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2293 |  |  |             if rows_points is not None and len(rows_points) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2294 |  |  |                 for row in rows_points: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2295 |  |  |                     point_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2296 |  |  |                                           "name": row[1]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2297 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2298 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2299 |  |  |                      " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2300 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2301 |  |  |             rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2302 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2303 |  |  |             meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2304 |  |  |             if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2305 |  |  |                 for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2306 |  |  |                     meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2307 |  |  |                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2308 |  |  |                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2309 |  |  |                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2310 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2311 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2312 |  |  |                      " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2313 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2314 |  |  |             rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2315 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2316 |  |  |             offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2317 |  |  |             if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2318 |  |  |                 for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2319 |  |  |                     offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2320 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2321 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2322 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2323 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2324 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2325 |  |  |                      " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2326 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2327 |  |  |             rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2328 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2329 |  |  |             virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2330 |  |  |             if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2331 |  |  |                 for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2332 |  |  |                     virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2333 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2334 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2335 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2337 |  |  |             query = (" SELECT id, name, parameter_type, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2338 |  |  |                      "        constant, point_id, numerator_meter_uuid, denominator_meter_uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2339 |  |  |                      " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2340 |  |  |                      " WHERE equipment_id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2341 |  |  |                      " ORDER BY id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2342 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2343 |  |  |             rows_parameters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2344 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2345 |  |  |             parameters_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2346 |  |  |             if rows_parameters is not None and len(rows_parameters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2347 |  |  |                 for row in rows_parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2348 |  |  |                     constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2349 |  |  |                     point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2350 |  |  |                     numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2351 |  |  |                     denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2352 |  |  |                     if row[2] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2353 |  |  |                         point = point_dict.get(row[4], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2354 |  |  |                         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2355 |  |  |                         numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2356 |  |  |                         denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2357 |  |  |                     elif row[2] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2358 |  |  |                         constant = row[3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2359 |  |  |                         point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2360 |  |  |                         numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2361 |  |  |                         denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2362 |  |  |                     elif row[2] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2363 |  |  |                         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2364 |  |  |                         point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2365 |  |  |                         # find numerator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2366 |  |  |                         numerator_meter = meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2367 |  |  |                         if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2368 |  |  |                             numerator_meter = virtual_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2369 |  |  |                         if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2370 |  |  |                             numerator_meter = offline_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2371 |  |  |                         # find denominator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2372 |  |  |                         denominator_meter = meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2373 |  |  |                         if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2374 |  |  |                             denominator_meter = virtual_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2375 |  |  |                         if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2376 |  |  |                             denominator_meter = offline_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2377 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2378 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2379 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2380 |  |  |                               "parameter_type": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2381 |  |  |                               "constant": constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2382 |  |  |                               "point": point, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2383 |  |  |                               "numerator_meter": numerator_meter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2384 |  |  |                               "denominator_meter": denominator_meter} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2385 |  |  |                     parameters_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2386 |  |  |                 meta_result['parameters'] = parameters_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2387 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2388 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2389 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2390 |  |  |         resp.text = json.dumps(meta_result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2392 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2393 |  |  | class EquipmentImport: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2394 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2395 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2396 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2397 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2398 |  |  |     def on_options(req, resp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2399 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2400 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2401 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2402 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2403 |  |  |     def on_post(req, resp): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2404 |  |  |         """Handles POST requests""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2405 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2406 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2407 |  |  |             raw_json = req.stream.read().decode('utf-8') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2408 |  |  |         except Exception as ex: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2409 |  |  |             print(str(ex)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2410 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2411 |  |  |                                    title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2412 |  |  |                                    description='API.FAILED_TO_READ_REQUEST_STREAM') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2413 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2414 |  |  |         new_values = json.loads(raw_json) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2415 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2416 |  |  |         if 'name' not in new_values.keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2417 |  |  |                 not isinstance(new_values['name'], str) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2418 |  |  |                 len(str.strip(new_values['name'])) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2419 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2420 |  |  |                                    description='API.INVALID_EQUIPMENT_NAME') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2421 |  |  |         name = str.strip(new_values['name']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2422 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2423 |  |  |         if 'is_input_counted' not in new_values.keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2424 |  |  |                 not isinstance(new_values['is_input_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2425 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2426 |  |  |                                    description='API.INVALID_IS_INPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2427 |  |  |         is_input_counted = new_values['is_input_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2428 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2429 |  |  |         if 'is_output_counted' not in new_values.keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2430 |  |  |                 not isinstance(new_values['is_output_counted'], bool): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2431 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2432 |  |  |                                    description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2433 |  |  |         is_output_counted = new_values['is_output_counted'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2434 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2435 |  |  |         if 'id' not in new_values['cost_center'].keys() or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2436 |  |  |                 not isinstance(new_values['cost_center']['id'], int) or \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2437 |  |  |                 new_values['cost_center']['id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2438 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2439 |  |  |                                    description='API.INVALID_COST_CENTER_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2440 |  |  |         cost_center_id = new_values['cost_center']['id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2441 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2442 |  |  |         svg = new_values.get('svg', None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2443 |  |  |         svg_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2444 |  |  |         if isinstance(svg, dict): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2445 |  |  |             svg_id = svg.get('id', None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2446 |  |  |             if not (isinstance(svg_id, int) and svg_id > 0): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2447 |  |  |                 svg_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2448 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2449 |  |  |         if 'camera_url' in new_values.keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2450 |  |  |                 new_values['camera_url'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2451 |  |  |                 len(str(new_values['camera_url'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2452 |  |  |             camera_url = str.strip(new_values['camera_url']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2453 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2454 |  |  |             camera_url = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2455 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2456 |  |  |         if 'description' in new_values.keys() and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2457 |  |  |                 new_values['description'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2458 |  |  |                 len(str(new_values['description'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2459 |  |  |             description = str.strip(new_values['description']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2460 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2461 |  |  |             description = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2462 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2463 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2464 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2465 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2466 |  |  |         cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2467 |  |  |                        " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2468 |  |  |                        " WHERE name = %s ", (name,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2469 |  |  |         if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2470 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2471 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2472 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2473 |  |  |                                    description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2474 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2475 |  |  |         if cost_center_id is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2476 |  |  |             cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2477 |  |  |                            " FROM tbl_cost_centers " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2478 |  |  |                            " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2479 |  |  |                            (new_values['cost_center']['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2480 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2481 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2482 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2483 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2484 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2485 |  |  |                                        description='API.COST_CENTER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2486 |  |  |         if svg_id is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2487 |  |  |             cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2488 |  |  |                            " FROM tbl_svgs " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2489 |  |  |                            " WHERE id = %s ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2490 |  |  |                            (svg_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2491 |  |  |             row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2492 |  |  |             if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2493 |  |  |                 cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2494 |  |  |                 cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2495 |  |  |                 raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2496 |  |  |                                        description='API.SVG_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2497 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2498 |  |  |         add_values = (" INSERT INTO tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2499 |  |  |                       "    (name, uuid, is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2500 |  |  |                       "     cost_center_id, svg_id, camera_url, description) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2501 |  |  |                       " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2502 |  |  |         cursor.execute(add_values, (name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2503 |  |  |                                     str(uuid.uuid4()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2504 |  |  |                                     is_input_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2505 |  |  |                                     is_output_counted, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2506 |  |  |                                     cost_center_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2507 |  |  |                                     svg_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2508 |  |  |                                     camera_url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2509 |  |  |                                     description)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2510 |  |  |         new_id = cursor.lastrowid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2511 |  |  |         if new_values['commands'] is not None and len(new_values['commands']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2512 |  |  |             for command in new_values['commands']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2513 |  |  |                 cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2514 |  |  |                                " FROM tbl_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2515 |  |  |                                " WHERE id = %s ", (command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2516 |  |  |                 if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2517 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2518 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2519 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2520 |  |  |                                            description='API.COMMAND_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2521 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2522 |  |  |                 query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2523 |  |  |                          " FROM tbl_equipments_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2524 |  |  |                          " WHERE equipment_id = %s AND command_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2525 |  |  |                 cursor.execute(query, (new_id, command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2526 |  |  |                 if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2527 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2528 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2529 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2530 |  |  |                                            description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2531 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2532 |  |  |                 add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2533 |  |  |                            " VALUES (%s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2534 |  |  |                 cursor.execute(add_row, (new_id, command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2535 |  |  |         if new_values['meters'] is not None and len(new_values['meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2536 |  |  |             for meter in new_values['meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2537 |  |  |                 cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2538 |  |  |                                " FROM tbl_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2539 |  |  |                                " WHERE id = %s ", (meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2540 |  |  |                 if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2541 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2542 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2543 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2544 |  |  |                                            description='API.METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2545 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2546 |  |  |                 query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2547 |  |  |                          " FROM tbl_equipments_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2548 |  |  |                          " WHERE equipment_id = %s AND meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2549 |  |  |                 cursor.execute(query, (new_id, meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2550 |  |  |                 if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2551 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2552 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2553 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2554 |  |  |                                            description='API.EQUIPMENT_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2555 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2556 |  |  |                 add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2557 |  |  |                            " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2558 |  |  |                 cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2559 |  |  |         if new_values['offline_meters'] is not None and len(new_values['offline_meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2560 |  |  |             for offline_meter in new_values['offline_meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2561 |  |  |                 cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2562 |  |  |                                " FROM tbl_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2563 |  |  |                                " WHERE id = %s ", (offline_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2564 |  |  |                 if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2565 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2566 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2567 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2568 |  |  |                                            description='API.OFFLINE_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2569 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2570 |  |  |                 query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2571 |  |  |                          " FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2572 |  |  |                          " WHERE equipment_id = %s AND offline_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2573 |  |  |                 cursor.execute(query, (new_id, offline_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2574 |  |  |                 if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2575 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2576 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2577 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2578 |  |  |                                            description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2579 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2580 |  |  |                 add_row = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2581 |  |  |                            " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2582 |  |  |                 cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2583 |  |  |         if new_values['virtual_meters'] is not None and len(new_values['virtual_meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2584 |  |  |             for virtual_meter in new_values['virtual_meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2585 |  |  |                 cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2586 |  |  |                                " FROM tbl_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2587 |  |  |                                " WHERE id = %s ", (virtual_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2588 |  |  |                 if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2589 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2590 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2591 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2592 |  |  |                                            description='API.VIRTUAL_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2593 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2594 |  |  |                 query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2595 |  |  |                          " FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2596 |  |  |                          " WHERE equipment_id = %s AND virtual_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2597 |  |  |                 cursor.execute(query, (new_id, virtual_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2598 |  |  |                 if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2599 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2600 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2601 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2602 |  |  |                                            description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2603 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2604 |  |  |                 add_row = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2605 |  |  |                            " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2606 |  |  |                 cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2607 |  |  |         if new_values['parameters'] is not None and len(new_values['parameters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2608 |  |  |             for parameters in new_values['parameters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2609 |  |  |                 cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2610 |  |  |                                " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2611 |  |  |                                " WHERE name = %s AND equipment_id = %s ", (parameters['name'], new_id)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2612 |  |  |                 if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2613 |  |  |                     cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2614 |  |  |                     cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2615 |  |  |                     raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2616 |  |  |                                            description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2617 |  |  |                 if 'point' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2618 |  |  |                     if parameters['point'] is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2619 |  |  |                         point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2620 |  |  |                     elif parameters['point']['id'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2621 |  |  |                             parameters['point']['id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2622 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2623 |  |  |                                                description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2624 |  |  |                     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2625 |  |  |                         point_id = parameters['point']['id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2626 |  |  |                 else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2627 |  |  |                     point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2628 |  |  |                 numerator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2629 |  |  |                 if 'numerator_meter' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2630 |  |  |                     if parameters['numerator_meter'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2631 |  |  |                             isinstance(parameters['numerator_meter']['uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2632 |  |  |                             len(str.strip(parameters['numerator_meter']['uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2633 |  |  |                         numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2634 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2635 |  |  |                 denominator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2636 |  |  |                 if 'denominator_meter' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2637 |  |  |                     if parameters['denominator_meter'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2638 |  |  |                             isinstance(parameters['denominator_meter']['uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2639 |  |  |                             len(str.strip(parameters['denominator_meter']['uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2640 |  |  |                         denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2641 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2642 |  |  |                 # validate by parameter type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2643 |  |  |                 if parameters['parameter_type'] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2644 |  |  |                     if point_id is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2645 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2646 |  |  |                                                description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2647 |  |  |                     query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2648 |  |  |                              " FROM tbl_points " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2649 |  |  |                              " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2650 |  |  |                     cursor.execute(query, (point_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2651 |  |  |                     if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2652 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2653 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2654 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2655 |  |  |                                                description='API.POINT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2656 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2657 |  |  |                 elif parameters['parameter_type'] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2658 |  |  |                     if parameters['constant'] is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2659 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2660 |  |  |                                                description='API.INVALID_CONSTANT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2661 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2662 |  |  |                 elif parameters['parameter_type'] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2663 |  |  |                     query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2664 |  |  |                              " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2665 |  |  |                     cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2666 |  |  |                     rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2667 |  |  |                     meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2668 |  |  |                     if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2669 |  |  |                         for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2670 |  |  |                             meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2671 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2672 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2673 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2674 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2675 |  |  |                     query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2676 |  |  |                              " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2677 |  |  |                     cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2678 |  |  |                     rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2679 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2680 |  |  |                     offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2681 |  |  |                     if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2682 |  |  |                         for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2683 |  |  |                             offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2684 |  |  |                                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2685 |  |  |                                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2686 |  |  |                                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2687 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2688 |  |  |                     query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2689 |  |  |                              " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2690 |  |  |                     cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2691 |  |  |                     rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2692 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2693 |  |  |                     virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2694 |  |  |                     if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2695 |  |  |                         for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2696 |  |  |                             virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2697 |  |  |                                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2698 |  |  |                                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2699 |  |  |                                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2700 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2701 |  |  |                     # validate numerator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2702 |  |  |                     if numerator_meter_uuid is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2703 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2704 |  |  |                                                description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2705 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2706 |  |  |                     if meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2707 |  |  |                             virtual_meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2708 |  |  |                             offline_meter_dict.get(numerator_meter_uuid) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2709 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2710 |  |  |                                                description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2711 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2712 |  |  |                     # validate denominator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2713 |  |  |                     if denominator_meter_uuid is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2714 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2715 |  |  |                                                description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2716 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2717 |  |  |                     if denominator_meter_uuid == numerator_meter_uuid: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2718 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2719 |  |  |                                                description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2720 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2721 |  |  |                     if denominator_meter_uuid not in meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2722 |  |  |                             denominator_meter_uuid not in virtual_meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2723 |  |  |                             denominator_meter_uuid not in offline_meter_dict: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2724 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2725 |  |  |                                                description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2726 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2727 |  |  |                 add_values = (" INSERT INTO tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2728 |  |  |                               "    (equipment_id, name, parameter_type, constant, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2729 |  |  |                               "     point_id, numerator_meter_uuid, denominator_meter_uuid) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2730 |  |  |                               " VALUES (%s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2731 |  |  |                 cursor.execute(add_values, (new_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2732 |  |  |                                             parameters['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2733 |  |  |                                             parameters['parameter_type'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2734 |  |  |                                             parameters['constant'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2735 |  |  |                                             point_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2736 |  |  |                                             numerator_meter_uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2737 |  |  |                                             denominator_meter_uuid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2738 |  |  |         cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2739 |  |  |         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2740 |  |  |         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2741 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2742 |  |  |         resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2743 |  |  |         resp.location = '/equipments/' + str(new_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2744 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2745 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2746 |  |  | class EquipmentClone: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2747 |  |  |     def __init__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2748 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2749 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2750 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2751 |  |  |     def on_options(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2752 |  |  |         _ = req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2753 |  |  |         resp.status = falcon.HTTP_200 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2754 |  |  |         _ = id_ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2755 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2756 |  |  |     @staticmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2757 |  |  |     @user_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2758 |  |  |     def on_post(req, resp, id_): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2759 |  |  |         admin_control(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2760 |  |  |         if not id_.isdigit() or int(id_) <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2761 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2762 |  |  |                                    description='API.INVALID_EQUIPMENT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2763 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2764 |  |  |         cnx = mysql.connector.connect(**config.myems_system_db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2765 |  |  |         cursor = cnx.cursor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2766 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2767 |  |  |         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2768 |  |  |                  " FROM tbl_cost_centers ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2769 |  |  |         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2770 |  |  |         rows_cost_centers = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2771 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2772 |  |  |         cost_center_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2773 |  |  |         if rows_cost_centers is not None and len(rows_cost_centers) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2774 |  |  |             for row in rows_cost_centers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2775 |  |  |                 cost_center_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2776 |  |  |                                             "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2777 |  |  |                                             "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2778 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2779 |  |  |         query = (" SELECT id, name, uuid, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2780 |  |  |                  "        is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2781 |  |  |                  "        cost_center_id, svg_id, camera_url, description " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2782 |  |  |                  " FROM tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2783 |  |  |                  " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2784 |  |  |         cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2785 |  |  |         row = cursor.fetchone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2786 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2787 |  |  |         if row is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2788 |  |  |             raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2789 |  |  |                                    description='API.EQUIPMENT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2790 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2791 |  |  |             meta_result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2792 |  |  |                            "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2793 |  |  |                            "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2794 |  |  |                            "is_input_counted": bool(row[3]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2795 |  |  |                            "is_output_counted": bool(row[4]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2796 |  |  |                            "cost_center": cost_center_dict.get(row[5], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2797 |  |  |                            "svg_id": row[6], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2798 |  |  |                            "camera_url": row[7], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2799 |  |  |                            "description": row[8], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2800 |  |  |                            "commands": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2801 |  |  |                            "meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2802 |  |  |                            "offline_meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2803 |  |  |                            "virtual_meters": None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2804 |  |  |                            "parameters": None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2805 |  |  |                            } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2806 |  |  |             query = (" SELECT c.id, c.name, c.uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2807 |  |  |                      " FROM tbl_equipments e, tbl_equipments_commands ec, tbl_commands c " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2808 |  |  |                      " WHERE ec.equipment_id = e.id AND c.id = ec.command_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2809 |  |  |                      " ORDER BY c.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2810 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2811 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2812 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2813 |  |  |             command_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2814 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2815 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2816 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2817 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2818 |  |  |                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2819 |  |  |                     command_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2820 |  |  |                 meta_result['commands'] = command_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2821 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2822 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2823 |  |  |                      " FROM tbl_energy_categories ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2824 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2825 |  |  |             rows_energy_categories = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2826 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2827 |  |  |             energy_category_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2828 |  |  |             if rows_energy_categories is not None and len(rows_energy_categories) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2829 |  |  |                 for row in rows_energy_categories: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2830 |  |  |                     energy_category_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2831 |  |  |                                                     "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2832 |  |  |                                                     "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2833 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2834 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2835 |  |  |                      " FROM tbl_equipments e, tbl_equipments_meters em, tbl_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2836 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2837 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2838 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2839 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2840 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2841 |  |  |             meter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2842 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2843 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2844 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2845 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2846 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2847 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2848 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2849 |  |  |                     meter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2850 |  |  |                 meta_result['meters'] = meter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2851 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2852 |  |  |                      " FROM tbl_equipments e, tbl_equipments_offline_meters em, tbl_offline_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2853 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.offline_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2854 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2855 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2856 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2857 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2858 |  |  |             offlinemeter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2859 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2860 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2861 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2862 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2863 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2864 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2865 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2866 |  |  |                     offlinemeter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2867 |  |  |                 meta_result['offline_meters'] = offlinemeter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2868 |  |  |             query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id, em.is_output " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2869 |  |  |                      " FROM tbl_equipments e, tbl_equipments_virtual_meters em, tbl_virtual_meters m " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2870 |  |  |                      " WHERE em.equipment_id = e.id AND m.id = em.virtual_meter_id AND e.id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2871 |  |  |                      " ORDER BY m.id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2872 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2873 |  |  |             rows = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2874 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2875 |  |  |             virtualmeter_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2876 |  |  |             if rows is not None and len(rows) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2877 |  |  |                 for row in rows: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2878 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2879 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2880 |  |  |                               "uuid": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2881 |  |  |                               "energy_category": energy_category_dict.get(row[3], None), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2882 |  |  |                               "is_output": bool(row[4])} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2883 |  |  |                     virtualmeter_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2884 |  |  |                 meta_result['virtual_meters'] = virtualmeter_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2885 |  |  |             query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2886 |  |  |                      " FROM tbl_points ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2887 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2888 |  |  |             rows_points = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2889 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2890 |  |  |             point_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2891 |  |  |             if rows_points is not None and len(rows_points) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2892 |  |  |                 for row in rows_points: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2893 |  |  |                     point_dict[row[0]] = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2894 |  |  |                                           "name": row[1]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2895 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2896 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2897 |  |  |                      " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2898 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2899 |  |  |             rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2900 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2901 |  |  |             meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2902 |  |  |             if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2903 |  |  |                 for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2904 |  |  |                     meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2905 |  |  |                                           "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2906 |  |  |                                           "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2907 |  |  |                                           "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2908 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2909 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2910 |  |  |                      " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2911 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2912 |  |  |             rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2913 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2914 |  |  |             offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2915 |  |  |             if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2916 |  |  |                 for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2917 |  |  |                     offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2918 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2919 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2920 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2921 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2922 |  |  |             query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2923 |  |  |                      " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2924 |  |  |             cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2925 |  |  |             rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2926 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2927 |  |  |             virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2928 |  |  |             if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2929 |  |  |                 for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2930 |  |  |                     virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2931 |  |  |                                                   "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2932 |  |  |                                                   "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2933 |  |  |                                                   "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2934 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2935 |  |  |             query = (" SELECT id, name, parameter_type, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2936 |  |  |                      "        constant, point_id, numerator_meter_uuid, denominator_meter_uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2937 |  |  |                      " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2938 |  |  |                      " WHERE equipment_id = %s " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2939 |  |  |                      " ORDER BY id ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2940 |  |  |             cursor.execute(query, (id_,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2941 |  |  |             rows_parameters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2942 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2943 |  |  |             parameters_result = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2944 |  |  |             if rows_parameters is not None and len(rows_parameters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2945 |  |  |                 for row in rows_parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2946 |  |  |                     constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2947 |  |  |                     point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2948 |  |  |                     numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2949 |  |  |                     denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2950 |  |  |                     if row[2] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2951 |  |  |                         point = point_dict.get(row[4], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2952 |  |  |                         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2953 |  |  |                         numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2954 |  |  |                         denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2955 |  |  |                     elif row[2] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2956 |  |  |                         constant = row[3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2957 |  |  |                         point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2958 |  |  |                         numerator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2959 |  |  |                         denominator_meter = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2960 |  |  |                     elif row[2] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2961 |  |  |                         constant = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2962 |  |  |                         point = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2963 |  |  |                         # find numerator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2964 |  |  |                         numerator_meter = meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2965 |  |  |                         if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2966 |  |  |                             numerator_meter = virtual_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2967 |  |  |                         if numerator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2968 |  |  |                             numerator_meter = offline_meter_dict.get(row[5], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2969 |  |  |                         # find denominator meter by uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2970 |  |  |                         denominator_meter = meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2971 |  |  |                         if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2972 |  |  |                             denominator_meter = virtual_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2973 |  |  |                         if denominator_meter is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2974 |  |  |                             denominator_meter = offline_meter_dict.get(row[6], None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2975 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2976 |  |  |                     result = {"id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2977 |  |  |                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2978 |  |  |                               "parameter_type": row[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2979 |  |  |                               "constant": constant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2980 |  |  |                               "point": point, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2981 |  |  |                               "numerator_meter": numerator_meter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2982 |  |  |                               "denominator_meter": denominator_meter} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2983 |  |  |                     parameters_result.append(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2984 |  |  |                 meta_result['parameters'] = parameters_result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2985 |  |  |             timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2986 |  |  |             if config.utc_offset[0] == '-': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2987 |  |  |                 timezone_offset = -timezone_offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2988 |  |  |             new_name = (str.strip(meta_result['name']) + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2989 |  |  |                         (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2990 |  |  |             add_values = (" INSERT INTO tbl_equipments " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2991 |  |  |                           "    (name, uuid, is_input_counted, is_output_counted, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2992 |  |  |                           "     cost_center_id, svg_id, camera_url, description) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2993 |  |  |                           " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2994 |  |  |             cursor.execute(add_values, (new_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2995 |  |  |                                         str(uuid.uuid4()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2996 |  |  |                                         meta_result['is_input_counted'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2997 |  |  |                                         meta_result['is_output_counted'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2998 |  |  |                                         meta_result['cost_center']['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2999 |  |  |                                         meta_result['svg_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3000 |  |  |                                         meta_result['camera_url'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3001 |  |  |                                         meta_result['description'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3002 |  |  |             new_id = cursor.lastrowid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3003 |  |  |             if meta_result['commands'] is not None and len(meta_result['commands']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3004 |  |  |                 for command in meta_result['commands']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3005 |  |  |                     cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3006 |  |  |                                    " FROM tbl_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3007 |  |  |                                    " WHERE id = %s ", (command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3008 |  |  |                     if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3009 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3010 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3011 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3012 |  |  |                                                description='API.COMMAND_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3013 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3014 |  |  |                     query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3015 |  |  |                              " FROM tbl_equipments_commands " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3016 |  |  |                              " WHERE equipment_id = %s AND command_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3017 |  |  |                     cursor.execute(query, (new_id, command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3018 |  |  |                     if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3019 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3020 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3021 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3022 |  |  |                                                description='API.EQUIPMENT_COMMAND_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3023 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3024 |  |  |                     add_row = (" INSERT INTO tbl_equipments_commands (equipment_id, command_id) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3025 |  |  |                                " VALUES (%s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3026 |  |  |                     cursor.execute(add_row, (new_id, command['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3027 |  |  |             if meta_result['meters'] is not None and len(meta_result['meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3028 |  |  |                 for meter in meta_result['meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3029 |  |  |                     cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3030 |  |  |                                    " FROM tbl_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3031 |  |  |                                    " WHERE id = %s ", (meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3032 |  |  |                     if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3033 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3034 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3035 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3036 |  |  |                                                description='API.METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3037 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3038 |  |  |                     query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3039 |  |  |                              " FROM tbl_equipments_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3040 |  |  |                              " WHERE equipment_id = %s AND meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3041 |  |  |                     cursor.execute(query, (new_id, meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3042 |  |  |                     if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3043 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3044 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3045 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3046 |  |  |                                                description='API.EQUIPMENT_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3047 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3048 |  |  |                     add_row = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3049 |  |  |                                " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3050 |  |  |                     cursor.execute(add_row, (new_id, meter['id'], meter['is_output'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3051 |  |  |             if meta_result['offline_meters'] is not None and len(meta_result['offline_meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3052 |  |  |                 for offline_meter in meta_result['offline_meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3053 |  |  |                     cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3054 |  |  |                                    " FROM tbl_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3055 |  |  |                                    " WHERE id = %s ", (offline_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3056 |  |  |                     if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3057 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3058 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3059 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3060 |  |  |                                                description='API.OFFLINE_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3061 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3062 |  |  |                     query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3063 |  |  |                              " FROM tbl_equipments_offline_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3064 |  |  |                              " WHERE equipment_id = %s AND offline_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3065 |  |  |                     cursor.execute(query, (new_id, offline_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3066 |  |  |                     if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3067 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3068 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3069 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3070 |  |  |                                                description='API.EQUIPMENT_OFFLINE_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3071 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3072 |  |  |                     add_row = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3073 |  |  |                         " INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3074 |  |  |                         " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3075 |  |  |                     cursor.execute(add_row, (new_id, offline_meter['id'], offline_meter['is_output'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3076 |  |  |             if meta_result['virtual_meters'] is not None and len(meta_result['virtual_meters']) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3077 |  |  |                 for virtual_meter in meta_result['virtual_meters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3078 |  |  |                     cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3079 |  |  |                                    " FROM tbl_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3080 |  |  |                                    " WHERE id = %s ", (virtual_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3081 |  |  |                     if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3082 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3083 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3084 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3085 |  |  |                                                description='API.VIRTUAL_METER_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3086 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3087 |  |  |                     query = (" SELECT id " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3088 |  |  |                              " FROM tbl_equipments_virtual_meters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3089 |  |  |                              " WHERE equipment_id = %s AND virtual_meter_id = %s") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3090 |  |  |                     cursor.execute(query, (new_id, virtual_meter['id'],)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3091 |  |  |                     if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3092 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3093 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3094 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3095 |  |  |                                                description='API.EQUIPMENT_VIRTUAL_METER_RELATION_EXISTS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3096 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3097 |  |  |                     add_row = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3098 |  |  |                         " INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3099 |  |  |                         " VALUES (%s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3100 |  |  |                     cursor.execute(add_row, (new_id, virtual_meter['id'], virtual_meter['is_output'])) | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 3101 |  | View Code Duplication |             if meta_result['parameters'] is not None and len(meta_result['parameters']) > 0: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3102 |  |  |                 for parameters in meta_result['parameters']: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3103 |  |  |                     cursor.execute(" SELECT name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3104 |  |  |                                    " FROM tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3105 |  |  |                                    " WHERE name = %s AND equipment_id = %s ", (parameters['name'], new_id)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3106 |  |  |                     if cursor.fetchone() is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3107 |  |  |                         cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3108 |  |  |                         cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3109 |  |  |                         raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3110 |  |  |                                                description='API.EQUIPMENT_PARAMETER_NAME_IS_ALREADY_IN_USE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3111 |  |  |                     if 'point' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3112 |  |  |                         if parameters['point'] is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3113 |  |  |                             point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3114 |  |  |                         elif parameters['point']['id'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3115 |  |  |                                 parameters['point']['id'] <= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3116 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3117 |  |  |                                                    description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3118 |  |  |                         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3119 |  |  |                             point_id = parameters['point']['id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3120 |  |  |                     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3121 |  |  |                         point_id = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3122 |  |  |                     numerator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3123 |  |  |                     if 'numerator_meter' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3124 |  |  |                         if parameters['numerator_meter'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3125 |  |  |                                 isinstance(parameters['numerator_meter']['uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3126 |  |  |                                 len(str.strip(parameters['numerator_meter']['uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3127 |  |  |                             numerator_meter_uuid = str.strip(parameters['numerator_meter']['uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3129 |  |  |                     denominator_meter_uuid = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3130 |  |  |                     if 'denominator_meter' in parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3131 |  |  |                         if parameters['denominator_meter'] is not None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3132 |  |  |                                 isinstance(parameters['denominator_meter']['uuid'], str) and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3133 |  |  |                                 len(str.strip(parameters['denominator_meter']['uuid'])) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3134 |  |  |                             denominator_meter_uuid = str.strip(parameters['denominator_meter']['uuid']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3136 |  |  |                     # validate by parameter type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3137 |  |  |                     if parameters['parameter_type'] == 'point': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3138 |  |  |                         if point_id is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3139 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3140 |  |  |                                                    description='API.INVALID_POINT_ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3141 |  |  |                         query = (" SELECT id, name " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3142 |  |  |                                  " FROM tbl_points " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3143 |  |  |                                  " WHERE id = %s ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3144 |  |  |                         cursor.execute(query, (point_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3145 |  |  |                         if cursor.fetchone() is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3146 |  |  |                             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3147 |  |  |                             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3148 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3149 |  |  |                                                    description='API.POINT_NOT_FOUND') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3151 |  |  |                     elif parameters['parameter_type'] == 'constant': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3152 |  |  |                         if parameters['constant'] is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3153 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3154 |  |  |                                                    description='API.INVALID_CONSTANT_VALUE') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3156 |  |  |                     elif parameters['parameter_type'] == 'fraction': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3157 |  |  |                         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3158 |  |  |                                  " FROM tbl_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3159 |  |  |                         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3160 |  |  |                         rows_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3161 |  |  |                         meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3162 |  |  |                         if rows_meters is not None and len(rows_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3163 |  |  |                             for row in rows_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3164 |  |  |                                 meter_dict[row[2]] = {"type": 'meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3165 |  |  |                                                       "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3166 |  |  |                                                       "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3167 |  |  |                                                       "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3169 |  |  |                         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3170 |  |  |                                  " FROM tbl_offline_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3171 |  |  |                         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3172 |  |  |                         rows_offline_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3174 |  |  |                         offline_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3175 |  |  |                         if rows_offline_meters is not None and len(rows_offline_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3176 |  |  |                             for row in rows_offline_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3177 |  |  |                                 offline_meter_dict[row[2]] = {"type": 'offline_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3178 |  |  |                                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3179 |  |  |                                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3180 |  |  |                                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3182 |  |  |                         query = (" SELECT id, name, uuid " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3183 |  |  |                                  " FROM tbl_virtual_meters ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3184 |  |  |                         cursor.execute(query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3185 |  |  |                         rows_virtual_meters = cursor.fetchall() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3187 |  |  |                         virtual_meter_dict = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3188 |  |  |                         if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3189 |  |  |                             for row in rows_virtual_meters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3190 |  |  |                                 virtual_meter_dict[row[2]] = {"type": 'virtual_meter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3191 |  |  |                                                               "id": row[0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3192 |  |  |                                                               "name": row[1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3193 |  |  |                                                               "uuid": row[2]} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3195 |  |  |                         # validate numerator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3196 |  |  |                         if numerator_meter_uuid is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3197 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3198 |  |  |                                                    description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3200 |  |  |                         if meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3201 |  |  |                                 virtual_meter_dict.get(numerator_meter_uuid) is None and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3202 |  |  |                                 offline_meter_dict.get(numerator_meter_uuid) is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3203 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3204 |  |  |                                                    description='API.INVALID_NUMERATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3206 |  |  |                         # validate denominator meter uuid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3207 |  |  |                         if denominator_meter_uuid == numerator_meter_uuid: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3208 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3209 |  |  |                                                    description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3211 |  |  |                         if denominator_meter_uuid not in meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3212 |  |  |                                 denominator_meter_uuid not in virtual_meter_dict and \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3213 |  |  |                                 denominator_meter_uuid not in offline_meter_dict: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3214 |  |  |                             raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3215 |  |  |                                                    description='API.INVALID_DENOMINATOR_METER_UUID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3217 |  |  |                     add_values = (" INSERT INTO tbl_equipments_parameters " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3218 |  |  |                                   "    (equipment_id, name, parameter_type, constant, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3219 |  |  |                                   "     point_id, numerator_meter_uuid, denominator_meter_uuid) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3220 |  |  |                                   " VALUES (%s, %s, %s, %s, %s, %s, %s) ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3221 |  |  |                     cursor.execute(add_values, (new_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3222 |  |  |                                                 parameters['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3223 |  |  |                                                 parameters['parameter_type'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3224 |  |  |                                                 parameters['constant'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3225 |  |  |                                                 point_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3226 |  |  |                                                 numerator_meter_uuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3227 |  |  |                                                 denominator_meter_uuid)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3228 |  |  |             cnx.commit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3229 |  |  |             cursor.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3230 |  |  |             cnx.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3232 |  |  |             resp.status = falcon.HTTP_201 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 3233 |  |  |             resp.location = '/equipments/' + str(new_id) | 
            
                                                        
            
                                    
            
            
                | 3234 |  |  |  |