Ejemplo n.º 1
0
        /// <summary>
        /// Module behavior:
        ///        Sends data periodically (with default frequency of 5 seconds).
        ///        Data trend:
        ///         - Machine Temperature regularly rises from 21C to 100C in regularly with jitter
        ///         - Machine Pressure correlates with Temperature 1 to 10psi
        ///         - Ambient temperature stable around 21C
        ///         - Humidity is stable with tiny jitter around 25%
        ///                Method for resetting the data stream.
        /// </summary>
        static async Task SendEvents(
            ModuleClient moduleClient,
            int messageCount,
            SimulatorParameters sim,
            CancellationTokenSource cts)
        {
            int    count       = 1;
            double currentTemp = sim.MachineTempMin;
            double normal      = (sim.MachinePressureMax - sim.MachinePressureMin) / (sim.MachineTempMax - sim.MachineTempMin);

            while (!cts.Token.IsCancellationRequested && (SendUnlimitedMessages(messageCount) || messageCount >= count))
            {
                if (Reset)
                {
                    currentTemp = sim.MachineTempMin;
                    Reset.Set(false);
                }

                if (currentTemp > sim.MachineTempMax)
                {
                    currentTemp += Rnd.NextDouble() - 0.5; // add value between [-0.5..0.5]
                }
                else
                {
                    currentTemp += -0.25 + (Rnd.NextDouble() * 1.5); // add value between [-0.25..1.25] - average +0.5
                }

                if (sendData)
                {
                    var tempData = new MessageBody
                    {
                        Machine = new Machine
                        {
                            Temperature = currentTemp,
                            Pressure    = sim.MachinePressureMin + ((currentTemp - sim.MachineTempMin) * normal),
                        },
                        Ambient = new Ambient
                        {
                            Temperature = sim.AmbientTemp + Rnd.NextDouble() - 0.5,
                            Humidity    = Rnd.Next(24, 27)
                        },
                        TimeCreated = DateTime.UtcNow
                    };

                    string dataBuffer   = JsonConvert.SerializeObject(tempData);
                    var    eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
                    eventMessage.Properties.Add("sequenceNumber", count.ToString());
                    eventMessage.Properties.Add("batchId", BatchId.ToString());
                    Console.WriteLine($"\t{DateTime.Now.ToLocalTime()}> Sending message: {count}, Body: [{dataBuffer}]");

                    await moduleClient.SendEventAsync("temperatureOutput", eventMessage);

                    count++;
                }

                await Task.Delay(messageDelay, cts.Token);
            }

            if (messageCount < count)
            {
                Console.WriteLine($"Done sending {messageCount} messages");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Module behavior:
        ///        Sends data periodically (with default frequency of 5 seconds).
        ///        Data trend:
        ///         - Temperature regularly rises from 21C to 100C in regularly with jitter
        ///         - Pressure correlates with Temperature 1 to 10psi
        ///         - Suction Pressure stable around 5psi
        ///         - Discharge Pressure stable around 5psi
        ///         - Ambient temperature stable around 21C
        ///         - Flow is stable with tiny jitter around 25%
        ///                Method for resetting the data stream
        /// </summary>
        static async Task SendEvents(
            ModuleClient moduleClient,
            int messageCount,
            SimulatorParameters sim,
            CancellationTokenSource cts)
        {
            int    count       = 1;
            double currentTemp = sim.TempMin;
            double normal      = (sim.PressureMax - sim.PressureMin) / (sim.TempMax - sim.TempMin);

            while (!cts.Token.IsCancellationRequested && (SendUnlimitedMessages(messageCount) || messageCount >= count))
            {
                if (Reset)
                {
                    currentTemp = sim.TempMin;
                    Reset.Set(false);
                }

                if (currentTemp > sim.TempMax)
                {
                    currentTemp += Rnd.NextDouble() - 0.5; // add value between [-0.5..0.5]
                }
                else
                {
                    currentTemp += -0.25 + (Rnd.NextDouble() * 1.5); // add value between [-0.25..1.25] - average +0.5
                }

                if (sendData)
                {
                    var events = new List <MessageEvent>();
                    events.Add(new MessageEvent
                    {
                        DeviceId    = Environment.GetEnvironmentVariable("DEVICE") ?? Environment.MachineName,
                        TimeStamp   = DateTime.UtcNow,
                        Temperature = new SensorReading
                        {
                            Value  = currentTemp,
                            Units  = "degC",
                            Status = 192
                        },
                        Pressure = new SensorReading
                        {
                            Value  = sim.PressureMin + ((currentTemp - sim.TempMin) * normal),
                            Units  = "psig",
                            Status = 192
                        },
                        SuctionPressure = new SensorReading
                        {
                            Value  = sim.PressureMin + 4 + ((currentTemp - sim.TempMin) * normal),
                            Units  = "psig",
                            Status = 192
                        },
                        DischargePressure = new SensorReading
                        {
                            Value  = sim.PressureMin + 1 + ((currentTemp - sim.TempMin) * normal),
                            Units  = "psig",
                            Status = 192
                        },
                        Flow = new SensorReading
                        {
                            Value  = Rnd.Next(78, 82),
                            Units  = "perc",
                            Status = 192,
                            Misc   = "GOOD"
                        }
                    });

                    var tempData = new MessageBody
                    {
                        Asset  = Environment.GetEnvironmentVariable("ASSET") ?? "PoC",
                        Source = "Simulator",
                        Events = events
                    };

                    string dataBuffer   = JsonConvert.SerializeObject(tempData);
                    var    eventMessage = new Message(Encoding.UTF8.GetBytes(dataBuffer));
                    eventMessage.Properties.Add("sequenceNumber", count.ToString());
                    eventMessage.Properties.Add("batchId", BatchId.ToString());
                    eventMessage.Properties.Add("asset", tempData.Asset);
                    Console.WriteLine($"\t{DateTime.Now.ToLocalTime()}> Sending message: {count}, Body: [{dataBuffer}]");

                    await moduleClient.SendEventAsync("temperatureOutput", eventMessage);

                    if (insights)
                    {
                        telemetryClient.GetMetric("SendEvent").TrackValue(1);
                    }
                    count++;
                }

                await Task.Delay(messageDelay, cts.Token);
            }

            if (messageCount < count)
            {
                Console.WriteLine($"Done sending {messageCount} messages");
            }
        }