Ejemplo n.º 1
0
def test_objects(start_processes: None, ars: ARServer) -> None:

    assert isinstance(ars.get_event(), events.c.ShowMainScreen)

    res = ars.call_rpc(rpc.o.GetObjectTypes.Request(get_id()),
                       rpc.o.GetObjectTypes.Response)
    assert res.result
    assert res.data is not None

    assert {obj.type
            for obj in res.data if not obj.built_in
            } == {LoggingTestObject.__name__, LoggingTestRobot.__name__}

    for obj in res.data:
        assert not obj.disabled, f"ObjectType {obj.type} disabled. {obj.problem}"

        actions = ars.call_rpc(
            rpc.o.GetActions.Request(get_id(), TypeArgs(obj.type)),
            rpc.o.GetActions.Response)
        assert actions.result
        assert actions.data is not None

        for act in actions.data:
            assert act.disabled == (act.problem is not None)
            assert not act.disabled, f"Action {act.name} of {obj.type} disabled. {act.problem}"
Ejemplo n.º 2
0
def test_robot_meta(start_processes: None, ars: ARServer) -> None:

    assert isinstance(ars.get_event(), events.c.ShowMainScreen)

    res = ars.call_rpc(rpc.r.GetRobotMeta.Request(get_id()), rpc.r.GetRobotMeta.Response)
    assert res.result
    assert res.data is not None

    robots: dict[str, RobotMeta] = {robot.type: robot for robot in res.data}

    magician = robots[DobotMagician.__name__]
    assert magician.features.move_to_pose
    assert magician.features.move_to_joints
    assert magician.features.inverse_kinematics
    assert magician.features.forward_kinematics
    assert not magician.features.stop
    assert not magician.features.hand_teaching

    m1 = robots[DobotM1.__name__]
    assert m1.features.move_to_pose
    assert m1.features.move_to_joints
    assert m1.features.hand_teaching
    assert not m1.features.inverse_kinematics
    assert not m1.features.forward_kinematics
    assert not m1.features.stop
Ejemplo n.º 3
0
def test_valid_object_types(start_processes: None, ars: ARServer, model: ObjectModel) -> None:

    event(ars, events.c.ShowMainScreen)

    type_name = "TestType"

    model.model().id = type_name

    if model.mesh:
        project_service.upload_file(mesh_filename, b"")

    assert ars.call_rpc(
        rpc.o.NewObjectType.Request(
            get_id(), objects.ObjectTypeMeta(type_name, base=CollisionObject.__name__, object_model=model)
        ),
        rpc.o.NewObjectType.Response,
    ).result

    evt = event(ars, events.o.ChangedObjectTypes)

    assert len(evt.data) == 1
    assert evt.data[0].has_pose
    assert evt.data[0].type == type_name
    assert evt.data[0].base == CollisionObject.__name__
    assert evt.data[0].object_model == model

    project_service.get_model(model.model().id, model.type)
    project_service.get_object_type(type_name)
Ejemplo n.º 4
0
def test_lock_events(start_processes: None, ars: ARServer, scene: cmn.Scene, project: cmn.Project) -> None:

    assert ars.call_rpc(rpc.p.OpenProject.Request(get_id(), IdArgs(project.id)), rpc.p.OpenProject.Response).result
    prj_evt = event(ars, events.p.OpenProject)

    event(ars, events.s.SceneState)

    prj = prj_evt.data.project

    # get default project objects
    ap = next(ap for ap in prj.action_points if ap.name == "ap")
    ap_ap = next(ap for ap in prj.action_points if ap.name == "ap_ap")
    ap_ap_ap = next(ap for ap in prj.action_points if ap.name == "ap_ap_ap")
    ori = ap_ap_ap.orientations[0]

    lock_object(ars, ap.id, True)

    # lock object and expect event about it on newly logged UI
    ars2 = ARServer(ars_connection_str(), timeout=30, event_mapping=event_mapping)
    event(ars2, events.p.OpenProject)
    event(ars2, events.s.SceneState)
    second_ui = "ars2"
    assert ars2.call_rpc(
        rpc.u.RegisterUser.Request(get_id(), rpc.u.RegisterUser.Request.Args(second_ui)),
        rpc.u.RegisterUser.Response,
    ).result
    locked_evt = event(ars2, events.lk.ObjectsLocked)
    assert locked_evt.data.owner == "testUser"
    for obj_id in (ap.id, ap_ap.id, ap_ap_ap.id, ori.id):
        assert obj_id in locked_evt.data.object_ids

    # attempt to lock/unlock objects locked by someone else
    for obj_id in (ap.id, ap_ap.id, ap_ap_ap.id, ori.id):
        # lock
        assert not ars2.call_rpc(
            rpc.lock.WriteLock.Request(get_id(), rpc.lock.WriteLock.Request.Args(obj_id)), rpc.lock.WriteLock.Response
        ).result
        # lock tree
        assert not ars2.call_rpc(
            rpc.lock.WriteLock.Request(get_id(), rpc.lock.WriteLock.Request.Args(obj_id, True)),
            rpc.lock.WriteLock.Response,
        ).result
        # unlock
        assert not ars2.call_rpc(
            rpc.lock.WriteUnlock.Request(get_id(), rpc.lock.WriteUnlock.Request.Args(obj_id)),
            rpc.lock.WriteUnlock.Response,
        ).result

    unlock_object(ars, ap.id)
    event(ars2, events.lk.ObjectsUnlocked)

    # test lock will stay locked after logout for a while
    lock_object(ars2, ori.id)
    event(ars, events.lk.ObjectsLocked)

    ars2.close()

    # wait for some time
    sleep(2)  # TODO fill this sleep with another actions

    # register again and check if objects still locked
    ars2 = ARServer(ars_connection_str(), timeout=30, event_mapping=event_mapping)
    event(ars2, events.p.OpenProject)
    event(ars2, events.s.SceneState)
    assert ars2.call_rpc(
        rpc.u.RegisterUser.Request(get_id(), rpc.u.RegisterUser.Request.Args("ars2")),
        rpc.u.RegisterUser.Response,
    ).result
    locked_evt = event(ars2, events.lk.ObjectsLocked)
    assert locked_evt.data.owner == second_ui
    assert len(locked_evt.data.object_ids) == 1
    assert ori.id in locked_evt.data.object_ids
    ars2.close()
