/// <summary> /// Форматировать значение входного канала /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; nfi.NumberDecimalDigits = cnlPropsIsNull ? DefDecDig : cnlProps.DecDigits; nfi.NumberDecimalSeparator = decSep == null ? defDecSep : decSep; nfi.NumberGroupSeparator = grSep == null ? defGrSep : grSep; text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { int unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException(string.Format(Localization.UseRussian ? "Ошибка при форматировании значения входного канала {0}" : "Error formatting value of input channel {0}", cnlNumStr), ex); } else { text = textWithUnit = FrmtErrVal; textIsNumber = false; } } }
/// <summary> /// Format input channel value /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { // getting dimension string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; // number format definition NumberFormatInfo nfi; bool sepDefined = !(decSep == null && grSep == null); if (cnlPropsIsNull || sepDefined) { nfi = sepDefined ? CreateFormatInfo(DefDecDig, decSep, grSep) : defNfi; } else if (cnlProps.FormatInfo == null) { nfi = cnlProps.FormatInfo = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else { nfi = cnlProps.FormatInfo; } // value formatting text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { var unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException($"Error formatting value of input channel {cnlNumStr}", ex); } text = textWithUnit = FrmtErrVal; textIsNumber = false; } }
/// <summary> /// Форматировать значение входного канала /// </summary> public void FormatCnlVal(double val, int stat, InCnlProps cnlProps, string decSep, string grSep, out string text, out string textWithUnit, out bool textIsNumber, bool throwOnError = false) { bool cnlPropsIsNull = cnlProps == null; try { if (stat <= 0) { text = textWithUnit = EmptyVal; textIsNumber = false; } else { text = textWithUnit = NoVal; textIsNumber = false; int formatID = cnlPropsIsNull ? 0 : cnlProps.FormatID; int unitArrLen = cnlPropsIsNull || cnlProps.UnitArr == null ? 0 : cnlProps.UnitArr.Length; if (cnlPropsIsNull || cnlProps.ShowNumber) { // получение размерности string unit = unitArrLen > 0 ? " " + cnlProps.UnitArr[0] : ""; // определение формата числа NumberFormatInfo nfi; bool sepDefined = !(decSep == null && grSep == null); if (cnlPropsIsNull) { nfi = sepDefined ? CreateFormatInfo(DefDecDig, decSep, grSep) : defNfi; } else if (sepDefined) { nfi = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else if (cnlProps.FormatInfo == null) { nfi = cnlProps.FormatInfo = CreateFormatInfo(cnlProps.DecDigits, decSep, grSep); } else { nfi = cnlProps.FormatInfo; } // форматирование значения text = val.ToString("N", nfi); textWithUnit = text + unit; textIsNumber = true; } else if (formatID == BaseValues.Formats.EnumText) { if (unitArrLen > 0) { int unitInd = (int)val; if (unitInd < 0) { unitInd = 0; } else if (unitInd >= unitArrLen) { unitInd = unitArrLen - 1; } text = textWithUnit = cnlProps.UnitArr[unitInd]; } } else if (formatID == BaseValues.Formats.AsciiText) { text = textWithUnit = ScadaUtils.DecodeAscii(val); } else if (formatID == BaseValues.Formats.UnicodeText) { text = textWithUnit = ScadaUtils.DecodeUnicode(val); } else if (formatID == BaseValues.Formats.DateTime) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedString(); } else if (formatID == BaseValues.Formats.Date) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedDateString(); } else if (formatID == BaseValues.Formats.Time) { text = textWithUnit = ScadaUtils.DecodeDateTime(val).ToLocalizedTimeString(); } } } catch (Exception ex) { if (throwOnError) { string cnlNumStr = cnlPropsIsNull ? "?" : cnlProps.CnlNum.ToString(); throw new ScadaException(string.Format(Localization.UseRussian ? "Ошибка при форматировании значения входного канала {0}" : "Error formatting value of input channel {0}", cnlNumStr), ex); } else { text = textWithUnit = FrmtErrVal; textIsNumber = false; } } }