Ejemplo n.º 1
0
        public static async Task <IMqttClient> GetConnectedClientAsync()
        {
            if (Client == null)
            {
                Client = new MqttFactory().CreateMqttClient();
                Client.ConnectedHandler    = new MqttClientConnectedHandlerDelegate(e => { });
                Client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(e => { });
                Client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
                {
                    Console.WriteLine(MqttUtils.ToString("client", e.ApplicationMessage));
                });
            }

            if (!Client.IsConnected)
            {
                var options = new MqttClientOptionsBuilder()
                              .WithTcpServer("127.0.0.1", 61613)
                              .WithCredentials("admin", "password")
                              .Build();
                var authenticateResult = await Client.ConnectAsync(options, CancellationToken.None);

                if (authenticateResult.ResultCode != MqttClientConnectResultCode.Success)
                {
                    throw new Exception($"Auth failed. Status={authenticateResult.ResultCode}");
                }
            }

            return(Client);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds a payload based on the device tag data to publish.
        /// </summary>
        public string BuildPayload(DeviceTag deviceTag, CnlData[] cnlData, int dataIndex)
        {
            int  dataLength    = deviceTag.DataLength;
            int  stat          = CnlDataConverter.GetStatus(cnlData, dataIndex, dataLength);
            bool formatIsEmpty = string.IsNullOrEmpty(dsOptions.PublishOptions.PublishFormat);

            if (formatIsEmpty && stat <= CnlStatusID.Undefined)
            {
                return(dsOptions.PublishOptions.UndefinedValue);
            }

            string valStr = "";

            switch (deviceTag.DataType)
            {
            case TagDataType.Double:
                valStr = deviceTag.IsNumericArray
                        ? ArrayToString(CnlDataConverter.GetDoubleArray(cnlData, dataIndex, dataLength))
                        : CnlDataConverter.GetDouble(cnlData, dataIndex).ToString(NumberFormatInfo.InvariantInfo);
                break;

            case TagDataType.Int64:
                valStr = deviceTag.IsNumericArray
                        ? ArrayToString(CnlDataConverter.GetInt64Array(cnlData, dataIndex, dataLength))
                        : CnlDataConverter.GetInt64(cnlData, dataIndex).ToString(NumberFormatInfo.InvariantInfo);
                break;

            case TagDataType.ASCII:
                valStr = CnlDataConverter.GetAscii(cnlData, dataIndex, dataLength);
                break;

            case TagDataType.Unicode:
                valStr = CnlDataConverter.GetUnicode(cnlData, dataIndex, dataLength);
                break;
            }

            return(formatIsEmpty
                ? valStr
                : MqttUtils.FormatPayload(dsOptions.PublishOptions.PublishFormat, valStr, stat));
        }
Ejemplo n.º 3
0
 public static void Run() =>
 MqttUtils.Subscribe(MqttConstantes.TOPICO_SUBSCRIBERS, EventHandler);