def UpdateWaypoints(robotId, waypoints): """ Update specified robot's waypoints """ if robotId is None: return BuildJsonResponse(False, 'No robot ID detected') if not isinstance(waypoints, list): return BuildJsonResponse(False, 'Waypoints must be formatted as a list') for waypoint in waypoints: if not isinstance(waypoint, list) or not len(waypoint) == 2: return BuildJsonResponse(False, 'Waypoints must be formatted as [xPos, yPos]') Waypoint.objects.filter(robotId = robotId, realWaypoint = False).delete() result = GetSpecificRobot(robotId) if not IsOperationSuccess(result): return result for waypoint in waypoints: newWaypoint = Waypoint(robotId = result['robot'], realWaypoint = False, x = int(waypoint[0]), y = int(waypoint[1])) newWaypoint.save() return BuildJsonResponse(True, 'Saved waypoints successfully')
def RobotAction(params, body, method): """ Performs actions required by robot """ robotId = GetRobotId(params) if method == "POST": # Parse input result = ParseRobotRequest(robotId, body) if not IsOperationSuccess(result): return result robotRequest = result['result'] # Update cells in map if specified if 'cells' in robotRequest: if not CheckMap(): return BuildJsonResponse(False, 'Map has not been built yet') for cell in robotRequest['cells']: if cell.ValidatePopulated(): mapData = Map.objects.get(x = cell.coordinate.x, y = cell.coordinate.y) mapData.state = cell.state mapData.save() # Create or update robot data in DB if specified if 'robot' in robotRequest: robotUpdates = robotRequest['robot'] if robotId is not None: result = GetSpecificRobot(robotId) if not IsOperationSuccess(result): return result robotDB = result['robot'] if robotUpdates.PositionExists(): robotDB.xPos = robotUpdates.position.x robotDB.yPos = robotUpdates.position.y if robotUpdates.ExactPositionExists(): robotDB.xExactPos = robotUpdates.exactPosition.x robotDB.yExactPos = robotUpdates.exactPosition.y newWaypoint = Waypoint(robotId = robotDB, realWaypoint = True, x = robotUpdates.exactPosition.x, y = robotUpdates.exactPosition.y) newWaypoint.save() robotDB.save() elif not robotUpdates.PositionExists() or not robotUpdates.ExactPositionExists(): return BuildJsonResponse(False, 'On creation, a robot must have an estimated position and exact position') else: robotDB = Robot(xPos = robotUpdates.position.x, yPos = robotUpdates.position.y, xExactPos = robotUpdates.exactPosition.x, yExactPos = robotUpdates.exactPosition.y) robotDB.save() newWaypoint = Waypoint(robotId = robotDB, realWaypoint = True, x = robotUpdates.exactPosition.x, y = robotUpdates.exactPosition.y) newWaypoint.save() robotId = robotDB.pk if robotUpdates.WaypointsExist(): result = UpdateWaypoints(robotId, robotUpdates.waypoints) if not IsOperationSuccess(result): result['robotId'] = robotId return result # Create a response message response = BuildJsonResponse(True, 'Successfully updated database') if 'robot' in robotRequest: response['robotId'] = robotId elif robotId is None: response = BuildJsonResponse(False, 'GET or DELETE requests must have a robot ID') elif method == "GET": # Access robot from DB result = GetSpecificRobot(robotId) if not IsOperationSuccess(result): return result waypoints = Waypoint.objects.filter(robotId = robotId, realWaypoint = False) robot = MapRobotDBToRobotObj(result['robot'], waypoints) response = BuildJsonResponse(True, 'Successfully got robot data') response['robot'] = robot.Dictify() elif method == "DELETE": # Find action to perform (Preserve original behaviour) result = TextToJson(body) if not IsOperationSuccess(result): return ClearRobot(robotId) rawData = result['rawData'] if 'data' not in rawData: return BuildJsonResponse(False, 'Must specify what sort of thing to delete') robotId = GetRobotId(params) if rawData['data'] == 'instruction': response = ResetInstruction(robotId) elif rawData['data'] == 'robot': response = ClearRobot(robotId) else: response = BuildJsonResponse(False, 'Specify instruction or robot in data field') else: response = BuildJsonResponse(False, 'Must use GET, POST, or DELETE') return response