Develop the publisher NetLogic and interface
Develop a NetLogic and integrate it with the interface.
- Develop the NetLogic
- InProject view, expandUI.
- Right-clickMainWindow (type)and select .
- Hover-over the NetLogic, select , and enterPublisherLogic.
- Double-click the NetLogic.The external code editor opens.
- In the code editor, replace the existing code with the following code:#region StandardUsing using System; using FTOptix.CoreBase; using FTOptix.HMIProject; using UAManagedCore; using OpcUa = UAManagedCore.OpcUa; using FTOptix.NetLogic; using FTOptix.UI; using FTOptix.OPCUAServer; #endregion using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; public class PublisherLogic : BaseNetLogic { public override void Start() { var brokerIpAddressVariable = Project.Current.GetVariable("Model/BrokerIpAddress"); // Create a client connecting to the broker (default port is 1883) publishClient = new MqttClient(brokerIpAddressVariable.Value); // Connect to the broker publishClient.Connect("PublisherClient"); // Assign a callback to be executed when a message is published to the broker publishClient.MqttMsgPublished += PublishClientMqttMsgPublished; } public override void Stop() { publishClient.Disconnect(); publishClient.MqttMsgPublished -= PublishClientMqttMsgPublished; } private void PublishClientMqttMsgPublished(object sender, MqttMsgPublishedEventArgs e) { Log.Info("Message " + e.MessageId + " - published = " + e.IsPublished); } [ExportMethod] public void PublishMessage() { var variable1 = Project.Current.GetVariable("Model/Variable1"); variable1.Value = new Random().Next(0, 101); // Publish a message ushort msgId = publishClient.Publish("/my_topic", // topic System.Text.Encoding.UTF8.GetBytes(((int)variable1.Value).ToString()), // message body MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level false); // retained } private MqttClient publishClient; }NOTE: The code retrieves the value to publish from thevariable that you will create later on.
- Save the code.
- Create the interface elements
- InProject view, right-clickModeland select .Variable1is created. The variable value is retrieved by thePublisherLogicscript.
- Add the publish button:
- InProject view, right-clickMainWindow (type)and select .
- Hover-over the button, select , and enterPublishButton.
- InProperties, setTexttoPublish
- InEvents, next toMouseClick event, select and select .
Provide Feedback