|
| 1 | +import { Struct } from 'google-protobuf/google/protobuf/struct_pb'; |
| 2 | +import type { RobotClient } from '../../robot'; |
| 3 | +import pb from '../../gen/component/gantry/v1/gantry_pb'; |
| 4 | +import { GantryServiceClient } from '../../gen/component/gantry/v1/gantry_pb_service'; |
| 5 | +import type { Options, StructType } from '../../types'; |
| 6 | +import { doCommandFromClient, promisify } from '../../utils'; |
| 7 | +import type { Gantry } from './gantry'; |
| 8 | + |
| 9 | +/** |
| 10 | + * A gRPC-web client for the Gantry component. |
| 11 | + * |
| 12 | + * @group Clients |
| 13 | + */ |
| 14 | +export class GantryClient implements Gantry { |
| 15 | + private client: GantryServiceClient; |
| 16 | + private readonly name: string; |
| 17 | + private readonly options: Options; |
| 18 | + |
| 19 | + constructor(client: RobotClient, name: string, options: Options = {}) { |
| 20 | + this.client = client.createServiceClient(GantryServiceClient); |
| 21 | + this.name = name; |
| 22 | + this.options = options; |
| 23 | + } |
| 24 | + |
| 25 | + private get GantryService() { |
| 26 | + return this.client; |
| 27 | + } |
| 28 | + |
| 29 | + async getPosition(extra = {}) { |
| 30 | + const gantryService = this.GantryService; |
| 31 | + const request = new pb.GetPositionRequest(); |
| 32 | + request.setName(this.name); |
| 33 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 34 | + |
| 35 | + this.options.requestLogger?.(request); |
| 36 | + |
| 37 | + const response = await promisify< |
| 38 | + pb.GetPositionRequest, |
| 39 | + pb.GetPositionResponse |
| 40 | + >(gantryService.getPosition.bind(gantryService), request); |
| 41 | + |
| 42 | + return response.getPositionsMmList(); |
| 43 | + } |
| 44 | + |
| 45 | + async moveToPosition( |
| 46 | + positionsMm: number[], |
| 47 | + speedsMmPerSec: number[], |
| 48 | + extra = {} |
| 49 | + ) { |
| 50 | + const gantryService = this.GantryService; |
| 51 | + |
| 52 | + const request = new pb.MoveToPositionRequest(); |
| 53 | + request.setName(this.name); |
| 54 | + request.setPositionsMmList(positionsMm); |
| 55 | + request.setSpeedsMmPerSecList(speedsMmPerSec); |
| 56 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 57 | + |
| 58 | + this.options.requestLogger?.(request); |
| 59 | + |
| 60 | + await promisify<pb.MoveToPositionRequest, pb.MoveToPositionResponse>( |
| 61 | + gantryService.moveToPosition.bind(gantryService), |
| 62 | + request |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + async home(extra = {}) { |
| 67 | + const gantryService = this.GantryService; |
| 68 | + const request = new pb.HomeRequest(); |
| 69 | + request.setName(this.name); |
| 70 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 71 | + |
| 72 | + this.options.requestLogger?.(request); |
| 73 | + |
| 74 | + const response = await promisify<pb.HomeRequest, pb.HomeResponse>( |
| 75 | + gantryService.home.bind(gantryService), |
| 76 | + request |
| 77 | + ); |
| 78 | + |
| 79 | + return response.getHomed(); |
| 80 | + } |
| 81 | + |
| 82 | + async getLengths(extra = {}) { |
| 83 | + const gantryService = this.GantryService; |
| 84 | + const request = new pb.GetLengthsRequest(); |
| 85 | + request.setName(this.name); |
| 86 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 87 | + |
| 88 | + this.options.requestLogger?.(request); |
| 89 | + |
| 90 | + const response = await promisify< |
| 91 | + pb.GetLengthsRequest, |
| 92 | + pb.GetLengthsResponse |
| 93 | + >(gantryService.getLengths.bind(gantryService), request); |
| 94 | + |
| 95 | + return response.getLengthsMmList(); |
| 96 | + } |
| 97 | + |
| 98 | + async stop(extra = {}) { |
| 99 | + const gantryService = this.GantryService; |
| 100 | + const request = new pb.StopRequest(); |
| 101 | + request.setName(this.name); |
| 102 | + request.setExtra(Struct.fromJavaScript(extra)); |
| 103 | + |
| 104 | + this.options.requestLogger?.(request); |
| 105 | + |
| 106 | + await promisify<pb.StopRequest, pb.StopResponse>( |
| 107 | + gantryService.stop.bind(gantryService), |
| 108 | + request |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + async isMoving() { |
| 113 | + const gantryService = this.GantryService; |
| 114 | + const request = new pb.IsMovingRequest(); |
| 115 | + request.setName(this.name); |
| 116 | + |
| 117 | + this.options.requestLogger?.(request); |
| 118 | + |
| 119 | + const response = await promisify<pb.IsMovingRequest, pb.IsMovingResponse>( |
| 120 | + gantryService.isMoving.bind(gantryService), |
| 121 | + request |
| 122 | + ); |
| 123 | + return response.getIsMoving(); |
| 124 | + } |
| 125 | + |
| 126 | + async doCommand(command: StructType): Promise<StructType> { |
| 127 | + const gantryService = this.GantryService; |
| 128 | + return doCommandFromClient(gantryService, this.name, command, this.options); |
| 129 | + } |
| 130 | +} |
0 commit comments