Code Duplication    Length = 101-107 lines in 12 locations

myems-api/core/space.py 6 locations

@@ 2232-2338 (lines=107) @@
2229
        resp.status = falcon.HTTP_204
2230
2231
2232
class SpaceTenantCollection:
2233
    @staticmethod
2234
    def __init__():
2235
        """Initializes Class"""
2236
        pass
2237
2238
    @staticmethod
2239
    def on_options(req, resp, id_):
2240
        resp.status = falcon.HTTP_200
2241
2242
    @staticmethod
2243
    def on_get(req, resp, id_):
2244
        if not id_.isdigit() or int(id_) <= 0:
2245
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2246
                                   description='API.INVALID_SPACE_ID')
2247
2248
        cnx = mysql.connector.connect(**config.myems_system_db)
2249
        cursor = cnx.cursor()
2250
2251
        cursor.execute(" SELECT name "
2252
                       " FROM tbl_spaces "
2253
                       " WHERE id = %s ", (id_,))
2254
        if cursor.fetchone() is None:
2255
            cursor.close()
2256
            cnx.close()
2257
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2258
                                   description='API.SPACE_NOT_FOUND')
2259
2260
        query = (" SELECT t.id, t.name, t.uuid "
2261
                 " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t "
2262
                 " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s "
2263
                 " ORDER BY t.id ")
2264
        cursor.execute(query, (id_,))
2265
        rows = cursor.fetchall()
2266
2267
        result = list()
2268
        if rows is not None and len(rows) > 0:
2269
            for row in rows:
2270
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2271
                result.append(meta_result)
2272
2273
        resp.text = json.dumps(result)
2274
2275
    @staticmethod
2276
    @user_logger
2277
    def on_post(req, resp, id_):
2278
        """Handles POST requests"""
2279
        access_control(req)
2280
        try:
2281
            raw_json = req.stream.read().decode('utf-8')
2282
        except Exception as ex:
2283
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2284
2285
        if not id_.isdigit() or int(id_) <= 0:
2286
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2287
                                   description='API.INVALID_SPACE_ID')
2288
2289
        new_values = json.loads(raw_json)
2290
2291
        if 'tenant_id' not in new_values['data'].keys() or \
2292
                not isinstance(new_values['data']['tenant_id'], int) or \
2293
                new_values['data']['tenant_id'] <= 0:
2294
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2295
                                   description='API.INVALID_TENANT_ID')
2296
        tenant_id = new_values['data']['tenant_id']
2297
2298
        cnx = mysql.connector.connect(**config.myems_system_db)
2299
        cursor = cnx.cursor()
2300
2301
        cursor.execute(" SELECT name "
2302
                       " from tbl_spaces "
2303
                       " WHERE id = %s ", (id_,))
2304
        if cursor.fetchone() is None:
2305
            cursor.close()
2306
            cnx.close()
2307
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2308
                                   description='API.SPACE_NOT_FOUND')
2309
2310
        cursor.execute(" SELECT name "
2311
                       " FROM tbl_tenants "
2312
                       " WHERE id = %s ", (tenant_id,))
2313
        if cursor.fetchone() is None:
2314
            cursor.close()
2315
            cnx.close()
2316
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2317
                                   description='API.TENANT_NOT_FOUND')
2318
2319
        query = (" SELECT id " 
2320
                 " FROM tbl_spaces_tenants "
2321
                 " WHERE space_id = %s AND tenant_id = %s")
2322
        cursor.execute(query, (id_, tenant_id,))
2323
        if cursor.fetchone() is not None:
2324
            cursor.close()
2325
            cnx.close()
2326
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2327
                                   description='API.SPACE_TENANT_RELATION_EXISTS')
2328
2329
        add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) "
2330
                   " VALUES (%s, %s) ")
2331
        cursor.execute(add_row, (id_, tenant_id,))
2332
        new_id = cursor.lastrowid
2333
        cnx.commit()
2334
        cursor.close()
2335
        cnx.close()
2336
2337
        resp.status = falcon.HTTP_201
2338
        resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id)
2339
2340
2341
class SpaceTenantItem:
@@ 2062-2168 (lines=107) @@
2059
        resp.status = falcon.HTTP_204
2060
2061
2062
class SpaceStoreCollection:
2063
    @staticmethod
2064
    def __init__():
2065
        """Initializes Class"""
2066
        pass
2067
2068
    @staticmethod
2069
    def on_options(req, resp, id_):
2070
        resp.status = falcon.HTTP_200
2071
2072
    @staticmethod
2073
    def on_get(req, resp, id_):
2074
        if not id_.isdigit() or int(id_) <= 0:
2075
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2076
                                   description='API.INVALID_SPACE_ID')
2077
2078
        cnx = mysql.connector.connect(**config.myems_system_db)
2079
        cursor = cnx.cursor()
2080
2081
        cursor.execute(" SELECT name "
2082
                       " FROM tbl_spaces "
2083
                       " WHERE id = %s ", (id_,))
2084
        if cursor.fetchone() is None:
2085
            cursor.close()
2086
            cnx.close()
2087
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2088
                                   description='API.SPACE_NOT_FOUND')
2089
2090
        query = (" SELECT t.id, t.name, t.uuid "
2091
                 " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t "
2092
                 " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s "
2093
                 " ORDER BY t.id ")
2094
        cursor.execute(query, (id_,))
2095
        rows = cursor.fetchall()
2096
2097
        result = list()
2098
        if rows is not None and len(rows) > 0:
2099
            for row in rows:
2100
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2101
                result.append(meta_result)
2102
2103
        resp.text = json.dumps(result)
2104
2105
    @staticmethod
2106
    @user_logger
2107
    def on_post(req, resp, id_):
2108
        """Handles POST requests"""
2109
        access_control(req)
2110
        try:
2111
            raw_json = req.stream.read().decode('utf-8')
2112
        except Exception as ex:
