Beispiel #1
0
      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);
      }
Beispiel #2
0
      /// <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("Setting SQL connection string...");
              string sqlConnectionString = desiredProperties["sqlConnectionString"];
              DataHelper.ConnectionString = sqlConnectionString;
              Console.WriteLine($"sqlConnectionString: {sqlConnectionString}");

              Console.WriteLine("Setting number of mins for dyno card history...");
              int historyInMins = desiredProperties["dynoCardHistoryInMins"];
              DataHelper.NumberOfMinutesForHistory = historyInMins;
              Console.WriteLine($"dynoCardHistoryInMins: {historyInMins}");

              string             json         = JsonConvert.SerializeObject(desiredProperties);
              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;

              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);
          }
      }