Develop the publisher NetLogic and interface

Develop a NetLogic and integrate it with the interface.
  1. Develop the NetLogic
  2. In the
    Project view
    pane, expand the
    UI
    folder.
  3. Right-click
    MainWindow (type)
    and select
    New
    Runtime NetLogic
    .
  4. Hover-over the NetLogic, select
    Edit
    , and enter
    PublisherLogic
    .
  5. Double-click the NetLogic.
    The external code editor opens.
  6. 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; }
    NOTA: The code retrieves the value to publish from the
    Model
    Variable1
    variable that you will create later on.
  7. Save the code.
  8. Create the interface elements
  9. In the
    Project view
    pane, right-click
    Model
    and select
    New
    Variable
    .
    Variable1
    is created. The variable value is retrieved by the
    PublisherLogic
    script.
  10. Add the publish button:
    1. In the
      Project view
      pane, right-click
      MainWindow (type)
      and select
      New
      Base controls
      Button
      .
    2. Hover-over the button, select
      Edit
      , and enter
      PublishButton
      .
    3. In the
      Properties
      pane, set the
      Text
      property value to
      Publish
    4. In the
      Events
      pane, next to
      MouseClick event
      , select
      Add
      and select
      ProjectName
      UI
      MainWindow (type)
      PublisherLogic
      PublishMessage
      .
Fornire un feedback
Hai domande o feedback su questa documentazione? invia il tuo feedback qui.