2113
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2114
2115
        if not id_.isdigit() or int(id_) <= 0:
2116
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2117
                                   description='API.INVALID_SPACE_ID')
2118
2119
        new_values = json.loads(raw_json)
2120
2121
        if 'store_id' not in new_values['data'].keys() or \
2122
                not isinstance(new_values['data']['store_id'], int) or \
2123
                new_values['data']['store_id'] <= 0:
2124
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2125
                                   description='API.INVALID_STORE_ID')
2126
        store_id = new_values['data']['store_id']
2127
2128
        cnx = mysql.connector.connect(**config.myems_system_db)
2129
        cursor = cnx.cursor()
2130
2131
        cursor.execute(" SELECT name "
2132
                       " from tbl_spaces "
2133
                       " WHERE id = %s ", (id_,))
2134
        if cursor.fetchone() is None:
2135
            cursor.close()
2136
            cnx.close()
2137
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2138
                                   description='API.SPACE_NOT_FOUND')
2139
2140
        cursor.execute(" SELECT name "
2141
                       " FROM tbl_stores "
2142
                       " WHERE id = %s ", (store_id,))
2143
        if cursor.fetchone() is None:
2144
            cursor.close()
2145
            cnx.close()
2146
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2147
                                   description='API.STORE_NOT_FOUND')
2148
2149
        query = (" SELECT id " 
2150
                 " FROM tbl_spaces_stores "
2151
                 " WHERE space_id = %s AND store_id = %s")
2152
        cursor.execute(query, (id_, store_id,))
2153
        if cursor.fetchone() is not None:
2154
            cursor.close()
2155
            cnx.close()
2156
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2157
                                   description='API.SPACE_STORE_RELATION_EXISTS')
2158
2159
        add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) "
2160
                   " VALUES (%s, %s) ")
2161
        cursor.execute(add_row, (id_, store_id,))
2162
        new_id = cursor.lastrowid
2163
        cnx.commit()
2164
        cursor.close()
2165
        cnx.close()
2166
2167
        resp.status = falcon.HTTP_201
2168
        resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id)
2169
2170
2171
class SpaceStoreItem:
@@ 1892-1998 (lines=107) @@
1889
        resp.status = falcon.HTTP_204
1890
1891
1892
class SpaceShopfloorCollection:
1893
    @staticmethod
1894
    def __init__():
1895
        """Initializes Class"""
1896
        pass
1897
1898
    @staticmethod
1899
    def on_options(req, resp, id_):
1900
        resp.status = falcon.HTTP_200
1901
1902
    @staticmethod
1903
    def on_get(req, resp, id_):
1904
        if not id_.isdigit() or int(id_) <= 0:
1905
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1906
                                   description='API.INVALID_SPACE_ID')
1907
1908
        cnx = mysql.connector.connect(**config.myems_system_db)
1909
        cursor = cnx.cursor()
1910
1911
        cursor.execute(" SELECT name "
1912
                       " FROM tbl_spaces "
1913
                       " WHERE id = %s ", (id_,))
1914
        if cursor.fetchone() is None:
1915
            cursor.close()
1916
            cnx.close()
1917
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1918
                                   description='API.SPACE_NOT_FOUND')
1919
1920
        query = (" SELECT sf.id, sf.name, sf.uuid "
1921
                 " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf "
1922
                 " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s "
1923
                 " ORDER BY sf.id ")
1924
        cursor.execute(query, (id_,))
1925
        rows = cursor.fetchall()
1926
1927
        result = list()
1928
        if rows is not None and len(rows) > 0:
1929
            for row in rows:
1930
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1931
                result.append(meta_result)
1932
1933
        resp.text = json.dumps(result)
1934
1935
    @staticmethod
1936
    @user_logger
1937
    def on_post(req, resp, id_):
1938
        """Handles POST requests"""
1939
        access_control(req)
1940
        try:
1941
            raw_json = req.stream.read().decode('utf-8')
1942
        except Exception as ex:
1943
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1944
1945
        if not id_.isdigit() or int(id_) <= 0:
1946
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1947
                                   description='API.INVALID_SPACE_ID')
1948
1949
        new_values = json.loads(raw_json)
1950
1951
        if 'shopfloor_id' not in new_values['data'].keys() or \
1952
                not isinstance(new_values['data']['shopfloor_id'], int) or \
1953
                new_values['data']['shopfloor_id'] <= 0:
1954
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1955
                                   description='API.INVALID_SHOPFLOOR_ID')
1956
        shopfloor_id = new_values['data']['shopfloor_id']
1957
1958
        cnx = mysql.connector.connect(**config.myems_system_db)
1959
        cursor = cnx.cursor()
1960
1961
        cursor.execute(" SELECT name "
1962
                       " from tbl_spaces "
1963
                       " WHERE id = %s ", (id_,))
1964
        if cursor.fetchone() is None:
1965
            cursor.close()
1966
            cnx.close()
1967
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1968
                                   description='API.SPACE_NOT_FOUND')
1969
1970
        cursor.execute(" SELECT name "
1971
                       " FROM tbl_shopfloors "
1972
                       " WHERE id = %s ", (shopfloor_id,))
1973
        if cursor.fetchone() is None:
1974
            cursor.close()
1975
            cnx.close()
1976
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1977
                                   description='API.SHOPFLOOR_NOT_FOUND')
1978
1979
        query = (" SELECT id " 
1980
                 " FROM tbl_spaces_shopfloors "
1981
                 " WHERE space_id = %s AND shopfloor_id = %s")
1982
        cursor.execute(query, (id_, shopfloor_id,))
1983
        if cursor.fetchone() is not None:
1984
            cursor.close()
1985
            cnx.close()
1986
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1987
                                   description='API.SPACE_SHOPFLOOR_RELATION_EXISTS')
1988
1989
        add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) "
1990
                   " VALUES (%s, %s) ")
1991
        cursor.execute(add_row, (id_, shopfloor_id,))
