2 using System.Collections.Generic;
5 using System.Xml.Serialization;
7 namespace Seci.Definitions
25 private String _group =
"None";
40 private String _runcontrolValue =
"";
43 private Boolean _applyScientificFormatting =
true;
44 private Boolean _applyFixedFormatting =
false;
45 private int _numDecimalPoints = 3;
49 private Boolean _visible =
true;
62 private Boolean _prevReadError =
false;
64 public static readonly String ErrorString =
"Error";
67 public String BlockName {
get {
return _blockName; } set { _blockName = value; } }
68 public String BlockUnits {
get {
return _blockUnits; } set { _blockUnits = value; } }
69 public String Alias {
get {
return _alias; } set { _alias = value; } }
71 public String OwningComponent {
get {
return _owningComponent; } set { _owningComponent = value; } }
72 public String Group {
get {
return _group; } set { _group = value; } }
73 public String NexusName {
get {
return _nexusName; } set { _nexusName = value; } }
74 public String NexusGroup {
get {
return _nexusGroup; } set { _nexusGroup = value; } }
75 public String ParentVI {
get {
return _parentVi; } set { _parentVi = value; } }
76 public String ReadControl {
get {
return _readControl; } set { _readControl = value; } }
77 public String WriteControl {
get {
return _writeControl; } set { _writeControl = value; } }
78 public String GoButton {
get {
return _goButton; } set { _goButton = value; } }
79 public String WaitForControl {
get {
return _waitForControl; } set { _waitForControl = value; } }
80 public Boolean UnderRunControl {
get {
return _underRunControl; } set { _underRunControl = value; } }
81 public Double UpperLimit {
get {
return _upperLimit; } set { _upperLimit = value; } }
82 public Double LowerLimit {
get {
return _lowerLimit; } set { _lowerLimit = value; } }
83 public String RunControlValue {
get {
return _runcontrolValue; } set { _runcontrolValue = value; } }
84 public Boolean InRange {
get {
return _inRange; } }
85 public Boolean BlockEnabled {
get {
return _blockEnabled; } set { _blockEnabled = value; } }
86 public Boolean Visible {
get {
return _visible; } set { _visible = value; } }
87 public Boolean ApplyFormatting {
get {
return _applyScientificFormatting; } set { _applyScientificFormatting = value; } }
88 public Boolean ApplyFixedFormatting {
get {
return _applyFixedFormatting; } set { _applyFixedFormatting = value; } }
89 public String ValueFormatting {
get {
return _valueFormatting; } set { _valueFormatting = value; } }
90 public int NumberDecimalPlaces {
get {
return _numDecimalPoints; } set { _numDecimalPoints = value; } }
91 public Boolean LogChangesOnly {
get {
return _logChangesNow; } set { _logChangesNow = value; } }
92 public Boolean LogChangesTolerance {
get {
return _logChangesTolerance; } set { _logChangesTolerance = value; } }
93 public Double Tolerance {
get {
return _tolerance; } set { _tolerance = value; } }
94 public Boolean LogValueToFile {
get {
return _logValueToFile; } set { _logValueToFile = value; } }
95 public Boolean SaveSettings {
get {
return _saveSettings; } set { _saveSettings = value; } }
96 public Boolean LogToSharedFile {
get {
return _logToSharedFile; } set { _logToSharedFile = value; } }
97 public Boolean PrevReadError {
get {
return _prevReadError; } set { _prevReadError = value; } }
99 public ControlType ReadType {
get {
return _readType; } set { _readType = value; } }
100 public Single LoggingRate
110 _loggingRate = value;
125 public String CurrentValue
131 return _currentValue;
140 if (_logChangesTolerance)
142 _currentValue = value;
145 if (_previousValue == null || ExceedsTolerance(_currentValue, _previousValue, _tolerance))
147 _previousValue = _currentValue;
152 _previousValue = _currentValue;
153 _currentValue = value;
162 public String FormattedCurrentValue
166 if (CurrentValue == null)
return "";
168 if (!_blockEnabled)
return "Disabled";
170 if ((_applyScientificFormatting || _applyFixedFormatting) && _readControl.EndsWith(
"(Numeric)"))
174 if (Double.TryParse(_currentValue, out temp))
176 return formatValue(temp);
180 return _currentValue.Trim();
189 public String SetPointValue
193 if (_writeControl != null)
195 return _setPointValue;
204 _setPointValue = value;
212 public String FormattedSetPointValue
216 if (SetPointValue == null)
return "";
218 if (!_blockEnabled)
return "";
220 if ((_applyScientificFormatting || _applyFixedFormatting) && _readControl.EndsWith(
"(Numeric)"))
222 if (SetPointValue ==
"0")
224 return SetPointValue.Trim();
229 if (Double.TryParse(SetPointValue, out temp))
231 return formatValue(temp);
235 return SetPointValue.Trim();
241 if (_applyScientificFormatting)
243 string format =
"{0:e" + _numDecimalPoints +
"}";
244 if (Math.Abs(value) > 100000f)
246 return String.Format(format, value);
248 else if (Math.Abs(value) < 0.0001f && value != 0)
250 return String.Format(format, value);
254 if (Math.Abs(value) < 1 && value != 0)
257 char[] temp = value.ToString().ToCharArray();
260 for (
int i = 0; i < temp.Length; ++i)
262 if (temp[i] !=
'0' && temp[i] !=
'-' && temp[i] !=
'.')
269 if (count >= _numDecimalPoints)
279 return Math.Round(value, _numDecimalPoints).ToString();
283 else if (_applyFixedFormatting)
285 double num = Math.Round(value, _numDecimalPoints);
287 return String.Format(
"{0:0." +
"".PadRight(_numDecimalPoints,
'0') +
"}", num);
291 return value.ToString();
315 public static Boolean
OkayToLog(DateTime timeNow, DateTime lastLogged, Double loggingRate, String currVal, String prevVal, Double tolerance, Boolean onAnyChange, Boolean onToleranceExceeded)
321 if (timeNow.ToUniversalTime() >= lastLogged.AddSeconds(loggingRate).ToUniversalTime())
323 if (onToleranceExceeded)
326 if (ExceedsTolerance(currVal, prevVal, tolerance))
339 else if (onToleranceExceeded || onAnyChange)
342 if (ExceedsTolerance(currVal, prevVal, tolerance))
358 public void LogValue(DateTime time, String shortName, String runNumber)
364 if (!_blockEnabled || !_logValueToFile)
return;
366 if (String.IsNullOrEmpty(runNumber))
369 runNumber =
"00000000";
372 if (OkayToLog(time, _lastLogged, _loggingRate, _currentValue, _previousLogValue, _tolerance, _logChangesNow, _logChangesTolerance))
374 logValue(time, shortName, runNumber);
376 _previousLogValue = _currentValue;
387 internal void ForceLogForChangesOnly(DateTime time, String shortName, String runNumber)
389 if (_logChangesNow || _logChangesTolerance)
391 logValue(time, shortName, runNumber);
401 private void logValue(DateTime time, String shortName, String runNumber)
403 writeToFile(time, shortName, runNumber);
405 if (Seci.Definitions.Status.LogToDatabase)
408 Seci.Helpers.DatabaseLogger.AddLogValue(convertTimeToString(ref time), _blockName, _currentValue);
419 private void writeToFile(DateTime time, String shortName, String runNumber)
421 String updateTime = convertTimeToString(ref time);
424 String fileName = shortName + runNumber +
"_" + _blockName +
".txt";
428 fileName = shortName + runNumber +
".log";
431 StreamWriter SW =
new StreamWriter(
"C:\\data\\" + fileName,
true);
435 if (_blockName !=
Status.
StatusBlockName && (_logToSharedFile || Seci.Definitions.Status.LogAllBlocksToSingleFile))
437 SW.WriteLine(updateTime +
"\t" + _blockName +
"\t" + _currentValue);
441 SW.WriteLine(updateTime +
"\t" + _currentValue);
447 throw new ArgumentException(e.Message);
463 System.Globalization.DateTimeFormatInfo myDTFI =
new System.Globalization.CultureInfo(
"en-US",
false).DateTimeFormat;
464 String updateTime = time.ToString(myDTFI.SortableDateTimePattern);
476 public static Boolean
ExceedsTolerance(String currValue, String prevValue, Double tolerance)
478 if (currValue == null)
485 if (prevValue == null)
491 if (currValue.ToLower() ==
"true" || currValue.ToLower() ==
"false")
494 if ((currValue.ToLower() ==
"true" && prevValue.ToLower() ==
"false") || (currValue.ToLower() ==
"false" && prevValue.ToLower() ==
"true"))
507 if (tolerance == 0.0)
509 if (prevValue != currValue)
521 if (Double.TryParse(currValue, out curr) && Double.TryParse(prevValue, out previous))
524 if (prevValue != null)
526 if (Math.Abs(curr - previous) >= tolerance)
556 if (_underRunControl ==
true)
559 if (_currentValue ==
"NaN")
571 Double low = _lowerLimit;
572 Double high = _upperLimit;
574 if (_lowerLimit > _upperLimit)
580 if ((Convert.ToDouble(_currentValue) < low))
586 if ((Convert.ToDouble(_currentValue) > high))
617 if (_currentValue.ToLower().Trim() == _runcontrolValue.ToLower().Trim())
662 SetRunControl(enabled);
673 SetRunControl(enabled);
674 _runcontrolValue = value;
683 _underRunControl = enabled;
693 return MemberwiseClone();
void SetRunControlLimits(Boolean enabled, Double low, Double high)
Method for setting the numeric run-control limits.
String _readControl
VI on which it appears.
void SetRunControlLimits(Boolean enabled, String value)
Method for setting the run-control limit for a single non-numeric value Note: data collection will pa...
String _currentValue
Log the data to a files that contains data from other blocks.
Double _upperLimit
Under run-control?
This class is used for storing the information for any blocks that are created and provides methods f...
Boolean _logValueToFile
Is the block visible?
DateTime _lastLogged
The current requested value.
String _waitForControl
Button to push after setting.
Double _tolerance
The last value LOGGED to file. Needed for log on change.
Boolean _logChangesNow
Rate at which block value is logged to file.
String _nexusName
The group this block belongs to.
void writeToFile(DateTime time, String shortName, String runNumber)
Method for actually writing to file. The filename is of the form "ShortNameRunNumber_BlockName.txt".
Class for holding the standard run-time settings for SECI. A large percentage of the information held...
static Boolean LogAllBlocksToSingleFile
bool OutOfRange()
Check whether the block has exceeded the run-control settings.
Boolean _inRange
Whether the run-control settings should be saved.
String _writeControl
The type of the read control.
Boolean _logToSharedFile
Log changes, but only if the value exceeds the tolerance.
static Boolean OkayToLog(DateTime timeNow, DateTime lastLogged, Double loggingRate, String currVal, String prevVal, Double tolerance, Boolean onAnyChange, Boolean onToleranceExceeded)
Checks whether to log the block value. Note: public for the purposes of testing
String _previousValue
The latest value of the block.
Single _loggingRate
Log to file?
String _owningComponent
Alias.
String _blockUnits
Name of block.
void logValue(DateTime time, String shortName, String runNumber)
Method for calling the appropriate method for logging the value.
Double _lowerLimit
Upper limit for run-control.
static String StatusBlockName
String _nexusGroup
NeXus name.
String formatValue(Double value)
BlockInfo()
Standard Constructor - necessary for XML serialization.
Boolean _saveSettings
Non-numeric run-control value.
String _parentVi
NeXus group.
String _goButton
Control that is written to.
static String convertTimeToString(ref DateTime time)
Method for converting a DateTime to the format need for log files
Boolean _underRunControl
Control to wait for before returning (i.e. motion light).
object Clone()
Clone method (only a shallow copy).
Boolean _logChangesTolerance
Just log changes when they occur.
Boolean _blockEnabled
How many decimal points to show (deprecated)
void SetRunControl(Boolean enabled)
Method for enabling and disabling run-control.
static Boolean ExceedsTolerance(String currValue, String prevValue, Double tolerance)
Check whether the current value has changed by an amount greater than the set tolerance. Note: is public for testing purposes
ControlType _readType
Control for reading current value.
String _setPointValue
This value represents how much a value has to change for it to be logged.
String _previousLogValue
Last value - used to check whether value changed.
void LogValue(DateTime time, String shortName, String runNumber)
Method for logging the current value of the block. The block is only logged to file if the value sati...