Exemplo n.º 1
0
        public async Task <bool> GenerateDefaultConfig()
        {
            Logger.Log("Gpio config file doesn't exist. press c to continue generating default config or q to quit...");
            await Task.Delay(500).ConfigureAwait(false);

            if (!Directory.Exists(Constants.ConfigDirectory))
            {
                Logger.Log("Config directory doesn't exist, creating one...");
                Directory.CreateDirectory(Constants.ConfigDirectory);
            }

            ConsoleKeyInfo?Key = Helpers.FetchUserInputSingleChar(TimeSpan.FromMinutes(1));

            if (!Key.HasValue)
            {
                Logger.Log("No value has been entered, continuing to run the program...");
            }
            else
            {
                switch (Key.Value.KeyChar)
                {
                case 'c':
                    break;

                case 'q':
                    Environment.Exit(-1);
                    return(false);

                default:
                    Logger.Log("Unknown value entered! continuing to run the program...");
                    break;
                }
            }

            Logger.Log("Generating default Gpio Config...");

            if (File.Exists(Constants.GpioConfigDirectory))
            {
                return(true);
            }

            GpioConfigHandler Config = new GpioConfigHandler {
                PinConfigs = new List <GpioPinConfig>()
            };

            for (int i = 0; i <= 41; i++)
            {
                GpioPinConfig PinConfig = new GpioPinConfig(i, PiController.GpioPinState.Off, PiController.GpioPinMode.Output, false, 0);
                Config.PinConfigs.Add(PinConfig);
            }

            Save(Config.PinConfigs);
            return(true);
        }
Exemplo n.º 2
0
        public override bool Equals(object?obj)
        {
            if (obj == null)
            {
                return(false);
            }

            GpioPinConfig config = (GpioPinConfig)obj;

            return(config.Pin == this.Pin);
        }
Exemplo n.º 3
0
        public async Task SaveAsync(GpioPinConfig config)
        {
            if (config == null)
            {
                return;
            }

            if (!Directory.Exists(Constants.ConfigDirectory))
            {
                Logger.Log("Config folder doesn't exist, creating one...");
                Directory.CreateDirectory(Constants.ConfigDirectory);
            }

            string filePath = Constants.GpioConfigDirectory;
            string json     = JsonConvert.SerializeObject(config, Formatting.Indented);

            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            string newFilePath = filePath + ".new";

            await ConfigSemaphore.WaitAsync().ConfigureAwait(false);

            IsConfigBeingAccessed = true;

            try {
                File.WriteAllText(newFilePath, json);

                if (File.Exists(filePath))
                {
                    File.Replace(newFilePath, filePath, null);
                }
                else
                {
                    File.Move(newFilePath, filePath);
                }

                Logger.Log("Saved config!", LogLevels.Trace);
            }
            catch (Exception e) {
                Logger.Log(e);
                Logger.Log("Failed to save config.", LogLevels.Trace);
                return;
            }
            finally {
                ConfigSemaphore.Release();
                IsConfigBeingAccessed = false;
            }
        }
Exemplo n.º 4
0
        private static void SaveConfig(GpioPinConfig config)
        {
            if (config == null)
            {
                return;
            }

            ConfigSemaphore.Wait();

            if (!Directory.Exists(Constants.GpioConfigDirectory))
            {
                Directory.CreateDirectory(Constants.GpioConfigDirectory);
            }

            string json = JsonConvert.SerializeObject(config, Formatting.Indented);

            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            string fileName    = config.Pin + ".json";
            string filePath    = Constants.GpioConfigDirectory + "/" + fileName;
            string newFilePath = filePath + ".new";

            try {
                File.WriteAllText(newFilePath, json);

                if (File.Exists(filePath))
                {
                    File.Replace(newFilePath, filePath, null);
                }
                else
                {
                    File.Move(newFilePath, filePath);
                }
            }
            catch (Exception e) {
                Logger.Log(e);
            }
            finally {
                ConfigSemaphore.Release();
            }
        }
Exemplo n.º 5
0
        public async Task <bool> RelayMorseCycle(string textToConvert, int relayPin)
        {
            if (string.IsNullOrEmpty(textToConvert))
            {
                Logger.Log("The specified text is either null or empty.", LogLevels.Warn);
                return(false);
            }

            if (Controller == null)
            {
                Logger.Log("Malfunctioning PinController.", LogLevels.Warn);
                return(false);
            }

            if (!PiController.IsValidPin(relayPin))
            {
                Logger.Log("Please specify a valid relay pin to run the cycle.", LogLevels.Warn);
                return(false);
            }

            Logger.Log($"Converting to Morse...", LogLevels.Info);
            string Morse = MorseCore.ConvertToMorseCode(textToConvert);

            if (string.IsNullOrEmpty(Morse))
            {
                Logger.Log("Conversion to Morse failed. cannot proceed.", LogLevels.Warn);
                return(false);
            }

            Logger.Log($"TEXT >> {textToConvert}");
            Logger.Log($"MORSE >> {Morse}");

            GpioPinConfig beforePinStatus = Controller.GetGpioConfig(relayPin);

            if (beforePinStatus.IsPinOn)
            {
                Controller.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off);
            }

            if (!MorseCore.IsValidMorse(Morse))
            {
                Logger.Log("The specified Morse is invalid!", LogLevels.Warn);
                return(false);
            }

            string pauseBetweenLetters = "_";                 // One Time Unit
            string pauseBetweenWords   = "_______";           // Seven Time Unit

            Morse = Morse.Replace("  ", pauseBetweenWords);
            Morse = Morse.Replace(" ", pauseBetweenLetters);

            foreach (char character in Morse.ToCharArray())
            {
                switch (character)
                {
                case '.':
                    Controller.SetGpioWithTimeout(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300));
                    break;

                case '-':
                    Controller.SetGpioWithTimeout(relayPin, GpioPinMode.Output, GpioPinState.On, TimeSpan.FromMilliseconds(300 * 3));
                    break;

                case '_':
                    await Task.Delay(300).ConfigureAwait(false);

                    break;
                }
            }

            GpioPinConfig afterPinStatus = Controller.GetGpioConfig(relayPin);

            if (afterPinStatus.IsPinOn)
            {
                Controller.SetGpioValue(relayPin, GpioPinMode.Output, GpioPinState.Off);
            }

            return(true);
        }