1992
        new_id = cursor.lastrowid
1993
        cnx.commit()
1994
        cursor.close()
1995
        cnx.close()
1996
1997
        resp.status = falcon.HTTP_201
1998
        resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id)
1999
2000
2001
class SpaceShopfloorItem:
@@ 1722-1828 (lines=107) @@
1719
        resp.status = falcon.HTTP_204
1720
1721
1722
class SpaceSensorCollection:
1723
    @staticmethod
1724
    def __init__():
1725
        """Initializes Class"""
1726
        pass
1727
1728
    @staticmethod
1729
    def on_options(req, resp, id_):
1730
        resp.status = falcon.HTTP_200
1731
1732
    @staticmethod
1733
    def on_get(req, resp, id_):
1734
        if not id_.isdigit() or int(id_) <= 0:
1735
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1736
                                   description='API.INVALID_SPACE_ID')
1737
1738
        cnx = mysql.connector.connect(**config.myems_system_db)
1739
        cursor = cnx.cursor()
1740
1741
        cursor.execute(" SELECT name "
1742
                       " FROM tbl_spaces "
1743
                       " WHERE id = %s ", (id_,))
1744
        if cursor.fetchone() is None:
1745
            cursor.close()
1746
            cnx.close()
1747
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1748
                                   description='API.SPACE_NOT_FOUND')
1749
1750
        query = (" SELECT se.id, se.name, se.uuid "
1751
                 " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se "
1752
                 " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
1753
                 " ORDER BY se.id ")
1754
        cursor.execute(query, (id_,))
1755
        rows = cursor.fetchall()
1756
1757
        result = list()
1758
        if rows is not None and len(rows) > 0:
1759
            for row in rows:
1760
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1761
                result.append(meta_result)
1762
1763
        resp.text = json.dumps(result)
1764
1765
    @staticmethod
1766
    @user_logger
1767
    def on_post(req, resp, id_):
1768
        """Handles POST requests"""
1769
        access_control(req)
1770
        try:
1771
            raw_json = req.stream.read().decode('utf-8')
1772
        except Exception as ex:
1773
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1774
1775
        if not id_.isdigit() or int(id_) <= 0:
1776
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1777
                                   description='API.INVALID_SPACE_ID')
1778
1779
        new_values = json.loads(raw_json)
1780
1781
        if 'sensor_id' not in new_values['data'].keys() or \
1782
                not isinstance(new_values['data']['sensor_id'], int) or \
1783
                new_values['data']['sensor_id'] <= 0:
1784
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1785
                                   description='API.INVALID_SENSOR_ID')
1786
        sensor_id = new_values['data']['sensor_id']
1787
1788
        cnx = mysql.connector.connect(**config.myems_system_db)
1789
        cursor = cnx.cursor()
1790
1791
        cursor.execute(" SELECT name "
1792
                       " from tbl_spaces "
1793
                       " WHERE id = %s ", (id_,))
1794
        if cursor.fetchone() is None:
1795
            cursor.close()
1796
            cnx.close()
1797
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1798
                                   description='API.SPACE_NOT_FOUND')
1799
1800
        cursor.execute(" SELECT name "
1801
                       " FROM tbl_sensors "
1802
                       " WHERE id = %s ", (sensor_id,))
1803
        if cursor.fetchone() is None:
1804
            cursor.close()
1805
            cnx.close()
1806
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1807
                                   description='API.SENSOR_NOT_FOUND')
1808
1809
        query = (" SELECT id " 
1810
                 " FROM tbl_spaces_sensors "
1811
                 " WHERE space_id = %s AND sensor_id = %s")
1812
        cursor.execute(query, (id_, sensor_id,))
1813
        if cursor.fetchone() is not None:
1814
            cursor.close()
1815
            cnx.close()
1816
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1817
                                   description='API.SPACE_SENSOR_RELATION_EXISTS')
1818
1819
        add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) "
1820
                   " VALUES (%s, %s) ")
1821
        cursor.execute(add_row, (id_, sensor_id,))
1822
        new_id = cursor.lastrowid
1823
        cnx.commit()
1824
        cursor.close()
1825
        cnx.close()
1826
1827
        resp.status = falcon.HTTP_201
1828
        resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id)
1829
1830
1831
class SpaceSensorItem:
@@ 997-1103 (lines=107) @@
994
        resp.status = falcon.HTTP_204
995
996
997
class SpaceEquipmentCollection:
998
    @staticmethod
999
    def __init__():
1000
        """Initializes Class"""
1001
        pass
1002
1003
    @staticmethod
1004
    def on_options(req, resp, id_):
1005
        resp.status = falcon.HTTP_200
1006
1007
    @staticmethod
1008
    def on_get(req, resp, id_):
1009
        if not id_.isdigit() or int(id_) <= 0:
1010
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1011
                                   description='API.INVALID_SPACE_ID')
1012
1013
        cnx = mysql.connector.connect(**config.myems_system_db)
1014
        cursor = cnx.cursor()
1015
1016
        cursor.execute(" SELECT name "
1017
                       " FROM tbl_spaces "
1018
                       " WHERE id = %s ", (id_,))
1019
        if cursor.fetchone() is None:
1020
            cursor.close()
1021
            cnx.close()
1022
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1023
                                   description='API.SPACE_NOT_FOUND')
1024
1025
        query = (" SELECT e.id, e.name, e.uuid "
1026
                 " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e "
1027
                 " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s "
1028
                 " ORDER BY e.id ")
1029
        cursor.execute(query, (id_,))
1030
        rows = cursor.fetchall()
1031
1032
        result = list()
1033
        if rows is not None and len(rows) > 0:
1034
            for row in rows:
1035
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1036
                result.append(meta_result)
1037
1038
        resp.text = json.dumps(result)
1039
1040
    @staticmethod
1041
    @user_logger
1042
    def on_post(req, resp, id_):
1043
        """Handles POST requests"""
1044
        access_control(req)
