Context

RobotContextprovides an interface about Cruzr system's global information, and its implementation is provided by the Cruzr system.RobotContext` allows accessing to the service resources of the Cruzr system, while also supporting application-level operations, such as publishing subscriptions, sending instructions and so on. It describes the environment information of an application, and the Cruzr system provides a concrete implementation class for this interface.

Cruzr has three types of context:

  • RobotSkill describes the context of the skills. RobotSkill.this returns the context of the current skill. Its action scope follows the life cycle of the skill.
  • RobotService describes the context of the services. RobotService.this returns the context of the current service. Its action scope follows the service life cycle of the service.
  • GlobalContext describes the whole context, Robot.globalContext () returns the whole context, and its action scope follows the life cycle of the process.

Obtain system services

/**
 * Get the robot system service by service name.
 * like SpeechManager, SkillManager, MotionManager and so on.
 *
 * @param serviceName the name of manager
 * @return the Manager which match the service name
 */
<T> T getSystemService(String serviceName);
SpeechManager manager = robotContext.getSystemService(SpeechManager.SERVICE);

Note: The corresponding system service can be obtained through the SERVICE constant

Subscription events

/**
 * subscribe the action publish by MasterService
 *
 * @param receiver       the event receiver
 * @param parameterClass the class of parameter
 * @param topic          the topic of event
 */
<T> void subscribe(EventReceiver<T> receiver, Class<T> parameterClass, String topic);

/**
 * cancel the subscribe
 *
 * @param receiver the event receiver
 */
void unsubscribe(EventReceiver<?> receiver);
private class BatteryEventReceiver implements EventReceiver<BatteryProperties> {

        @Override
        public void onReceive(RobotContext robotContext, String action,
                              final BatteryProperties batteryProperties) {
             // get BatteryProperties
        }
 }

 mReceiver = new BatteryEventReceiver();
 robotContext.subscribe(mReceiver, BatteryProperties.class,
                "event.action.power.BATTERY_CHANGE");

 robotContext.unsubscribe(mReceiver);

Note: subscribe can receive published events through EventReceiver <T>, where parameterclass is the parameter type of the event, and Cruzr will get the final parameters according to automatically deserialize the parameter type of the published event. Please refer to serialization and deserialization .

Distribution of instructions

/**
 * Dispatch a inProcess directive to skill with a param.
 *
 * @param skillAction the action of directive
 * @param paramObj    the param to dispatch to the directive
 * @return the promise of the result
 */
Promise<Void, DispatchException> dispatchDirective(String skillAction, Object paramObj);

/**
 * Dispatch a inProcess directive to skill with no param.
 *
 * @param skillAction he action of directive
 * @return the promise of the result
 */
Promise<Void, DispatchException> dispatchDirective(String skillAction);

RobotContext can be used to implement inter-process instruction distribution. For inter-process instruction distribution, please refer to Skill Management .

  • In-process nonparametric instruction distribution
robotContext.dispatchDirective("music.PLAY")
    .done(new DoneCallback<Void>() {
        @Override
        public void onDone(Void aVoid) {
            // If the distribution is successful, it will run here
        }
    })
    .fail(new FailCallback<DispatchException>() {
        @Override
        public void onFail(DispatchException e  ) {
            // Here will be executed if the distribution failed
        }
    });
  • In-process parameter instruction distribution

java Music music = new Music(); robotContext.dispatchDirective("music.PLAY", music);

   > Note: In-process parameter distribution doesn't need to consider serialization