Пример #1
0
        /// <summary>
        /// Update Start from module Twin.
        /// </summary>
        static async Task  DoTwinUpdate(TwinCollection desiredProperties, ModuleClient ioTHubModuleClient)
        {
            string serializedStr = JsonConvert.SerializeObject(desiredProperties);

            Console.WriteLine("Updating desired properties:");
            Console.WriteLine(serializedStr);

            if (string.IsNullOrEmpty(serializedStr))
            {
                Console.WriteLine("No configuration provided for the module Twin.");
            }
            else if (!desiredProperties.Contains(DriveProfileConfig.DriveProfileConfigSection))
            {
                Console.WriteLine("No driverProfile configuration defined in Twin desired properties or local settings");
                Console.WriteLine("Configuration must contain required section " + DriveProfileConfig.DriveProfileConfigSection);
            }
            else
            {
                Console.WriteLine("Attempt to parse configuration");
                DriveProfileConfig config = JsonConvert.DeserializeObject <DriveProfileConfig>(serializedStr);


                ModuleMessageHandler moduleHandle = ModuleMessageHandler.CreateFromConfig(config);

                if (moduleHandle != null)
                {
                    var userContext = new Tuple <ModuleClient, ModuleMessageHandler>(ioTHubModuleClient, moduleHandle);
                    // Register callback to be called when a message is received by the module
                    await ioTHubModuleClient.SetInputMessageHandlerAsync(
                        "driveProfileInput",
                        PipeMessage,
                        userContext);

                    Console.WriteLine("DriverProfile module was loaded sucessfully and listining for modbus messages.");
                }
                else
                {
                    Console.WriteLine("Error creating modbus message handler. Message processing stopped.");
                }

                // Report current module config via moduleTwin
                serializedStr = JsonConvert.SerializeObject(config);
                TwinCollection reported = JsonConvert.DeserializeObject <TwinCollection>(serializedStr);
                await ioTHubModuleClient.UpdateReportedPropertiesAsync(reported);
            }
        }
Пример #2
0
        public static ModuleMessageHandler CreateFromConfig(DriveProfileConfig config)
        {
            if (config == null || config.SignalConfigs == null)
            {
                return(null);
            }

            ModuleMessageHandler moduleHandle = new ModuleMessageHandler();

            foreach (string keyName in config.SignalConfigs.Keys)
            {
                DriveProfileSignalConfig signalConfig    = config.SignalConfigs[keyName];
                SignalProcessor          signalProcessor = new SignalProcessor(keyName, signalConfig);
                moduleHandle.SignalProcessorList.Add(signalProcessor);
            }

            return(moduleHandle);
        }