public static ModuleHandle CreateHandleFromConfiguration(ModuleConfig config) { ModuleHandle handle = new ModuleHandle(); foreach (var database in config.DataBases) { var connection = new SqliteConnection("" + new SqliteConnectionStringBuilder { DataSource = database.Value.DbPath }); try { connection.Open(); foreach (var tbl in database.Value.Tables) { TryCreateTable(connection, tbl.Value); } } catch (Exception e) { Console.WriteLine($"Exception while opening database, err message: {e.Message}"); Console.WriteLine("Check if the database file is created or being mounted into the conainter correctly"); } handle.connections.Add(database.Value.DbPath, connection); } return(handle); }
/// <summary> /// Update Start from module Twin. /// </summary> static async Task UpdateStartFromTwin(TwinCollection desiredProperties, ModuleClient ioTHubModuleClient) { ModuleConfig config; ModuleHandle moduleHandle; string jsonStr = null; string serializedStr; serializedStr = JsonConvert.SerializeObject(desiredProperties); Console.WriteLine("Desired property change:"); Console.WriteLine(serializedStr); if (desiredProperties.Contains(ModuleConfig.ConfigKeyName)) { // get config from Twin jsonStr = serializedStr; } else { Console.WriteLine("No configuration found in desired properties, look in local..."); if (File.Exists(@"iot-edge-sqlite.json")) { try { // get config from local file jsonStr = File.ReadAllText(@"iot-edge-sqlite.json"); } catch (Exception ex) { Console.WriteLine("Load configuration error: " + ex.Message); } } else { Console.WriteLine("No configuration found in local file."); } } if (!string.IsNullOrEmpty(jsonStr)) { Console.WriteLine("Attempt to load configuration: " + jsonStr); config = ModuleConfig.CreateConfigFromJSONString(jsonStr); if (config.IsValidate()) { moduleHandle = ModuleHandle.CreateHandleFromConfiguration(config); if (moduleHandle != null) { var userContext = new Tuple <ModuleClient, ModuleHandle>(ioTHubModuleClient, moduleHandle); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync( "input1", PipeMessage, userContext); } } } }
public static ModuleConfig CreateConfigFromJSONString(string jsonStr) { ModuleConfig config = new ModuleConfig(); Dictionary <string, DataBase> databases = new Dictionary <string, DataBase>(); config.DataBases = databases; JToken configToken = JObject.Parse(jsonStr).GetValue(ConfigKeyName).First; while (configToken != null) { JProperty configProperty = (JProperty)configToken; if (configProperty.Name.ToString() != "$version") { DataBase database = new DataBase(); Dictionary <string, Table> tables = new Dictionary <string, Table>(); database.Tables = tables; JToken dbToken = configToken.First.First; while (dbToken != null) { JProperty dbProperty = (JProperty)dbToken; if (dbProperty.Name.ToString() == "DbPath") { database.DbPath = dbProperty.Value.ToString(); } else { Table table = new Table(); Dictionary <string, Column> columns = new Dictionary <string, Column>(); table.Columns = columns; JToken tableToken = dbToken.First.First; while (tableToken != null) { JProperty tableProperty = (JProperty)tableToken; if (tableProperty.Name.ToString() == "TableName") { table.TableName = tableProperty.Value.ToString(); } else { Column column = JsonConvert.DeserializeObject <Column>(tableProperty.Value.ToString()); columns.Add(tableProperty.Name.ToString(), column); } tableToken = tableToken.Next; } tables.Add(dbProperty.Name.ToString(), table); } dbToken = dbToken.Next; } databases.Add(configProperty.Name.ToString(), database); } configToken = configToken.Next; } return(config); }