Code Duplication    Length = 98-104 lines in 12 locations

space.py 6 locations

@@ 2166-2269 (lines=104) @@
2163
        resp.status = falcon.HTTP_204
2164
2165
2166
class SpaceTenantCollection:
2167
    @staticmethod
2168
    def __init__():
2169
        pass
2170
2171
    @staticmethod
2172
    def on_options(req, resp, id_):
2173
        resp.status = falcon.HTTP_200
2174
2175
    @staticmethod
2176
    def on_get(req, resp, id_):
2177
        if not id_.isdigit() or int(id_) <= 0:
2178
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2179
                                   description='API.INVALID_SPACE_ID')
2180
2181
        cnx = mysql.connector.connect(**config.myems_system_db)
2182
        cursor = cnx.cursor()
2183
2184
        cursor.execute(" SELECT name "
2185
                       " FROM tbl_spaces "
2186
                       " WHERE id = %s ", (id_,))
2187
        if cursor.fetchone() is None:
2188
            cursor.close()
2189
            cnx.disconnect()
2190
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2191
                                   description='API.SPACE_NOT_FOUND')
2192
2193
        query = (" SELECT t.id, t.name, t.uuid "
2194
                 " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t "
2195
                 " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s "
2196
                 " ORDER BY t.id ")
2197
        cursor.execute(query, (id_,))
2198
        rows = cursor.fetchall()
2199
2200
        result = list()
2201
        if rows is not None and len(rows) > 0:
2202
            for row in rows:
2203
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2204
                result.append(meta_result)
2205
2206
        resp.body = json.dumps(result)
2207
2208
    @staticmethod
2209
    def on_post(req, resp, id_):
2210
        """Handles POST requests"""
2211
        try:
2212
            raw_json = req.stream.read().decode('utf-8')
2213
        except Exception as ex:
2214
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2215
2216
        if not id_.isdigit() or int(id_) <= 0:
2217
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2218
                                   description='API.INVALID_SPACE_ID')
2219
2220
        new_values = json.loads(raw_json)
2221
2222
        if 'tenant_id' not in new_values['data'].keys() or \
2223
                not isinstance(new_values['data']['tenant_id'], int) or \
2224
                new_values['data']['tenant_id'] <= 0:
2225
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2226
                                   description='API.INVALID_TENANT_ID')
2227
        tenant_id = new_values['data']['tenant_id']
2228
2229
        cnx = mysql.connector.connect(**config.myems_system_db)
2230
        cursor = cnx.cursor()
2231
2232
        cursor.execute(" SELECT name "
2233
                       " from tbl_spaces "
2234
                       " WHERE id = %s ", (id_,))
2235
        if cursor.fetchone() is None:
2236
            cursor.close()
2237
            cnx.disconnect()
2238
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2239
                                   description='API.SPACE_NOT_FOUND')
2240
2241
        cursor.execute(" SELECT name "
2242
                       " FROM tbl_tenants "
2243
                       " WHERE id = %s ", (tenant_id,))
2244
        if cursor.fetchone() is None:
2245
            cursor.close()
2246
            cnx.disconnect()
2247
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2248
                                   description='API.TENANT_NOT_FOUND')
2249
2250
        query = (" SELECT id " 
2251
                 " FROM tbl_spaces_tenants "
2252
                 " WHERE space_id = %s AND tenant_id = %s")
2253
        cursor.execute(query, (id_, tenant_id,))
2254
        if cursor.fetchone() is not None:
2255
            cursor.close()
2256
            cnx.disconnect()
2257
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2258
                                   description='API.SPACE_TENANT_RELATION_EXISTED')
2259
2260
        add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) "
2261
                   " VALUES (%s, %s) ")
2262
        cursor.execute(add_row, (id_, tenant_id,))
2263
        new_id = cursor.lastrowid
2264
        cnx.commit()
2265
        cursor.close()
2266
        cnx.disconnect()
2267
2268
        resp.status = falcon.HTTP_201
