Example #1
0
        private static Device[] generateObjects()
        {
            Device[] resultArray = new Device[numDevices];

            for (var i = 0; i < numDevices; i++)
            {
                string caseSwitch = itemArray[i];
                string itemName   = itemNameArray[i];

                switch (caseSwitch)
                {
                case "P":
                    Device p = new Pump();
                    p.Id           = i;
                    p.Name         = itemName;
                    resultArray[i] = p;
                    break;

                case "H":
                    Heater h = new Heater();
                    h.Id           = i;
                    h.Name         = itemName;
                    resultArray[i] = h;
                    break;

                case "C":
                    Cooler c = new Cooler();
                    c.Id           = i;
                    c.Name         = itemName;
                    resultArray[i] = c;
                    break;

                case "Con":
                    Controller con = new Controller();
                    con.Id         = i;
                    con.Name       = itemName;
                    resultArray[i] = con;
                    break;

                case "A":
                    Analyser a = new Analyser();
                    a.Id           = i;
                    a.Name         = itemName;
                    resultArray[i] = a;
                    break;

                default:
                    Tank t = new Tank();
                    t.Id           = i;
                    t.Name         = itemName;
                    resultArray[i] = t;
                    break;
                }
            }
            return(resultArray);
        }
Example #2
0
        // Creates an Event Hub client and sends the device info to the event hub.
        private static async Task SendMessagesToEventHub(Device[] DeviceArray)
        {
            DateTime date = DateTime.Now.ToLocalTime();
            int      t_d  = date.Second;

            for (var i = 0; i < numDevices; i++)
            {
                Random r  = new Random();
                float  rf = (float)r.NextDouble();

                var    message    = "";
                string caseSwitch = DeviceArray[i].Type;

                try
                {
                    switch (caseSwitch)
                    {
                    case "Pump":
                        Pump p = (Pump)DeviceArray[i];
                        p.Speed             = p.Speed + rf;                          //rpm
                        p.Temperature       = 5 + (float)0.1 * rf;                   // degrees Celcius
                        p.SuctionPressure   = (float)(Math.Sin(1 * t_d) + 0.1 * rf); // Bar
                        p.DischargePressure = (float)(Math.Sin(2 * t_d) + 0.1 * rf); // Bar
                        p.FlowRate          = p.Speed / 100;                         // dm3/s
                        p.Vibration         = (float)0.1 * rf;
                        p.Time  = date;
                        message = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        break;

                    case "Tank":
                        Tank t = (Tank)DeviceArray[i];
                        t.Temperature = 5 + (float)0.1 * rf;     // degrees Celcius
                        t.Time        = date;
                        t.Vibration   = (float)0.1 * rf;
                        message       = Newtonsoft.Json.JsonConvert.SerializeObject(t);
                        break;

                    case "Cooler":
                        Cooler c = (Cooler)DeviceArray[i];
                        c.Temperature = 5 + (float)0.1 * rf;     // degrees Celcius
                        c.Time        = date;
                        c.Vibration   = (float)0.1 * rf;
                        message       = Newtonsoft.Json.JsonConvert.SerializeObject(c);
                        break;

                    case "Heater":
                        Heater h = (Heater)DeviceArray[i];
                        h.Temperature = 5 + (float)0.1 * rf;     // degrees Celcius
                        h.Time        = date;
                        h.Vibration   = (float)0.1 * rf;
                        message       = Newtonsoft.Json.JsonConvert.SerializeObject(h);
                        break;

                    case "Controller":
                        Controller con = (Controller)DeviceArray[i];
                        con.Temperature = 5 + (float)0.1 * rf;     // degrees Celcius
                        con.FlowRate    = (float)(50 + 10 * rf);
                        con.Vibration   = (float)0.1 * rf;
                        con.Time        = date;
                        message         = Newtonsoft.Json.JsonConvert.SerializeObject(con);
                        break;

                    case "Analyser":
                        Analyser a          = (Analyser)DeviceArray[i];
                        float    percentage = 10 * rf;             //%
                        a.Temperature       = 5 + (float)0.1 * rf; // degrees Celcius
                        a.CulturePercentage = (int)percentage;
                        a.Vibration         = (float)0.1 * rf;
                        a.Time  = date;
                        message = Newtonsoft.Json.JsonConvert.SerializeObject(a);
                        break;

                    default:
                        break;
                    }

                    //var message = $"Message";
                    //Console.WriteLine($"Sending message: {message}");
                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
                }

                await Task.Delay(10);
            }

            Console.WriteLine($"{numDevices} messages sent.");
        }