Esempio n. 1
0
        /// <summary>
        /// This method is called whenever the module is sent a message from the EdgeHub.
        /// It just pipe the messages without any change.
        /// It prints all the incoming messages.
        /// </summary>
        static async Task <MessageResponse> PipeMessage(Message message, object userContext)
        {
            Console.WriteLine("pids18b20 - Received command");
            int          counterValue = Interlocked.Increment(ref counter);
            DeviceClient deviceClient = (DeviceClient)userContext;

            var userContextValues = userContext as Tuple <DeviceClient, Sensors.ModuleConfig>;

            if (userContextValues == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " +
                                                    "expected values");
            }
            DeviceClient ioTHubModuleClient = userContextValues.Item1;

            Sensors.ModuleConfig moduleConfig = userContextValues.Item2;

            byte[] messageBytes  = message.GetBytes();
            string messageString = Encoding.UTF8.GetString(messageBytes);

            Console.WriteLine($"Received message: {counterValue}, Body: [{messageString}]");

            if (!string.IsNullOrEmpty(messageString))
            {
                var pipeMessage = new Message(messageBytes);
                foreach (var prop in message.Properties)
                {
                    pipeMessage.Properties.Add(prop.Key, prop.Value);
                }
                await deviceClient.SendEventAsync("output1", pipeMessage);

                Console.WriteLine("Received message sent");
            }
            return(MessageResponse.Completed);
        }
Esempio n. 2
0
        /// <summary>
        /// Iterate through each sensor to poll data
        /// </summary>
        static async Task Start(object userContext)
        {
            var userContextValues = userContext as Tuple <DeviceClient, Sensors.ModuleConfig>;

            if (userContextValues == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " +
                                                    "expected values");
            }

            DeviceClient ioTHubModuleClient = userContextValues.Item1;

            Sensors.ModuleConfig moduleConfig = userContextValues.Item2;

            while (m_run)
            {
                foreach (string s in moduleConfig.SensorConfigs.Keys)
                {
                    SensorConfig sc = moduleConfig.SensorConfigs[s];
                    if (moduleConfig.VerboseLogging)
                    {
                        Console.WriteLine("Attempting to read sensor: " + sc);
                    }
                    SensorReading sensorReading = moduleConfig.ReadSensor(sc.SensorId);
                    if (moduleConfig.VerboseLogging)
                    {
                        Console.WriteLine("SensorReading:" + sensorReading);
                    }
                    Message message = null;
                    message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(sensorReading)));
                    message.Properties.Add("content-type", "application/edge-pids18b20-json");
                    message.ContentEncoding = "utf-8";
                    if (moduleConfig.VerboseLogging)
                    {
                        Console.Write("Sending message: " + message.BodyStream);
                    }

                    if (message != null)
                    {
                        await ioTHubModuleClient.SendEventAsync("pds18b20Output", message);
                    }
                    if (!m_run)
                    {
                        break;
                    }
                }

                await Task.Delay(1000 *moduleConfig.PublishIntervalSeconds);
            }
        }
Esempio n. 3
0
        public SensorReading(ModuleConfig mc, string sensorId)
        {
            SensorConfig sc = mc.SensorConfigs[sensorId];

            PublishTimestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            SensorName       = string.IsNullOrEmpty(sc.SensorName) ? ModuleConfigDefaults.DefaultSensorName : sc.SensorName;
            UOM         = string.IsNullOrEmpty(sc.UOM) ? ModuleConfigDefaults.DefaultSensorUOM : sc.UOM;
            Temperature = -9999;

            string sensorfile   = mc.SensorDevicePath + sc.SensorId + mc.SensorDevicePathSuffix;
            bool   deviceReadOk = false;

            if (mc.VerboseLogging)
            {
                Console.WriteLine("Opening file: " + sensorfile);
            }
            // Open the sensor file
            string[] lines = { "NO", "READING=-9999" };
            try
            {
                lines        = System.IO.File.ReadAllLines(sensorfile);
                deviceReadOk = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to read device file " + sensorfile + ": " + ex.Message);
            }

            if (deviceReadOk)
            {
                // DS18B20 device files have the following structure:
                // Line 1: Status
                // Line 2: Reading
                // Status will be YES if the reading is valid, if it's not YES then we should report an error
                // Reading takes the form of string=number, where number is the Celcius temperature in thousandths of a degree

                if (lines[0].Contains("YES"))
                {
                    string[] readingString = lines[1].Split('=');
                    float    readingC1000  = Convert.ToSingle(readingString[1]);
                    Temperature = readingC1000 / (float)1000.0;
                    if (sc.UOM.Equals("F"))
                    {
                        Temperature = (float)32 + ((float)1.8 * Temperature);
                    }
                }
            }
        }