2269
        resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id)
2270
2271
2272
class SpaceTenantItem:
@@ 2002-2105 (lines=104) @@
1999
        resp.status = falcon.HTTP_204
2000
2001
2002
class SpaceStoreCollection:
2003
    @staticmethod
2004
    def __init__():
2005
        pass
2006
2007
    @staticmethod
2008
    def on_options(req, resp, id_):
2009
        resp.status = falcon.HTTP_200
2010
2011
    @staticmethod
2012
    def on_get(req, resp, id_):
2013
        if not id_.isdigit() or int(id_) <= 0:
2014
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2015
                                   description='API.INVALID_SPACE_ID')
2016
2017
        cnx = mysql.connector.connect(**config.myems_system_db)
2018
        cursor = cnx.cursor()
2019
2020
        cursor.execute(" SELECT name "
2021
                       " FROM tbl_spaces "
2022
                       " WHERE id = %s ", (id_,))
2023
        if cursor.fetchone() is None:
2024
            cursor.close()
2025
            cnx.disconnect()
2026
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2027
                                   description='API.SPACE_NOT_FOUND')
2028
2029
        query = (" SELECT t.id, t.name, t.uuid "
2030
                 " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t "
2031
                 " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s "
2032
                 " ORDER BY t.id ")
2033
        cursor.execute(query, (id_,))
2034
        rows = cursor.fetchall()
2035
2036
        result = list()
2037
        if rows is not None and len(rows) > 0:
2038
            for row in rows:
2039
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2040
                result.append(meta_result)
2041
2042
        resp.body = json.dumps(result)
2043
2044
    @staticmethod
2045
    def on_post(req, resp, id_):
2046
        """Handles POST requests"""
2047
        try:
2048
            raw_json = req.stream.read().decode('utf-8')
2049
        except Exception as ex:
2050
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2051
2052
        if not id_.isdigit() or int(id_) <= 0:
2053
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2054
                                   description='API.INVALID_SPACE_ID')
2055
2056
        new_values = json.loads(raw_json)
2057
2058
        if 'store_id' not in new_values['data'].keys() or \
2059
                not isinstance(new_values['data']['store_id'], int) or \
2060
                new_values['data']['store_id'] <= 0:
2061
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2062
                                   description='API.INVALID_STORE_ID')
2063
        store_id = new_values['data']['store_id']
2064
2065
        cnx = mysql.connector.connect(**config.myems_system_db)
2066
        cursor = cnx.cursor()
2067
2068
        cursor.execute(" SELECT name "
2069
                       " from tbl_spaces "
2070
                       " WHERE id = %s ", (id_,))
2071
        if cursor.fetchone() is None:
2072
            cursor.close()
2073
            cnx.disconnect()
2074
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2075
                                   description='API.SPACE_NOT_FOUND')
2076
2077
        cursor.execute(" SELECT name "
2078
                       " FROM tbl_stores "
2079
                       " WHERE id = %s ", (store_id,))
2080
        if cursor.fetchone() is None:
2081
            cursor.close()
2082
            cnx.disconnect()
2083
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2084
                                   description='API.STORE_NOT_FOUND')
2085
2086
        query = (" SELECT id " 
2087
                 " FROM tbl_spaces_stores "
2088
                 " WHERE space_id = %s AND store_id = %s")
2089
        cursor.execute(query, (id_, store_id,))
2090
        if cursor.fetchone() is not None:
2091
            cursor.close()
2092
            cnx.disconnect()
2093
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2094
                                   description='API.SPACE_STORE_RELATION_EXISTED')
2095
2096
        add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) "
2097
                   " VALUES (%s, %s) ")
2098
        cursor.execute(add_row, (id_, store_id,))
2099
        new_id = cursor.lastrowid
2100
        cnx.commit()
2101
        cursor.close()
2102
        cnx.disconnect()
2103
2104
        resp.status = falcon.HTTP_201
2105
        resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id)
