Example #1
0
        /// <summary>
        /// Saves the pin configuration as a whole.
        /// </summary>
        /// <param name="pinConfig">The pin config collection <see cref="PinConfig"/></param>
        /// <returns>The <see cref="Task"/></returns>
        internal async Task SaveConfig()
        {
            if (PinConfigs == null || PinConfigs.Count <= 0)
            {
                return;
            }

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

            try {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);

                ClearFile(Constants.GpioConfigPath);
                using (FileStream fileStream = new FileStream(Constants.GpioConfigPath, FileMode.OpenOrCreate, FileAccess.Write)) {
                    using (StreamWriter writer = new StreamWriter(fileStream)) {
                        await writer.WriteAsync(json).ConfigureAwait(false);

                        await writer.FlushAsync().ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e) {
                Logger.Exception(e);
                return;
            }
            finally {
                Sync.Release();
            }
        }
Example #2
0
        /// <summary>
        /// Loads the internally defined shell commands, then loads the external commands libraries, then starts the shell instance.
        /// </summary>
        /// <returns>Boolean, indicating if the process was successful.</returns>
        internal static async Task <bool> InitInterpreterAsync()
        {
            if (InitCompleted)
            {
                return(false);
            }

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

            try {
                await Init.LoadInternalCommandsAsync <IShellCommand>().ConfigureAwait(false);

                await Init.LoadCommandsAsync <IShellCommand>().ConfigureAwait(false);

                InitCompleted = true;
                Helpers.InBackground(async() => await ReplAsync().ConfigureAwait(false));
                return(InitCompleted);
            }
            catch (Exception e) {
                Logger.Exception(e);
                return(false);
            }
            finally {
                Sync.Release();
            }
        }
Example #3
0
        public static void HandleTaskExceptions(object?sender, UnobservedTaskExceptionEventArgs e)
        {
            if (sender == null || e == null || e.Exception == null)
            {
                return;
            }

            Logger.Exception(e.Exception);
            e.SetObserved();
        }
Example #4
0
        /// <summary>
        /// Log a error message followed by an exception
        /// </summary>
        /// <param name="exception">Exception</param>
        /// <param name="message">Error message</param>
        public static void Exception(Exception exception, string message)
        {
            Check.Argument.IsNotNull(exception, "exception");
            Check.Argument.IsNotNullOrEmpty(message, "message");

            InternalLogger.Exception(exception, message);
        }
Example #5
0
        internal async Task <bool> LoadInternalCommandsAsync <T>() where T : IShellCommand
        {
            Assembly currentAssembly = Assembly.GetExecutingAssembly();
            await LoadSync.WaitAsync().ConfigureAwait(false);

            try {
                ConventionBuilder conventions = new ConventionBuilder();
                conventions.ForTypesDerivedFrom <T>().Export <T>();
                IEnumerable <Assembly> psuedoCollection = new HashSet <Assembly>()
                {
                    currentAssembly
                };

                ContainerConfiguration configuration = new ContainerConfiguration().WithAssemblies(psuedoCollection, conventions);
                using CompositionHost container = configuration.CreateContainer();
                List <T> list = container.GetExports <T>().ToList();

                if (list.Count <= 0)
                {
                    return(false);
                }

                foreach (T command in list)
                {
                    if (await IsExistingCommand <T>(command.UniqueId).ConfigureAwait(false))
                    {
                        Logger.Warn($"'{command.CommandName}' shell command is already loaded; skipping from loading process...");
                        continue;
                    }

                    await command.InitAsync().ConfigureAwait(false);

                    Interpreter.Commands.Add(command.CommandKey, command);
                    Logger.Trace($"Loaded shell command -> {command.CommandName}");
                }

                return(true);
            }
            catch (Exception e) {
                Logger.Exception(e);
                return(false);
            }
            finally {
                LoadSync.Release();
            }
        }
Example #6
0
        internal async Task SaveAsync()
        {
            if (!Directory.Exists(Constants.ConfigDirectory))
            {
                Directory.CreateDirectory(Constants.ConfigDirectory);
            }

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

            Core.GetFileWatcher().Pause();

            Logger.Trace("Saving core config...");

            try {
                string filePath    = Constants.CoreConfigPath;
                string json        = JsonConvert.SerializeObject(this, Formatting.Indented);
                string newFilePath = filePath + ".new";

                using (StreamWriter writer = new StreamWriter(newFilePath)) {
                    writer.Write(json);
                    writer.Flush();
                }

                if (File.Exists(filePath))
                {
                    File.Replace(newFilePath, filePath, null);
                }
                else
                {
                    File.Move(newFilePath, filePath);
                }
            }
            catch (Exception e) {
                Logger.Exception(e);
                return;
            }
            finally {
                ConfigSemaphore.Release();
                Core.GetFileWatcher().Resume();
            }

            Logger.Trace("Saved core config!");
        }
Example #7
0
        internal static string Encrypt(string data)
        {
            if (string.IsNullOrEmpty(data) || string.IsNullOrWhiteSpace(data))
            {
                return(string.Empty);
            }

            try {
                using (RijndaelManaged myRijndael = new RijndaelManaged()) {
                    myRijndael.GenerateKey();
                    myRijndael.GenerateIV();
                    return(Convert.ToBase64String(EncryptStringToBytes(data, myRijndael.Key, myRijndael.IV)));
                }
            }
            catch (Exception e) {
                Logger.Exception(e);
                return(string.Empty);
            }
        }
Example #8
0
        internal async Task <GithubResponse> LoadAsync()
        {
            if (string.IsNullOrEmpty(Constants.GitReleaseUrl) || Client == null || Sync == null)
            {
                return(this);
            }

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

            try {
                string?json = null;

                for (int i = 0; i < MAX_TRIES; i++)
                {
                    HttpResponseMessage responseMessage = await Client.SendAsync(new HttpRequestMessage(HttpMethod.Get, Constants.GitReleaseUrl)).ConfigureAwait(false);

                    if (responseMessage == null || responseMessage.StatusCode != System.Net.HttpStatusCode.OK || responseMessage.Content == null)
                    {
                        Logger.Trace($"Request failed ({i})");
                        Logger.Trace(responseMessage?.ReasonPhrase);
                        continue;
                    }

                    json = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                    break;
                }

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

                GithubResponse obj = JsonConvert.DeserializeObject <GithubResponse>(json);
                this.Assets          = obj.Assets;
                this.PublishedAt     = obj.PublishedAt;
                this.ReleaseFileName = obj.ReleaseFileName;
                this.ReleaseTagName  = obj.ReleaseTagName;
                this.ReleaseUrl      = obj.ReleaseUrl;
                return(this);
            }
            catch (Exception e) {
                Logger.Exception(e);
                return(this);
            }
            finally {
                Sync.Release();
            }
        }
Example #9
0
        internal async Task <bool> LoadAsync(bool isEnabled)
        {
            if (!isEnabled)
            {
                return(false);
            }

            var assemblyCollection = LoadAssemblies();

            if (assemblyCollection == null || assemblyCollection.Count <= 0)
            {
                Logger.Trace("No assemblies found.");
                return(false);
            }

            assemblyCollection.Add(Assembly.GetExecutingAssembly());
            await ModuleLoaderSemaphore.WaitAsync().ConfigureAwait(false);

            try {
                ConventionBuilder conventions = new ConventionBuilder();
                conventions.ForTypesDerivedFrom <IModule>().Export <IModule>();
                ContainerConfiguration configuration = new ContainerConfiguration().WithAssemblies(assemblyCollection, conventions);

                using CompositionHost container = configuration.CreateContainer();

                foreach (IModule module in container.GetExports <IModule>())
                {
                    Logger.Trace($"Loading module {module.ModuleIdentifier} ...");
                    module.ModuleIdentifier = GenerateModuleIdentifier(module);
                    ModuleWrapper <IModule> data = new ModuleWrapper <IModule>(module.ModuleIdentifier, module, true);
                    Logger.Trace($"Loaded module with id {module.ModuleIdentifier} !");

                    if (!IsExisitingModule(module))
                    {
                        Modules.Add(module);
                    }

                    ModulesCache.Add(data);
                }
            }
            catch (Exception e) {
                Logger.Exception(e);
                return(false);
            }
            finally {
                ModuleLoaderSemaphore.Release();
            }

            return(true);
        }
Example #10
0
 private static void JobManagerOnException(JobExceptionInfo obj) => Logger.Exception(obj.Exception);
Example #11
0
        /// <summary>
        /// Log an exception
        /// </summary>
        /// <param name="exception">Exception</param>
        public static void Exception(Exception exception)
        {
            Check.Argument.IsNotNull(exception, "exception");

            InternalLogger.Exception(exception);
        }