def users_user_id_put(userId, newUser): user = get_users_by_id(userId) if (user == 404): return 'User with the provided ID does not exist', 404 # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newUser: try: building = Building.get(newUser['buildingId']) except Exception as e: return 'Building with specified ID does not exist' # Check that if email is changed, it is not the same as another user's if 'email' in newUser: newemail = newUser['email'] u = get_users_by_email(newemail) if len(u) != 0 and newemail != user.attribute_values['email']: return 'A user with the given email id already exists', 400 attributes = user.attribute_values.keys() for key in newUser.keys(): if key in attributes and key != 'id': user.update_item(key, value=newUser[key], action='PUT') return 'User updated successfully'
def buildings_building_id_delete(buildingId) -> str: try: building = Building.get(buildingId) building.delete() except Exception as e: return 'Building with id=%s does not exist.' % (buildingId) return 'Successfully deleted buildingId=%s.' % (buildingId)
def robots_robot_id_put(robotId, newRobot): """ method to update a robot using robot id """ try: robot = Robot.get(robotId) except Exception: return 'Robot with id=%s does not exist.' % (robotId),404 # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newRobot: try: building = Building.get(newRobot['buildingId']) except Exception as e: return 'Building with specified ID does not exist',412 # Do the same for sensor ID if 'sensorId' in newRobot: for val in newRobot['sensorId']: try: #print(val) sensor = Sensor.get(val) except Exception as e: return 'Sensor with specified ID does not exist',412 attributes = robot.attribute_values.keys() for key in newRobot.keys(): if key in attributes and key!= "id": # print(key) robot.update_item(key, value=newRobot[key], action='PUT') return 'Robot with id=%s updated successfully.' % (robotId),200
def buildings_building_id_get(buildingId): """ method to get building using building id """ try: building = Building.get(buildingId) except Exception: return 'Building with id=%s does not exist.' % (buildingId) return building.attribute_values
def robots_robot_id_put(robotId, newRobot): """ method to update a robot using robot id """ try: robot = Robot.get(robotId) except Exception: return 'Robot with id=%s does not exist.' % (robotId) # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newRobot: try: building = Building.get(newRobot['buildingId']) except Exception as e: return 'Building with specified ID does not exist' # Do the same for sensor ID if 'sensorId' in newRobot: try: sensor = Sensor.get(newRobot['sensorId']) except Exception as e: return 'Sensor with specified ID does not exist' attributes = robot.attribute_values.keys() for key in newRobot.keys(): if key in attributes and key is not 'id': robot.update_item(key, value=newRobot[key], action='PUT') return 'Robot with id=%s updated successfully.' % (robotId)
def buildings_building_id_get(buildingId) -> str: try: building = Building.get(buildingId) except Exception as e: print(e) return 'Building with id=%s does not exist.' % (buildingId) return building.attribute_values
def db_put(id,type,newSensor): if type not in types: return 1 if type == 'sensor': try: sensor = Sensor.get(id) except Exception: return 2 # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newSensor: try: building = Building.get(newSensor['buildingId']) except Exception as e: return 2 attributes = sensor.attribute_values.keys() for key in newSensor.keys(): if key in attributes and key != 'id': if newSensor[key] == '': sensor.update_item(key, value=' ', action='PUT') else: sensor.update_item(key, value=newSensor[key], action='PUT') return 0
def buildings_building_id_delete(buildingId): """ method to delete building using building id """ try: building = Building.get(buildingId) building.delete() except Exception: return 'Building with id=%s does not exist.' % (buildingId) return 'Successfully deleted buildingId=%s.' % (buildingId)
def buildings_building_id_sensors_post(buildingId): """ method to post a sensor using sensor id """ try: building = Building.get(buildingId) except Exception: return 'Building with id=%s does not exist.' % (buildingId) newid = str(uuid.uuid4()) newobj = Sensor(id=newid, buildingId=buildingId) newobj.save() return newobj.attribute_values
def buildings_building_id_robots_post(buildingId): """ method to post robot under a building """ try: building = Building.get(buildingId) except Exception: return 'Building with id %s does not exist.' % (buildingId) newid = str(uuid.uuid4()) newobj = Robot(id=newid, buildingId=buildingId) newobj.save() return newobj.attribute_values
def buildings_building_id_put(buildingId, newBuilding) -> str: try: building = Building.get(buildingId) except Exception as e: return 'Building with id=%s does not exist.' % (buildingId) attributes = building.attribute_values.keys() for key in newBuilding.keys(): if key in attributes and key is not 'id': building.update_item(key, value=newBuilding[key], action='PUT') return 'Building with id=%s updated successfully.' % (buildingId)
def buildings_building_id_sensors_post(buildingId) -> str: try: building = Building.get(buildingId) except Exception as e: print(e) return 'Building with id=%s does not exist.' % (buildingId) newID = str(uuid.uuid4()) newObj = Sensor(id=newID, buildingId=buildingId) newObj.save() return newObj.attribute_values
def buildings_building_id_robots_get(buildingId, capability = None): resultList = [] try: building = Building.get(buildingId) except Exception as e: return 'Building with id=%s does not exist.' % (buildingId) if capability is not None and capability != '': for item in Robot.scan(buildingId__eq=buildingId, capabilities__contains=capability, conditional_operator='AND'): resultList.append(item.attribute_values) else: for item in Robot.scan(buildingId__eq=buildingId): resultList.append(item.attribute_values) return resultList
def buildings_building_id_robots_get(buildingId, capability=None): resultList = [] try: building = Building.get(buildingId) except Exception as e: return 'Building with id=%s does not exist.' % (buildingId) if capability is not None and capability != '': for item in Robot.scan(buildingId__eq=buildingId, capabilities__contains=capability, conditional_operator='AND'): resultList.append(item.attribute_values) else: for item in Robot.scan(buildingId__eq=buildingId): resultList.append(item.attribute_values) return resultList
def users_user_id_put(userId, newUser): """ method to update a user using user id """ try: user = User.get(userId) except Exception: return 'User with id=%s does not exist.' % (userId) # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newuser: try: building = Building.get(newuser['buildingId']) except Exception as e: return 'Building with specified ID does not exist' attributes = user.attribute_values.keys() for key in newUser.keys(): if key in attributes and key is not 'id': user.update_item(key, value=newUser[key], action='PUT') return 'User with id=%s updated successfully.' % (userId)
def sensors_sensor_id_put(sensorId, newSensor): """ method to update sensor using sensor id """ try: sensor = Sensor.get(sensorId) except Exception: return 'Sensor with id=%s does not exist.' % (sensorId) # Need to check that if building ID is changed, the new Building exists if 'buildingId' in newSensor: try: building = Building.get(newSensor['buildingId']) except Exception as e: return 'Building with specified ID does not exist' attributes = sensor.attribute_values.keys() for key in newSensor.keys(): if key in attributes and key is not 'id': sensor.update_item(key, value=newSensor[key], action='PUT') return 'Sensor with id=%s updated successfully.' % (sensorId)