1045
        try:
1046
            raw_json = req.stream.read().decode('utf-8')
1047
        except Exception as ex:
1048
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1049
1050
        if not id_.isdigit() or int(id_) <= 0:
1051
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1052
                                   description='API.INVALID_SPACE_ID')
1053
1054
        new_values = json.loads(raw_json)
1055
1056
        if 'equipment_id' not in new_values['data'].keys() or \
1057
                not isinstance(new_values['data']['equipment_id'], int) or \
1058
                new_values['data']['equipment_id'] <= 0:
1059
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1060
                                   description='API.INVALID_EQUIPMENT_ID')
1061
        equipment_id = new_values['data']['equipment_id']
1062
1063
        cnx = mysql.connector.connect(**config.myems_system_db)
1064
        cursor = cnx.cursor()
1065
1066
        cursor.execute(" SELECT name "
1067
                       " from tbl_spaces "
1068
                       " WHERE id = %s ", (id_,))
1069
        if cursor.fetchone() is None:
1070
            cursor.close()
1071
            cnx.close()
1072
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1073
                                   description='API.SPACE_NOT_FOUND')
1074
1075
        cursor.execute(" SELECT name "
1076
                       " FROM tbl_equipments "
1077
                       " WHERE id = %s ", (equipment_id,))
1078
        if cursor.fetchone() is None:
1079
            cursor.close()
1080
            cnx.close()
1081
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1082
                                   description='API.EQUIPMENT_NOT_FOUND')
1083
1084
        query = (" SELECT id " 
1085
                 " FROM tbl_spaces_equipments "
1086
                 " WHERE space_id = %s AND equipment_id = %s")
1087
        cursor.execute(query, (id_, equipment_id,))
1088
        if cursor.fetchone() is not None:
1089
            cursor.close()
1090
            cnx.close()
1091
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1092
                                   description='API.SPACE_EQUIPMENT_RELATION_EXISTS')
1093
1094
        add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) "
1095
                   " VALUES (%s, %s) ")
1096
        cursor.execute(add_row, (id_, equipment_id,))
1097
        new_id = cursor.lastrowid
1098
        cnx.commit()
1099
        cursor.close()
1100
        cnx.close()
1101
1102
        resp.status = falcon.HTTP_201
1103
        resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id)
1104
1105
1106
class SpaceEquipmentItem:
@@ 826-932 (lines=107) @@
823
        resp.text = json.dumps(result)
824
825
826
class SpaceCombinedEquipmentCollection:
827
    @staticmethod
828
    def __init__():
829
        """Initializes Class"""
830
        pass
831
832
    @staticmethod
833
    def on_options(req, resp, id_):
834
        resp.status = falcon.HTTP_200
835
836
    @staticmethod
837
    def on_get(req, resp, id_):
838
        if not id_.isdigit() or int(id_) <= 0:
839
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
840
                                   description='API.INVALID_SPACE_ID')
841
842
        cnx = mysql.connector.connect(**config.myems_system_db)
843
        cursor = cnx.cursor()
844
845
        cursor.execute(" SELECT name "
846
                       " FROM tbl_spaces "
847
                       " WHERE id = %s ", (id_,))
848
        if cursor.fetchone() is None:
849
            cursor.close()
850
            cnx.close()
851
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
852
                                   description='API.SPACE_NOT_FOUND')
853
854
        query = (" SELECT e.id, e.name, e.uuid "
855
                 " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e "
856
                 " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s "
857
                 " ORDER BY e.id ")
858
        cursor.execute(query, (id_,))
859
        rows = cursor.fetchall()
860
861
        result = list()
862
        if rows is not None and len(rows) > 0:
863
            for row in rows:
864
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
865
                result.append(meta_result)
866
867
        resp.text = json.dumps(result)
868
869
    @staticmethod
870
    @user_logger
871
    def on_post(req, resp, id_):
872
        """Handles POST requests"""
873
        access_control(req)
874
        try:
875
            raw_json = req.stream.read().decode('utf-8')
876
        except Exception as ex:
877
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
878
879
        if not id_.isdigit() or int(id_) <= 0:
880
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
881
                                   description='API.INVALID_SPACE_ID')
882
883
        new_values = json.loads(raw_json)
884
885
        if 'combined_equipment_id' not in new_values['data'].keys() or \
886
                not isinstance(new_values['data']['combined_equipment_id'], int) or \
887
                new_values['data']['combined_equipment_id'] <= 0:
888
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
889
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
890
        combined_equipment_id = new_values['data']['combined_equipment_id']
891
892
        cnx = mysql.connector.connect(**config.myems_system_db)
893
        cursor = cnx.cursor()
894
895
        cursor.execute(" SELECT name "
896
                       " from tbl_spaces "
897
                       " WHERE id = %s ", (id_,))
898
        if cursor.fetchone() is None:
899
            cursor.close()
900
            cnx.close()
901
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
902
                                   description='API.SPACE_NOT_FOUND')
903
904
        cursor.execute(" SELECT name "
905
                       " FROM tbl_combined_equipments "
906
                       " WHERE id = %s ", (combined_equipment_id,))
907
        if cursor.fetchone() is None:
908
            cursor.close()
909
            cnx.close()
910
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
911
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
912
913
        query = (" SELECT id " 
914
                 " FROM tbl_spaces_combined_equipments "
915
                 " WHERE space_id = %s AND combined_equipment_id = %s")
916
        cursor.execute(query, (id_, combined_equipment_id,))
917
        if cursor.fetchone() is not None:
918
            cursor.close()
919
            cnx.close()
920
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
921
                                   description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTS')
922
923
        add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) "
924
                   " VALUES (%s, %s) ")
925
        cursor.execute(add_row, (id_, combined_equipment_id,))
926
        new_id = cursor.lastrowid
927
        cnx.commit()
928
        cursor.close()
929
        cnx.close()
930
931
        resp.status = falcon.HTTP_201
