private async Task <List <BlockJsonModel> > GetBlocksAsync(string configTomlFileContent, bool addonlyConfigTomlBlocks) { var blocksFromGitRepository = await BatchProcessBlockFilesAsync().ConfigureAwait(false); var dataFromFile = TomlFileReader.ReadDataFromString <ConfigurationReadModel>(configTomlFileContent); dataFromFile.Network.TryGetValue("blocks", out var blocksContent); var blocksFromFile = new List <BlockJsonModel>(); if (blocksContent is Dictionary <string, object>[] dictionaries) { foreach (var dictionary in dictionaries) { dictionary.TryGetValue("type", out var type); dictionary.TryGetValue("tag", out var tag); dictionary.TryGetValue("args", out var argument); var tempBlock = blocksFromGitRepository.FirstOrDefault(x => x.Type == (string)type); if (tempBlock != null) { var block = tempBlock.Copy(); block.Tag = tag == null ? string.Empty : tag.ToString(); var arguments = block.Args; if (argument is Dictionary <string, object> args) { foreach (var(key, value) in args) { var updatedArgument = arguments.FirstOrDefault(x => x.Name == key); if (updatedArgument != null) { updatedArgument.Value = (string)value; } } } blocksFromFile.Add(block); } } } if (!addonlyConfigTomlBlocks) { foreach (var jsonModel in blocksFromGitRepository) { if (!blocksFromFile.Contains(jsonModel)) { blocksFromFile.Add(jsonModel); } } } FixIndex(blocksFromFile); return(blocksFromFile); }
/// <summary> /// Gets the list of devices asynchronous. /// </summary> /// <returns></returns> public async Task <List <Dictionary <string, object> > > GetListOfDevicesAsync() { string filePath = _deviceGitConnectionOptions.DeviceToml; var tomlSettings = TomlFileReader.LoadLowerCaseTomlSettings(); var fileContent = await File.ReadAllTextAsync(filePath); var fileData = Toml.ReadString(fileContent, tomlSettings); var dictionary = fileData.ToDictionary(); var dictionaryDevices = (Dictionary <string, object>[])dictionary["devices"]; return(dictionaryDevices.ToList()); }
/// <summary> /// Gets the list of modules. /// </summary> /// <param name="configTomlFile">The configuration toml file.</param> /// <returns></returns> private static IEnumerable <ModuleReadModel> GetListOfModules(string configTomlFile) { EnsureArg.IsNotEmptyOrWhiteSpace(configTomlFile); var data = TomlFileReader.ReadDataFromString <ConfigurationReadModel>(configTomlFile); var listOfModules = data.Module; listOfModules = listOfModules.Select((module, index) => new ModuleReadModel { Id = index, Config = module.Config, Name = module.Name, UUID = module.UUID }).ToList(); return(listOfModules); }
/// <summary> /// Gets the list of modules asynchronous. /// </summary> /// <param name="firmwareVersion">The firmware version.</param> /// <param name="deviceType">Type of the device.</param> /// <param name="deviceTomlFilePath">The device toml file path.</param> /// <returns></returns> public async Task <List <ModuleReadModel> > GetListOfModulesAsync(string firmwareVersion, string deviceType) { var listOfModules = new List <ModuleReadModel>(); var fileContent = await GetDeviceTomlFileContentAsync(firmwareVersion) .ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(fileContent)) { var data = TomlFileReader.ReadDataFromString <ConfigurationReadModel>(data: fileContent); listOfModules = data.Module; } // fix the indexes. listOfModules = listOfModules.Select((item, index) => { item.Id = index; return(item); }).ToList(); return(listOfModules); }
/// <summary> /// Gets the module icon URL. /// </summary> /// <param name="module">The module.</param> /// <param name="moduleFilePath">The module file path.</param> /// <param name="metaTomlFilePath">The meta toml file path.</param> /// <returns></returns> private static string GetModuleIconUrl(ModuleReadModel module, string moduleFilePath, string metaTomlFilePath) { EnsureArg.IsNotNull(module); var iconUrl = string.Empty; var moduleFolder = FileReaderExtensions.GetSubDirectoryPath(moduleFilePath, module.Name); if (string.IsNullOrWhiteSpace(moduleFolder)) { return(string.Empty); } var metaTomlFile = Path.Combine(moduleFolder, metaTomlFilePath); try { if (File.Exists(metaTomlFile)) { var tml = Toml.ReadFile(metaTomlFile, TomlFileReader.LoadLowerCaseTomlSettings()); var dict = tml.ToDictionary(); var moduleValues = dict["module"]; if (moduleValues is Dictionary <string, object> ) { var moduleFromToml = (Dictionary <string, object>)dict["module"]; if (moduleFromToml != null && (string)moduleFromToml["name"] == module.Name) { iconUrl = moduleFromToml["iconUrl"].ToString(); if (!iconUrl.IsPathUrl()) { return(string.Empty); } } } } } catch (Exception) { iconUrl = string.Empty; } return(iconUrl); }
private static Dictionary <string, object> GetConfigValues(string fileContent, string moduleName) { var tomlSettings = TomlFileReader.LoadLowerCaseTomlSettings(); var fileData = Toml.ReadString(fileContent, tomlSettings); var configValues = new Dictionary <string, object>(); var dictionary = fileData.ToDictionary(); var listOfModules = (Dictionary <string, object>[])dictionary["module"]; // here message.name means Power, j1939 etc. var module = listOfModules.FirstOrDefault(dic => dic.Values.Contains <object>(moduleName)); if (module?.ContainsKey("config") == true) { configValues = (Dictionary <string, object>)module["config"]; } return(configValues); }
/// <summary> /// Processes the block file asynchronous. /// </summary> /// <param name="filesData">The files data.</param> /// <returns></returns> private static async Task <List <BlockJsonModel> > ProcessBlockFileAsync(IDictionary <string, string> filesData) { var blocks = new List <BlockJsonModel>(); var tomlSettings = TomlFileReader.LoadLowerCaseTomlSettings(); foreach (var data in filesData) { var blockReadModel = Toml.ReadString <BlockReadModel>(data.Value, tomlSettings); var blockFileName = Path.GetFileNameWithoutExtension(data.Key); var blockTask = GetBlockAsync(blockReadModel, blockFileName); var modulesTask = GetModulesAsync(blockReadModel); await Task.WhenAll(blockTask, modulesTask); var block = blockTask.Result; block.Modules.AddRange(modulesTask.Result); blocks.Add(block); } return(blocks); }