Esempio n. 1
0
        /// <summary>
        /// Reads the topics from Wiren Board.
        /// </summary>
        private void ReadTopics()
        {
            IMqttClient mqttClient = null;

            try
            {
                MqttFactory mqttFactory = new();
                mqttClient = mqttFactory.CreateMqttClient();
                mqttClient.UseApplicationMessageReceivedHandler(MqttClient_ApplicationMessageReceived);

                logHelper.WriteMessage(string.Format(Locale.IsRussian ?
                                                     "Соединение с {0}:{1}" :
                                                     "Connect to {0}:{1}",
                                                     connOptions.Server, connOptions.Port));

                MqttClientConnectResultCode resultCode =
                    mqttClient.ConnectAsync(connOptions.ToMqttClientOptions(), tokenSource.Token)
                    .Result.ResultCode;

                if (resultCode == MqttClientConnectResultCode.Success)
                {
                    logHelper.WriteMessage(Locale.IsRussian ?
                                           "Соединение установлено успешно" :
                                           "Connected successfully");

                    logHelper.WriteMessage(string.Format(Locale.IsRussian ?
                                                         "Подписка на {0}" :
                                                         "Subscribe to {0}", MainTopic));
                    MqttClientSubscribeOptions subscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
                                                                  .WithTopicFilter(f => f.WithTopic(MainTopic)).Build();
                    mqttClient.SubscribeAsync(subscribeOptions, tokenSource.Token).Wait();

                    while (!stopReading && !tokenSource.IsCancellationRequested)
                    {
                        Thread.Sleep(ScadaUtils.ThreadDelay);

                        lock (mqttLock)
                        {
                            if (DateTime.UtcNow - messageDT > ReadTimeout)
                            {
                                stopReading = true;
                                logHelper.WriteMessage(Locale.IsRussian ?
                                                       "Таймаут чтения истёк" :
                                                       "Read timeout expired");
                            }
                        }
                    }

                    if (WirenBoardModel.Devices.Count > 0)
                    {
                        logHelper.WriteSuccess(Locale.IsRussian ?
                                               "Чтение топиков завершено успешно" :
                                               "Reading topics completed successfully");
                        ReadResult = true;
                    }
                    else
                    {
                        logHelper.WriteError(Locale.IsRussian ?
                                             "Подключенные устройства не найдены" :
                                             "Connected devices not found");
                    }
                }
                else
                {
                    logHelper.WriteError(string.Format(Locale.IsRussian ?
                                                       "Не удалось установить соединение: {0}" :
                                                       "Unable to connect: {0}", resultCode));
                }
            }
            catch (Exception ex)
            {
                logHelper.WriteError(ex.Message);
            }
            finally
            {
                Disconnect(mqttClient);
                OnCompleted();
            }
        }