import grpc
from concurrent import futures
import time
import agv_calib_control_pb2 as pb2
import agv_calib_control_pb2_grpc as pb2_grpc

# 扮演 Windows 车端的角色
class FakeAgvServer(pb2_grpc.AgvCalibControlServiceServicer):
    def SetControlMode(self, request, context):
        print(f"\n[🚙 Windows 假车端] 收到 Linux 夺权指令! 目标模式: {request.target_mode}")
        time.sleep(0.5) # 假装底层继电器切换花了点时间
        print("[🚙 Windows 假车端] 避障已切断，乖乖交出控制权！")
        
        # 返回成功回执给 Linux
        return pb2.StandardResponse(success=True, message="Windows: 已交出底盘控制权！")

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    pb2_grpc.add_AgvCalibControlServiceServicer_to_server(FakeAgvServer(), server)
    server.add_insecure_port('[::]:50051')
    print("🚀 [Windows 假车端] 已启动，正在监听 50051 端口，等待 Linux 大脑连接...")
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    serve()