/// <summary> /// Sets the entity properties according to the controls. /// </summary> private void ControlsToEntity() { LimEntity = new Lim { LimID = Convert.ToInt32(numLimID.Value), Name = txtName.Text, IsBoundToCnl = chkIsBoundToCnl.Checked, IsShared = chkIsShared.Checked, LoLo = txtLoLo.Text == "" ? null : ScadaUtils.ParseDouble(txtLoLo.Text), Low = txtLow.Text == "" ? null : ScadaUtils.ParseDouble(txtLow.Text), High = txtHigh.Text == "" ? null : ScadaUtils.ParseDouble(txtHigh.Text), HiHi = txtHiHi.Text == "" ? null : ScadaUtils.ParseDouble(txtHiHi.Text), Deadband = txtDeadband.Text == "" ? null : ScadaUtils.ParseDouble(txtDeadband.Text) }; }
/// <summary> /// Creates a new filter according to the controls. /// </summary> private FilterExtended CreateFilter(ColumnInfo columnInfo) { FilterExtended filter = new() { ColumnName = columnInfo.Column.Name, StringOperation = cbStringOperation.SelectedIndex, MathOperation = cbMathOperation.SelectedIndex }; if (columnInfo.IsText) { filter.Argument = columnInfo.IsNumber ? ScadaUtils.ParseDouble(txtValue.Text) : txtValue.Text; } else if (columnInfo.IsComboBox) { filter.Argument = (cbValue.SelectedValue is int val) ? val : -1; } else if (columnInfo.IsCheckBox) { filter.Argument = cbBoolean.SelectedIndex > 0; } return(filter); }
/// <summary> /// Загрузить из файла команду и проверить её корретность /// </summary> private bool LoadCmdFromFile(string fileName, out string cmdType, out Dictionary <string, string> cmdParams, out Command cmd) { bool result = false; cmdType = ""; cmdParams = null; cmd = null; FileStream fileStream = null; StreamReader streamReader = null; try { // считывание команды из файла string target = ""; DateTime dateTime = DateTime.MinValue; int lifeTime = 0; bool endFound = false; fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); streamReader = new StreamReader(fileStream, Encoding.Default); while (!streamReader.EndOfStream) { string line = streamReader.ReadLine().Trim(); string lineL = line.ToLowerInvariant(); if (cmdParams == null) { if (lineL == "[command]") { cmdParams = new Dictionary <string, string>(); } } else { if (lineL.StartsWith("target=")) { target = lineL.Remove(0, 7); } else if (lineL.StartsWith("datetime=")) { dateTime = DateTime.Parse(lineL.Remove(0, 9), DateTimeFormatInfo.InvariantInfo); } else if (lineL.StartsWith("lifetime=")) { lifeTime = int.Parse(lineL.Remove(0, 9)); } else if (lineL.StartsWith("cmdtype=")) { cmdType = line.Remove(0, 8); int cmdTypeID = BaseValues.CmdTypes.ParseCmdTypeCode(cmdType); if (cmdTypeID >= 0) { cmd = new Command(cmdTypeID); } } else if (lineL.StartsWith("end=")) { endFound = true; } else { int ind = lineL.IndexOf("="); if (ind >= 0) { cmdParams[lineL.Substring(0, ind)] = lineL.Substring(ind + 1); } if (cmd != null) { if (lineL.StartsWith("kpnum=")) { cmd.KPNum = int.Parse(lineL.Remove(0, 6)); } else if (lineL.StartsWith("cmdnum=")) { if (cmd.CmdTypeID != BaseValues.CmdTypes.Request) { cmd.CmdNum = int.Parse(lineL.Remove(0, 7)); } } else if (lineL.StartsWith("cmdval=")) { if (cmd.CmdTypeID == BaseValues.CmdTypes.Standard) { cmd.CmdVal = ScadaUtils.ParseDouble(lineL.Remove(0, 7)); } } else if (lineL.StartsWith("cmddata=")) { if (cmd.CmdTypeID == BaseValues.CmdTypes.Binary) { byte[] cmdData; if (ScadaUtils.HexToBytes(lineL.Remove(0, 8), out cmdData)) { cmd.CmdData = cmdData; } } } } } } } if (cmdParams != null && target == "scadacommsvc" && endFound) { // проверка актуальности команды DateTime nowDT = DateTime.Now; if (nowDT.AddSeconds(-lifeTime) <= dateTime && dateTime <= nowDT.AddSeconds(lifeTime)) { log.WriteAction((Localization.UseRussian ? "Получена команда из файла: " : "The command is received from file: ") + GetCmdShortDescr(cmd)); result = true; } else { log.WriteAction((Localization.UseRussian ? "Получена неактуальная команда из файла: " : "The outdated command is received from file: ") + GetCmdShortDescr(cmd)); } cmdType = cmdType.ToLowerInvariant(); } } catch (ThreadAbortException) { } catch (Exception ex) { log.WriteException(ex, Localization.UseRussian ? "Ошибка при приёме команды из файла {0}" : "Error receiving command from file {0}", Path.GetFileName(fileName)); } finally { if (streamReader != null) { streamReader.Close(); } if (fileStream != null) { fileStream.Close(); } } if (result) { // удаление успешно обработанного файла команды try { File.Delete(fileName); } catch { } return(true); } else { // переименование файла команды, который не был обработан try { File.Move(fileName, fileName + ".err"); } catch { } return(false); } }