2106
2107
2108
class SpaceStoreItem:
@@ 1838-1941 (lines=104) @@
1835
        resp.status = falcon.HTTP_204
1836
1837
1838
class SpaceShopfloorCollection:
1839
    @staticmethod
1840
    def __init__():
1841
        pass
1842
1843
    @staticmethod
1844
    def on_options(req, resp, id_):
1845
        resp.status = falcon.HTTP_200
1846
1847
    @staticmethod
1848
    def on_get(req, resp, id_):
1849
        if not id_.isdigit() or int(id_) <= 0:
1850
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1851
                                   description='API.INVALID_SPACE_ID')
1852
1853
        cnx = mysql.connector.connect(**config.myems_system_db)
1854
        cursor = cnx.cursor()
1855
1856
        cursor.execute(" SELECT name "
1857
                       " FROM tbl_spaces "
1858
                       " WHERE id = %s ", (id_,))
1859
        if cursor.fetchone() is None:
1860
            cursor.close()
1861
            cnx.disconnect()
1862
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1863
                                   description='API.SPACE_NOT_FOUND')
1864
1865
        query = (" SELECT sf.id, sf.name, sf.uuid "
1866
                 " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf "
1867
                 " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s "
1868
                 " ORDER BY sf.id ")
1869
        cursor.execute(query, (id_,))
1870
        rows = cursor.fetchall()
1871
1872
        result = list()
1873
        if rows is not None and len(rows) > 0:
1874
            for row in rows:
1875
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1876
                result.append(meta_result)
1877
1878
        resp.body = json.dumps(result)
1879
1880
    @staticmethod
1881
    def on_post(req, resp, id_):
1882
        """Handles POST requests"""
1883
        try:
1884
            raw_json = req.stream.read().decode('utf-8')
1885
        except Exception as ex:
1886
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1887
1888
        if not id_.isdigit() or int(id_) <= 0:
1889
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1890
                                   description='API.INVALID_SPACE_ID')
1891
1892
        new_values = json.loads(raw_json)
1893
1894
        if 'shopfloor_id' not in new_values['data'].keys() or \
1895
                not isinstance(new_values['data']['shopfloor_id'], int) or \
1896
                new_values['data']['shopfloor_id'] <= 0:
1897
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1898
                                   description='API.INVALID_SHOPFLOOR_ID')
1899
        shopfloor_id = new_values['data']['shopfloor_id']
1900
1901
        cnx = mysql.connector.connect(**config.myems_system_db)
1902
        cursor = cnx.cursor()
1903
1904
        cursor.execute(" SELECT name "
1905
                       " from tbl_spaces "
1906
                       " WHERE id = %s ", (id_,))
1907
        if cursor.fetchone() is None:
1908
            cursor.close()
1909
            cnx.disconnect()
1910
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1911
                                   description='API.SPACE_NOT_FOUND')
1912
1913
        cursor.execute(" SELECT name "
1914
                       " FROM tbl_shopfloors "
1915
                       " WHERE id = %s ", (shopfloor_id,))
1916
        if cursor.fetchone() is None:
1917
            cursor.close()
1918
            cnx.disconnect()
1919
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1920
                                   description='API.SHOPFLOOR_NOT_FOUND')
1921
1922
        query = (" SELECT id " 
1923
                 " FROM tbl_spaces_shopfloors "
1924
                 " WHERE space_id = %s AND shopfloor_id = %s")
1925
        cursor.execute(query, (id_, shopfloor_id,))
1926
        if cursor.fetchone() is not None:
1927
            cursor.close()
1928
            cnx.disconnect()
1929
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1930
                                   description='API.SPACE_SHOPFLOOR_RELATION_EXISTED')
1931
1932
        add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) "
1933
                   " VALUES (%s, %s) ")
1934
        cursor.execute(add_row, (id_, shopfloor_id,))
1935
        new_id = cursor.lastrowid
1936
        cnx.commit()
1937
        cursor.close()