932
        resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id)
933
934
935
class SpaceCombinedEquipmentItem:

myems-api/core/tenant.py 1 location

@@ 1267-1373 (lines=107) @@
1264
        resp.status = falcon.HTTP_204
1265
1266
1267
class TenantSensorCollection:
1268
    @staticmethod
1269
    def __init__():
1270
        """Initializes Class"""
1271
        pass
1272
1273
    @staticmethod
1274
    def on_options(req, resp, id_):
1275
        resp.status = falcon.HTTP_200
1276
1277
    @staticmethod
1278
    def on_get(req, resp, id_):
1279
        if not id_.isdigit() or int(id_) <= 0:
1280
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1281
                                   description='API.INVALID_TENANT_ID')
1282
1283
        cnx = mysql.connector.connect(**config.myems_system_db)
1284
        cursor = cnx.cursor()
1285
1286
        cursor.execute(" SELECT name "
1287
                       " FROM tbl_tenants "
1288
                       " WHERE id = %s ", (id_,))
1289
        if cursor.fetchone() is None:
1290
            cursor.close()
1291
            cnx.close()
1292
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1293
                                   description='API.TENANT_NOT_FOUND')
1294
1295
        query = (" SELECT s.id, s.name, s.uuid "
1296
                 " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s "
1297
                 " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1298
                 " ORDER BY s.id ")
1299
        cursor.execute(query, (id_,))
1300
        rows = cursor.fetchall()
1301
1302
        result = list()
1303
        if rows is not None and len(rows) > 0:
1304
            for row in rows:
1305
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1306
                result.append(meta_result)
1307
1308
        resp.text = json.dumps(result)
1309
1310
    @staticmethod
1311
    @user_logger
1312
    def on_post(req, resp, id_):
1313
        """Handles POST requests"""
1314
        access_control(req)
1315
        try:
1316
            raw_json = req.stream.read().decode('utf-8')
1317
        except Exception as ex:
1318
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1319
1320
        if not id_.isdigit() or int(id_) <= 0:
1321
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1322
                                   description='API.INVALID_TENANT_ID')
1323
1324
        new_values = json.loads(raw_json)
1325
1326
        if 'sensor_id' not in new_values['data'].keys() or \
1327
                not isinstance(new_values['data']['sensor_id'], int) or \
1328
                new_values['data']['sensor_id'] <= 0:
1329
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1330
                                   description='API.INVALID_SENSOR_ID')
1331
        sensor_id = new_values['data']['sensor_id']
1332
1333
        cnx = mysql.connector.connect(**config.myems_system_db)
1334
        cursor = cnx.cursor()
1335
1336
        cursor.execute(" SELECT name "
1337
                       " from tbl_tenants "
1338
                       " WHERE id = %s ", (id_,))
1339
        if cursor.fetchone() is None:
1340
            cursor.close()
1341
            cnx.close()
1342
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1343
                                   description='API.TENANT_NOT_FOUND')
1344
1345
        cursor.execute(" SELECT name "
1346
                       " FROM tbl_sensors "
1347
                       " WHERE id = %s ", (sensor_id,))
1348
        if cursor.fetchone() is None:
1349
            cursor.close()
1350
            cnx.close()
1351
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1352
                                   description='API.SENSOR_NOT_FOUND')
1353
1354
        query = (" SELECT id " 
1355
                 " FROM tbl_tenants_sensors "
1356
                 " WHERE tenant_id = %s AND sensor_id = %s")
1357
        cursor.execute(query, (id_, sensor_id,))
1358
        if cursor.fetchone() is not None:
1359
            cursor.close()
1360
            cnx.close()
1361
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1362
                                   description='API.TENANT_SENSOR_RELATION_EXISTS')
1363
1364
        add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) "
1365
                   " VALUES (%s, %s) ")
1366
        cursor.execute(add_row, (id_, sensor_id,))
1367
        new_id = cursor.lastrowid
1368
        cnx.commit()
1369
        cursor.close()
1370
        cnx.close()
1371
1372
        resp.status = falcon.HTTP_201
1373
        resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id)
1374
1375
1376
class TenantSensorItem:

myems-api/core/shopfloor.py 2 locations

@@ 1227-1333 (lines=107) @@
1224
        resp.status = falcon.HTTP_204
1225
1226
1227
class ShopfloorSensorCollection:
1228
    @staticmethod
1229
    def __init__():
1230
        """Initializes ShopfloorSensorCollection"""
1231
        pass
1232
1233
    @staticmethod
1234
    def on_options(req, resp, id_):
1235
        resp.status = falcon.HTTP_200
1236
1237
    @staticmethod
1238
    def on_get(req, resp, id_):
1239
        if not id_.isdigit() or int(id_) <= 0:
1240
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1241
                                   description='API.INVALID_SHOPFLOOR_ID')
1242
1243
        cnx = mysql.connector.connect(**config.myems_system_db)
1244
        cursor = cnx.cursor()
1245
1246
        cursor.execute(" SELECT name "
1247
                       " FROM tbl_shopfloors "
1248
                       " WHERE id = %s ", (id_,))
1249
        if cursor.fetchone() is None:
1250
            cursor.close()
1251
            cnx.close()
1252
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1253
                                   description='API.SHOPFLOOR_NOT_FOUND')
1254
1255
        query = (" SELECT se.id, se.name, se.uuid "
1256
                 " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se "
1257
                 " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
1258
                 " ORDER BY se.id ")
1259
        cursor.execute(query, (id_,))
1260
        rows = cursor.fetchall()
1261
1262
        result = list()
1263
        if rows is not None and len(rows) > 0:
1264
            for row in rows:
1265
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1266
                result.append(meta_result)
1267
1268
        resp.text = json.dumps(result)
1269
1270
    @staticmethod
1271
    @user_logger
1272
    def on_post(req, resp, id_):
1273
        """Handles POST requests"""
1274
        access_control(req)
1275
        try:
