|
@@ 2062-2232 (lines=171) @@
|
| 2059 |
|
resp.status = falcon.HTTP_204 |
| 2060 |
|
|
| 2061 |
|
|
| 2062 |
|
class HybridPowerStationGeneratorCollection: |
| 2063 |
|
def __init__(self): |
| 2064 |
|
"""Initializes Class""" |
| 2065 |
|
pass |
| 2066 |
|
|
| 2067 |
|
@staticmethod |
| 2068 |
|
def on_options(req, resp, id_): |
| 2069 |
|
resp.status = falcon.HTTP_200 |
| 2070 |
|
|
| 2071 |
|
@staticmethod |
| 2072 |
|
def on_get(req, resp, id_): |
| 2073 |
|
access_control(req) |
| 2074 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2075 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2076 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 2077 |
|
|
| 2078 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2079 |
|
cursor = cnx.cursor() |
| 2080 |
|
|
| 2081 |
|
cursor.execute(" SELECT name " |
| 2082 |
|
" FROM tbl_hybrid_power_stations " |
| 2083 |
|
" WHERE id = %s ", (id_,)) |
| 2084 |
|
if cursor.fetchone() is None: |
| 2085 |
|
cursor.close() |
| 2086 |
|
cnx.close() |
| 2087 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2088 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2089 |
|
# query meter dict |
| 2090 |
|
query = (" SELECT id, name, uuid " |
| 2091 |
|
" FROM tbl_meters ") |
| 2092 |
|
cursor.execute(query) |
| 2093 |
|
rows_meters = cursor.fetchall() |
| 2094 |
|
|
| 2095 |
|
meter_dict = dict() |
| 2096 |
|
if rows_meters is not None and len(rows_meters) > 0: |
| 2097 |
|
for row in rows_meters: |
| 2098 |
|
meter_dict[row[0]] = {"id": row[0], |
| 2099 |
|
"name": row[1], |
| 2100 |
|
"uuid": row[2]} |
| 2101 |
|
# query point dict |
| 2102 |
|
query = (" SELECT id, name " |
| 2103 |
|
" FROM tbl_points ") |
| 2104 |
|
cursor.execute(query) |
| 2105 |
|
rows_points = cursor.fetchall() |
| 2106 |
|
|
| 2107 |
|
point_dict = dict() |
| 2108 |
|
if rows_points is not None and len(rows_points) > 0: |
| 2109 |
|
for row in rows_points: |
| 2110 |
|
point_dict[row[0]] = {"id": row[0], |
| 2111 |
|
"name": row[1]} |
| 2112 |
|
|
| 2113 |
|
query = (" SELECT id, name, uuid, operating_status_point_id, " |
| 2114 |
|
" fuel_consumption_meter_id, power_generation_meter_id " |
| 2115 |
|
" FROM tbl_hybrid_power_stations_generators " |
| 2116 |
|
" WHERE hybrid_power_station_id = %s " |
| 2117 |
|
" ORDER BY name ") |
| 2118 |
|
cursor.execute(query, (id_,)) |
| 2119 |
|
rows = cursor.fetchall() |
| 2120 |
|
|
| 2121 |
|
result = list() |
| 2122 |
|
if rows is not None and len(rows) > 0: |
| 2123 |
|
for row in rows: |
| 2124 |
|
meta_result = {"id": row[0], |
| 2125 |
|
"name": row[1], |
| 2126 |
|
"uuid": row[2], |
| 2127 |
|
"operating_status_point": point_dict.get(row[3]), |
| 2128 |
|
"fuel_consumption_meter": meter_dict.get(row[4]), |
| 2129 |
|
"power_generation_meter": meter_dict.get(row[5]) |
| 2130 |
|
} |
| 2131 |
|
result.append(meta_result) |
| 2132 |
|
|
| 2133 |
|
resp.text = json.dumps(result) |
| 2134 |
|
|
| 2135 |
|
@staticmethod |
| 2136 |
|
@user_logger |
| 2137 |
|
def on_post(req, resp, id_): |
| 2138 |
|
"""Handles POST requests""" |
| 2139 |
|
admin_control(req) |
| 2140 |
|
try: |
| 2141 |
|
raw_json = req.stream.read().decode('utf-8') |
| 2142 |
|
except Exception as ex: |
| 2143 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 2144 |
|
title='API.BAD_REQUEST', |
| 2145 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 2146 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 2147 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2148 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 2149 |
|
|
| 2150 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2151 |
|
cursor = cnx.cursor() |
| 2152 |
|
|
| 2153 |
|
cursor.execute(" SELECT name " |
| 2154 |
|
" FROM tbl_hybrid_power_stations " |
| 2155 |
|
" WHERE id = %s ", (id_,)) |
| 2156 |
|
if cursor.fetchone() is None: |
| 2157 |
|
cursor.close() |
| 2158 |
|
cnx.close() |
| 2159 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2160 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2161 |
|
|
| 2162 |
|
new_values = json.loads(raw_json) |
| 2163 |
|
|
| 2164 |
|
if 'name' not in new_values['data'].keys() or \ |
| 2165 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 2166 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 2167 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2168 |
|
description='API.INVALID_HYBRID_POWER_STATION_GENERATOR_NAME') |
| 2169 |
|
name = str.strip(new_values['data']['name']) |
| 2170 |
|
|
| 2171 |
|
if 'operating_status_point_id' not in new_values['data'].keys() or \ |
| 2172 |
|
not isinstance(new_values['data']['operating_status_point_id'], int) or \ |
| 2173 |
|
new_values['data']['operating_status_point_id'] <= 0: |
| 2174 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2175 |
|
description='API.INVALID_OPERATING_STATUS_POINT_ID') |
| 2176 |
|
operating_status_point_id = new_values['data']['operating_status_point_id'] |
| 2177 |
|
|
| 2178 |
|
if 'fuel_consumption_meter_id' not in new_values['data'].keys() or \ |
| 2179 |
|
not isinstance(new_values['data']['fuel_consumption_meter_id'], int) or \ |
| 2180 |
|
new_values['data']['fuel_consumption_meter_id'] <= 0: |
| 2181 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2182 |
|
description='API.INVALID_FUEL_CONSUMPTION_METER_ID') |
| 2183 |
|
fuel_consumption_meter_id = new_values['data']['fuel_consumption_meter_id'] |
| 2184 |
|
|
| 2185 |
|
if 'power_generation_meter_id' not in new_values['data'].keys() or \ |
| 2186 |
|
not isinstance(new_values['data']['power_generation_meter_id'], int) or \ |
| 2187 |
|
new_values['data']['power_generation_meter_id'] <= 0: |
| 2188 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2189 |
|
description='API.INVALID_POWER_GENERATION_METER_ID') |
| 2190 |
|
power_generation_meter_id = new_values['data']['power_generation_meter_id'] |
| 2191 |
|
|
| 2192 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 2193 |
|
cursor = cnx.cursor() |
| 2194 |
|
|
| 2195 |
|
cursor.execute(" SELECT name " |
| 2196 |
|
" FROM tbl_hybrid_power_stations " |
| 2197 |
|
" WHERE id = %s ", |
| 2198 |
|
(id_,)) |
| 2199 |
|
if cursor.fetchone() is None: |
| 2200 |
|
cursor.close() |
| 2201 |
|
cnx.close() |
| 2202 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 2203 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 2204 |
|
|
| 2205 |
|
cursor.execute(" SELECT name " |
| 2206 |
|
" FROM tbl_hybrid_power_stations_generators " |
| 2207 |
|
" WHERE hybrid_power_station_id = %s AND name = %s ", |
| 2208 |
|
(id_, name,)) |
| 2209 |
|
if cursor.fetchone() is not None: |
| 2210 |
|
cursor.close() |
| 2211 |
|
cnx.close() |
| 2212 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 2213 |
|
description='API.HYBRID_POWER_STATION_GENERATOR_NAME_IS_ALREADY_IN_USE') |
| 2214 |
|
|
| 2215 |
|
add_values = (" INSERT INTO tbl_hybrid_power_stations_generators " |
| 2216 |
|
" (name, uuid, hybrid_power_station_id, operating_status_point_id, " |
| 2217 |
|
" fule_consumption_meter_id, power_generatoion_meter_id) " |
| 2218 |
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
| 2219 |
|
cursor.execute(add_values, (name, |
| 2220 |
|
str(uuid.uuid4()), |
| 2221 |
|
id_, |
| 2222 |
|
operating_status_point_id, |
| 2223 |
|
fuel_consumption_meter_id, |
| 2224 |
|
power_generation_meter_id |
| 2225 |
|
)) |
| 2226 |
|
new_id = cursor.lastrowid |
| 2227 |
|
cnx.commit() |
| 2228 |
|
cursor.close() |
| 2229 |
|
cnx.close() |
| 2230 |
|
|
| 2231 |
|
resp.status = falcon.HTTP_201 |
| 2232 |
|
resp.location = '/hybridpowerstation/' + str(id_) + '/generators/' + str(new_id) |
| 2233 |
|
|
| 2234 |
|
|
| 2235 |
|
class HybridPowerStationGeneratorItem: |
|
@@ 3810-3979 (lines=170) @@
|
| 3807 |
|
resp.status = falcon.HTTP_204 |
| 3808 |
|
|
| 3809 |
|
|
| 3810 |
|
class HybridPowerStationPCSCollection: |
| 3811 |
|
def __init__(self): |
| 3812 |
|
"""Initializes Class""" |
| 3813 |
|
pass |
| 3814 |
|
|
| 3815 |
|
@staticmethod |
| 3816 |
|
def on_options(req, resp, id_): |
| 3817 |
|
resp.status = falcon.HTTP_200 |
| 3818 |
|
|
| 3819 |
|
@staticmethod |
| 3820 |
|
def on_get(req, resp, id_): |
| 3821 |
|
access_control(req) |
| 3822 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3823 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3824 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 3825 |
|
|
| 3826 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3827 |
|
cursor = cnx.cursor() |
| 3828 |
|
|
| 3829 |
|
cursor.execute(" SELECT name " |
| 3830 |
|
" FROM tbl_hybrid_power_stations " |
| 3831 |
|
" WHERE id = %s ", (id_,)) |
| 3832 |
|
if cursor.fetchone() is None: |
| 3833 |
|
cursor.close() |
| 3834 |
|
cnx.close() |
| 3835 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3836 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3837 |
|
# query meter dict |
| 3838 |
|
query = (" SELECT id, name, uuid " |
| 3839 |
|
" FROM tbl_meters ") |
| 3840 |
|
cursor.execute(query) |
| 3841 |
|
rows_meters = cursor.fetchall() |
| 3842 |
|
|
| 3843 |
|
meter_dict = dict() |
| 3844 |
|
if rows_meters is not None and len(rows_meters) > 0: |
| 3845 |
|
for row in rows_meters: |
| 3846 |
|
meter_dict[row[0]] = {"id": row[0], |
| 3847 |
|
"name": row[1], |
| 3848 |
|
"uuid": row[2]} |
| 3849 |
|
# query point dict |
| 3850 |
|
query = (" SELECT id, name " |
| 3851 |
|
" FROM tbl_points ") |
| 3852 |
|
cursor.execute(query) |
| 3853 |
|
rows_points = cursor.fetchall() |
| 3854 |
|
|
| 3855 |
|
point_dict = dict() |
| 3856 |
|
if rows_points is not None and len(rows_points) > 0: |
| 3857 |
|
for row in rows_points: |
| 3858 |
|
point_dict[row[0]] = {"id": row[0], |
| 3859 |
|
"name": row[1]} |
| 3860 |
|
|
| 3861 |
|
query = (" SELECT id, name, uuid, operating_status_point_id, charge_meter_id, discharge_meter_id " |
| 3862 |
|
" FROM tbl_hybrid_power_stations_pcses " |
| 3863 |
|
" WHERE hybrid_power_station_id = %s " |
| 3864 |
|
" ORDER BY name ") |
| 3865 |
|
cursor.execute(query, (id_,)) |
| 3866 |
|
rows = cursor.fetchall() |
| 3867 |
|
|
| 3868 |
|
result = list() |
| 3869 |
|
if rows is not None and len(rows) > 0: |
| 3870 |
|
for row in rows: |
| 3871 |
|
meta_result = {"id": row[0], |
| 3872 |
|
"name": row[1], |
| 3873 |
|
"uuid": row[2], |
| 3874 |
|
"operating_status_point": point_dict.get(row[3]), |
| 3875 |
|
"charge_meter": meter_dict.get(row[4]), |
| 3876 |
|
"discharge_meter": meter_dict.get(row[5]) |
| 3877 |
|
} |
| 3878 |
|
result.append(meta_result) |
| 3879 |
|
|
| 3880 |
|
resp.text = json.dumps(result) |
| 3881 |
|
|
| 3882 |
|
@staticmethod |
| 3883 |
|
@user_logger |
| 3884 |
|
def on_post(req, resp, id_): |
| 3885 |
|
"""Handles POST requests""" |
| 3886 |
|
admin_control(req) |
| 3887 |
|
try: |
| 3888 |
|
raw_json = req.stream.read().decode('utf-8') |
| 3889 |
|
except Exception as ex: |
| 3890 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, |
| 3891 |
|
title='API.BAD_REQUEST', |
| 3892 |
|
description='API.FAILED_TO_READ_REQUEST_STREAM') |
| 3893 |
|
if not id_.isdigit() or int(id_) <= 0: |
| 3894 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3895 |
|
description='API.INVALID_HYBRID_POWER_STATION_ID') |
| 3896 |
|
|
| 3897 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3898 |
|
cursor = cnx.cursor() |
| 3899 |
|
|
| 3900 |
|
cursor.execute(" SELECT name " |
| 3901 |
|
" FROM tbl_hybrid_power_stations " |
| 3902 |
|
" WHERE id = %s ", (id_,)) |
| 3903 |
|
if cursor.fetchone() is None: |
| 3904 |
|
cursor.close() |
| 3905 |
|
cnx.close() |
| 3906 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3907 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3908 |
|
|
| 3909 |
|
new_values = json.loads(raw_json) |
| 3910 |
|
|
| 3911 |
|
if 'name' not in new_values['data'].keys() or \ |
| 3912 |
|
not isinstance(new_values['data']['name'], str) or \ |
| 3913 |
|
len(str.strip(new_values['data']['name'])) == 0: |
| 3914 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3915 |
|
description='API.INVALID_HYBRID_POWER_STATION_PCS_NAME') |
| 3916 |
|
name = str.strip(new_values['data']['name']) |
| 3917 |
|
|
| 3918 |
|
if 'operating_status_point_id' not in new_values['data'].keys() or \ |
| 3919 |
|
not isinstance(new_values['data']['operating_status_point_id'], int) or \ |
| 3920 |
|
new_values['data']['operating_status_point_id'] <= 0: |
| 3921 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3922 |
|
description='API.INVALID_OPERATING_STATUS_POINT_ID') |
| 3923 |
|
operating_status_point_id = new_values['data']['operating_status_point_id'] |
| 3924 |
|
|
| 3925 |
|
if 'charge_meter_id' not in new_values['data'].keys() or \ |
| 3926 |
|
not isinstance(new_values['data']['charge_meter_id'], int) or \ |
| 3927 |
|
new_values['data']['charge_meter_id'] <= 0: |
| 3928 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3929 |
|
description='API.INVALID_CHARGE_METER_ID') |
| 3930 |
|
charge_meter_id = new_values['data']['charge_meter_id'] |
| 3931 |
|
|
| 3932 |
|
if 'discharge_meter_id' not in new_values['data'].keys() or \ |
| 3933 |
|
not isinstance(new_values['data']['discharge_meter_id'], int) or \ |
| 3934 |
|
new_values['data']['discharge_meter_id'] <= 0: |
| 3935 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3936 |
|
description='API.INVALID_DISCHARGE_METER_ID') |
| 3937 |
|
discharge_meter_id = new_values['data']['discharge_meter_id'] |
| 3938 |
|
|
| 3939 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 3940 |
|
cursor = cnx.cursor() |
| 3941 |
|
|
| 3942 |
|
cursor.execute(" SELECT name " |
| 3943 |
|
" FROM tbl_hybrid_power_stations " |
| 3944 |
|
" WHERE id = %s ", |
| 3945 |
|
(id_,)) |
| 3946 |
|
if cursor.fetchone() is None: |
| 3947 |
|
cursor.close() |
| 3948 |
|
cnx.close() |
| 3949 |
|
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
| 3950 |
|
description='API.HYBRID_POWER_STATION_NOT_FOUND') |
| 3951 |
|
|
| 3952 |
|
cursor.execute(" SELECT name " |
| 3953 |
|
" FROM tbl_hybrid_power_stations_pcses " |
| 3954 |
|
" WHERE hybrid_power_station_id = %s AND name = %s ", |
| 3955 |
|
(id_, name,)) |
| 3956 |
|
if cursor.fetchone() is not None: |
| 3957 |
|
cursor.close() |
| 3958 |
|
cnx.close() |
| 3959 |
|
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
| 3960 |
|
description='API.HYBRID_POWER_STATION_PCS_NAME_IS_ALREADY_IN_USE') |
| 3961 |
|
|
| 3962 |
|
add_values = (" INSERT INTO tbl_hybrid_power_stations_pcses " |
| 3963 |
|
" (name, uuid, hybrid_power_station_id, operating_status_point_id, charge_meter_id, " |
| 3964 |
|
" discharge_meter_id) " |
| 3965 |
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
| 3966 |
|
cursor.execute(add_values, (name, |
| 3967 |
|
str(uuid.uuid4()), |
| 3968 |
|
id_, |
| 3969 |
|
operating_status_point_id, |
| 3970 |
|
charge_meter_id, |
| 3971 |
|
discharge_meter_id |
| 3972 |
|
)) |
| 3973 |
|
new_id = cursor.lastrowid |
| 3974 |
|
cnx.commit() |
| 3975 |
|
cursor.close() |
| 3976 |
|
cnx.close() |
| 3977 |
|
|
| 3978 |
|
resp.status = falcon.HTTP_201 |
| 3979 |
|
resp.location = '/hybridpowerstation/' + str(id_) + '/pcses/' + str(new_id) |
| 3980 |
|
|
| 3981 |
|
|
| 3982 |
|
class HybridPowerStationPCSItem: |