/// <summary> /// Handles a message received event. /// </summary> private void MqttClient_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e) { try { string topic = e.ApplicationMessage.Topic; string payload = e.ApplicationMessage.ConvertPayloadToString(); dsLog.WriteAction("{0} {1} = {2}", CommPhrases.ReceiveNotation, topic, payload.GetPreview(MqttUtils.MessagePreviewLength)); // parse and pass command if (topic == commandTopic) { ReceivedCommand command = JsonSerializer.Deserialize <ReceivedCommand>(payload); CommContext.SendCommand(new TeleCommand { CommandID = ScadaUtils.GenerateUniqueID(), CreationTime = DateTime.UtcNow, DeviceNum = command.DeviceNum, CmdCode = command.CmdCode, CmdVal = command.CmdVal, CmdData = ScadaUtils.HexToBytes(command.CmdData, false, true) }, DriverUtils.DriverCode); } } catch (JsonException) { dsLog.WriteError(Locale.IsRussian ? "Ошибка при получении команды из JSON." : "Error parsing command from JSON."); } catch (Exception ex) { dsLog.WriteError(ex, Locale.IsRussian ? "Ошибка при обработке полученного сообщения. Топик: {0}" : "Error handling the received message. Topic: {0}", e?.ApplicationMessage?.Topic); } }
/// <summary> /// Receives telecontrol commands from the server. /// </summary> private void ReceiveCommands() { try { int cmdCnt = 0; while (scadaClient.GetCommand() is TeleCommand cmd) { CommContext.SendCommand(cmd, Code); if (++cmdCnt == MaxCommandCount) { break; } } } catch (Exception ex) { log.WriteError(ex, CommPhrases.DataSourceMessage, Code, Locale.IsRussian ? "Ошибка при приёме команд ТУ" : "Error receiving telecontrol commands"); } }