async def add_action_cb(req: rpc.project.AddActionRequest, ui: WsClient) -> None: assert glob.PROJECT assert glob.SCENE ap = glob.PROJECT.action_point(req.args.action_point_id) unique_name(req.args.name, glob.PROJECT.action_user_names()) if not hlp.is_valid_identifier(req.args.name): raise Arcor2Exception("Action name has to be valid Python identifier.") new_action = common.Action(common.uid(), req.args.name, req.args.type, req.args.parameters) updated_project = copy.deepcopy(glob.PROJECT) updated_ap = updated_project.action_point(req.args.action_point_id) updated_ap.actions.append(new_action) check_action_params(updated_project, new_action, find_object_action(new_action)) if req.dry_run: return None ap.actions.append(new_action) glob.PROJECT.update_modified() asyncio.ensure_future( notif.broadcast_event( events.ActionChanged(events.EventType.ADD, ap.id, data=new_action))) return None
async def build_and_upload_package(project_id: str, package_name: str) -> str: """ Builds package and uploads it to the Execution unit. :param project_id: :param package_name: :return: generated package ID. """ package_id = common.uid() # call build service # TODO store data in memory with tempfile.TemporaryDirectory() as tmpdirname: path = os.path.join(tmpdirname, "publish.zip") await hlp.run_in_executor( rest.download, f"{glob.BUILDER_URL}/project/{project_id}/publish", path, None, {"package_name": package_name}) with open(path, "rb") as zip_file: b64_bytes = base64.b64encode(zip_file.read()) b64_str = b64_bytes.decode() # send data to execution service exe_req = rpc.execution.UploadPackageRequest( uuid.uuid4().int, args=rpc.execution.UploadPackageArgs(package_id, b64_str)) exe_resp = await manager_request(exe_req) if not exe_resp.result: if not exe_resp.messages: raise Arcor2Exception("Upload to the Execution unit failed.") raise Arcor2Exception("\n".join(exe_resp.messages)) return package_id
async def new_scene_cb(req: rpc.scene.NewSceneRequest, ui: WsClient) -> None: """ Creates and opens a new scene on the server. Fails if any scene is open or if scene id/name already exists. :param req: :return: """ if glob.PACKAGE_STATE.state in (common.PackageStateEnum.PAUSED, common.PackageStateEnum.RUNNING): raise Arcor2Exception("Can't create scene while package runs.") assert glob.SCENE is None for scene_id in (await storage.get_scenes()).items: if req.args.name == scene_id.name: raise Arcor2Exception("Name already used.") if req.dry_run: return None glob.SCENE = common.Scene(common.uid(), req.args.name, desc=req.args.desc) asyncio.ensure_future( notif.broadcast_event( events.OpenScene(data=events.OpenSceneData(glob.SCENE)))) return None
async def add_action_point_orientation_using_robot_cb( req: rpc.project.AddActionPointOrientationUsingRobotRequest, ui: WsClient) -> None: """ Adds orientation and joints to the action point. :param req: :return: """ assert glob.SCENE and glob.PROJECT ap = glob.PROJECT.action_point(req.args.action_point_id) unique_name(req.args.name, ap.orientation_names()) if req.dry_run: return None new_pose = await get_end_effector_pose(req.args.robot.robot_id, req.args.robot.end_effector) if ap.parent: new_pose = tr.make_pose_rel_to_parent(glob.SCENE, glob.PROJECT, new_pose, ap.parent) orientation = common.NamedOrientation(common.uid(), req.args.name, new_pose.orientation) ap.orientations.append(orientation) glob.PROJECT.update_modified() asyncio.ensure_future( notif.broadcast_event( events.OrientationChanged(events.EventType.ADD, ap.id, data=orientation))) return None
async def add_action_point_orientation_cb( req: rpc.project.AddActionPointOrientationRequest, ui: WsClient) -> None: """ Adds orientation and joints to the action point. :param req: :return: """ assert glob.SCENE and glob.PROJECT ap = glob.PROJECT.action_point(req.args.action_point_id) unique_name(req.args.name, ap.orientation_names()) if req.dry_run: return None orientation = common.NamedOrientation(common.uid(), req.args.name, req.args.orientation) ap.orientations.append(orientation) glob.PROJECT.update_modified() asyncio.ensure_future( notif.broadcast_event( events.OrientationChanged(events.EventType.ADD, ap.id, data=orientation))) return None
def main() -> None: # robots aubo = Aubo(uid("rbt"), "Whatever", Pose(), RobotSettings("http://127.0.0.1:13000", "aubo")) simatic = Aubo(uid("rbt"), "Whatever", Pose(), RobotSettings("http://127.0.0.1:13001", "simatic")) # objects with pose, with 'System' and 'Configurations' controllers barcode = Barcode(uid("srv"), "Whatever", Pose(), settings=Settings("http://127.0.0.1:14000", "simulator")) search = Search(uid("srv"), "Whatever", Pose(), settings=Settings("http://127.0.0.1:12000", "simulator")) ict = Ict(uid("srv"), "Whatever", Pose(), settings=Settings("http://127.0.0.1:19000", "simulator")) # objects without pose, without 'System' and 'Configurations' controllers interaction = Interaction(uid("srv"), "Whatever", SimpleSettings("http://127.0.0.1:17000")) statistic = Statistic(uid("srv"), "Whatever", SimpleSettings("http://127.0.0.1:16000")) # Add @action decorator to all actions of each object using patch_object_actions. # It has to be called exactly once for each type! # Certain functions of Execution unit (as e.g. pausing script execution) would not work without this step. # Note: in a generated script, this is done within the Resources context manager. # Side effect: a lot of JSON data will be printed out to the console when running the script manually. patch_object_actions(Aubo) patch_object_actions(Barcode) patch_object_actions(Search) patch_object_actions(Ict) patch_object_actions(Interaction) patch_object_actions(Statistic) scene_service.delete_all_collisions() scene_service.upsert_collision(Box("box_id", 0.1, 0.1, 0.1), Pose()) scene_service.start() # this is normally done by auto-generated Resources class aubo.move("suction", Pose(), MoveTypeEnum.SIMPLE, safe=False) simatic.set_joints(ProjectRobotJoints("", "", [Joint("x", 0), Joint("y", 0)]), MoveTypeEnum.SIMPLE) barcode.scan() search.set_search_parameters(SearchEngineParameters(search_log_level=SearchLogLevel(LogLevel.DEBUG))) search.grab_image() interaction.add_notification("Test", NotificationLevelEnum.INFO) try: statistic.get_groups() except rest.RestHttpException as e: # service returned error code print(f"The error code is {e.error_code}.") except rest.RestException as e: # request totally failed (timeout, connection error, etc) print(str(e)) if ict.ready(): test = ict.test("OK") print(test) scene_service.stop() # this is normally done by auto-generated Resources class
async def add_object_to_scene_cb(req: rpc.scene.AddObjectToSceneRequest, ui: WsClient) -> None: assert glob.SCENE obj = common.SceneObject(common.uid(), req.args.name, req.args.type, req.args.pose) await add_object_to_scene(obj, dry_run=req.dry_run) if req.dry_run: return None glob.SCENE.update_modified() asyncio.ensure_future( notif.broadcast_event( events.SceneObjectChanged(events.EventType.ADD, data=obj))) return None
async def new_project_cb(req: rpc.project.NewProjectRequest, ui: WsClient) -> None: if glob.PACKAGE_STATE.state in (common.PackageStateEnum.PAUSED, common.PackageStateEnum.RUNNING): raise Arcor2Exception("Can't create project while package runs.") unique_name(req.args.name, (await project_names())) if req.dry_run: return None if glob.SCENE: if glob.SCENE.id != req.args.scene_id: raise Arcor2Exception("Another scene is opened.") if glob.SCENE.has_changes(): await storage.update_scene(glob.SCENE) glob.SCENE.modified = (await storage.get_scene(glob.SCENE.id)).modified else: if req.args.scene_id not in { scene.id for scene in (await storage.get_scenes()).items }: raise Arcor2Exception("Unknown scene id.") await open_scene(req.args.scene_id) glob.PROJECT = common.Project(common.uid(), req.args.name, req.args.scene_id, desc=req.args.desc, has_logic=req.args.has_logic) assert glob.SCENE asyncio.ensure_future( notif.broadcast_event( events.OpenProject( data=events.OpenProjectData(glob.SCENE, glob.PROJECT)))) return None
async def managed_scene(scene_id: str, make_copy: bool = False): save_back = False if glob.SCENE and glob.SCENE.id == scene_id: if make_copy: scene = copy.deepcopy(glob.SCENE) save_back = True else: scene = glob.SCENE else: save_back = True scene = await storage.get_scene(scene_id) if make_copy: scene.id = common.uid() try: yield scene finally: if save_back: asyncio.ensure_future(storage.update_scene(scene))
async def managed_project(project_id: str, make_copy: bool = False): save_back = False if glob.PROJECT and glob.PROJECT.id == project_id: if make_copy: project = copy.deepcopy(glob.PROJECT) save_back = True else: project = glob.PROJECT else: save_back = True project = await storage.get_project(project_id) if make_copy: project.id = common.uid() try: yield project finally: if save_back: asyncio.ensure_future(storage.update_project(project))
async def add_action_point_joints_cb( req: rpc.project.AddActionPointJointsRequest, ui: WsClient) -> None: assert glob.SCENE and glob.PROJECT ap = glob.PROJECT.action_point(req.args.action_point_id) unique_name(req.args.name, ap.joints_names()) if req.dry_run: return None new_joints = await get_robot_joints(req.args.robot_id) prj = common.ProjectRobotJoints(common.uid(), req.args.name, req.args.robot_id, new_joints, True) ap.robot_joints.append(prj) glob.PROJECT.update_modified() asyncio.ensure_future( notif.broadcast_event( events.JointsChanged(events.EventType.ADD, ap.id, data=prj))) return None
async def add_action_point_cb(req: rpc.project.AddActionPointRequest, ui: WsClient) -> None: assert glob.SCENE assert glob.PROJECT unique_name(req.args.name, glob.PROJECT.action_points_names) check_ap_parent(req.args.parent) if not hlp.is_valid_identifier(req.args.name): raise Arcor2Exception("Name has to be valid Python identifier.") if req.dry_run: return None ap = common.ProjectActionPoint(common.uid(), req.args.name, req.args.position, req.args.parent) glob.PROJECT.action_points.append(ap) glob.PROJECT.update_modified() asyncio.ensure_future( notif.broadcast_event( events.ActionPointChanged(events.EventType.ADD, data=ap))) return None
def main(res: Resources) -> None: statistic = Statistic(uid("obj"), "Whatever", SimpleSettings("http://127.0.0.1:16000")) statistic.add_value(res.project.id, "value_name", 1.0, an="action_name")