Ejemplo n.º 1
0
        private static void RunClient(string clientId, string server)
        {
            /* create the client instance */
            client = new MqttClient(server);

            /* register handler for connectio closed event */
            client.ConnectionClosed += ConnectionClosed;

            client.MqttMsgPublishReceived += (sender, eventArgs) =>
            {
                ApplicationMessageReceived(sender, eventArgs);
            };
            /* connect to the server */
            var connectClient = new Task(() => Connect(false));

            connectClient.Start();

            while (!cancel.IsCancellationRequested)
            {
                try
                {
                    Console.WriteLine("1 = Send time series messages");
                    Console.WriteLine("2 = send alarm {0} message", alarmOnOff ? "off" : "on");
                    Console.WriteLine("3 = send a state changed message");
                    Console.WriteLine("4 = send a sample set message");
                    Console.WriteLine("5 = send a compressed container");
                    Console.WriteLine("6 = send available sensors");
                    Console.WriteLine("7 = Subscribe Transmit lists");
                    Console.WriteLine("Q = quit");
                    var pressedKey = Console.ReadKey(true);
                    if (pressedKey.Key == ConsoleKey.Q)
                    {
                        cancel.Cancel();
                        if (client.IsConnected)
                        {
                            Console.WriteLine("### Disconnecting from server ###");
                            client.Disconnect();
                        }
                        Console.WriteLine("### Done, press any key to continue ###");
                        Console.ReadKey(true);
                        continue;
                    }

                    if (!client.IsConnected)
                    {
                        Console.WriteLine("### Not Connected to the server, try again later ### ");
                        continue;
                    }

                    /* send timeseries doubles messages (a sinus periode) */
                    if (pressedKey.Key == ConsoleKey.D1)
                    {
                        double i;
                        for (i = 0.0; i < 360.0; i++)
                        {
                            DateTimeOffset time = DateTimeOffset.Now.AddYears(0);

                            TimeseriesDoublesReplicationMessage tds = new TimeseriesDoublesReplicationMessage("TimeSeries01", "source1", time, Math.Sin(Math.PI / 180 * i));
                            var messageWrpper = tds.ToMessageWrapper();
                            client.Publish(Topics.CloudBound, messageWrpper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                            var delay = Delay(800);
                            delay.Wait();
                        }
                        Console.WriteLine("Sent time series messages");
                    }

                    /* send alarm event messages, toggles the alarm on/off */
                    if (pressedKey.Key == ConsoleKey.D2)
                    {
                        AlarmStateType alarmState;
                        alarmOnOff = !alarmOnOff;

                        if (alarmOnOff)
                        {
                            alarmState = AlarmStateType.AlarmState;
                        }
                        else
                        {
                            alarmState = AlarmStateType.NormalState;
                        }

                        var alarm = new AlarmReplicationMessage
                        {
                            SensorId = "Alarm01"
                        };
                        AlarmEvent aEv = new AlarmEvent(
                            DateTime.UtcNow,
                            AlarmLevelType.EmergencyLevel,
                            alarmState,
                            "Info level, Normal state");
                        alarm.AlarmEvents.Add(aEv);
                        var messageWrapper = alarm.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                        var delay = Delay(10);
                        delay.Wait();
                        Console.WriteLine("Sent alarm message");
                    }

                    /* send state change event messages */
                    if (pressedKey.Key == ConsoleKey.D3)
                    {
                        state %= 5;
                        var stateChanged = new StateChangeReplicationMessage()
                        {
                            SensorId = "StateChangeEvent01",
                        };
                        var sEv = new StateChange(
                            DateTime.UtcNow,
                            "Donald Duck",
                            true,
                            state);
                        stateChanged.StateChanges.Add(sEv);
                        var messageWrapper = stateChanged.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                        state++;
                        Console.WriteLine("Sent state change message");
                    }

                    /* send sample set messages, alternates between a sinus/cosinus periode */
                    if (pressedKey.Key == ConsoleKey.D4)
                    {
                        toggleCosSin = !toggleCosSin;
                        /* create a sample set */
                        List <double> samples = new List <double>();
                        double        i;
                        for (i = 0.0; i < 360.0; i++)
                        {
                            int    count = 0;
                            double value;
                            if (toggleCosSin)
                            {
                                value = Math.Sin(Math.PI / 180 * i);
                            }
                            else
                            {
                                value = Math.Cos(Math.PI / 180 * i);
                            }

                            samples.Add(value);
                            count++;
                        }

                        DataframeColumn dataframeColumn = new DataframeColumn(samples);
                        DataframeEvent  dataFrame       = new DataframeEvent(DateTimeOffset.UtcNow, dataframeColumn);

                        DataframeReplicationMessage samplesetReplicationMessage = new DataframeReplicationMessage("SampleSet01", "SampleSet01", dataFrame);
                        var messageWrapper = samplesetReplicationMessage.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                        Console.WriteLine("Sent sample set message");
                    }

                    /* send a compressed container consisting of time series,
                     * alarm, state changes and sample set
                     */
                    if (pressedKey.Key == ConsoleKey.D5)
                    {
                        int          i;
                        MessageArray array = new MessageArray();

                        /* add some timeseries messages */
                        for (i = 0; i < 10; i++)
                        {
                            DateTimeOffset time = DateTimeOffset.UtcNow;

                            TimeseriesDoublesReplicationMessage tds = new TimeseriesDoublesReplicationMessage("TimeSeries01", "TimeSeries01", time, i + 1);
                            array.Messages.Add(tds.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }

                        /* add some state change event messages */
                        bool manOverride  = true;
                        var  stateChanged = new StateChangeReplicationMessage
                        {
                            SensorId = "StateChangeEvent01",
                        };
                        for (i = 0; i < 10; i++)
                        {
                            manOverride = !manOverride;
                            var sEv = new StateChange(
                                DateTime.UtcNow,
                                "Donald Duck",
                                manOverride,
                                (uint)i + 1);
                            stateChanged.StateChanges.Add(sEv);
                            var delay = Delay(10);
                            delay.Wait();
                        }
                        array.Messages.Add(stateChanged.ToMessageWrapper());

                        /* add some sample set messages */
                        for (i = 0; i < 10; i++)
                        {
                            List <double> samples = new List <double>();
                            for (int j = 0; j < 10; j++)
                            {
                                samples.Add(j);
                            }
                            DataframeColumn             dataframeColumn             = new DataframeColumn(samples);
                            DataframeEvent              dataFrame                   = new DataframeEvent(DateTimeOffset.UtcNow, dataframeColumn);
                            DataframeReplicationMessage samplesetReplicationMessage = new DataframeReplicationMessage("SampleSet01", "SampleSet01", dataFrame);
                            array.Messages.Add(samplesetReplicationMessage.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }

                        /* add some alarm event messages */
                        alarmOnOff = false;
                        for (i = 0; i < 10; i++)
                        {
                            Kognifai.Serialization.AlarmStateType alarmState;
                            alarmOnOff = !alarmOnOff;
                            alarmState = alarmOnOff ? AlarmStateType.AlarmState : AlarmStateType.NormalState;

                            var alarm = new AlarmReplicationMessage
                            {
                                SensorId = "Alarm01",
                            };
                            AlarmEvent aEv = new AlarmEvent(
                                DateTime.UtcNow,
                                AlarmLevelType.EmergencyLevel,
                                alarmState,
                                "Info level, Normal state");
                            alarm.AlarmEvents.Add(aEv);
                            array.Messages.Add(alarm.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }
                        MessageArrayContainer container = new MessageArrayContainer("", array, true);
                        client.Publish(Topics.CloudBoundContainer, container.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);

                        Console.WriteLine("Sent cloudBoundContainer messages");
                    }


                    /*send available sensors
                     * Place your avaialablesensor.csv file under GatewayConfigurationFiles folder
                     */
                    if (pressedKey.Key == ConsoleKey.D6)
                    {
                        var    applicationPath = AppDomain.CurrentDomain.BaseDirectory;
                        string filePath        = Path.Combine(applicationPath, "GatewayConfigurationFiles", "availablesensorlist.csv");

                        RemoteSourceAvailableSensors availableSensors = new RemoteSourceAvailableSensors()
                        {
                            EventType = EventType.SensorDataEventType,
                            SourceId  = "source1"
                        };

#if NET_FRAMEWORK40
                        var            availableSensorsList = AvailableSensor.GetAvailableSensors(availableSensors.SourceId, EventType.SensorDataEventType, filePath);
                        MessageWrapper messageWrapper       = new MessageWrapper();
                        messageWrapper.SubprotocolNumber      = (int)KnownSubprotocols.EdgeGatewayProtocol;
                        messageWrapper.SubprotocolMessageType = (int)EdgeGatewayMessageType.RemoteSourceAvailableSensorsMessageType;
                        availableSensors.Sensors.AddRange(availableSensorsList.Sensors);
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Serializer.Serialize(ms, availableSensors);
                            messageWrapper.MessageBytes = ms.ToArray();
                        }
#else
                        MessageWrapper messageWrapper = AvailableSensor.GetAvailableSensors(availableSensors.SourceId, EventType.SensorDataEventType, filePath);
#endif

                        client.Publish(Topics.AvailableSensorList, messageWrapper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                        Console.WriteLine("Published available sensor list");
                    }

                    if (pressedKey.Key == ConsoleKey.D7)
                    {
                        var    applicationPath = AppDomain.CurrentDomain.BaseDirectory;
                        string filePath        = Path.Combine(applicationPath, "GatewayConfigurationFiles", "availablesensorlist.csv");

                        RemoteSourceRequestConfiguredSensors message = new RemoteSourceRequestConfiguredSensors()
                        {
                            EventType = EventType.SensorDataEventType,
                            SourceId  = "source1"
                        };

                        client.Subscribe(new[] { Topics.TransmitLists }, new[] { GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]) });

#if NET_FRAMEWORK40
                        MessageWrapper messageWrapper = new MessageWrapper();
                        messageWrapper.SubprotocolNumber      = (int)KnownSubprotocols.EdgeGatewayProtocol;
                        messageWrapper.SubprotocolMessageType = (int)EdgeGatewayMessageType.RemoteSourceRequestConfiguredSensorsMessageType;
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Serializer.Serialize(ms, message);
                            messageWrapper.MessageBytes = ms.ToArray();
                        }
#else
                        var messageWrapper = message.ToMessageWrapper();
#endif
                        client.Publish(Topics.SensorDataTransmitList, messageWrapper.ToByteArray(), GetQualityOfService(ConfigurationManager.AppSettings["qualityofservice"]), false);
                        Console.WriteLine("Published sensor transmit list");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
            cancel.Dispose();
        }
Ejemplo n.º 2
0
        private static void RunClient(string clientId, string server)
        {
            /* create the client instance */
            client = new MqttClient(server);

            /* register handler for connectio closed event */
            client.ConnectionClosed += ConnectionClosed;

            /* connect to the server */
            var connectClient = new Task(() => Connect(false));

            connectClient.Start();

            while (!cancel.IsCancellationRequested)
            {
                try
                {
                    Console.WriteLine("1 = Send time series messages");
                    Console.WriteLine("2 = send alarm {0} message", alarmOnOff ? "off" : "on");
                    Console.WriteLine("3 = send a state changed message");
                    Console.WriteLine("4 = send a sample set message");
                    Console.WriteLine("5 = send a compressed container");
                    Console.WriteLine("6 = send available sensors");
                    Console.WriteLine("Q = quit");
                    var pressedKey = Console.ReadKey(true);
                    if (pressedKey.Key == ConsoleKey.Q)
                    {
                        cancel.Cancel();
                        if (client.IsConnected)
                        {
                            Console.WriteLine("### Disconnecting from server ###");
                            client.Disconnect();
                        }
                        Console.WriteLine("### Done, press any key to continue ###");
                        Console.ReadKey(true);
                        continue;
                    }

                    if (!client.IsConnected)
                    {
                        Console.WriteLine("### Not Connected to the server, try again later ### ");
                        continue;
                    }

                    /* send timeseries doubles messages (a sinus periode) */
                    if (pressedKey.Key == ConsoleKey.D1)
                    {
                        double i;
                        for (i = 0.0; i < 360.0; i++)
                        {
                            DateTimeOffset time = DateTimeOffset.Now.AddYears(0);

                            TimeseriesDoublesReplicationMessage tds = new TimeseriesDoublesReplicationMessage("TimeSeries01", time, Math.Sin(Math.PI / 180 * i));
                            var messageWrpper = tds.ToMessageWrapper();
                            client.Publish(Topics.CloudBound, messageWrpper.ToByteArray(), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                            var delay = Delay(1);
                            delay.Wait();
                        }
                    }

                    /* send alarm event messages, toggles the alarm on/off */
                    if (pressedKey.Key == ConsoleKey.D2)
                    {
                        AlarmStateType alarmState;
                        alarmOnOff = !alarmOnOff;

                        if (alarmOnOff)
                        {
                            alarmState = AlarmStateType.AlarmState;
                        }
                        else
                        {
                            alarmState = AlarmStateType.NormalState;
                        }

                        var alarm = new AlarmReplicationMessage
                        {
                            ExternalId = "Alarm01"
                        };
                        AlarmEvent aEv = new AlarmEvent(
                            DateTime.UtcNow,
                            AlarmLevelType.EmergencyLevel,
                            alarmState,
                            "Info level, Normal state");
                        alarm.AlarmEvents.Add(aEv);
                        var messageWrapper = alarm.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                        var delay = Delay(10);
                        delay.Wait();
                    }

                    /* send state change event messages */
                    if (pressedKey.Key == ConsoleKey.D3)
                    {
                        state %= 5;
                        var stateChanged = new StateChangeReplicationMessage()
                        {
                            ExternalId = "StateChangeEvent01",
                        };
                        var sEv = new StateChange(
                            DateTime.UtcNow,
                            "Donald Duck",
                            true,
                            state);
                        stateChanged.StateChanges.Add(sEv);
                        var messageWrapper = stateChanged.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                        state++;
                    }

                    /* send sample set messages, alternates between a sinus/cosinus periode */
                    if (pressedKey.Key == ConsoleKey.D4)
                    {
                        toggleCosSin = !toggleCosSin;
                        /* create a sample set */
                        List <double> samples = new List <double>();
                        double        i;
                        for (i = 0.0; i < 360.0; i++)
                        {
                            int    count = 0;
                            double value;
                            if (toggleCosSin)
                            {
                                value = Math.Sin(Math.PI / 180 * i);
                            }
                            else
                            {
                                value = Math.Cos(Math.PI / 180 * i);
                            }
                            samples.Add(value);
                            count++;
                        }

                        DataframeColumn dataframeColumn = new DataframeColumn(samples);
                        DataframeEvent  dataFrame       = new DataframeEvent(DateTimeOffset.UtcNow, dataframeColumn);

                        DataframeReplicationMessage samplesetReplicationMessage = new DataframeReplicationMessage("SampleSet01", dataFrame);
                        var messageWrapper = samplesetReplicationMessage.ToMessageWrapper();
                        client.Publish(Topics.CloudBound, messageWrapper.ToByteArray(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                    }

                    /* send a compressed container consisting of time series,
                     * alarm, state changes and sample set
                     */
                    if (pressedKey.Key == ConsoleKey.D5)
                    {
                        int          i;
                        MessageArray array = new MessageArray();

                        /* add some timeseries messages */
                        for (i = 0; i < 10; i++)
                        {
                            DateTimeOffset time = DateTimeOffset.UtcNow;

                            TimeseriesDoublesReplicationMessage tds = new TimeseriesDoublesReplicationMessage("TimeSeries01", time, i + 1);
                            array.Messages.Add(tds.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }

                        /* add some state change event messages */
                        bool manOverride  = true;
                        var  stateChanged = new StateChangeReplicationMessage
                        {
                            ExternalId = "StateChangeEvent01",
                        };
                        for (i = 0; i < 10; i++)
                        {
                            manOverride = !manOverride;
                            var sEv = new StateChange(
                                DateTime.UtcNow,
                                "Donald Duck",
                                manOverride,
                                (uint)i + 1);
                            stateChanged.StateChanges.Add(sEv);
                            var delay = Delay(10);
                            delay.Wait();
                        }
                        array.Messages.Add(stateChanged.ToMessageWrapper());

                        /* add some sample set messages */
                        for (i = 0; i < 10; i++)
                        {
                            List <double> samples = new List <double>();
                            for (int j = 0; j < 10; j++)
                            {
                                samples.Add(j);
                            }
                            DataframeColumn             dataframeColumn             = new DataframeColumn(samples);
                            DataframeEvent              dataFrame                   = new DataframeEvent(DateTimeOffset.UtcNow, dataframeColumn);
                            DataframeReplicationMessage samplesetReplicationMessage = new DataframeReplicationMessage("SampleSet01", dataFrame);
                            array.Messages.Add(samplesetReplicationMessage.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }

                        /* add some alarm event messages */
                        alarmOnOff = false;
                        for (i = 0; i < 10; i++)
                        {
                            Kognifai.Serialization.AlarmStateType alarmState;
                            alarmOnOff = !alarmOnOff;
                            alarmState = alarmOnOff ? AlarmStateType.AlarmState : AlarmStateType.NormalState;

                            var alarm = new AlarmReplicationMessage
                            {
                                ExternalId = "Alarm01",
                            };
                            AlarmEvent aEv = new AlarmEvent(
                                DateTime.UtcNow,
                                AlarmLevelType.EmergencyLevel,
                                alarmState,
                                "Info level, Normal state");
                            alarm.AlarmEvents.Add(aEv);
                            array.Messages.Add(alarm.ToMessageWrapper());
                            var delay = Delay(10);
                            delay.Wait();
                        }
                        MessageArrayContainer container = new MessageArrayContainer("", array, true);
                        client.Publish(Topics.CloudBoundContainer, container.ToByteArray(), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                    }


                    /*send available sensors
                     * Place your avaialablesensor.csv file under GatewayConfigurationFiles folder
                     */
                    if (pressedKey.Key == ConsoleKey.D6)
                    {
                        var sensors = CsvFileReader.ReadDataFromCsvFile("..\\GatewayConfigurationFiles\\availablesensorlist.csv");
                        AvailableSensorListReplicationMessage availableSensors = new AvailableSensorListReplicationMessage();
                        availableSensors.ConnectorType = "Mqtt";
                        availableSensors.SourceName    = "source1";
                        availableSensors.Sensors.AddRange(sensors);
                        var messageWrapper = availableSensors.ToMessageWrapper();
                        client.Publish(Topics.AvaialableSensorList, messageWrapper.ToByteArray(), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                        Console.WriteLine("Published available sensor list");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
            cancel.Dispose();
        }