robofit / arcor2

Solution for end-user programming of (collaborative) robots using Augmented Reality. From AR to Python and back!
GNU Lesser General Public License v3.0
15 stars 16 forks source link

feat(arcor2_runtime): provide on pause and on resume callbacks #862

Closed jirzajic closed 2 months ago

jirzajic commented 3 months ago

The intent is to allow registering/unregistering callbacks to pause and resume events and execute registered callbacks on these events.

Singleton class was implemented to provide this functionality and integrated into the action wrapper. It provides callback registration methods that can be used from the package main script (or object types if needed). Example usage will be:

from typing import cast
from arcor2_runtime.action import PackageStateHandler
from arcor2_runtime.resources import Resources
from object_types.some_object_type import SomeObjectType

global_resources: Resources

def on_pause() -> None:
    global global_resources
    some_object_type: SomeObjectType = cast(
        SomeObjectType, global_resources.objects["some_object_id"]
    )
    some_object_type.foo()

PackageStateHandler.get_instance().add_on_pause_callback(on_pause)

def main():
    global global_resources
    with Resources() as resources:
        global_resources = resources

        # script code ...

if __name__ == "__main__":
    main()

Since actions can be used inside callbacks and the callback itself is handled in the action wrapper, it was necessary to disable the action wrapper in case when action is called from inside callback.