コード例 #1
0
    public override async Task <Sensormanagement.AllSensorsOfTypeResponse> GetAllSensorsOfType(
        Sensormanagement.AllSensorsOfTypeRequest request, ServerCallContext context)
    {
        List <Sensor> sensors = new List <Sensor>();
        List <Sensormanagement.Sensor> protobufSensors = new List <Sensormanagement.Sensor>();

        // Create the event that triggers when the execution of the action is finished.
        ManualResetEvent signalEvent = new ManualResetEvent(false);

        // The action that will we executed on the Unity MainThread
        Action action = () =>
        {
            sensors.AddRange(SensorManager.FindAllSensorsOfType((SensorType)request.Type));

            foreach (Sensor sensor in sensors)
            {
                protobufSensors.Add(ConvertSensorToSensormanagementSensor(sensor));
            }

            signalEvent.Set();
        };

        // Call the exection of the action
        ThreadManager.ExecuteOnMainThread(action);

        // Wait for the event to be triggered from the action, signaling that the action is finished
        signalEvent.WaitOne();
        signalEvent.Close();

        return(await Task.FromResult(new Sensormanagement.AllSensorsOfTypeResponse
        {
            Sensors = { protobufSensors.ToArray() }
        }));
    }