1938
        cnx.disconnect()
1939
1940
        resp.status = falcon.HTTP_201
1941
        resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id)
1942
1943
1944
class SpaceShopfloorItem:
@@ 1674-1777 (lines=104) @@
1671
        resp.status = falcon.HTTP_204
1672
1673
1674
class SpaceSensorCollection:
1675
    @staticmethod
1676
    def __init__():
1677
        pass
1678
1679
    @staticmethod
1680
    def on_options(req, resp, id_):
1681
        resp.status = falcon.HTTP_200
1682
1683
    @staticmethod
1684
    def on_get(req, resp, id_):
1685
        if not id_.isdigit() or int(id_) <= 0:
1686
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1687
                                   description='API.INVALID_SPACE_ID')
1688
1689
        cnx = mysql.connector.connect(**config.myems_system_db)
1690
        cursor = cnx.cursor()
1691
1692
        cursor.execute(" SELECT name "
1693
                       " FROM tbl_spaces "
1694
                       " WHERE id = %s ", (id_,))
1695
        if cursor.fetchone() is None:
1696
            cursor.close()
1697
            cnx.disconnect()
1698
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1699
                                   description='API.SPACE_NOT_FOUND')
1700
1701
        query = (" SELECT se.id, se.name, se.uuid "
1702
                 " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se "
1703
                 " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
1704
                 " ORDER BY se.id ")
1705
        cursor.execute(query, (id_,))
1706
        rows = cursor.fetchall()
1707
1708
        result = list()
1709
        if rows is not None and len(rows) > 0:
1710
            for row in rows:
1711
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1712
                result.append(meta_result)
1713
1714
        resp.body = json.dumps(result)
1715
1716
    @staticmethod
1717
    def on_post(req, resp, id_):
1718
        """Handles POST requests"""
1719
        try:
1720
            raw_json = req.stream.read().decode('utf-8')
1721
        except Exception as ex:
1722
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1723
1724
        if not id_.isdigit() or int(id_) <= 0:
1725
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1726
                                   description='API.INVALID_SPACE_ID')
1727
1728
        new_values = json.loads(raw_json)
1729
1730
        if 'sensor_id' not in new_values['data'].keys() or \
1731
                not isinstance(new_values['data']['sensor_id'], int) or \
1732
                new_values['data']['sensor_id'] <= 0:
1733
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1734
                                   description='API.INVALID_SENSOR_ID')
1735
        sensor_id = new_values['data']['sensor_id']
1736
1737
        cnx = mysql.connector.connect(**config.myems_system_db)
1738
        cursor = cnx.cursor()
1739
1740
        cursor.execute(" SELECT name "
1741
                       " from tbl_spaces "
1742
                       " WHERE id = %s ", (id_,))
1743
        if cursor.fetchone() is None:
1744
            cursor.close()
1745
            cnx.disconnect()
1746
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1747
                                   description='API.SPACE_NOT_FOUND')
1748
1749
        cursor.execute(" SELECT name "
1750
                       " FROM tbl_sensors "
1751
                       " WHERE id = %s ", (sensor_id,))
1752
        if cursor.fetchone() is None:
1753
            cursor.close()
1754
            cnx.disconnect()
1755
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1756
                                   description='API.SENSOR_NOT_FOUND')
1757
1758
        query = (" SELECT id " 
1759
                 " FROM tbl_spaces_sensors "
1760
                 " WHERE space_id = %s AND sensor_id = %s")
1761
        cursor.execute(query, (id_, sensor_id,))
1762
        if cursor.fetchone() is not None:
1763
            cursor.close()
1764
            cnx.disconnect()
1765
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1766
                                   description='API.SPACE_SENSOR_RELATION_EXISTED')
1767
1768
        add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) "
1769
                   " VALUES (%s, %s) ")
1770
        cursor.execute(add_row, (id_, sensor_id,))
1771
        new_id = cursor.lastrowid
1772
        cnx.commit()
1773
        cursor.close()
