/// <summary> /// Writes the message to a terminal. /// </summary> public void WriteMessage(string text) { logHelper.WriteMessage(text); }
/// <summary> /// Builds a project configuration. /// </summary> public void Build(List <DeviceModel> selectedDevices, int commLineNum, int startDeviceNum, int startCnlNum, int?objNum) { ArgumentNullException.ThrowIfNull(selectedDevices, nameof(selectedDevices)); try { ChannelNumberingOptions options = adminContext.AppConfig.ChannelNumberingOptions; int deviceNum = startDeviceNum; int cnlNum = startCnlNum; int?deviceTypeID = project.ConfigDatabase.DevTypeTable.Enumerate() .Where(devType => devType.Driver == MqttDriverName).FirstOrDefault()?.DevTypeID; if (deviceTypeID == null) { logHelper.WriteError(string.Format(Locale.IsRussian ? "Драйвер {0} не найден в таблице Типы устройств" : "Driver {0} not found in the Device Types table", MqttDriverName)); return; } foreach (DeviceModel deviceModel in selectedDevices) { bool deviceIgnored = true; if (deviceModel.Controls.Count > 0) { DeviceConfigEntry entry = new(); // device entity Device deviceEntity = entry.DeviceEntity; deviceEntity.DeviceNum = deviceNum; deviceEntity.Name = deviceModel.Meta.Name; deviceEntity.Code = deviceModel.Code; deviceEntity.DevTypeID = deviceTypeID; deviceEntity.CommLineNum = commLineNum; // device configuration DeviceConfig deviceConfig = entry.DeviceConfig; deviceConfig.DeviceNum = deviceNum; deviceConfig.Name = deviceModel.Meta.Name; deviceConfig.Driver = MqttDriverName; // channels and subscriptions BuildCnls(entry, deviceModel, objNum, deviceNum, ref cnlNum); BuildMqttDeviceConfig(entry, deviceModel); foreach (ControlModel controlModel in deviceModel.Controls) { BuildCnls(entry, controlModel, options, objNum, deviceNum, ref cnlNum); BuildMqttDeviceConfig(entry, controlModel); } if (entry.Cnls.Count > 0) { logHelper.WriteMessage(string.Format(Locale.IsRussian ? "Устройство [{0}] {1}" : "Device [{0}] {1}", deviceNum, deviceModel.Meta.Name)); logHelper.WriteMessage(string.Format(Locale.IsRussian ? "Каналы: {0}" : "Channels: {0}", entry.Cnls.Select(cnl => cnl.CnlNum).ToArray().ToRangeString())); DeviceConfigs.Add(entry); deviceIgnored = false; } deviceNum++; } if (deviceIgnored) { logHelper.WriteWarning(string.Format(Locale.IsRussian ? "Устройство {0} игнорируется" : "Device {0} ignored", deviceModel.Meta.Name)); } logHelper.WriteLine(); cnlNum = AdjustCnlNum(options, cnlNum); } BuildResult = DeviceConfigs.Count > 0; } catch (Exception ex) { logHelper.WriteError(ex.Message); } }
/// <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(); } }