1276
            raw_json = req.stream.read().decode('utf-8')
1277
        except Exception as ex:
1278
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1279
1280
        if not id_.isdigit() or int(id_) <= 0:
1281
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1282
                                   description='API.INVALID_SHOPFLOOR_ID')
1283
1284
        new_values = json.loads(raw_json)
1285
1286
        if 'sensor_id' not in new_values['data'].keys() or \
1287
                not isinstance(new_values['data']['sensor_id'], int) or \
1288
                new_values['data']['sensor_id'] <= 0:
1289
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1290
                                   description='API.INVALID_SENSOR_ID')
1291
        sensor_id = new_values['data']['sensor_id']
1292
1293
        cnx = mysql.connector.connect(**config.myems_system_db)
1294
        cursor = cnx.cursor()
1295
1296
        cursor.execute(" SELECT name "
1297
                       " from tbl_shopfloors "
1298
                       " WHERE id = %s ", (id_,))
1299
        if cursor.fetchone() is None:
1300
            cursor.close()
1301
            cnx.close()
1302
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1303
                                   description='API.SHOPFLOOR_NOT_FOUND')
1304
1305
        cursor.execute(" SELECT name "
1306
                       " FROM tbl_sensors "
1307
                       " WHERE id = %s ", (sensor_id,))
1308
        if cursor.fetchone() is None:
1309
            cursor.close()
1310
            cnx.close()
1311
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1312
                                   description='API.SENSOR_NOT_FOUND')
1313
1314
        query = (" SELECT id " 
1315
                 " FROM tbl_shopfloors_sensors "
1316
                 " WHERE shopfloor_id = %s AND sensor_id = %s")
1317
        cursor.execute(query, (id_, sensor_id,))
1318
        if cursor.fetchone() is not None:
1319
            cursor.close()
1320
            cnx.close()
1321
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1322
                                   description='API.SHOPFLOOR_SENSOR_RELATION_EXISTS')
1323
1324
        add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) "
1325
                   " VALUES (%s, %s) ")
1326
        cursor.execute(add_row, (id_, sensor_id,))
1327
        new_id = cursor.lastrowid
1328
        cnx.commit()
1329
        cursor.close()
1330
        cnx.close()
1331
1332
        resp.status = falcon.HTTP_201
1333
        resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id)
1334
1335
1336
class ShopfloorSensorItem:
@@ 503-609 (lines=107) @@
500
        resp.status = falcon.HTTP_200
501
502
503
class ShopfloorEquipmentCollection:
504
    @staticmethod
505
    def __init__():
506
        """Initializes ShopfloorEquipmentCollection"""
507
        pass
508
509
    @staticmethod
510
    def on_options(req, resp, id_):
511
        resp.status = falcon.HTTP_200
512
513
    @staticmethod
514
    def on_get(req, resp, id_):
515
        if not id_.isdigit() or int(id_) <= 0:
516
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
517
                                   description='API.INVALID_SHOPFLOOR_ID')
518
519
        cnx = mysql.connector.connect(**config.myems_system_db)
520
        cursor = cnx.cursor()
521
522
        cursor.execute(" SELECT name "
523
                       " FROM tbl_shopfloors "
524
                       " WHERE id = %s ", (id_,))
525
        if cursor.fetchone() is None:
526
            cursor.close()
527
            cnx.close()
528
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
529
                                   description='API.SHOPFLOOR_NOT_FOUND')
530
531
        query = (" SELECT e.id, e.name, e.uuid "
532
                 " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e "
533
                 " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s "
534
                 " ORDER BY e.id ")
535
        cursor.execute(query, (id_,))
536
        rows = cursor.fetchall()
537
538
        result = list()
539
        if rows is not None and len(rows) > 0:
540
            for row in rows:
541
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
542
                result.append(meta_result)
543
544
        resp.text = json.dumps(result)
545
546
    @staticmethod
547
    @user_logger
548
    def on_post(req, resp, id_):
549
        """Handles POST requests"""
550
        access_control(req)
551
        try:
552
            raw_json = req.stream.read().decode('utf-8')
553
        except Exception as ex:
554
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
555
556
        if not id_.isdigit() or int(id_) <= 0:
557
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
558
                                   description='API.INVALID_SHOPFLOOR_ID')
559
560
        new_values = json.loads(raw_json)
561
562
        if 'equipment_id' not in new_values['data'].keys() or \
563
                not isinstance(new_values['data']['equipment_id'], int) or \
564
                new_values['data']['equipment_id'] <= 0:
565
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
566
                                   description='API.INVALID_EQUIPMENT_ID')
567
        equipment_id = new_values['data']['equipment_id']
568
569
        cnx = mysql.connector.connect(**config.myems_system_db)
570
        cursor = cnx.cursor()
571
572
        cursor.execute(" SELECT name "
573
                       " from tbl_shopfloors "
574
                       " WHERE id = %s ", (id_,))
575
        if cursor.fetchone() is None:
576
            cursor.close()
577
            cnx.close()
578
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
579
                                   description='API.SHOPFLOOR_NOT_FOUND')
580
581
        cursor.execute(" SELECT name "
582
                       " FROM tbl_equipments "
583
                       " WHERE id = %s ", (equipment_id,))
584
        if cursor.fetchone() is None:
585
            cursor.close()
586
            cnx.close()
587
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
588
                                   description='API.EQUIPMENT_NOT_FOUND')
589
590
        query = (" SELECT id " 
591
                 " FROM tbl_shopfloors_equipments "
592
                 " WHERE shopfloor_id = %s AND equipment_id = %s")
593
        cursor.execute(query, (id_, equipment_id,))
594
        if cursor.fetchone() is not None:
595
            cursor.close()
596
            cnx.close()
597
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
598
                                   description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTS')
599
600
        add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) "
601
                   " VALUES (%s, %s) ")
602
        cursor.execute(add_row, (id_, equipment_id,))
603
        new_id = cursor.lastrowid