1774
        cnx.disconnect()
1775
1776
        resp.status = falcon.HTTP_201
1777
        resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id)
1778
1779
1780
class SpaceSensorItem:
@@ 975-1078 (lines=104) @@
972
        resp.status = falcon.HTTP_204
973
974
975
class SpaceEquipmentCollection:
976
    @staticmethod
977
    def __init__():
978
        pass
979
980
    @staticmethod
981
    def on_options(req, resp, id_):
982
        resp.status = falcon.HTTP_200
983
984
    @staticmethod
985
    def on_get(req, resp, id_):
986
        if not id_.isdigit() or int(id_) <= 0:
987
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
988
                                   description='API.INVALID_SPACE_ID')
989
990
        cnx = mysql.connector.connect(**config.myems_system_db)
991
        cursor = cnx.cursor()
992
993
        cursor.execute(" SELECT name "
994
                       " FROM tbl_spaces "
995
                       " WHERE id = %s ", (id_,))
996
        if cursor.fetchone() is None:
997
            cursor.close()
998
            cnx.disconnect()
999
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1000
                                   description='API.SPACE_NOT_FOUND')
1001
1002
        query = (" SELECT e.id, e.name, e.uuid "
1003
                 " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e "
1004
                 " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s "
1005
                 " ORDER BY e.id ")
1006
        cursor.execute(query, (id_,))
1007
        rows = cursor.fetchall()
1008
1009
        result = list()
1010
        if rows is not None and len(rows) > 0:
1011
            for row in rows:
1012
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1013
                result.append(meta_result)
1014
1015
        resp.body = json.dumps(result)
1016
1017
    @staticmethod
1018
    def on_post(req, resp, id_):
1019
        """Handles POST requests"""
1020
        try:
1021
            raw_json = req.stream.read().decode('utf-8')
1022
        except Exception as ex:
1023
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1024
1025
        if not id_.isdigit() or int(id_) <= 0:
1026
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1027
                                   description='API.INVALID_SPACE_ID')
1028
1029
        new_values = json.loads(raw_json)
1030
1031
        if 'equipment_id' not in new_values['data'].keys() or \
1032
                not isinstance(new_values['data']['equipment_id'], int) or \
1033
                new_values['data']['equipment_id'] <= 0:
1034
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1035
                                   description='API.INVALID_EQUIPMENT_ID')
1036
        equipment_id = new_values['data']['equipment_id']
1037
1038
        cnx = mysql.connector.connect(**config.myems_system_db)
1039
        cursor = cnx.cursor()
1040
1041
        cursor.execute(" SELECT name "
1042
                       " from tbl_spaces "
1043
                       " WHERE id = %s ", (id_,))
1044
        if cursor.fetchone() is None:
1045
            cursor.close()
1046
            cnx.disconnect()
1047
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1048
                                   description='API.SPACE_NOT_FOUND')
1049
1050
        cursor.execute(" SELECT name "
1051
                       " FROM tbl_equipments "
1052
                       " WHERE id = %s ", (equipment_id,))
1053
        if cursor.fetchone() is None:
1054
            cursor.close()
1055
            cnx.disconnect()
1056
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1057
                                   description='API.EQUIPMENT_NOT_FOUND')
1058
1059
        query = (" SELECT id " 
1060
                 " FROM tbl_spaces_equipments "
1061
                 " WHERE space_id = %s AND equipment_id = %s")
1062
        cursor.execute(query, (id_, equipment_id,))
1063
        if cursor.fetchone() is not None:
1064
            cursor.close()
1065
            cnx.disconnect()
1066
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1067
                                   description='API.SPACE_EQUIPMENT_RELATION_EXISTED')
1068
1069
        add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) "
1070
                   " VALUES (%s, %s) ")
1071
        cursor.execute(add_row, (id_, equipment_id,))
1072
        new_id = cursor.lastrowid
1073
        cnx.commit()
1074
        cursor.close()
1075
        cnx.disconnect()