Ejemplo n.º 5
0
def test_list_problems(start_processes: None, ars: ARServer) -> None:
    """This is a test for a bug in get_scene_problems and get_project_problems
    functions.

    Because of the bug, the RPC returned result=False when there was non-existing ObjectType.

    :param start_processes:
    :param ars:
    :return:
    """

    upload_def(DummyMultiArmRobot
               )  # OT has to exist first, otherwise scene can't be stored
    invalid_scene_1 = Scene("invalidScene",
                            objects=[
                                SceneObject("invalidObject",
                                            DummyMultiArmRobot.__name__,
                                            Pose())
                            ])
    invalid_project_1 = Project("invalidProject", invalid_scene_1.id)
    project_service.update_scene(invalid_scene_1)
    project_service.update_project(invalid_project_1)
    project_service.delete_object_type(
        DummyMultiArmRobot.__name__)  # now the OT can be deleted

    # initial event
    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.ScenesList

    scenes = ars.call_rpc(rpc.s.ListScenes.Request(get_id()),
                          rpc.s.ListScenes.Response)
    assert scenes.result
    assert scenes.data
    assert len(scenes.data) == 1
    assert scenes.data[0].id == invalid_scene_1.id
    assert scenes.data[0].name == invalid_scene_1.name
    assert scenes.data[0].problems
    assert len(scenes.data[0].problems) == 1

    projects = ars.call_rpc(rpc.p.ListProjects.Request(get_id()),
                            rpc.p.ListProjects.Response)
    assert projects.result
    assert projects.data
    assert len(projects.data) == 1
    assert projects.data[0].id == invalid_project_1.id
    assert projects.data[0].name == invalid_project_1.name
    assert projects.data[0].problems
    assert len(projects.data[0].problems) == 1

    upload_def(DummyMultiArmRobot)

    # now the ObjectType exists, so the scene/project should be ok
    scenes2 = ars.call_rpc(rpc.s.ListScenes.Request(get_id()),
                           rpc.s.ListScenes.Response)
    assert scenes2.result
    assert scenes2.data
    assert len(scenes2.data) == 1
    assert scenes2.data[0].id == invalid_scene_1.id
    assert scenes2.data[0].name == invalid_scene_1.name
    assert scenes2.data[0].problems is None

    projects2 = ars.call_rpc(rpc.p.ListProjects.Request(get_id()),
                             rpc.p.ListProjects.Response)
    assert projects2.result
    assert projects2.data
    assert len(projects2.data) == 1
    assert projects2.data[0].id == invalid_project_1.id
    assert projects2.data[0].name == invalid_project_1.name
    assert projects2.data[0].problems is None
