Command Service
This client side service provides a handle to send commands to a component which is registered in location service.
Command service has following APIs:
API | Input args | Returns |
---|---|---|
validate | ControlCommand | ValidateResponse |
submit | ControlCommand | SubmitResponse |
oneway | ControlCommand | OnewayResponse |
query | runId | SubmitResponse |
queryFinal | runId, timeoutInSeconds | SubmitResponse |
subscribeCurrentState | stateNames, onStateChangeCallback | Subscription |
submitAndWait | ControlCommand, timeoutInSeconds | SubmitResponse |
submitAllAndWait | ControlCommand [ ], timeoutInSeconds | SubmitResponse[ ] |
Creation of Command Service
Pre-requisite
In order to use command service for a specific component:
- The component needs to be up and running behind the gateway server.
GatewayException(InvalidComponent)
will be thrown if the specified component is not found. - Authorization Token with correct access role. To read more on how to fetch access token. link.
For the given example : Prefix(ESW.Component1)
needs to be registered in the location service as any of the component type (HCD
, Assembly
, etc.).
To create Command service client for a component
- Typescript
-
const tokenFactory = () => auth.token const commandService: CommandService = await CommandService( new ComponentId(new Prefix('ESW', 'Component1'), 'HCD'), tokenFactory )
Control Commands
In order to call following API, one of the control command needs to be sent. Depending on your use case, you will be sending either Setup or Observe Command.
Following examples show how to create control commands:
- Typescript
-
// Definition of control command looks like following type ControlCommand = Setup | Observe // examples const setupCommand: Setup = new Setup( new Prefix('ESW', 'Component1'), 'move-command', paramSet, 'obs-id' ) const observeCommand: Observe = new Observe( new Prefix('ESW', 'Component1'), 'c1', paramSet, 'obs-id' )
APIs
Note that the examples are using async/await which makes handling of promises more readable.
Validate
This API takes Control command as input parameter and return a promise of ValidateResponse
.
The following example shows how to call validate API :
- Typescript
-
const validateResponse1: ValidateResponse = await commandService.validate( setupCommand ) // or const validateResponse2: ValidateResponse = await commandService.validate( observeCommand )
Submit
This API takes Control command as input parameter and return a promise of SubmitResponse
.
The following example shows how to call submit API :
- Typescript
-
const submitResponse1: SubmitResponse = await commandService.submit( setupCommand ) // or const submitResponse2: SubmitResponse = await commandService.submit( observeCommand )
Oneway
This API takes Control command as input parameter and return a promise of OnewayResponse
.
The following example shows how to call oneway API :
- Typescript
-
const onewayResponse1: OnewayResponse = await commandService.oneway( setupCommand ) // or const onewayResponse2: OnewayResponse = await commandService.oneway( observeCommand )
Query
This API takes runId of already submitted command as input parameter and return a promise of SubmitResponse
.
The following example shows how to call query API :
- Typescript
-
// Submit a long running command const res: SubmitResponse = await commandService.submit(setupCommand) // . // . // . // Get the current result of long running command as : const queryResponse: SubmitResponse = await commandService.query(res.runId)
QueryFinal
This API is same as query , only difference is takes time-out (seconds) along with runId of already submitted command as input parameter and return a promise of SubmitResponse
.
The following example shows how to call query final API :
- Typescript
-
// Submit a long running command const result: SubmitResponse = await commandService.submit(setupCommand) // . // . // . // Get the final result of long running command within 10 seconds : const queryFinalResponse: SubmitResponse = await commandService.queryFinal( result.runId, 10 )
SubscribeCurrentState
This API takes set of current states to be subscribed along with a callback which will get triggered on change of the mentioned states.(stateName1
,stateName2
)
The following example shows how subscribeCurrentState API call would look like :
- Typescript
-
// subscribe to this set of current states const currentStates = new Set(['stateName1', 'stateName2']) // this callback gets called whenever the state changes const onStateChangeCallback = (currentState: CurrentState) => { // do something when state changes console.log('changed state:', currentState) } // subscribe call const subscription: Subscription = await commandService.subscribeCurrentState( currentStates )(onStateChangeCallback) // . // . // . // subscription can be cancelled when it is not required any more subscription.cancel()
SubmitAndWait
This API takes Control command as input parameter along with time-out(seconds) and return a promise of SubmitResponse
after waiting for a specified amount of time.
The following example shows how submitAndWait API call would look like :
- Typescript
-
// Submit a long running command and wait for the result for specific time const submitAndWaitResponse: SubmitResponse = await commandService.submitAndWait( setupCommand, 10 )
SubmitAllAndWait
This API takes multiple control commands as input parameter along with time-out(seconds) and return a promise of SubmitResponse[]
after waiting for a specified amount of time.
The following example shows how submitAllAndWait API call would look like :
- Typescript
-
// Submit multiple commands and wait for the result of each submitted command for specific time const submitAllAndWaitResponse: SubmitResponse[] = await commandService.submitAllAndWait( [setupCommand, observeCommand], 10 )