1076
1077
        resp.status = falcon.HTTP_201
1078
        resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id)
1079
1080
1081
class SpaceEquipmentItem:
@@ 810-913 (lines=104) @@
807
        resp.body = json.dumps(result)
808
809
810
class SpaceCombinedEquipmentCollection:
811
    @staticmethod
812
    def __init__():
813
        pass
814
815
    @staticmethod
816
    def on_options(req, resp, id_):
817
        resp.status = falcon.HTTP_200
818
819
    @staticmethod
820
    def on_get(req, resp, id_):
821
        if not id_.isdigit() or int(id_) <= 0:
822
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
823
                                   description='API.INVALID_SPACE_ID')
824
825
        cnx = mysql.connector.connect(**config.myems_system_db)
826
        cursor = cnx.cursor()
827
828
        cursor.execute(" SELECT name "
829
                       " FROM tbl_spaces "
830
                       " WHERE id = %s ", (id_,))
831
        if cursor.fetchone() is None:
832
            cursor.close()
833
            cnx.disconnect()
834
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
835
                                   description='API.SPACE_NOT_FOUND')
836
837
        query = (" SELECT e.id, e.name, e.uuid "
838
                 " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e "
839
                 " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s "
840
                 " ORDER BY e.id ")
841
        cursor.execute(query, (id_,))
842
        rows = cursor.fetchall()
843
844
        result = list()
845
        if rows is not None and len(rows) > 0:
846
            for row in rows:
847
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
848
                result.append(meta_result)
849
850
        resp.body = json.dumps(result)
851
852
    @staticmethod
853
    def on_post(req, resp, id_):
854
        """Handles POST requests"""
855
        try:
856
            raw_json = req.stream.read().decode('utf-8')
857
        except Exception as ex:
858
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
859
860
        if not id_.isdigit() or int(id_) <= 0:
861
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
862
                                   description='API.INVALID_SPACE_ID')
863
864
        new_values = json.loads(raw_json)
865
866
        if 'combined_equipment_id' not in new_values['data'].keys() or \
867
                not isinstance(new_values['data']['combined_equipment_id'], int) or \
868
                new_values['data']['combined_equipment_id'] <= 0:
869
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
870
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
871
        combined_equipment_id = new_values['data']['combined_equipment_id']
872
873
        cnx = mysql.connector.connect(**config.myems_system_db)
874
        cursor = cnx.cursor()
875
876
        cursor.execute(" SELECT name "
877
                       " from tbl_spaces "
878
                       " WHERE id = %s ", (id_,))
879
        if cursor.fetchone() is None:
880
            cursor.close()
881
            cnx.disconnect()
882
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
883
                                   description='API.SPACE_NOT_FOUND')
884
885
        cursor.execute(" SELECT name "
886
                       " FROM tbl_combined_equipments "
887
                       " WHERE id = %s ", (combined_equipment_id,))
888
        if cursor.fetchone() is None:
889
            cursor.close()
890
            cnx.disconnect()
891
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
892
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
893
894
        query = (" SELECT id " 
895
                 " FROM tbl_spaces_combined_equipments "
896
                 " WHERE space_id = %s AND combined_equipment_id = %s")
897
        cursor.execute(query, (id_, combined_equipment_id,))
898
        if cursor.fetchone() is not None:
899
            cursor.close()
900
            cnx.disconnect()
901
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
902
                                   description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTED')
903
904
        add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) "
905
                   " VALUES (%s, %s) ")
906
        cursor.execute(add_row, (id_, combined_equipment_id,))
907
        new_id = cursor.lastrowid
908
        cnx.commit()
909
        cursor.close()
910
        cnx.disconnect()
911
912
        resp.status = falcon.HTTP_201
913
        resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id)
914
915
916
class SpaceCombinedEquipmentItem:

tenant.py 1 location

@@ 1222-1325 (lines=104) @@
1219
        resp.status = falcon.HTTP_204