Ejemplo n.º 6
0
def test_scene_basic_rpcs(start_processes: None, ars: ARServer) -> None:

    test = "Test"

    # initial event
    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.ScenesList

    # first, there are no scenes
    scenes = ars.call_rpc(rpc.s.ListScenes.Request(get_id()),
                          rpc.s.ListScenes.Response)
    assert scenes.result
    assert not scenes.data

    assert ars.call_rpc(
        rpc.s.NewScene.Request(get_id(),
                               rpc.s.NewScene.Request.Args(test, test)),
        rpc.s.NewScene.Response).result

    open_scene_event = event(ars, events.s.OpenScene)
    assert open_scene_event.parent_id is None
    assert open_scene_event.change_type is None
    assert open_scene_event.data
    assert open_scene_event.data.scene.id
    scene_id = open_scene_event.data.scene.id
    assert open_scene_event.data.scene.name == test
    assert open_scene_event.data.scene.description == test
    assert not open_scene_event.data.scene.objects

    event(ars, events.s.SceneState)

    # attempt to create a new scene while scene is open should fail
    assert not ars.call_rpc(
        rpc.s.NewScene.Request(get_id(), rpc.s.NewScene.Request.Args(
            test, test)), rpc.s.NewScene.Response).result

    assert ars.call_rpc(rpc.s.SaveScene.Request(get_id()),
                        rpc.s.SaveScene.Response).result

    event(ars, events.s.SceneSaved)

    assert ars.call_rpc(rpc.s.CloseScene.Request(get_id()),
                        rpc.s.CloseScene.Response).result

    event(ars, events.s.SceneClosed)

    show_main_screen_event_2 = event(ars, events.c.ShowMainScreen)
    assert show_main_screen_event_2.data
    assert show_main_screen_event_2.data.what == events.c.ShowMainScreen.Data.WhatEnum.ScenesList
    assert show_main_screen_event_2.data.highlight == scene_id

    # attempt to open non-existent scene
    assert not ars.call_rpc(
        rpc.s.OpenScene.Request(get_id(), IdArgs("some-random-nonsense")),
        rpc.s.OpenScene.Response).result

    list_of_scenes = ars.call_rpc(rpc.s.ListScenes.Request(get_id()),
                                  rpc.s.ListScenes.Response)
    assert list_of_scenes.result
    assert list_of_scenes.data
    assert len(list_of_scenes.data) == 1
    assert list_of_scenes.data[0].id == scene_id

    # open previously saved scene
    assert ars.call_rpc(rpc.s.OpenScene.Request(get_id(), IdArgs(scene_id)),
                        rpc.s.OpenScene.Response).result

    open_scene_event_2 = event(ars, events.s.OpenScene)
    assert open_scene_event_2.data
    assert open_scene_event_2.data.scene.id == scene_id

    event(ars, events.s.SceneState)

    assert ars.call_rpc(rpc.s.CloseScene.Request(get_id()),
                        rpc.s.CloseScene.Response).result
    event(ars, events.s.SceneClosed)

    show_main_screen_event_3 = event(ars, events.c.ShowMainScreen)
    assert show_main_screen_event_3.data
    assert show_main_screen_event_3.data.what == events.c.ShowMainScreen.Data.WhatEnum.ScenesList
    assert show_main_screen_event_3.data.highlight == scene_id

    with ARServer(ars_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.ScenesList
        assert smse.data.highlight is None

    assert ars.call_rpc(rpc.s.DeleteScene.Request(get_id(), 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_scenes_2 = ars.call_rpc(rpc.s.ListScenes.Request(get_id()),
                                    rpc.s.ListScenes.Response)
    assert list_of_scenes_2.result
    assert not list_of_scenes_2.data
Ejemplo n.º 7
0
def test_update_object_pose(start_processes: None, ars: ARServer) -> None:

    upload_def(Box, BoxModel("Box", 0.1, 0.1, 0.1))
    upload_def(DummyMultiArmRobot)

    test = "test"

    event(ars, events.c.ShowMainScreen)

    assert ars.call_rpc(
        rpc.s.NewScene.Request(get_id(), rpc.s.NewScene.Request.Args(test)),
        rpc.s.NewScene.Response).result

    assert len(event(ars, events.o.ChangedObjectTypes).data) == 2

    event(ars, events.s.OpenScene)
    event(ars, events.s.SceneState)

    assert ars.call_rpc(
        rpc.s.AddObjectToScene.Request(
            get_id(),
            rpc.s.AddObjectToScene.Request.Args("obj", Box.__name__, Pose())),
        rpc.s.AddObjectToScene.Response,
    ).result

    scene_obj = event(ars, events.s.SceneObjectChanged).data

    assert ars.call_rpc(
        rpc.s.AddObjectToScene.Request(
            get_id(),
            rpc.s.AddObjectToScene.Request.Args("robot",
                                                DummyMultiArmRobot.__name__,
                                                Pose())),
        rpc.s.AddObjectToScene.Response,
    ).result

    scene_robot = event(ars, events.s.SceneObjectChanged).data

    new_pose = Pose(Position(1))

    lock_object(ars, scene_obj.id)

    assert ars.call_rpc(
        rpc.s.UpdateObjectPose.Request(
            get_id(),
            rpc.s.UpdateObjectPose.Request.Args(scene_obj.id, new_pose)),
        rpc.s.UpdateObjectPose.Response,
    ).result

    assert event(ars, events.s.SceneObjectChanged).data.pose == new_pose
    unlock_object(ars, scene_obj.id)

    lock_object(ars, scene_robot.id)

    assert ars.call_rpc(
        rpc.s.UpdateObjectPose.Request(
            get_id(),
            rpc.s.UpdateObjectPose.Request.Args(scene_robot.id, new_pose)),
        rpc.s.UpdateObjectPose.Response,
    ).result

    assert event(ars, events.s.SceneObjectChanged).data.pose == new_pose
    unlock_object(ars, scene_robot.id)

    assert ars.call_rpc(
        rpc.s.StartScene.Request(get_id()),
        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

    lock_object(ars, scene_obj.id)

    # with started scene, it should still be possible to manipulate the object
    assert ars.call_rpc(
        rpc.s.UpdateObjectPose.Request(
            get_id(),
            rpc.s.UpdateObjectPose.Request.Args(scene_obj.id, Pose())),
        rpc.s.UpdateObjectPose.Response,
    ).result

    assert event(ars, events.s.SceneObjectChanged).data.pose == Pose()
    unlock_object(ars, scene_obj.id)

    lock_object(ars, scene_robot.id)

    # ...but it should not be possible to manipulate the robot
    assert not ars.call_rpc(
        rpc.s.UpdateObjectPose.Request(
            get_id(),
            rpc.s.UpdateObjectPose.Request.Args(scene_robot.id, Pose())),
        rpc.s.UpdateObjectPose.Response,
    ).result

    unlock_object(ars, scene_robot.id)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
def test_object_parameters(start_processes: None, ars: ARServer,
                           scene: Scene) -> None:

    assert ars.call_rpc(rpc.s.OpenScene.Request(get_id(), 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(
            get_id(),
            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(
            get_id(),
            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(
            get_id(), rpc.p.NewProject.Request.Args(scene.id, "Project name")),
        rpc.p.NewProject.Response,
    ).result

    event(ars, events.s.SceneSaved)
    event(ars, events.p.OpenProject)
    event(ars, events.s.SceneState)

    assert not ars.call_rpc(
        rpc.o.DeleteOverride.Request(
            get_id(),
            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(
            get_id(),
            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(get_id()),
        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(
            get_id(), 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(
            get_id(), 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(get_id()),
        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
Ejemplo n.º 10
0
def test_virtual_collision_object(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("TestScene")), rpc.s.NewScene.Response
    ).result

    event(ars, events.s.OpenScene)
    event(ars, events.s.SceneState)

    name = "vco"
    box = Box(name, 0.1, 0.1, 0.1)
    om = ObjectModel(box.type(), box=box)

    add_vco = ars.call_rpc(
        rpc.s.AddVirtualCollisionObjectToScene.Request(
            get_id(), rpc.s.AddVirtualCollisionObjectToScene.Request.Args(name, Pose(), om)
        ),
        rpc.s.AddVirtualCollisionObjectToScene.Response,
    )

    ot = project_service.get_object_type(name)
    assert ot.source

    assert add_vco.result

    ot_evt = event(ars, events.o.ChangedObjectTypes)
    assert ot_evt.change_type == ot_evt.Type.ADD
    assert len(ot_evt.data) == 1
    assert ot_evt.data[0].object_model == om
    assert ot_evt.data[0].type == name

    scn_evt = event(ars, events.s.SceneObjectChanged)
    assert scn_evt.change_type == ot_evt.Type.ADD
    assert scn_evt.data.name == name

    assert ars.call_rpc(
        rpc.s.StartScene.Request(get_id()),
        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

    # stop can't be called just after start, because of assertion in start_scene coroutine (arserver):
    # assert ret == await scene_srv.started()
    # assertions are disabled in release, so this is issue only when running tests, and it happens randomly
    time.sleep(1)

    assert ars.call_rpc(
        rpc.s.StopScene.Request(get_id()),
        rpc.s.StopScene.Response,
    ).result

    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

    delete_res_2 = ars.call_rpc(
        rpc.s.RemoveFromScene.Request(get_id(), rpc.s.RemoveFromScene.Request.Args(scn_evt.data.id)),
        rpc.s.RemoveFromScene.Response,
    )

    assert delete_res_2.result

    scn_evt2 = event(ars, events.s.SceneObjectChanged)
    assert scn_evt2.change_type == ot_evt.Type.REMOVE
    assert scn_evt2.data.name == name

    assert ars.call_rpc(
        rpc.s.SaveScene.Request(get_id()),
        rpc.s.SaveScene.Response,
    ).result

    assert event(ars, events.s.SceneSaved)

    ot_evt2 = event(ars, events.o.ChangedObjectTypes)
    assert ot_evt2.change_type == ot_evt2.Type.REMOVE
    assert len(ot_evt2.data) == 1
    assert ot_evt2.data[0].object_model == om
    assert ot_evt2.data[0].type == name

    assert name not in {ot.id for ot in project_service.get_object_type_ids()}
Ejemplo n.º 11
0
def test_object_aiming(start_processes: None, ars: ARServer) -> None:

    mesh = Mesh(Box.__name__, "mesh.dae", [Pose(), Pose(), Pose()])
    assert mesh.focus_points
    Box.mesh_filename = mesh.data_id

    with tempfile.NamedTemporaryFile("wb") as file:
        upload_def(Box, model=mesh, file_to_upload=file.name)

    upload_def(DummyMultiArmRobot)

    test = "test"

    event(ars, events.c.ShowMainScreen)

    assert ars.call_rpc(
        rpc.s.NewScene.Request(get_id(), rpc.s.NewScene.Request.Args(test)), rpc.s.NewScene.Response
    ).result

    assert len(event(ars, events.o.ChangedObjectTypes).data) == 2

    event(ars, events.s.OpenScene)
    event(ars, events.s.SceneState)

    assert ars.call_rpc(
        rpc.s.AddObjectToScene.Request(get_id(), rpc.s.AddObjectToScene.Request.Args("obj", Box.__name__, Pose())),
        rpc.s.AddObjectToScene.Response,
    ).result

    scene_obj = event(ars, events.s.SceneObjectChanged).data

    assert ars.call_rpc(
        rpc.s.AddObjectToScene.Request(
            get_id(), rpc.s.AddObjectToScene.Request.Args("robot", DummyMultiArmRobot.__name__, Pose())
        ),
        rpc.s.AddObjectToScene.Response,
    ).result

    scene_robot = event(ars, events.s.SceneObjectChanged).data

    assert ars.call_rpc(
        rpc.s.StartScene.Request(get_id()),
        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

    arm = DummyMultiArmRobot.Arms.left
    robot_arg = RobotArg(scene_robot.id, list(DummyMultiArmRobot.EEF[arm])[0], arm)

    # ------------------------------------------------------------------------------------------------------------------

    lock_object(ars, scene_obj.id)
    lock_object(ars, scene_robot.id)

    assert ars.call_rpc(
        rpc.o.ObjectAimingStart.Request(get_id(), rpc.o.ObjectAimingStart.Request.Args(scene_obj.id, robot_arg)),
        rpc.o.ObjectAimingStart.Response,
    ).result

    assert not ars.call_rpc(
        rpc.o.ObjectAimingStart.Request(get_id(), rpc.o.ObjectAimingStart.Request.Args(scene_obj.id, robot_arg)),
        rpc.o.ObjectAimingStart.Response,
    ).result

    assert ars.call_rpc(
        rpc.o.ObjectAimingAddPoint.Request(get_id(), rpc.o.ObjectAimingAddPoint.Request.Args(0)),
        rpc.o.ObjectAimingAddPoint.Response,
    ).result

    assert ars.call_rpc(
        rpc.o.ObjectAimingCancel.Request(get_id()),
        rpc.o.ObjectAimingCancel.Response,
    ).result

    unlock_object(ars, scene_obj.id)
    unlock_object(ars, scene_robot.id)

    # ------------------------------------------------------------------------------------------------------------------

    lock_object(ars, scene_obj.id)
    lock_object(ars, scene_robot.id)

    assert ars.call_rpc(
        rpc.o.ObjectAimingStart.Request(get_id(), rpc.o.ObjectAimingStart.Request.Args(scene_obj.id, robot_arg)),
        rpc.o.ObjectAimingStart.Response,
    ).result

    assert not ars.call_rpc(
        rpc.o.ObjectAimingAddPoint.Request(get_id(), rpc.o.ObjectAimingAddPoint.Request.Args(-1)),
        rpc.o.ObjectAimingAddPoint.Response,
    ).result

    assert not ars.call_rpc(
        rpc.o.ObjectAimingAddPoint.Request(get_id(), rpc.o.ObjectAimingAddPoint.Request.Args(len(mesh.focus_points))),
        rpc.o.ObjectAimingAddPoint.Response,
    ).result

    assert not ars.call_rpc(
        rpc.o.ObjectAimingDone.Request(get_id()),
        rpc.o.ObjectAimingDone.Response,
    ).result

    for idx in range(len(mesh.focus_points)):

        assert ars.call_rpc(
            rpc.o.ObjectAimingAddPoint.Request(get_id(), rpc.o.ObjectAimingAddPoint.Request.Args(idx)),
            rpc.o.ObjectAimingAddPoint.Response,
        ).result

    assert ars.call_rpc(
        rpc.o.ObjectAimingDone.Request(get_id()),
        rpc.o.ObjectAimingDone.Response,
    ).result

    assert event(ars, events.s.SceneObjectChanged).data.id == scene_obj.id

    unlock_object(ars, scene_obj.id)
    unlock_object(ars, scene_robot.id)
Ejemplo n.º 12
0
def test_update_object_model(start_processes: None, ars: ARServer) -> None:

    upload_def(BoxType, Box(BoxType.__name__, 0.1, 0.1, 0.1))
    upload_def(DummyMultiArmRobot)

    event(ars, events.c.ShowMainScreen)

    assert ars.call_rpc(
        rpc.s.NewScene.Request(get_id(), rpc.s.NewScene.Request.Args("TestScene")), rpc.s.NewScene.Response
    ).result

    assert len(event(ars, events.o.ChangedObjectTypes).data) == 2

    event(ars, events.s.OpenScene)
    event(ars, events.s.SceneState)

    lock_object(ars, BoxType.__name__)

    om = ObjectModel(Model3dType.SPHERE, sphere=Sphere(BoxType.__name__, 0.1))

    assert ars.call_rpc(
        rpc.o.UpdateObjectModel.Request(get_id(), rpc.o.UpdateObjectModel.Request.Args(BoxType.__name__, om)),
        rpc.o.UpdateObjectModel.Response,
    ).result

    ot_evt = event(ars, events.o.ChangedObjectTypes)
    assert ot_evt.change_type == ot_evt.Type.UPDATE
    assert len(ot_evt.data) == 1
    assert ot_evt.data[0].object_model == om
    assert ot_evt.data[0].type == BoxType.__name__
    assert ot_evt.data[0].has_pose
    assert not ot_evt.data[0].disabled

    assert BoxType.__name__ in {obj.id for obj in project_service.get_object_type_ids()}
    assert BoxType.__name__ in {mod.id for mod in project_service.get_models()}

    unlock_object(ars, BoxType.__name__)

    lock_object(ars, DummyMultiArmRobot.__name__)

    assert not ars.call_rpc(
        rpc.o.UpdateObjectModel.Request(
            get_id(),
            rpc.o.UpdateObjectModel.Request.Args(
                DummyMultiArmRobot.__name__,
                ObjectModel(Model3dType.SPHERE, sphere=Sphere(DummyMultiArmRobot.__name__, 0.1)),
            ),
        ),
        rpc.o.UpdateObjectModel.Response,
    ).result

    unlock_object(ars, DummyMultiArmRobot.__name__)

    delete_res_1 = ars.call_rpc(
        rpc.o.DeleteObjectTypes.Request(get_id(), {BoxType.__name__}, dry_run=True),
        rpc.o.DeleteObjectTypes.Response,
    )

    assert delete_res_1.result
    assert delete_res_1.data is None

    delete_res_2 = ars.call_rpc(
        rpc.o.DeleteObjectTypes.Request(get_id(), {BoxType.__name__}),
        rpc.o.DeleteObjectTypes.Response,
    )

    assert delete_res_2.result
    assert delete_res_2.data is None

    ot_evt2 = event(ars, events.o.ChangedObjectTypes)
    assert ot_evt2.change_type == ot_evt.Type.REMOVE
    assert len(ot_evt2.data) == 1
    assert ot_evt2.data[0].object_model == om
    assert ot_evt2.data[0].type == BoxType.__name__

    assert BoxType.__name__ not in {obj.id for obj in project_service.get_object_type_ids()}
    assert BoxType.__name__ not in {mod.id for mod in project_service.get_models()}
Ejemplo n.º 13
0
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(get_id()),
                            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(
            get_id(),
            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(get_id(), IdArgs("some-random-nonsense")),
        rpc.p.OpenProject.Response).result

    # correct scene_id
    assert ars.call_rpc(
        rpc.p.NewProject.Request(
            get_id(), 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 len(
        open_project_evt.data.project.parameters) == 2  # scene_id + project_id
    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(
            get_id(),
            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(get_id()),
                                    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

    get_scene_resp = ars.call_rpc(
        rpc.s.GetScene.Request(get_id(), IdArgs(scene.id)),
        rpc.s.GetScene.Response)
    assert get_scene_resp.result
    assert get_scene_resp.data
    assert get_scene_resp.data.id == scene.id  # actually whole scene should be the same (except 'modified')

    get_project_resp = ars.call_rpc(
        rpc.p.GetProject.Request(get_id(), IdArgs(project_id)),
        rpc.p.GetProject.Response)
    assert get_project_resp.result
    assert get_project_resp.data
    assert get_project_resp.data.id == project_id  # actually whole project should be the same (except 'modified')
    """ 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(get_id(), IdArgs(scene.id)),
        rpc.s.DeleteScene.Response).result

    assert ars.call_rpc(
        rpc.p.DeleteProject.Request(get_id(), 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(get_id(), 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(get_id()),
                                      rpc.p.ListProjects.Response)
    assert list_of_projects_2.result
    assert not list_of_projects_2.data
Ejemplo n.º 14
0
def test_project_const(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("random_actions",
                                                RandomActions.__name__)),
        rpc.s.AddObjectToScene.Response,
    ).result

    obj = event(ars, events.s.SceneObjectChanged).data
    assert obj

    # ------------------------------------------------------------------------------------------------------------------

    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)
    open_project = event(ars, events.p.OpenProject).data
    assert open_project

    proj = open_project.project

    # test project parameters added by the arserver
    d: dict[str, common.ProjectParameter] = {
        par.name: par
        for par in proj.parameters
    }
    assert len(d) == 2
    assert d["scene_id"].type == "string"
    assert json.loads(d["scene_id"].value) == scene.id
    assert d["project_id"].type == "string"
    assert json.loads(d["project_id"].value) == proj.id

    event(ars, events.s.SceneState)

    assert ars.call_rpc(
        rpc.p.AddProjectParameter.Request(
            get_id(),
            rpc.p.AddProjectParameter.Request.Args("min_time", "double",
                                                   json.dumps(0.45))),
        rpc.p.AddProjectParameter.Response,
    ).result

    c1 = event(ars, events.p.ProjectParameterChanged).data
    assert c1

    assert not ars.call_rpc(
        rpc.p.AddProjectParameter.Request(
            get_id(),
            rpc.p.AddProjectParameter.Request.Args("min_time", "double",
                                                   json.dumps(0.62))),
        rpc.p.AddProjectParameter.Response,
    ).result

    assert not ars.call_rpc(  # attempt to update without lock
        rpc.p.UpdateProjectParameter.Request(
            get_id(),
            rpc.p.UpdateProjectParameter.Request.Args(
                c1.id, name="min_time_updated")),
        rpc.p.UpdateProjectParameter.Response,
    ).result

    # ------------------------------------------------------------------------------------------------------------------
    # the user opens a menu and then closes it without actually changing anything

    lock_object(ars, c1.id)

    assert ars.call_rpc(
        rpc.p.UpdateProjectParameter.Request(
            get_id(),
            rpc.p.UpdateProjectParameter.Request.Args(c1.id,
                                                      name="min_time_1"),
            dry_run=True),
        rpc.p.UpdateProjectParameter.Response,
    ).result

    assert ars.call_rpc(
        rpc.p.UpdateProjectParameter.Request(
            get_id(),
            rpc.p.UpdateProjectParameter.Request.Args(c1.id,
                                                      name="min_time_2"),
            dry_run=True),
        rpc.p.UpdateProjectParameter.Response,
    ).result

    unlock_object(ars, c1.id)

    # ------------------------------------------------------------------------------------------------------------------

    lock_object(ars, c1.id)

    assert ars.call_rpc(
        rpc.p.UpdateProjectParameter.Request(
            get_id(),
            rpc.p.UpdateProjectParameter.Request.Args(
                c1.id, name="min_time_updated")),
        rpc.p.UpdateProjectParameter.Response,
    ).result

    c1u = event(ars, events.p.ProjectParameterChanged).data
    assert c1u

    event(ars, events.lk.ObjectsUnlocked)

    assert c1u.id == c1.id
    assert c1.name != c1u.name
    assert c1.type == c1u.type

    # ------------------------------------------------------------------------------------------------------------------
    # try to add and remove

    assert ars.call_rpc(
        rpc.p.AddProjectParameter.Request(
            get_id(),
            rpc.p.AddProjectParameter.Request.Args("min_time_2", "double",
                                                   json.dumps(0.62))),
        rpc.p.AddProjectParameter.Response,
    ).result

    c2 = event(ars, events.p.ProjectParameterChanged).data
    assert c2

    # attempt to rename param to already used name
    assert not ars.call_rpc(
        rpc.p.UpdateProjectParameter.Request(
            get_id(), rpc.p.UpdateProjectParameter.Request.Args(
                c2.id, c1u.name)),
        rpc.p.AddProjectParameter.Response,
    ).result

    assert ars.call_rpc(
        rpc.p.RemoveProjectParameter.Request(
            get_id(), rpc.p.RemoveProjectParameter.Request.Args(c2.id)),
        rpc.p.RemoveProjectParameter.Response,
    ).result

    c2e = event(ars, events.p.ProjectParameterChanged)
    assert c2e.data
    assert c2e.data.id == c2.id
    assert c2e.change_type == c2e.Type.REMOVE

    # ------------------------------------------------------------------------------------------------------------------
    # attempt to add a constant with duplicate name

    assert not ars.call_rpc(
        rpc.p.AddProjectParameter.Request(
            get_id(),
            rpc.p.AddProjectParameter.Request.Args(c1u.name, "double",
                                                   json.dumps(0.62))),
        rpc.p.AddProjectParameter.Response,
    ).result

    # ------------------------------------------------------------------------------------------------------------------

    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.AddAction.Request(
            get_id(),
            rpc.p.AddAction.Request.Args(
                ap.id,
                "test_action",
                f"{obj.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

    assert not ars.call_rpc(
        rpc.p.RemoveProjectParameter.Request(
            get_id(), rpc.p.RemoveProjectParameter.Request.Args(c1.id)),
        rpc.p.RemoveProjectParameter.Response,
    ).result

    # ------------------------------------------------------------------------------------------------------------------
    # try to execute action using constant parameter

    assert ars.call_rpc((rpc.s.StartScene.Request(get_id())),
                        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

    assert ars.call_rpc(
        rpc.p.ExecuteAction.Request(
            get_id(), rpc.p.ExecuteAction.Request.Args(action.id)),
        rpc.p.ExecuteAction.Response)

    event(ars, events.a.ActionExecution)
    res = event(ars, events.a.ActionResult)

    assert res.data
    assert res.data.action_id == action.id
    assert not res.data.error

    assert ars.call_rpc((rpc.s.StopScene.Request(get_id())),
                        rpc.s.StopScene.Response).result
    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

    assert ars.call_rpc(
        rpc.p.RemoveAction.Request(get_id(), rpc.p.IdArgs(action.id)),
        rpc.p.RemoveAction.Response).result
    assert event(ars, events.p.ActionChanged).data

    assert ars.call_rpc(
        rpc.p.RemoveProjectParameter.Request(
            get_id(), rpc.p.RemoveProjectParameter.Request.Args(c1.id)),
        rpc.p.RemoveProjectParameter.Response,
    ).result
    event(ars, events.p.ProjectParameterChanged)
Ejemplo n.º 15
0
def test_project_ap_rpcs(start_processes: None, ars: ARServer) -> None:

    upload_def(Box, BoxModel(Box.__name__, 1, 2, 3))

    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

    assert len(event(ars, events.o.ChangedObjectTypes).data) == 1

    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("box", Box.__name__, Pose())),
        rpc.s.AddObjectToScene.Response,
    ).result

    obj = event(ars, events.s.SceneObjectChanged).data
    assert obj

    # ------------------------------------------------------------------------------------------------------------------

    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)
    event(ars, events.p.OpenProject)
    event(ars, events.s.SceneState)

    assert ars.call_rpc(
        rpc.p.AddActionPoint.Request(get_id(), rpc.p.AddActionPoint.Request.Args("parent_ap", Position())),
        rpc.p.AddActionPoint.Response,
    ).result

    parent_ap_evt = event(ars, events.p.ActionPointChanged)

    assert ars.call_rpc(
        rpc.p.AddActionPoint.Request(
            get_id(), rpc.p.AddActionPoint.Request.Args("child_ap", Position(-1), parent_ap_evt.data.id)
        ),
        rpc.p.AddActionPoint.Response,
    ).result

    child_ap_evt = event(ars, events.p.ActionPointChanged)
    assert child_ap_evt.data.parent == parent_ap_evt.data.id

    lock_object(ars, child_ap_evt.data.id)

    assert ars.call_rpc(
        rpc.p.AddActionPointOrientation.Request(
            get_id(), rpc.p.AddActionPointOrientation.Request.Args(child_ap_evt.data.id, Orientation())
        ),
        rpc.p.AddActionPointOrientation.Response,
    ).result

    ori = event(ars, events.p.OrientationChanged)

    assert ars.call_rpc(
        rpc.p.AddAction.Request(
            get_id(),
            rpc.p.AddAction.Request.Args(
                child_ap_evt.data.id,
                "act_name",
                f"{obj.id}/{Box.update_pose.__name__}",
                [ActionParameter("new_pose", PosePlugin.type_name(), json.dumps(ori.data.id))],
                [Flow()],
            ),
        ),
        rpc.p.AddAction.Response,
    ).result

    event(ars, events.p.ActionChanged)

    unlock_object(ars, child_ap_evt.data.id)

    ars.event_mapping[ActionChanged.__name__] = ActionChanged

    assert ars.call_rpc(
        rpc.p.CopyActionPoint.Request(get_id(), rpc.p.CopyActionPoint.Request.Args(parent_ap_evt.data.id)),
        rpc.p.CopyActionPoint.Response,
    ).result

    new_parent_ap = event(ars, events.p.ActionPointChanged)
    assert not new_parent_ap.data.parent

    new_child_ap = event(ars, events.p.ActionPointChanged)
    assert new_child_ap.data.parent == new_parent_ap.data.id

    new_ori = event(ars, events.p.OrientationChanged)
    assert new_ori.parent_id == new_child_ap.data.id

    # with events.p.ActionChanged it would return only BareAction (without parameters)
    new_action = event(ars, ActionChanged)
    ars.event_mapping[ActionChanged.__name__] = events.p.ActionChanged
    assert new_action.parent_id == new_child_ap.data.id

    # Pose parameter (orientation id) should be updated now
    assert len(new_action.data.parameters) == 1
    assert json.loads(new_action.data.parameters[0].value) == new_ori.data.id