/// <summary>
        /// Receive robot output data from HEL
        /// </summary>
        private async void ReceiveData()
        {
            var conn   = new Grpc.Core.Channel(EmulatorManager.DEFAULT_HOST + ":" + ((EmulatorManager.programType == UserProgram.Type.JAVA) ? DEFAULT_JAVA_PORT : DEFAULT_NATIVE_PORT), Grpc.Core.ChannelCredentials.Insecure);
            var client = new EmulationReader.EmulationReaderClient(conn);

            while (EmulatorManager.IsTryingToRunRobotCode() && Instance && isConnectionOpen) // Run while robot code is running or until the object stops existing
            {
                try
                {
                    using (var call = client.RobotOutputs(new RobotOutputsRequest {
                        Api = API_VERSION,
                        TargetPlatform = EmulatorManager.programType == UserProgram.Type.JAVA ? TargetPlatform.Java : TargetPlatform.Native,
                    }))
                    {
                        while (await call.ResponseStream.MoveNext())
                        {
                            receiverConnected            = true;
                            EmulatedRoboRIO.RobotOutputs = call.ResponseStream.Current.OutputData;
                            // Debug.Log("Received " + EmulatedRoboRIO.RobotOutputs);
                        }
                    }
                }
                catch (Exception)
                {
                    receiverConnected = false;
                    await Task.Delay(ERROR_DELAY); // ms
                }
            }
            receiverConnected = false;
            isConnectionOpen  = false;
        }
        /// <summary>
        /// Send robot input data to HEL
        /// </summary>
        private async void SendData()
        {
            var conn   = new Grpc.Core.Channel(EmulatorManager.DEFAULT_HOST + ":" + ((EmulatorManager.programType == UserProgram.Type.JAVA) ? DEFAULT_JAVA_PORT : DEFAULT_NATIVE_PORT), Grpc.Core.ChannelCredentials.Insecure);
            var client = new EmulationWriter.EmulationWriterClient(conn);

            while (EmulatorManager.IsTryingToRunRobotCode() && Instance && isConnectionOpen) // Run while robot code is running or until the object stops existing
            {
                try
                {
                    using (var call = client.RobotInputs())
                    {
                        while (EmulatorManager.IsTryingToRunRobotCode() && Instance && isConnectionOpen)
                        {
                            await call.RequestStream.WriteAsync(new UpdateRobotInputsRequest
                            {
                                Api            = API_VERSION,
                                TargetPlatform = EmulatorManager.programType == UserProgram.Type.JAVA ? TargetPlatform.Java : TargetPlatform.Native,
                                InputData      = EmulatedRoboRIO.RobotInputs,
                            });

                            senderConnected = true;
                            // Debug.Log("Sending " + EmulatedRoboRIO.RobotInputs);
                            await Task.Delay(LOOP_DELAY); // ms
                        }
                    }
                }
                catch (Exception)
                {
                    senderConnected = false;
                    await Task.Delay(ERROR_DELAY); // ms
                }
            }
            using (var call = client.RobotInputs())
            {
                await call.RequestStream.CompleteAsync();
            }
            senderConnected  = false;
            isConnectionOpen = false;
        }
 public void OnApplicationQuit()
 {
     EmulatorManager.KillEmulator();
 }
예제 #4
0
        /// <summary>
        /// Check to see if the given requirement is met, dispatch a warning if not
        /// </summary>
        /// <param name="requirement">The requirement to check</param>
        /// <returns>True if the requirement the requirements it depends on are met</returns>
        public static bool CheckRequirement(Requirement requirement)
        {
            switch (requirement)
            {
            case Requirement.UseEmulation:
                if (!EmulatorManager.UseEmulation)
                {
                    UserMessageManager.Dispatch("Not configured to use emulation", WARNING_DURATION);
                    return(false);
                }
                return(true);

            case Requirement.VMInstalled:
                if (!EmulatorManager.IsVMInstalled())
                {
                    UserMessageManager.Dispatch("Emulator not installed", WARNING_DURATION);
                    return(false);
                }
                return(true);

            case Requirement.VMRunning:
                if (!EmulatorManager.IsVMRunning())
                {
                    if (CheckRequirement(Requirement.VMInstalled))
                    {
                        UserMessageManager.Dispatch("Emulator not running", WARNING_DURATION);
                    }
                    return(false);
                }
                return(true);

            case Requirement.VMConnected:
                if (!EmulatorManager.IsVMConnected())
                {
                    if (CheckRequirement(Requirement.VMRunning))
                    {
                        UserMessageManager.Dispatch("Waiting for emulator to boot", WARNING_DURATION);
                    }
                    return(false);
                }
                return(true);

            case Requirement.UserProgramPresent:
                if (!EmulatorManager.IsFRCUserProgramPresent())
                {
                    if (CheckRequirement(Requirement.VMConnected))
                    {
                        UserMessageManager.Dispatch("User program not found", WARNING_DURATION);
                    }
                    return(false);
                }
                return(true);

            case Requirement.UserProgramNotRestarting:
                if (EmulatorManager.IsRobotCodeRestarting())
                {
                    UserMessageManager.Dispatch("Waiting for user program to start", WARNING_DURATION);
                    return(false);
                }
                return(true);

            case Requirement.UserProgramRunnerRunning:
                if (!EmulatorManager.IsRunningRobotCodeRunner())
                {
                    if (CheckRequirement(Requirement.UserProgramPresent))
                    {
                        if (EmulatorManager.IsTryingToRunRobotCode())
                        {
                            UserMessageManager.Dispatch("Waiting for user program to start", WARNING_DURATION);
                        }
                        else
                        {
                            UserMessageManager.Dispatch("User program not running", WARNING_DURATION);
                        }
                    }
                    return(false);
                }
                return(true);

            case Requirement.UserProgramConnected:
                if (!EmulatorNetworkConnection.Instance.IsConnected())
                {
                    if (CheckRequirement(Requirement.UserProgramRunnerRunning))
                    {
                        UserMessageManager.Dispatch("Connecting to user program (may take a few seconds)", WARNING_DURATION);
                    }
                    return(false);
                }
                return(true);

            default:
                throw new NotSupportedException("Unknown emulation requirement");
            }
        }