1220
1221
1222
class TenantSensorCollection:
1223
    @staticmethod
1224
    def __init__():
1225
        pass
1226
1227
    @staticmethod
1228
    def on_options(req, resp, id_):
1229
        resp.status = falcon.HTTP_200
1230
1231
    @staticmethod
1232
    def on_get(req, resp, id_):
1233
        if not id_.isdigit() or int(id_) <= 0:
1234
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1235
                                   description='API.INVALID_TENANT_ID')
1236
1237
        cnx = mysql.connector.connect(**config.myems_system_db)
1238
        cursor = cnx.cursor()
1239
1240
        cursor.execute(" SELECT name "
1241
                       " FROM tbl_tenants "
1242
                       " WHERE id = %s ", (id_,))
1243
        if cursor.fetchone() is None:
1244
            cursor.close()
1245
            cnx.disconnect()
1246
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1247
                                   description='API.TENANT_NOT_FOUND')
1248
1249
        query = (" SELECT s.id, s.name, s.uuid "
1250
                 " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s "
1251
                 " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1252
                 " ORDER BY s.id ")
1253
        cursor.execute(query, (id_,))
1254
        rows = cursor.fetchall()
1255
1256
        result = list()
1257
        if rows is not None and len(rows) > 0:
1258
            for row in rows:
1259
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1260
                result.append(meta_result)
1261
1262
        resp.body = json.dumps(result)
1263
1264
    @staticmethod
1265
    def on_post(req, resp, id_):
1266
        """Handles POST requests"""
1267
        try:
1268
            raw_json = req.stream.read().decode('utf-8')
1269
        except Exception as ex:
1270
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1271
1272
        if not id_.isdigit() or int(id_) <= 0:
1273
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1274
                                   description='API.INVALID_TENANT_ID')
1275
1276
        new_values = json.loads(raw_json)
1277
1278
        if 'sensor_id' not in new_values['data'].keys() or \
1279
                not isinstance(new_values['data']['sensor_id'], int) or \
1280
                new_values['data']['sensor_id'] <= 0:
1281
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1282
                                   description='API.INVALID_SENSOR_ID')
1283
        sensor_id = new_values['data']['sensor_id']
1284
1285
        cnx = mysql.connector.connect(**config.myems_system_db)
1286
        cursor = cnx.cursor()
1287
1288
        cursor.execute(" SELECT name "
1289
                       " from tbl_tenants "
1290
                       " WHERE id = %s ", (id_,))
1291
        if cursor.fetchone() is None:
1292
            cursor.close()
1293
            cnx.disconnect()
1294
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1295
                                   description='API.TENANT_NOT_FOUND')
1296
1297
        cursor.execute(" SELECT name "
1298
                       " FROM tbl_sensors "
1299
                       " WHERE id = %s ", (sensor_id,))
1300
        if cursor.fetchone() is None:
1301
            cursor.close()
1302
            cnx.disconnect()
1303
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1304
                                   description='API.SENSOR_NOT_FOUND')
1305
1306
        query = (" SELECT id " 
1307
                 " FROM tbl_tenants_sensors "
1308
                 " WHERE tenant_id = %s AND sensor_id = %s")
1309
        cursor.execute(query, (id_, sensor_id,))
1310
        if cursor.fetchone() is not None:
1311
            cursor.close()
1312
            cnx.disconnect()
1313
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1314
                                   description='API.TENANT_SENSOR_RELATION_EXISTED')
1315
1316
        add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) "
1317
                   " VALUES (%s, %s) ")
1318
        cursor.execute(add_row, (id_, sensor_id,))
1319
        new_id = cursor.lastrowid
1320
        cnx.commit()
1321
        cursor.close()
1322
        cnx.disconnect()
1323
1324
        resp.status = falcon.HTTP_201
1325
        resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id)
1326
1327
1328
class TenantSensorItem:

shopfloor.py 2 locations

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

store.py 1 location

@@ 1125-1228 (lines=104) @@
1122
        resp.status = falcon.HTTP_204
