/// <summary> /// Получить описание команды с указанием канала управления и пользователя /// </summary> public string GetCmdDescr(int ctrlCnlNum, int userID) { const int VisCmdDataLen = 10; // длина отображаемой части данных команды StringBuilder sb = new StringBuilder(); if (Localization.UseRussian) { sb.Append("Команда ТУ: "); if (ctrlCnlNum > 0) { sb.Append("канал упр. = ").Append(ctrlCnlNum).Append(", "); } if (userID > 0) { sb.Append("ид. польз. = ").Append(userID).Append(", "); } sb.Append("тип = ").Append(BaseValues.CmdTypes.GetCmdTypeCode(CmdTypeID)); if (KPNum > 0) { sb.Append(", номер КП = ").Append(KPNum); } if (CmdTypeID == BaseValues.CmdTypes.Standard) { sb.Append(", значение = ").AppendFormat(CmdVal.ToString("N3", Localization.Culture)); } if (CmdTypeID == BaseValues.CmdTypes.Binary && CmdData != null) { sb.Append(", данные = ").Append(ScadaUtils.BytesToHex(CmdData, 0, Math.Min(VisCmdDataLen, CmdData.Length))); } } else { sb.Append("Command: "); if (ctrlCnlNum > 0) { sb.Append("out channel = ").Append(ctrlCnlNum).Append(", "); } if (userID > 0) { sb.Append("user ID = ").Append(userID).Append(", "); } sb.Append("type = ").Append(BaseValues.CmdTypes.GetCmdTypeCode(CmdTypeID)); if (KPNum > 0) { sb.Append(", device num. = ").Append(KPNum); } if (CmdTypeID == BaseValues.CmdTypes.Standard) { sb.Append(", value = ").AppendFormat(CmdVal.ToString("N3", Localization.Culture)); } if (CmdTypeID == BaseValues.CmdTypes.Binary && CmdData != null) { sb.Append(", data = ").Append(ScadaUtils.BytesToHex(CmdData, 0, Math.Min(VisCmdDataLen, CmdData.Length))); } } return(sb.ToString()); }
/// <summary> /// Gets the command description. /// </summary> private string GetCommandDescr(TeleCommand cmd) { StringBuilder sb = new StringBuilder(); if (cmd.CmdNum > 0) { sb.Append("Num=").Append(cmd.CmdNum).Append(", "); } if (!string.IsNullOrEmpty(cmd.CmdCode)) { sb.Append("Code=").Append(cmd.CmdCode).Append(", "); } if (cmd.CmdData == null) { sb.Append("Val=").Append(cmd.CmdVal.ToString("N3", Locale.Culture)); } else { const int MaxDisplayLength = 10; int displayLength = Math.Min(cmd.CmdData.Length, MaxDisplayLength); sb.Append("Data=").Append(ScadaUtils.BytesToHex(cmd.CmdData, 0, displayLength)); if (displayLength < cmd.CmdData.Length) { sb.Append("..."); } } return(sb.ToString()); }
/// <summary> /// Saves the command to the specified file. /// </summary> public bool Save(string fileName, out string errMsg) { try { StringBuilder sb = new StringBuilder("[TeleCommand]") .Append("CommandID=").AppendLine(CommandID.ToString()) .Append("CreationTime=").AppendLine(CreationTime.ToString(DateTimeFormatInfo.InvariantInfo)) .Append("UserID=").AppendLine(UserID.ToString()) .Append("OutCnlNum=").AppendLine(OutCnlNum.ToString()) .Append("CmdTypeID=").AppendLine(CmdTypeID.ToString()) .Append("ObjNum=").AppendLine(ObjNum.ToString()) .Append("DeviceNum=").AppendLine(DeviceNum.ToString()) .Append("CmdNum=").AppendLine(CmdNum.ToString()) .Append("CmdCode=").AppendLine("CmdCode") .Append("CmdVal=").AppendLine(CmdVal.ToString(NumberFormatInfo.InvariantInfo)) .Append("CmdData=").AppendLine(ScadaUtils.BytesToHex(CmdData)) .Append("RecursionLevel=").AppendLine(RecursionLevel.ToString()) .AppendLine("End="); using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.UTF8)) { writer.Write(sb.ToString()); } errMsg = ""; return(false); } catch (Exception ex) { errMsg = string.Format(Locale.IsRussian ? "Ошибка при сохранении команды ТУ: " : "Error saving telecontrol command: ") + ex.Message; return(false); } }
/// <summary> /// Saves the options into the XML node. /// </summary> public void SaveToXml(XmlElement xmlElem) { if (xmlElem == null) { throw new ArgumentNullException(nameof(xmlElem)); } if (!string.IsNullOrEmpty(Name)) { xmlElem.AppendElem("Name", Name); } xmlElem.AppendElem("Host", Host); xmlElem.AppendElem("Port", Port); xmlElem.AppendElem("Username", Username); xmlElem.AppendElem("Password", ScadaUtils.Encrypt(Password)); if (!string.IsNullOrEmpty(Instance)) { xmlElem.AppendElem("Instance", Instance); } xmlElem.AppendElem("Timeout", Timeout); xmlElem.AppendElem("SecretKey", ScadaUtils.BytesToHex(SecretKey)); }
/// <summary> /// Saves the options to the specified writer. /// </summary> public void Save(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } StringBuilder sb = new StringBuilder() .AppendLine("[TeleCommand]") .Append("CommandID=").AppendLine(CommandID.ToString()) .Append("CreationTime=").AppendLine(CreationTime.ToString(DateTimeFormatInfo.InvariantInfo)) .Append("ClientName=").AppendLine(ClientName) .Append("UserID=").AppendLine(UserID.ToString()) .Append("CnlNum=").AppendLine(CnlNum.ToString()) .Append("ObjNum=").AppendLine(ObjNum.ToString()) .Append("DeviceNum=").AppendLine(DeviceNum.ToString()) .Append("CmdNum=").AppendLine(CmdNum.ToString()) .Append("CmdCode=").AppendLine(CmdCode) .Append("CmdVal=").AppendLine(CmdVal.ToString(NumberFormatInfo.InvariantInfo)) .Append("CmdData=").AppendLine(ScadaUtils.BytesToHex(CmdData)) .Append("RecursionLevel=").AppendLine(RecursionLevel.ToString()) .AppendLine("End="); using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) { writer.Write(sb.ToString()); } }
/// <summary> /// Записать команду КП в файл /// </summary> public static bool SaveCmd(string cmdDir, string sender, Command cmd, out string msg) { string[] cmdParams = new string[] { "KPNum=" + cmd.KPNum, "CmdNum=" + cmd.CmdNum, "CmdVal=" + cmd.CmdVal.ToString(), "CmdData=" + (cmd.CmdData == null ? "" : ScadaUtils.BytesToHex(cmd.CmdData)) }; return(SaveCmd(cmdDir, sender, BaseValues.CmdTypes.GetCmdTypeCode(cmd.CmdTypeID), cmdParams, out msg)); }
/// <summary> /// Saves the options into the XML node. /// </summary> public void SaveToXml(XmlElement xmlElem) { if (xmlElem == null) { throw new ArgumentNullException(nameof(xmlElem)); } xmlElem.AppendElem("Port", Port); xmlElem.AppendElem("Timeout", Timeout); xmlElem.AppendElem("SecretKey", ScadaUtils.BytesToHex(SecretKey)); }
/// <summary> /// Установить элементы управления в соответствии с настройками /// </summary> private void SettingsToControls() { if (ConnectionSettings != null) { txtName.Text = ConnectionSettings.Name; txtHost.Text = ConnectionSettings.Host; numPort.SetValue(ConnectionSettings.Port); txtUsername.Text = ConnectionSettings.Username; txtPassword.Text = ConnectionSettings.Password; txtScadaInstance.Text = ConnectionSettings.ScadaInstance; txtSecretKey.Text = ScadaUtils.BytesToHex(ConnectionSettings.SecretKey); } }
/// <summary> /// Setup the controls according to the settings. /// </summary> private void SettingsToControls() { if (Profile != null) { ConnectionSettings connSettings = Profile.ConnectionSettings; txtProfileName.Text = Profile.Name; txtHost.Text = connSettings.Host; numPort.SetValue(connSettings.Port); txtUsername.Text = connSettings.Username; txtPassword.Text = connSettings.Password; txtSecretKey.Text = ScadaUtils.BytesToHex(connSettings.SecretKey); } }
/// <summary> /// Saves the settings into the XML node. /// </summary> public void SaveToXml(XmlElement xmlElem) { if (xmlElem == null) { throw new ArgumentNullException("xmlElem"); } xmlElem.AppendElem("Host", Host); xmlElem.AppendElem("Port", Port); xmlElem.AppendElem("Username", Username); xmlElem.AppendElem("Password", ScadaUtils.Encrypt(Password)); xmlElem.AppendElem("ScadaInstance", ScadaInstance); xmlElem.AppendElem("SecretKey", ScadaUtils.BytesToHex(SecretKey)); }
/// <summary> /// Gets a key that identifies the connection options. /// </summary> protected string GetOptionsKey(ConnectionOptions connectionOptions) { if (string.IsNullOrEmpty(connectionOptions.AccessKey)) { connectionOptions.AccessKey = new StringBuilder() .Append(connectionOptions.Name).Append(';') .Append(connectionOptions.Host).Append(';') .Append(connectionOptions.Port).Append(';') .Append(connectionOptions.Username).Append(';') .Append(connectionOptions.Password).Append(';') .Append(connectionOptions.Instance).Append(';') .Append(connectionOptions.Timeout).Append(';') .Append(ScadaUtils.BytesToHex(connectionOptions.SecretKey)) .ToString(); } return(connectionOptions.AccessKey); }
/// <summary> /// Gets the command description. /// </summary> private string GetCommandDescr(TeleCommand cmd) { StringBuilder sb = new StringBuilder(); void AppendComma() { if (sb.Length > 0) { sb.Append(", "); } } if (cmd.CmdNum > 0) { sb.Append("Num=").Append(cmd.CmdNum); } if (!string.IsNullOrEmpty(cmd.CmdCode)) { AppendComma(); sb.Append("Code=").Append(cmd.CmdCode); } if (!double.IsNaN(cmd.CmdVal)) { AppendComma(); sb.Append("Val=").Append(cmd.CmdVal.ToString(CnlDataFormatter.DefaultFormat, Locale.Culture)); } if (cmd.CmdData != null && cmd.CmdData.Length > 0) { AppendComma(); sb.Append("Data=").Append(ScadaUtils.BytesToHex(cmd.CmdData, 0, Math.Min(CnlDataFormatter.DataDisplayLength, cmd.CmdData.Length))); if (CnlDataFormatter.DataDisplayLength < cmd.CmdData.Length) { sb.Append("..."); } } return(sb.ToString()); }
/// <summary> /// Sets the controls according to the configuration. /// </summary> private void ConfigToControls() { changing = true; // general options GeneralOptions generalOptions = serverConfig.GeneralOptions; numUnrelIfInactive.SetValue(generalOptions.UnrelIfInactive); chkGenerateAckCmd.Checked = generalOptions.GenerateAckCmd; numMaxLogSize.SetValue(generalOptions.MaxLogSize); chkDisableFormulas.Checked = generalOptions.DisableFormulas; txtEnableFormulasObjNums.Text = generalOptions.EnableFormulasObjNums.ToRangeString(); // listener options ListenerOptions listenerOptions = serverConfig.ListenerOptions; numPort.SetValue(listenerOptions.Port); numTimeout.SetValue(listenerOptions.Timeout); txtSecretKey.Text = ScadaUtils.BytesToHex(listenerOptions.SecretKey); changing = false; }
/// <summary> /// Receives an identification packet. /// </summary> protected bool ReceiveHelloPacket(TcpConnection conn, out string helloStr, out string helloHex) { Log.WriteAction(Locale.IsRussian ? "Приём идентификационного пакета данных от клиента {0}" : "Receive an identification packet from the client {0}", conn.RemoteAddress); int readCnt = conn.ReadAvailable(inBuf, 0, HelloPacketLength, ProtocolFormat.String, out string logText); Log.WriteLine(logText); if (readCnt > 0) { helloStr = conn.Encoding.GetString(inBuf, 0, readCnt); helloHex = "0x" + ScadaUtils.BytesToHex(inBuf, 0, readCnt); return(true); } else { helloStr = ""; helloHex = ""; return(false); } }
/// <summary> /// Get command description with control channel and user /// </summary> public string GetCmdDescr(int ctrlCnlNum, int userID) { const int VisCmdDataLen = 10; // the length of the displayed data command var sb = new StringBuilder(); sb.Append("Command: "); if (ctrlCnlNum > 0) { sb.Append("out ch.=").Append(ctrlCnlNum).Append(", "); } if (userID > 0) { sb.Append("user="******", "); } sb.Append("type=").Append(GetCmdTypeCode()); if (KPNum > 0) { sb.Append(", device=").Append(KPNum); } if (CmdNum > 0) { sb.Append(", number=").Append(CmdNum); } if (CmdTypeID == BaseValues.CmdTypes.Standard) { sb.Append(", value=").AppendFormat(CmdVal.ToString("N3", Localization.Culture)); } if (CmdTypeID == BaseValues.CmdTypes.Binary && CmdData != null) { sb.Append(", data=") .Append(ScadaUtils.BytesToHex(CmdData, 0, Math.Min(VisCmdDataLen, CmdData.Length))) .Append(VisCmdDataLen < CmdData.Length ? "..." : ""); } return(sb.ToString()); }
private void btnGenerateKey_Click(object sender, EventArgs e) { txtSecretKey.Text = ScadaUtils.BytesToHex(ScadaUtils.GetRandomBytes(ScadaUtils.SecretKeySize)); txtSecretKey.Focus(); }