/// <summary>
        /// Initialize, but only once.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task <bool> Initialize(string url, int port, string topic)
        {
            if (IsInitialized)
            {
                return(true);
            }

            try
            {
                PublishClient = new MqttFactory().CreateMqttClient();
                await MqttHelpers.ConnectClient(PublishClient, url, port);

                SubscribeClient = new MqttFactory().CreateMqttClient();
                await MqttHelpers.ConnectClient(SubscribeClient, url, port);

                MqttHelpers.MqttSubscribeSetHandler(SubscribeClient, HandleReceive);
                //var result = Task.Run(() => ConnectClient(publishClient, url, port)).Result;

                CancellationToken cancelToken = new CancellationToken();
                MqttHelpers.MqttSubscribe(SubscribeClient, topic, cancelToken);

                IsInitialized = true;
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /// <summary>
        /// Method called when the simulation run is initialized.
        /// (after Run is specified but before the simulation actually begins)
        /// </summary>
        public void Initialize()
        {
            try
            {
                if (MqttConnector.ServerUrl == null)
                {
                    return;
                }

                // Subscribe to the MQTT server and topic
                SubscriberClient = new MqttFactory().CreateMqttClient();
                var task = Task.Run(() => MqttHelpers.ConnectClient(SubscriberClient, MqttConnector.ServerUrl, MqttConnector.ServerPort));
                task.Wait();

                // Get ready to receive
                MqttHelpers.MqttSubscribeSetHandler(SubscriberClient, HandlePayload);

                // Subscribe to the MQTT's Server/Broker for a topic
                CancellationToken cancelToken = new CancellationToken();
                MqttHelpers.MqttSubscribe(SubscriberClient, SubscribeTopic, cancelToken);
            }
            catch (Exception ex)
            {
                Alert($"Initialize. Subscribing to MQTT:{ex.Message}");
            }
        }
        /// <summary>
        /// Method called when the simulation run starts.
        /// Called after the constructor.
        /// </summary>
        public void Initialize()
        {
            try
            {
                ////var mqttEvent = _data.Events["MqttEvent"];

                PublishClient = new MqttFactory().CreateMqttClient();
                var t = Task.Run(() => MqttHelpers.ConnectClient(PublishClient, ServerUrl, ServerPort));
                //Todo: This will throw an error if there are no MQTT Servers.
                t.Wait();

                var    info    = _data.ExecutionContext.ExecutionInformation;
                string payload = $"Project:{info.ProjectFolder} {info.ProjectName} Model={info.ModelName} Scenario={info.ScenarioName} Replication={info.ReplicationNumber}";
            }
            catch (Exception ex)
            {
                Alert($"Cannot Initialize. Err={ex.Message}");
            }
        }
Example #4
0
        /// <summary>
        /// Method called when the simulation run starts.
        /// Called after the constructor.
        /// </summary>
        public void Initialize()
        {
            try
            {
                using (SubscribeClient = new MqttFactory().CreateMqttClient())
                {
                    var t = Task.Run(() => MqttHelpers.ConnectClient(SubscribeClient, ServerUrl, ServerPort));
                    //Todo: This will throw an error if there are no MQTT Servers.
                    t.Wait(2000);

                    var    info    = _data.ExecutionContext.ExecutionInformation;
                    string payload = $"Project:{info.ProjectFolder} {info.ProjectName} Model={info.ModelName} Scenario={info.ScenarioName} Replication={info.ReplicationNumber}";
                }

                LogIt($"Info: Connecting to Server: Url={ServerUrl} Port={ServerPort}");
            }
            catch (Exception ex)
            {
                Alert($"MQTT Subscribe. Cannot Initalize. Err={ex.Message}");
            }
        }
Example #5
0
        /// <summary>
        /// Method called when a process token executes the step.
        /// Does an MQTT publish to the MqttServer with MqttTopic.
        /// The data that is published is MqttPayload
        /// </summary>
        public ExitType Execute(IStepExecutionContext context)
        {
            string topic = "";

            try
            {
                // Example of how to get the value of a step property.
                var eprPayload = (IExpressionPropertyReader)prPayload;
                var payload    = eprPayload.GetExpressionValue(context).ToString();

                topic = prTopic.GetStringValue(context);

                // Get the MQTT connector which holds the MQTT client information
                MqttPublishConnector mqttConnector = (MqttPublishConnector)prServerElement.GetElement(context);

                // The referenced element has a MQTT client
                IMqttClient client = (IMqttClient)mqttConnector.PublishClient;

                if (client.IsConnected)
                {
                    MqttHelpers.MqttPublish(client, topic, payload); // payload);
                    Logit(context, $"Info: Topic={topic} Payload={payload}");
                    return(ExitType.FirstExit);
                }
                else
                {
                    Logit(context, $"Execute. Step Topic={topic} .Client not connected. Topic={topic} Payload={payload}.");
                    return(ExitType.AlternateExit);
                }
            }
            catch (Exception ex)
            {
                Logit(context, $"Execute Topic={topic} Error={ex.Message}");
                return(ExitType.AlternateExit);
            }
        }