59 lines
2.0 KiB
Swift
59 lines
2.0 KiB
Swift
//
|
|
// MotorProvider.swift
|
|
//
|
|
//
|
|
// Created by Michael Pivato on 13/5/20.
|
|
//
|
|
|
|
import Foundation
|
|
import GRPC
|
|
import NIO
|
|
import SwiftProtobuf
|
|
import Swift2dCar
|
|
|
|
class MotorProvider: MotorControl_CarControlProvider{
|
|
private var vehicle: Vehicle2D
|
|
|
|
init(vehicle: Vehicle2D){
|
|
self.vehicle = vehicle
|
|
}
|
|
|
|
func set_throttle(request: MotorControl_ThrottleRequest, context: StatusOnlyCallContext) -> EventLoopFuture<MotorControl_ThrottleResponse> {
|
|
self.vehicle.throttle = request.throttle
|
|
return context.eventLoop.makeSucceededFuture(.with{ throttle in
|
|
throttle.throttleSet = true
|
|
})
|
|
}
|
|
|
|
func set_steering(request: MotorControl_SteeringRequest, context: StatusOnlyCallContext) -> EventLoopFuture<MotorControl_SteeringResponse> {
|
|
self.vehicle.steering = request.steering
|
|
return context.eventLoop.makeSucceededFuture(.with{
|
|
$0.steeringSet = true
|
|
})
|
|
}
|
|
|
|
func stream_vehicle_2d(context: UnaryResponseCallContext<Google_Protobuf_Empty>) -> EventLoopFuture<(StreamEvent<MotorControl_Vehicle2DRequest>) -> Void> {
|
|
return context.eventLoop.makeSucceededFuture({event in
|
|
switch event{
|
|
case .message(let movement):
|
|
self.vehicle.throttle = movement.throttle.throttle
|
|
self.vehicle.steering = movement.steering.steering
|
|
case .end:
|
|
context.responsePromise.succeed(Google_Protobuf_Empty())
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
func record(request: MotorControl_RecordingReqeust, context: StatusOnlyCallContext) -> EventLoopFuture<Google_Protobuf_Empty> {
|
|
// TODO: Recording...
|
|
return context.eventLoop.makeSucceededFuture(Google_Protobuf_Empty())
|
|
}
|
|
|
|
func save_recorded_data(request: MotorControl_SaveRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Google_Protobuf_Empty> {
|
|
// TODO Recording...
|
|
return context.eventLoop.makeSucceededFuture(Google_Protobuf_Empty())
|
|
}
|
|
}
|
|
|