1123
1124
1125
class StoreSensorCollection:
1126
    @staticmethod
1127
    def __init__():
1128
        pass
1129
1130
    @staticmethod
1131
    def on_options(req, resp, id_):
1132
        resp.status = falcon.HTTP_200
1133
1134
    @staticmethod
1135
    def on_get(req, resp, id_):
1136
        if not id_.isdigit() or int(id_) <= 0:
1137
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1138
                                   description='API.INVALID_STORE_ID')
1139
1140
        cnx = mysql.connector.connect(**config.myems_system_db)
1141
        cursor = cnx.cursor()
1142
1143
        cursor.execute(" SELECT name "
1144
                       " FROM tbl_stores "
1145
                       " WHERE id = %s ", (id_,))
1146
        if cursor.fetchone() is None:
1147
            cursor.close()
1148
            cnx.disconnect()
1149
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1150
                                   description='API.STORE_NOT_FOUND')
1151
1152
        query = (" SELECT s.id, s.name, s.uuid "
1153
                 " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s "
1154
                 " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1155
                 " ORDER BY s.id ")
1156
        cursor.execute(query, (id_,))
1157
        rows = cursor.fetchall()
1158
1159
        result = list()
1160
        if rows is not None and len(rows) > 0:
1161
            for row in rows:
1162
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1163
                result.append(meta_result)
1164
1165
        resp.body = json.dumps(result)
1166
1167
    @staticmethod
1168
    def on_post(req, resp, id_):
1169
        """Handles POST requests"""
1170
        try:
1171
            raw_json = req.stream.read().decode('utf-8')
1172
        except Exception as ex:
1173
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1174
1175
        if not id_.isdigit() or int(id_) <= 0:
1176
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1177
                                   description='API.INVALID_STORE_ID')
1178
1179
        new_values = json.loads(raw_json)
1180
1181
        if 'sensor_id' not in new_values['data'].keys() or \
1182
                not isinstance(new_values['data']['sensor_id'], int) or \
1183
                new_values['data']['sensor_id'] <= 0:
1184
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1185
                                   description='API.INVALID_SENSOR_ID')
1186
        sensor_id = new_values['data']['sensor_id']
1187
1188
        cnx = mysql.connector.connect(**config.myems_system_db)
1189
        cursor = cnx.cursor()
1190
1191
        cursor.execute(" SELECT name "
1192
                       " from tbl_stores "
1193
                       " WHERE id = %s ", (id_,))
1194
        if cursor.fetchone() is None:
1195
            cursor.close()
1196
            cnx.disconnect()
1197
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1198
                                   description='API.STORE_NOT_FOUND')
1199
1200
        cursor.execute(" SELECT name "
1201
                       " FROM tbl_sensors "
1202
                       " WHERE id = %s ", (sensor_id,))
1203
        if cursor.fetchone() is None:
1204
            cursor.close()
1205
            cnx.disconnect()
1206
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1207
                                   description='API.SENSOR_NOT_FOUND')
1208
1209
        query = (" SELECT id " 
1210
                 " FROM tbl_stores_sensors "
1211
                 " WHERE store_id = %s AND sensor_id = %s")
1212
        cursor.execute(query, (id_, sensor_id,))
1213
        if cursor.fetchone() is not None:
1214
            cursor.close()
1215
            cnx.disconnect()
1216
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1217
                                   description='API.STORE_SENSOR_RELATION_EXISTED')
1218
1219
        add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) "
1220
                   " VALUES (%s, %s) ")
1221
        cursor.execute(add_row, (id_, sensor_id,))
1222
        new_id = cursor.lastrowid
1223
        cnx.commit()
1224
        cursor.close()
1225
        cnx.disconnect()
1226
1227
        resp.status = falcon.HTTP_201
1228
        resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id)
1229
1230
1231
class StoreSensorItem:

combinedequipment.py 1 location

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

energyflowdiagram.py 1 location

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