604
        cnx.commit()
605
        cursor.close()
606
        cnx.close()
607
608
        resp.status = falcon.HTTP_201
609
        resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id)
610
611
612
class ShopfloorEquipmentItem:

myems-api/core/store.py 1 location

@@ 1164-1270 (lines=107) @@
1161
        resp.status = falcon.HTTP_204
1162
1163
1164
class StoreSensorCollection:
1165
    @staticmethod
1166
    def __init__():
1167
        """Initializes Class"""
1168
        pass
1169
1170
    @staticmethod
1171
    def on_options(req, resp, id_):
1172
        resp.status = falcon.HTTP_200
1173
1174
    @staticmethod
1175
    def on_get(req, resp, id_):
1176
        if not id_.isdigit() or int(id_) <= 0:
1177
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1178
                                   description='API.INVALID_STORE_ID')
1179
1180
        cnx = mysql.connector.connect(**config.myems_system_db)
1181
        cursor = cnx.cursor()
1182
1183
        cursor.execute(" SELECT name "
1184
                       " FROM tbl_stores "
1185
                       " WHERE id = %s ", (id_,))
1186
        if cursor.fetchone() is None:
1187
            cursor.close()
1188
            cnx.close()
1189
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1190
                                   description='API.STORE_NOT_FOUND')
1191
1192
        query = (" SELECT s.id, s.name, s.uuid "
1193
                 " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s "
1194
                 " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1195
                 " ORDER BY s.id ")
1196
        cursor.execute(query, (id_,))
1197
        rows = cursor.fetchall()
1198
1199
        result = list()
1200
        if rows is not None and len(rows) > 0:
1201
            for row in rows:
1202
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1203
                result.append(meta_result)
1204
1205
        resp.text = json.dumps(result)
1206
1207
    @staticmethod
1208
    @user_logger
1209
    def on_post(req, resp, id_):
1210
        """Handles POST requests"""
1211
        access_control(req)
1212
        try:
1213
            raw_json = req.stream.read().decode('utf-8')
1214
        except Exception as ex:
1215
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1216
1217
        if not id_.isdigit() or int(id_) <= 0:
1218
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1219
                                   description='API.INVALID_STORE_ID')
1220
1221
        new_values = json.loads(raw_json)
1222
1223
        if 'sensor_id' not in new_values['data'].keys() or \
1224
                not isinstance(new_values['data']['sensor_id'], int) or \
1225
                new_values['data']['sensor_id'] <= 0:
1226
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1227
                                   description='API.INVALID_SENSOR_ID')
1228
        sensor_id = new_values['data']['sensor_id']
1229
1230
        cnx = mysql.connector.connect(**config.myems_system_db)
1231
        cursor = cnx.cursor()
1232
1233
        cursor.execute(" SELECT name "
1234
                       " from tbl_stores "
1235
                       " WHERE id = %s ", (id_,))
1236
        if cursor.fetchone() is None:
1237
            cursor.close()
1238
            cnx.close()
1239
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1240
                                   description='API.STORE_NOT_FOUND')
1241
1242
        cursor.execute(" SELECT name "
1243
                       " FROM tbl_sensors "
1244
                       " WHERE id = %s ", (sensor_id,))
1245
        if cursor.fetchone() is None:
1246
            cursor.close()
1247
            cnx.close()
1248
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1249
                                   description='API.SENSOR_NOT_FOUND')
1250
1251
        query = (" SELECT id " 
1252
                 " FROM tbl_stores_sensors "
1253
                 " WHERE store_id = %s AND sensor_id = %s")
1254
        cursor.execute(query, (id_, sensor_id,))
1255
        if cursor.fetchone() is not None:
1256
            cursor.close()
1257
            cnx.close()
1258
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1259
                                   description='API.STORE_SENSOR_RELATION_EXISTS')
1260
1261
        add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) "
1262
                   " VALUES (%s, %s) ")
1263
        cursor.execute(add_row, (id_, sensor_id,))
1264
        new_id = cursor.lastrowid
1265
        cnx.commit()
1266
        cursor.close()
1267
        cnx.close()
1268
1269
        resp.status = falcon.HTTP_201
1270
        resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id)
1271
1272
1273
class StoreSensorItem:

myems-api/core/combinedequipment.py 1 location

@@ 528-634 (lines=107) @@
525
        resp.location = '/combinedequipments/' + str(new_id)
526
527
528
class CombinedEquipmentEquipmentCollection:
529
    @staticmethod
530
    def __init__():
531
        """Initializes CombinedEquipmentEquipmentCollection"""
532
        pass
533
534
    @staticmethod
535
    def on_options(req, resp, id_):
536
        resp.status = falcon.HTTP_200
537
538
    @staticmethod
539
    def on_get(req, resp, id_):
540
        if not id_.isdigit() or int(id_) <= 0:
541
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
542
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
543
544
        cnx = mysql.connector.connect(**config.myems_system_db)
545
        cursor = cnx.cursor()
546
547
        cursor.execute(" SELECT name "
548
                       " FROM tbl_combined_equipments "
549
                       " WHERE id = %s ", (id_,))
550
        if cursor.fetchone() is None:
551
            cursor.close()
552
            cnx.close()
553
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
554
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
555
556
        query = (" SELECT e.id, e.name, e.uuid "
557
                 " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e "
558
                 " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s "
559
                 " ORDER BY e.id ")
560
        cursor.execute(query, (id_,))
561
        rows = cursor.fetchall()
562
563
        result = list()
564
        if rows is not None and len(rows) > 0:
565
            for row in rows:
566
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
567
                result.append(meta_result)
568
569
        resp.text = json.dumps(result)
570
571
    @staticmethod
572
    @user_logger
573
    def on_post(req, resp, id_):
574
        """Handles POST requests"""
575
        access_control(req)
576
        try:
577
            raw_json = req.stream.read().decode('utf-8')
578
        except Exception as ex:
