コード例 #1
0
        private async Task <bool> ProcessCommand(Enums.PiContext context, Enums.PiState state, Enums.PiPinNumber pinNumber, Enums.PiVoltage voltage)
        {
            bool Result = false;

            switch (context)
            {
            case Enums.PiContext.GPIO:
                Result = Controller.SetGPIO((int)pinNumber, (GpioPinDriveMode)state, voltage == Enums.PiVoltage.HIGH ? GpioPinValue.High : GpioPinValue.Low);
                await System.Threading.Tasks.Task.Delay(300).ConfigureAwait(false);

                return(Result);

            default:
                Logger.Log("Context doesnt satisfy. (ProcessCommand())", Enums.LogLevels.Warn);
                await System.Threading.Tasks.Task.Delay(300).ConfigureAwait(false);

                return(Result);
            }
        }
コード例 #2
0
        // Command Format
        // AUTH_CODE|CONTEXT|STATE|VALUE1|VALUE2|VALUE3
        // 3033|GPIO|OUTPUT|2 (pin number)|LOW (pin voltage)
        // 3033|FETCH|2 (pin number) => OK|IsOn status|Pin Number|Mode status
        private async Task <string> OnRecevied(string datarecevied)
        {
            if (string.IsNullOrEmpty(datarecevied) || string.IsNullOrWhiteSpace(datarecevied))
            {
                Logger.Log("Data is null!", Enums.LogLevels.Error);
                return("Failed!");
            }

            if (!VerifyAuthentication(datarecevied))
            {
                Logger.Log("Incorrect Authentication code. cannot proceed...", Enums.LogLevels.Error);
                return("Failed!");
            }

            Enums.PiContext   PiContext  = Enums.PiContext.GPIO;
            Enums.PiState     PiState    = Enums.PiState.OUTPUT;
            Enums.PiPinNumber PinNumber  = Enums.PiPinNumber.PIN_2;
            Enums.PiVoltage   PinVoltage = Enums.PiVoltage.LOW;

            string[] rawData = datarecevied.Split('|');

            switch (rawData[1].Trim())
            {
            case "GPIO":
                PiContext = Enums.PiContext.GPIO;
                switch (rawData[2].Trim())
                {
                case "OUTPUT":
                    PiState = Enums.PiState.OUTPUT;

                    switch (rawData[4].Trim())
                    {
                    case "HIGH":
                        PinVoltage = Enums.PiVoltage.HIGH;

                        try {
                            PinNumber = (Enums.PiPinNumber)Convert.ToInt32(rawData[3].Trim());
                        }
                        catch (Exception) {
                            Logger.Log("Pin number doesnt satisfy.", Enums.LogLevels.Warn);
                            return("Pin number doesnt satisfy.");
                        }

                        break;

                    case "LOW":
                        PinVoltage = Enums.PiVoltage.LOW;

                        try {
                            PinNumber = (Enums.PiPinNumber)Convert.ToInt32(rawData[3].Trim());
                        }
                        catch (Exception) {
                            Logger.Log("Pin number doesnt satisfy.", Enums.LogLevels.Warn);
                            return("Pin number doesnt satisfy.");
                        }

                        break;

                    default:
                        Logger.Log("Voltage doesnt satisfy.", Enums.LogLevels.Warn);
                        return("Voltage doesnt satisfy.");
                    }

                    break;

                case "INPUT":
                    PiState = Enums.PiState.INPUT;

                    switch (rawData[4].Trim())
                    {
                    case "HIGH":
                        PinVoltage = Enums.PiVoltage.HIGH;

                        try {
                            PinNumber = (Enums.PiPinNumber)Convert.ToInt32(rawData[3].Trim());
                        }
                        catch (Exception) {
                            Logger.Log("Pin number doesnt satisfy.", Enums.LogLevels.Warn);
                            return("Pin number doesnt satisfy.");
                        }

                        break;

                    case "LOW":
                        PinVoltage = Enums.PiVoltage.LOW;

                        try {
                            PinNumber = (Enums.PiPinNumber)Convert.ToInt32(rawData[3].Trim());
                        }
                        catch (Exception) {
                            Logger.Log("Pin number doesnt satisfy.", Enums.LogLevels.Warn);
                            return("Pin number doesnt satisfy.");
                        }

                        break;

                    default:
                        Logger.Log("Voltage doesnt satisfy.", Enums.LogLevels.Warn);
                        return("Voltage doesnt satisfy.");
                    }

                    break;

                default:
                    Logger.Log("States doesnt satisfy.", Enums.LogLevels.Warn);
                    return("States doesnt satisfy.");
                }
                break;

            case "FETCH":
                PiContext = Enums.PiContext.FETCH;
                int           pinNumber = Convert.ToInt32(rawData[2].Trim());
                GPIOPinConfig status    = Controller.FetchPinStatus(pinNumber);
                if (status == null)
                {
                    Logger.Log("Fetch failed.");
                    return($"FAILED|FETCH_FAIL");
                }
                else
                {
                    Logger.Log("Fetch command sucess!");
                    return($"OK|{status.IsOn.ToString()}|{pinNumber.ToString()}|{status.Mode.ToString()}");
                }

            case "SHUTDOWN":
                PiContext = Enums.PiContext.SHUTDOWN;
                switch (rawData[2].Trim())
                {
                case "TESS":
                    Helpers.InBackground(async() => {
                        Task.Delay(2000).Wait();
                        await Exit().ConfigureAwait(false);
                    });
                    return($"Exiting {Core.AssistantName} in 2 seconds...");

                case "PI":
                    Helpers.InBackground(() => {
                        Task.Delay(2000).Wait();
                        Helpers.ExecuteCommand("sudo shutdown -h now");
                    });
                    return("Rasperry Pi shutting down in 2 seconds...");
                }
                break;

            case "RESTART":
                PiContext = Enums.PiContext.RESTART;
                switch (rawData[2].Trim())
                {
                case "TESS":
                    Helpers.InBackground(async() => {
                        Task.Delay(2000).Wait();
                        await Restart().ConfigureAwait(false);
                    });
                    return($"Restarting {Core.AssistantName} in 2 seconds...");
                }
                break;

            case "RELAY":
                PiContext = Enums.PiContext.RELAY;
                switch (Convert.ToInt32(rawData[2].Trim()))
                {
                case (int)Enums.GPIOCycles.Cycle:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.Cycle).ConfigureAwait(false), "Relay Cycle");
                    return("Cycle completed!");

                case (int)Enums.GPIOCycles.OneMany:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.OneMany).ConfigureAwait(false), "Relay Cycle");
                    return("OneMany cycle completed!");

                case (int)Enums.GPIOCycles.OneOne:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.OneOne).ConfigureAwait(false), "Relay Cycle");
                    return("OneOne cycle comepleted!");

                case (int)Enums.GPIOCycles.OneTwo:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.OneTwo).ConfigureAwait(false), "Relay Cycle");
                    return("OneTwo cycle completed!");

                case (int)Enums.GPIOCycles.Base:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.Base).ConfigureAwait(false), "Relay Cycle");
                    return("Base cycle completed!");

                case (int)Enums.GPIOCycles.Single:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.Single, Convert.ToInt32(rawData[3].Trim())).ConfigureAwait(false), "Relay Cycle");
                    return($"Sucessfully configured {rawData[3].Trim()} pin");

                case (int)Enums.GPIOCycles.Default:
                    Helpers.InBackgroundThread(async() => await Controller.RelayTestService(Enums.GPIOCycles.Default).ConfigureAwait(false), "Relay Cycle");
                    return("Sucessfully completed default cycle!");
                }
                break;

            default:
                Logger.Log("Context doesnt satisfy.", Enums.LogLevels.Warn);
                return("Failed context!");
            }

            bool Status = await ProcessCommand(PiContext, PiState, PinNumber, PinVoltage).ConfigureAwait(false);

            if (Status)
            {
                return($"OK|Sucessfully configured GPIO PIN {(int) PinNumber}");
            }
            else
            {
                return($"FAIL|Unable to configure.");
            }
        }