def test_object_parameters(start_processes: None, ars: ARServer, scene: Scene) -> None: assert ars.call_rpc(rpc.s.OpenScene.Request(uid(), IdArgs(scene.id)), rpc.s.OpenScene.Response).result event(ars, events.s.OpenScene) event(ars, events.s.SceneState) req_params = [ # TODO create settings_to_params and use it Parameter("str_param", "string", json.dumps("str_param")), Parameter("float_param", "double", json.dumps(1.0)), Parameter("int_param", "integer", json.dumps(1)), Parameter("bool_param", "boolean", json.dumps(True)), ] assert ars.call_rpc( rpc.s.AddObjectToScene.Request( uid(), rpc.s.AddObjectToScene.Request.Args("ows", ObjectWithSettings.__name__, parameters=req_params) ), rpc.s.AddObjectToScene.Response, ).result soc = event(ars, events.s.SceneObjectChanged) assert soc.change_type assert soc.change_type == soc.change_type.ADD assert soc.data.type == ObjectWithSettings.__name__ assert soc.data.parameters == req_params obj_id = soc.data.id req_params2 = [ # TODO create settings_to_params and use it Parameter("str_param", "string", json.dumps("str_param2")), Parameter("float_param", "double", json.dumps(2.0)), Parameter("int_param", "integer", json.dumps(2)), Parameter("bool_param", "boolean", json.dumps(True)), ] lock_object(ars, soc.data.id) assert ars.call_rpc( rpc.s.UpdateObjectParameters.Request( uid(), rpc.s.UpdateObjectParameters.Request.Args(soc.data.id, parameters=req_params2) ), rpc.s.AddObjectToScene.Response, ).result soc2 = event(ars, events.s.SceneObjectChanged) assert soc2.change_type == soc.change_type.UPDATE assert soc2.data.type == ObjectWithSettings.__name__ assert soc2.data.parameters == req_params2 unlock_object(ars, soc.data.id) # let's continue with testing overrides assert ars.call_rpc( rpc.p.NewProject.Request(uid(), rpc.p.NewProject.Request.Args(scene.id, "Project name")), rpc.p.NewProject.Response, ).result event(ars, events.p.OpenProject) event(ars, events.s.SceneState) assert not ars.call_rpc( rpc.o.DeleteOverride.Request( uid(), rpc.o.DeleteOverride.Request.Args(obj_id, Parameter("bool_param", "boolean", json.dumps(True))) ), rpc.o.DeleteOverride.Response, ).result assert not ars.call_rpc( rpc.o.UpdateOverride.Request( uid(), rpc.o.UpdateOverride.Request.Args(obj_id, Parameter("bool_param", "boolean", json.dumps(True))) ), rpc.o.UpdateOverride.Response, ).result # int_param has to be set to special value, so without override, StartScene should fail now assert ars.call_rpc( rpc.s.StartScene.Request(uid()), rpc.s.StartScene.Response, ).result assert event(ars, events.s.SceneState).data.state == events.s.SceneState.Data.StateEnum.Starting assert event(ars, events.s.SceneState).data.state == events.s.SceneState.Data.StateEnum.Stopping assert event(ars, events.s.SceneState).data.state == events.s.SceneState.Data.StateEnum.Stopped lock_object(ars, obj_id) override = Parameter("int_param", "integer", json.dumps(ObjectWithSettings.INT_PARAM_SPECIAL_VALUE)) assert ars.call_rpc( rpc.o.AddOverride.Request(uid(), rpc.o.AddOverride.Request.Args(obj_id, override)), rpc.o.AddOverride.Response, ).result override_evt = event(ars, events.o.OverrideUpdated) assert override_evt.data == override assert override_evt.change_type assert override_evt.change_type == override_evt.change_type.ADD assert override_evt.parent_id == obj_id override2 = Parameter("str_param", "string", json.dumps("test")) assert ars.call_rpc( rpc.o.AddOverride.Request(uid(), rpc.o.AddOverride.Request.Args(obj_id, override2)), rpc.o.AddOverride.Response, ).result event(ars, events.o.OverrideUpdated) unlock_object(ars, obj_id) save_project(ars) # now it should be possible to start the scene assert ars.call_rpc( rpc.s.StartScene.Request(uid()), rpc.s.StartScene.Response, ).result assert event(ars, events.s.SceneState).data.state == events.s.SceneState.Data.StateEnum.Starting assert event(ars, events.s.SceneState).data.state == events.s.SceneState.Data.StateEnum.Started
def test_object_parameters(start_processes: None, ars: ARServer, scene: Scene) -> None: assert ars.call_rpc(rpc.s.OpenScene.Request(uid(), IdArgs(scene.id)), rpc.s.OpenScene.Response).result event(ars, events.s.OpenScene) assert ars.call_rpc( rpc.s.AddObjectToScene.Request( uid(), rpc.s.AddObjectToScene.Request.Args("ows", ObjectWithActions.__name__)), rpc.s.AddObjectToScene.Response, ).result obj = event(ars, events.s.SceneObjectChanged).data assert obj is not None assert ars.call_rpc( rpc.p.NewProject.Request( uid(), rpc.p.NewProject.Request.Args(scene.id, "Project name")), rpc.p.NewProject.Response, ).result event(ars, events.p.OpenProject) assert ars.call_rpc( rpc.p.AddActionPoint.Request( uid(), rpc.p.AddActionPoint.Request.Args("ap", Position())), rpc.p.AddActionPoint.Response, ).result ap = event(ars, events.p.ActionPointChanged).data assert ap is not None assert ars.call_rpc( rpc.p.AddAction.Request( uid(), rpc.p.AddAction.Request.Args( ap.id, "a1", f"{obj.id}/{ObjectWithActions.bool_action.__name__}", [], [Flow(outputs=["bool_result"])], ), ), rpc.p.AddAction.Response, ).result a1 = event(ars, events.p.ActionChanged).data assert a1 is not None assert ars.call_rpc( rpc.p.AddAction.Request( uid(), rpc.p.AddAction.Request.Args( ap.id, "a2", f"{obj.id}/{ObjectWithActions.str_action.__name__}", [], [Flow()], ), ), rpc.p.AddAction.Response, ).result a2 = event(ars, events.p.ActionChanged).data assert a2 is not None add_logic_item(ars, LogicItem.START, a1.id) add_logic_item( ars, a1.id, a2.id, ProjectLogicIf(f"{a1.id}/{FlowTypes.DEFAULT}/{0}", json.dumps(True))) add_logic_item(ars, a2.id, LogicItem.END) add_logic_item( ars, a1.id, LogicItem.END, ProjectLogicIf(f"{a1.id}/{FlowTypes.DEFAULT}/{0}", json.dumps(False))) # TODO try to add some invalid connections here? save_project(ars)
def test_run_simple_project(start_processes: None, ars: ARServer) -> None: event(ars, events.c.ShowMainScreen) assert ars.call_rpc( rpc.s.NewScene.Request(get_id(), rpc.s.NewScene.Request.Args("Test scene")), rpc.s.NewScene.Response).result scene_data = event(ars, events.s.OpenScene).data assert scene_data scene = scene_data.scene event(ars, events.s.SceneState) assert ars.call_rpc( rpc.s.AddObjectToScene.Request( get_id(), rpc.s.AddObjectToScene.Request.Args("time_actions", TimeActions.__name__)), rpc.s.AddObjectToScene.Response, ).result obj = event(ars, events.s.SceneObjectChanged).data assert obj assert ars.call_rpc( rpc.s.AddObjectToScene.Request( get_id(), rpc.s.AddObjectToScene.Request.Args("random_actions", RandomActions.__name__)), rpc.s.AddObjectToScene.Response, ).result obj2 = event(ars, events.s.SceneObjectChanged).data assert obj2 # ------------------------------------------------------------------------------------------------------------------ assert ars.call_rpc( rpc.p.NewProject.Request( get_id(), rpc.p.NewProject.Request.Args(scene.id, "Project name")), rpc.p.NewProject.Response, ).result event(ars, events.s.SceneSaved) proj = event(ars, events.p.OpenProject).data assert proj event(ars, events.s.SceneState) assert ars.call_rpc( rpc.p.AddActionPoint.Request( get_id(), rpc.p.AddActionPoint.Request.Args("ap1", common.Position())), rpc.p.AddActionPoint.Response, ).result ap = event(ars, events.p.ActionPointChanged).data assert ap is not None assert ars.call_rpc( rpc.p.AddProjectParameter.Request( get_id(), rpc.p.AddProjectParameter.Request.Args("min_time", "double", json.dumps(0.45))), rpc.p.AddActionPoint.Response, ).result c1 = event(ars, events.p.ProjectParameterChanged).data assert c1 assert ars.call_rpc( rpc.p.AddAction.Request( get_id(), rpc.p.AddAction.Request.Args( ap.id, "test_action", f"{obj2.id}/{RandomActions.random_double.__name__}", [ common.ActionParameter( "range_min", common.ActionParameter.TypeEnum.PROJECT_PARAMETER, json.dumps(c1.id)), common.ActionParameter("range_max", "double", "0.55"), ], [common.Flow(outputs=["random_value"])], ), ), rpc.p.AddAction.Response, ).result action = event(ars, events.p.ActionChanged).data assert action is not None assert ars.call_rpc( rpc.p.AddAction.Request( get_id(), rpc.p.AddAction.Request.Args( ap.id, "test_action2", f"{obj.id}/{TimeActions.sleep.__name__}", [ common.ActionParameter( "seconds", common.ActionParameter.TypeEnum.LINK, json.dumps( f"{action.id}/{common.FlowTypes.DEFAULT}/0"), ) ], [common.Flow()], ), ), rpc.p.AddAction.Response, ).result action2 = event(ars, events.p.ActionChanged).data assert action2 is not None add_logic_item(ars, common.LogicItem.START, action.id) event(ars, events.lk.ObjectsUnlocked) add_logic_item(ars, action.id, action2.id) event(ars, events.lk.ObjectsUnlocked) add_logic_item(ars, action2.id, common.LogicItem.END) event(ars, events.lk.ObjectsUnlocked) save_project(ars) LOGGER.debug(project_service.get_project(proj.project.id)) # TODO test also temporary package close_project(ars) # ------------------------------------------------------------------------------------------------------------------ event(ars, events.c.ShowMainScreen) assert ars.call_rpc( rpc.b.BuildProject.Request( get_id(), rpc.b.BuildProject.Request.Args(proj.project.id, "Package name")), rpc.b.BuildProject.Response, ).result package = event(ars, eevents.PackageChanged).data assert package is not None assert ars.call_rpc( erpc.RunPackage.Request(get_id(), erpc.RunPackage.Request.Args(package.id)), erpc.RunPackage.Response).result ps = event(ars, arcor2_events.PackageState).data assert ps assert ps.package_id == package.id assert ps.state == ps.state.RUNNING pi = event(ars, arcor2_events.PackageInfo).data assert pi assert pi.package_id == package.id # random_double action act_state_before = event(ars, arcor2_events.ActionStateBefore).data assert act_state_before assert act_state_before.action_id == action.id assert act_state_before.parameters assert len(act_state_before.parameters) == 2 act_state_after = event(ars, arcor2_events.ActionStateAfter).data assert act_state_after assert act_state_after.action_id == action.id assert act_state_after.results assert len(act_state_after.results) == 1 assert isinstance(json.loads(act_state_after.results[0]), float) # sleep action act2_state_before = event(ars, arcor2_events.ActionStateBefore).data assert act2_state_before assert act2_state_before.action_id == action2.id assert act2_state_before.parameters assert len(act2_state_before.parameters) == 1 act2_state_after = event(ars, arcor2_events.ActionStateAfter).data assert act2_state_after assert act2_state_after.action_id == action2.id assert act2_state_after.results is None # TODO pause, resume assert ars.call_rpc(erpc.StopPackage.Request(get_id()), erpc.StopPackage.Response).result ps2 = wait_for_event(ars, arcor2_events.PackageState).data assert ps2 assert ps2.package_id == package.id assert ps2.state == ps.state.STOPPING ps3 = wait_for_event(ars, arcor2_events.PackageState).data assert ps3 assert ps3.package_id == package.id assert ps3.state == ps.state.STOPPED show_main_screen_event = event(ars, events.c.ShowMainScreen) assert show_main_screen_event.data assert show_main_screen_event.data.what == events.c.ShowMainScreen.Data.WhatEnum.PackagesList assert show_main_screen_event.data.highlight == package.id
def test_project_basic_rpcs(start_processes: None, ars: ARServer, scene: common.Scene) -> None: # first, there are no projects projects = ars.call_rpc(rpc.p.ListProjects.Request(uid()), rpc.p.ListProjects.Response) assert projects.result assert not projects.data project_name = "Test project" # attempt to use non-existent scene_id assert not ars.call_rpc( rpc.p.NewProject.Request( uid(), rpc.p.NewProject.Request.Args("some non-sense string", project_name)), rpc.p.NewProject.Response, ).result # attempt to open non-existent project assert not ars.call_rpc( rpc.p.OpenProject.Request(uid(), IdArgs("some-random-nonsense")), rpc.p.OpenProject.Response).result # correct scene_id assert ars.call_rpc( rpc.p.NewProject.Request( uid(), rpc.p.NewProject.Request.Args(scene.id, project_name)), rpc.p.NewProject.Response, ).result open_project_evt = event(ars, events.p.OpenProject) event(ars, events.s.SceneState) assert open_project_evt.data assert open_project_evt.change_type is None assert open_project_evt.parent_id is None assert open_project_evt.data.scene.id == scene.id project_id = open_project_evt.data.project.id assert open_project_evt.data.project.name == project_name assert not open_project_evt.data.project.action_points assert not open_project_evt.data.project.parameters assert not open_project_evt.data.project.functions assert not open_project_evt.data.project.logic # attempt to create project while another project is opened assert not ars.call_rpc( rpc.p.NewProject.Request( uid(), rpc.p.NewProject.Request.Args("some non-sense string", "blah")), rpc.p.NewProject.Response, ).result save_project(ars) close_project(ars) show_main_screen_event = event(ars, events.c.ShowMainScreen) assert show_main_screen_event.data assert show_main_screen_event.data.what == events.c.ShowMainScreen.Data.WhatEnum.ProjectsList assert show_main_screen_event.data.highlight == project_id list_of_projects = ars.call_rpc(rpc.p.ListProjects.Request(uid()), rpc.p.ListProjects.Response) assert list_of_projects.result assert list_of_projects.data assert len(list_of_projects.data) == 1 assert list_of_projects.data[0].id == project_id """ TODO: fix this with ARServer(WS_CONNECTION_STR, timeout=10, event_mapping=event_mapping) as ars_2: smse = event(ars_2, events.c.ShowMainScreen) assert smse.data assert smse.data.what == events.c.ShowMainScreen.Data.WhatEnum.ProjectsList assert smse.data.highlight is None """ # it should not be possible to delete scene used by a project assert not ars.call_rpc(rpc.s.DeleteScene.Request(uid(), IdArgs(scene.id)), rpc.s.DeleteScene.Response).result assert ars.call_rpc(rpc.p.DeleteProject.Request(uid(), IdArgs(project_id)), rpc.p.DeleteProject.Response).result project_changed_evt = event(ars, events.p.ProjectChanged) assert project_changed_evt.data assert project_changed_evt.data.id == project_id assert project_changed_evt.change_type == Event.Type.REMOVE assert ars.call_rpc(rpc.s.DeleteScene.Request(uid(), IdArgs(scene.id)), rpc.s.DeleteScene.Response).result scene_changed_evt = event(ars, events.s.SceneChanged) assert scene_changed_evt.data assert scene_changed_evt.data.id == scene.id assert scene_changed_evt.change_type == Event.Type.REMOVE list_of_projects_2 = ars.call_rpc(rpc.p.ListProjects.Request(uid()), rpc.p.ListProjects.Response) assert list_of_projects_2.result assert not list_of_projects_2.data