579
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
580
581
        if not id_.isdigit() or int(id_) <= 0:
582
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
583
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
584
585
        new_values = json.loads(raw_json)
586
587
        if 'equipment_id' not in new_values['data'].keys() or \
588
                not isinstance(new_values['data']['equipment_id'], int) or \
589
                new_values['data']['equipment_id'] <= 0:
590
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
591
                                   description='API.INVALID_EQUIPMENT_ID')
592
        equipment_id = new_values['data']['equipment_id']
593
594
        cnx = mysql.connector.connect(**config.myems_system_db)
595
        cursor = cnx.cursor()
596
597
        cursor.execute(" SELECT name "
598
                       " from tbl_combined_equipments "
599
                       " WHERE id = %s ", (id_,))
600
        if cursor.fetchone() is None:
601
            cursor.close()
602
            cnx.close()
603
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
604
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
605
606
        cursor.execute(" SELECT name "
607
                       " FROM tbl_equipments "
608
                       " WHERE id = %s ", (equipment_id,))
609
        if cursor.fetchone() is None:
610
            cursor.close()
611
            cnx.close()
612
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
613
                                   description='API.EQUIPMENT_NOT_FOUND')
614
615
        query = (" SELECT id " 
616
                 " FROM tbl_combined_equipments_equipments "
617
                 " WHERE combined_equipment_id = %s AND equipment_id = %s")
618
        cursor.execute(query, (id_, equipment_id,))
619
        if cursor.fetchone() is not None:
620
            cursor.close()
621
            cnx.close()
622
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
623
                                   description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTS')
624
625
        add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) "
626
                   " VALUES (%s, %s) ")
627
        cursor.execute(add_row, (id_, equipment_id,))
628
        new_id = cursor.lastrowid
629
        cnx.commit()
630
        cursor.close()
631
        cnx.close()
632
633
        resp.status = falcon.HTTP_201
634
        resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id)
635
636
637
class CombinedEquipmentEquipmentItem:

myems-api/core/energyflowdiagram.py 1 location

@@ 933-1033 (lines=101) @@
930
        resp.status = falcon.HTTP_200
931
932
933
class EnergyFlowDiagramNodeCollection:
934
    @staticmethod
935
    def __init__():
936
        """"Initializes EnergyFlowDiagramNodeCollection"""
937
        pass
938
939
    @staticmethod
940
    def on_options(req, resp, id_):
941
        resp.status = falcon.HTTP_200
942
943
    @staticmethod
944
    def on_get(req, resp, id_):
945
        if not id_.isdigit() or int(id_) <= 0:
946
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
947
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID')
948
949
        cnx = mysql.connector.connect(**config.myems_system_db)
950
        cursor = cnx.cursor(dictionary=True)
951
952
        cursor.execute(" SELECT name "
953
                       " FROM tbl_energy_flow_diagrams "
954
                       " WHERE id = %s ", (id_,))
955
        if cursor.fetchone() is None:
956
            cursor.close()
957
            cnx.close()
958
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
959
                                   description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND')
960
961
        query = (" SELECT id, name "
962
                 " FROM tbl_energy_flow_diagrams_nodes "
963
                 " WHERE energy_flow_diagram_id = %s "
964
                 " ORDER BY id ")
965
        cursor.execute(query, (id_, ))
966
        rows_nodes = cursor.fetchall()
967
968
        result = list()
969
        if rows_nodes is not None and len(rows_nodes) > 0:
970
            for row in rows_nodes:
971
                meta_result = {"id": row['id'],
972
                               "name": row['name']}
973
                result.append(meta_result)
974
975
        cursor.close()
976
        cnx.close()
977
        resp.text = json.dumps(result)
978
979
    @staticmethod
980
    @user_logger
981
    def on_post(req, resp, id_):
982
        """Handles POST requests"""
983
        access_control(req)
984
        if not id_.isdigit() or int(id_) <= 0:
985
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
986
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID')
987
        try:
988
            raw_json = req.stream.read().decode('utf-8')
989
        except Exception as ex:
990
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
991
992
        new_values = json.loads(raw_json)
993
994
        if 'name' not in new_values['data'].keys() or \
995
                not isinstance(new_values['data']['name'], str) or \
996
                len(str.strip(new_values['data']['name'])) == 0:
997
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
998
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_NODE_NAME')
999
        name = str.strip(new_values['data']['name'])
1000
1001
        cnx = mysql.connector.connect(**config.myems_system_db)
1002
        cursor = cnx.cursor(dictionary=True)
1003
1004
        cursor.execute(" SELECT name "
1005
                       " FROM tbl_energy_flow_diagrams "
1006
                       " WHERE id = %s ", (id_,))
1007
        if cursor.fetchone() is None:
1008
            cursor.close()
1009
            cnx.close()
1010
            raise falcon.HTTPError(falcon.HTTP_400, title='API.NOT_FOUND',
1011
                                   description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND')
1012
1013
        cursor.execute(" SELECT name "
1014
                       " FROM tbl_energy_flow_diagrams_nodes "
1015
                       " WHERE name = %s AND energy_flow_diagram_id = %s ", (name, id_))
1016
        if cursor.fetchone() is not None:
1017
            cursor.close()
1018
            cnx.close()
1019
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1020
                                   description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE')
1021
1022
        add_values = (" INSERT INTO tbl_energy_flow_diagrams_nodes "
1023
                      "    (energy_flow_diagram_id, name) "
1024
                      " VALUES (%s, %s) ")
1025
        cursor.execute(add_values, (id_,
1026
                                    name))
1027
        new_id = cursor.lastrowid
1028
        cnx.commit()
1029
        cursor.close()
1030
        cnx.close()
1031
1032
        resp.status = falcon.HTTP_201
1033
        resp.location = '/energyflowdiagrams/' + str(id_) + 'nodes/' + str(new_id)
1034
1035
1036
class EnergyFlowDiagramNodeItem: