예제 #1
0
파일: es01.py 프로젝트: miolad/IoTLabs
    def PUT(self, *uri, **params):
        # Add Service
        if len(uri) == 1 and uri[0] == "addService":
            body = cherrypy.request.body.read()
            try:
                body = json.loads(body)
            except:
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = "Invalid JSON"
                return json.dumps(self.responseFailure)

            # Parse the service
            try:
                s = Service.parseService(body)
            except ValueError as e:
                # There was an error parsing the service's json
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = str(e)
                return json.dumps(self.responseFailure)

            # Add the timestamp
            s.timestamp = str(datetime.datetime.now())

            # Now insert the newly created service into the database
            self.database["services"][s.serviceID] = s

            # Save the new JSON file
            self.serializeCatalogToJSONFile()

            return self.responseSuccessJSON

        # Add User
        if len(uri) == 1 and uri[0] == "addUser":
            body = cherrypy.request.body.read()
            try:
                body = json.loads(body)
            except:
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = "Invalid JSON"
                return json.dumps(self.responseFailure)

            try:
                u = User.parseUser(body)
            except ValueError as e:
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = str(e)
                return json.dumps(self.responseFailure)

            # Insert the user into the global database
            self.database["users"][u.userID] = u

            # Save the new JSON file
            self.serializeCatalogToJSONFile()

            return self.responseSuccessJSON

        # Add Device
        if len(uri) == 1 and uri[0] == "addDevice":
            body = cherrypy.request.body.read()
            try:
                body = json.loads(body)
            except:
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = "Bad Request: Invalid JSON"
                return json.dumps(self.responseFailure)

            try:
                d = Device.parseDevice(body)
            except ValueError as e:
                cherrypy.response.status = 400  # Bad Request
                self.responseFailure["reason"] = str(e)
                return json.dumps(self.responseFailure)

            # Add timestamp
            d.timestamp = str(datetime.datetime.now())

            # Now insert the newly created device into the database
            self.database["devices"][d.deviceID] = d

            # Save the new JSON file
            self.serializeCatalogToJSONFile()

            return self.responseSuccessJSON

        # The requested service does not exist
        cherrypy.response.status = 404  # Not Found
        self.responseFailure["reason"] = "Not Found"
        return json.dumps(self.responseFailure)