-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APP-7497: Add Switch interface and client (#452)
- Loading branch information
1 parent
f595ede
commit 6fddc56
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type { Switch } from './switch/switch'; | ||
export { SwitchClient } from './switch/client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Struct, type JsonValue } from '@bufbuild/protobuf'; | ||
import type { CallOptions, PromiseClient } from '@connectrpc/connect'; | ||
import { SwitchService } from '../../gen/component/switch/v1/switch_connect'; | ||
import { | ||
SetPositionRequest, | ||
GetPositionRequest, | ||
GetNumberOfPositionsRequest, | ||
} from '../../gen/component/switch/v1/switch_pb'; | ||
import type { RobotClient } from '../../robot'; | ||
import type { Options } from '../../types'; | ||
import { doCommandFromClient } from '../../utils'; | ||
import type { Switch } from './switch'; | ||
|
||
/** | ||
* A gRPC-web client for the Switch component. | ||
* | ||
* @group Clients | ||
*/ | ||
export class SwitchClient implements Switch { | ||
private client: PromiseClient<typeof SwitchService>; | ||
private readonly name: string; | ||
private readonly options: Options; | ||
public callOptions: CallOptions = { headers: {} as Record<string, string> }; | ||
|
||
constructor(client: RobotClient, name: string, options: Options = {}) { | ||
this.client = client.createServiceClient(SwitchService); | ||
this.name = name; | ||
this.options = options; | ||
} | ||
|
||
async setPosition( | ||
position: number, | ||
extra = {}, | ||
callOptions = this.callOptions | ||
) { | ||
const request = new SetPositionRequest({ | ||
name: this.name, | ||
position, | ||
extra: Struct.fromJson(extra), | ||
}); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
await this.client.setPosition(request, callOptions); | ||
} | ||
|
||
async getPosition(extra = {}, callOptions = this.callOptions) { | ||
const request = new GetPositionRequest({ | ||
name: this.name, | ||
extra: Struct.fromJson(extra), | ||
}); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const resp = await this.client.getPosition(request, callOptions); | ||
return resp.position; | ||
} | ||
|
||
async getNumberOfPositions(extra = {}, callOptions = this.callOptions) { | ||
const request = new GetNumberOfPositionsRequest({ | ||
name: this.name, | ||
extra: Struct.fromJson(extra), | ||
}); | ||
|
||
this.options.requestLogger?.(request); | ||
|
||
const resp = await this.client.getNumberOfPositions(request, callOptions); | ||
return resp.numberOfPositions; | ||
} | ||
|
||
async doCommand( | ||
command: Struct, | ||
callOptions = this.callOptions | ||
): Promise<JsonValue> { | ||
return doCommandFromClient( | ||
this.client.doCommand, | ||
this.name, | ||
command, | ||
this.options, | ||
callOptions | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Struct } from '@bufbuild/protobuf'; | ||
import type { Resource } from '../../types'; | ||
|
||
/** Represents a physical switch with multiple positions. */ | ||
export interface Switch extends Resource { | ||
/** Set the switch to a specific position. */ | ||
setPosition: (position: number, extra?: Struct) => Promise<void>; | ||
|
||
/** Get the current position of the switch. */ | ||
getPosition: (extra?: Struct) => Promise<number>; | ||
|
||
/** Get the total number of positions available on the switch. */ | ||
getNumberOfPositions: (extra?: Struct) => Promise<number>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters