Desarrollar el suscriptor NetLogic y la interfaz

Desarrolle un NetLogic e intégrelo con la interfaz.
  1. Desarrollar NetLogic
  2. Haga clic con el botón derecho en
    MainWindow (type)
    y seleccione
    Nuevo
    NetLogic de tiempo de ejecución
    .
  3. Pase el cursor por encima de NetLogic, seleccione
    Icono Editar
    y escriba
    SubscriberLogic
    .
  4. Haga doble clic en NetLogic.
    Se abre el editor de código externo.
  5. En el editor de código, reemplace el código existente por el siguiente código:
    #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 SubscriberLogic : BaseNetLogic { public override void Start() { var brokerIpAddressVariable = Project.Current.GetVariable("Model/BrokerIpAddress"); // Create a client connecting to the broker (default port is 1883) subscribeClient = new MqttClient(brokerIpAddressVariable.Value); // Connect to the broker subscribeClient.Connect("
    SubscriberClient
    "); // Assign a callback to be executed when a message is received from the broker subscribeClient.MqttMsgPublishReceived += SubscribeClientMqttMsgPublishReceived; // Subscribe to the "my_topic" topic with QoS 2 ushort msgId = subscribeClient.Subscribe(new string[] { "/my_topic" }, // topic new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); // QoS level messageVariable = Project.Current.GetVariable("Model/Message"); } public override void Stop() { subscribeClient.Unsubscribe(new string[] { "/my_topic" }); subscribeClient.Disconnect(); } private void SubscribeClientMqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { messageVariable.Value = "Message received: " + System.Text.Encoding.UTF8.GetString(e.Message); } private MqttClient subscribeClient; private IUAVariable messageVariable; }
  6. Guarde el código.
  7. Crear los elementos de la interfaz
  8. Cree la variable de mensaje realizando las siguientes acciones:
    1. En
      Vista del proyecto
      , haga clic con el botón derecho en
      Modelo
      y seleccione
      Nuevo
      Variable
      .
    2. Pase el cursor por encima de la variable, seleccione
      Icono Editar
      y escriba
      Message
      .
    3. En
      Propiedades
      , seleccione
      Int32
      y seleccione
      String
      .
  9. Cree la etiqueta Subscribir realizando las siguientes acciones:
    1. En
      Vista del proyecto
      , haga clic con el botón derecho en
      MainWindow (type)
      y seleccione
      Nuevo
      Controles base
      Etiqueta
      .
    2. Pase el cursor por encima de la etiqueta, seleccione
      Icono Editar
      y escriba
      SubscribeLabel
      .
    3. En
      Propiedades
      , cree un vínculo dinámico entre la propiedad
      Texto
      y la variable
      Message
      .
      Para obtener más información sobre vínculos dinámicos, consulte Crear vínculos dinámicos.
  10. Organice los elementos de la interfaz.
Diseño de cuadrícula con un campo de texto que muestra símbolos de almohadilla y un botón etiquetado Publicar.
Entregue su opinión
¿Tiene dudas o comentarios acerca de esta documentación? Por favor deje su opinión aquí.
Normal