private static Task OnDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext) { try { string json = JsonConvert.SerializeObject(desiredProperties); Console.WriteLine("Desired property change:"); ModbusLayoutConfig modbusConfig = JsonConvert.DeserializeObject <ModbusLayoutConfig>(json); ModbusMessage.SurfaceLayoutConfig = modbusConfig.SurfaceCardConfiguration; ModbusMessage.PumpLayoutConfig = modbusConfig.PumpCardConfiguration; string sqlConnectionString = desiredProperties["sqlConnectionString"]; DataHelper.ConnectionString = sqlConnectionString; Console.WriteLine($"sqlConnectionString: {sqlConnectionString}"); } catch (AggregateException ex) { foreach (Exception exception in ex.InnerExceptions) { Console.WriteLine(); Console.WriteLine("Error when receiving desired property: {0}", exception); } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine("Error when receiving desired property: {0}", ex.Message); } return(Task.CompletedTask); }
private static Task OnDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext) { try { string json = JsonConvert.SerializeObject(desiredProperties); Console.WriteLine("Desired property change:"); ModbusLayoutConfig modbusConfig = JsonConvert.DeserializeObject <ModbusLayoutConfig>(json); ModbusMessage.SurfaceLayoutConfig = modbusConfig.SurfaceCardConfiguration; ModbusMessage.PumpLayoutConfig = modbusConfig.PumpCardConfiguration; OpcMessageConfig opcCardConfig = JsonConvert.DeserializeObject <OpcMessageConfig>(json); OpcMessage.PumpCardConfig = opcCardConfig.PumpCard; OpcMessage.SurfaceCardConfig = opcCardConfig.SurfaceCard; string sqlConnectionString = desiredProperties["sqlConnectionString"]; DataHelper.ConnectionString = sqlConnectionString; Console.WriteLine($"sqlConnectionString: {sqlConnectionString}"); try { int numberOfMinsForHistory = desiredProperties["dynoCardHistoryInMins"]; DataHelper.NumberOfMinutesForHistory = numberOfMinsForHistory; Console.WriteLine($"dynoCardHistoryInMins: {numberOfMinsForHistory}"); } catch (Exception ex) { System.Console.WriteLine("Error reading dynoCardHistoryInMins property, setting to default value - 1. " + ex.Message); DataHelper.NumberOfMinutesForHistory = 1; } } catch (AggregateException ex) { foreach (Exception exception in ex.InnerExceptions) { Console.WriteLine(); Console.WriteLine("Error when receiving desired property: {0}", exception); } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine("Error when receiving desired property: {0}", ex.Message); } return(Task.CompletedTask); }
/// <summary> /// Initializes the DeviceClient and sets up the callback to receive /// messages containing temperature information /// </summary> private static async Task Init(string connectionString, bool bypassCertVerification = false) { Console.WriteLine("Connection String {0}", connectionString); MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); // During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production if (bypassCertVerification) { mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; } ITransportSettings[] settings = { mqttSetting }; // Open a connection to the Edge runtime DeviceClient ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings); await ioTHubModuleClient.OpenAsync(); Console.WriteLine("IoT Hub module client initializing..."); // Read Module Twin Desired Properties var moduleTwin = await ioTHubModuleClient.GetTwinAsync(); var desiredProperties = moduleTwin.Properties.Desired; string json = JsonConvert.SerializeObject(desiredProperties); ModbusLayoutConfig modbusConfig = JsonConvert.DeserializeObject <ModbusLayoutConfig>(json); ModbusMessage.SurfaceLayoutConfig = modbusConfig.SurfaceCardConfiguration; ModbusMessage.PumpLayoutConfig = modbusConfig.PumpCardConfiguration; string sqlConnectionString = desiredProperties["sqlConnectionString"]; DataHelper.ConnectionString = sqlConnectionString; Console.WriteLine($"sqlConnectionString: {sqlConnectionString}"); try { Console.WriteLine("Setting module twin property handler"); // Attach callback for Twin desired properties updates await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null); Console.WriteLine("Setting modbusInput handler"); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("modbusInput", ProcessModbusInput, ioTHubModuleClient); Console.WriteLine("Setting opcInput handler"); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("opcInput", ProcessOPCInput, ioTHubModuleClient); Console.WriteLine("Setting alertInput handler"); // Register callback to be called when an alert is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("alertInput", ProcessAlert, ioTHubModuleClient); Console.WriteLine("Setting config update direct method handler"); // Register callback to be called when an alert is received by the module await ioTHubModuleClient.SetMethodHandlerAsync("configUpdate", ConfigurationUpdate, ioTHubModuleClient); Console.WriteLine("Done setting inputs"); } catch (Exception ex) { Console.WriteLine($"Exception during Init: {ex.Message}"); Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Initializes the DeviceClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init() { Console.WriteLine("Starting Init process..."); try { Console.WriteLine("Setting transport settings..."); AmqpTransportSettings amqpTransportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); ITransportSettings[] settings = { amqpTransportSetting }; Console.WriteLine("Setting up module client..."); // Open a connection to the Edge runtime ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings); await ioTHubModuleClient.OpenAsync(); Console.WriteLine("IoT Hub module client initialized."); Console.WriteLine("Setting module twin info..."); var moduleTwin = await ioTHubModuleClient.GetTwinAsync(); Console.WriteLine("Getting module twin desired props..."); var desiredProperties = moduleTwin.Properties.Desired; Console.WriteLine("Starting SQL connection string..."); string sqlConnectionString = desiredProperties["sqlConnectionString"]; DataHelper.ConnectionString = sqlConnectionString; Console.WriteLine($"sqlConnectionString: {sqlConnectionString}"); string json = JsonConvert.SerializeObject(desiredProperties); ModbusLayoutConfig modbusConfig = JsonConvert.DeserializeObject <ModbusLayoutConfig>(json); ModbusMessage.SurfaceLayoutConfig = modbusConfig.SurfaceCardConfiguration; ModbusMessage.PumpLayoutConfig = modbusConfig.PumpCardConfiguration; Console.WriteLine("Setting module twin property handler"); // Attach callback for Twin desired properties updates await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null); Console.WriteLine("Setting modbusInput handler"); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("modbusInput", ProcessModbusInput, ioTHubModuleClient); Console.WriteLine("Setting opcInput handler"); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("opcInput", ProcessOPCInput, ioTHubModuleClient); Console.WriteLine("Setting generated telemetry handler"); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("telemetryInput", GeneratedTelemetryInput, ioTHubModuleClient); Console.WriteLine("Setting alertInput handler"); // Register callback to be called when an alert is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync("alertInput", ProcessAlert, ioTHubModuleClient); Console.WriteLine("Setting config update direct method handler"); // Register callback to be called when an alert is received by the module await ioTHubModuleClient.SetMethodHandlerAsync("configUpdate", ConfigurationUpdate, ioTHubModuleClient); Console.WriteLine("Done setting inputs"); } catch (Exception ex) { Console.WriteLine($"Exception during Init: {ex.Message}"); Console.WriteLine(ex.StackTrace); } }