SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
BlockMgr.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Seci.Definitions;
5 
6 namespace Seci.Managers
7 {
11  public static class BlockMgr
12  {
16  private static BlockDictionary _blocks = new BlockDictionary();
17  private static SerialisableList<Definitions.BlockGroup> _groups = new SerialisableList<Definitions.BlockGroup>();
18  private static DateTime _timeWentOutOfRange = new DateTime();
19  private static Boolean _logValuesEnabled = true;
20 
21  public static BlockDictionary Blocks { get { return _blocks; } }
22  public static SerialisableList<BlockGroup> Groups { get { return _groups; } internal set { _groups = value; } }
23 
24  public delegate void EnableLoggingChangedEventHandler(Boolean enabled);
25  public static event EnableLoggingChangedEventHandler OnEnableLoggingUpdated;
26  public static Boolean LogValuesEnabled
27  {
28  get { return _logValuesEnabled; }
29  set
30  {
31  _logValuesEnabled = value;
32  if (OnEnableLoggingUpdated != null)
33  {
34  OnEnableLoggingUpdated(value);
35  }
36  }
37  }
38 
39  #region Public Methods
40 
45  public static void SetBlocks(BlockDictionary blocks)
46  {
47  _blocks = blocks;
48  if (Seci.Definitions.Status.LogToDatabase)
49  {
50  Seci.Helpers.DatabaseLogger.WriteBlockDetails();
51  }
52  }
53 
60  {
61  BlockDictionary blocks;
62 
63  try
64  {
65  //Get list of all blocks include any in components
66  blocks = (BlockDictionary)_blocks.Clone();
67 
68  if (blocks == null)
69  {
70  //config does not contain any blocks
71  blocks = new BlockDictionary();
72  }
73  }
74  catch (Exception err)
75  {
76  Helpers.ErrorLogger.SeciError("GetAllBlocks", err);
77  blocks = new BlockDictionary();
78  }
79 
80  return blocks;
81  }
82 
92  public static List<String> GetAllBlockValues(Boolean visibleOnly, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaitState)
93  {
94  List<String> blocks = new List<String>();
95 
96  if (_blocks.Count != 0)
97  {
98  blocks.AddRange(_blocks.GetFormattedBlockValues(visibleOnly, includeSetpoint, includeGroup, includeWaitState));
99  }
100 
101  if (blocks.Count != 0)
102  {
103  return blocks;
104  }
105  else
106  {
107  return null;
108  }
109  }
110 
116  public static String GetBlockValue(String blockname)
117  {
118  return _blocks.GetBlockValue(blockname);
119  }
120 
129  public static String GetFormattedBlockValue(String blockname, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaiting)
130  {
131  return _blocks.GetFormattedBlockValue(blockname, includeSetpoint, includeGroup, includeWaiting);
132  }
133 
139  public static String GetBlockSetpoint(String block)
140  {
141  return _blocks.GetSetpointValue(block);
142  }
143 
153  public static Boolean SetBlockRunControl(String blockname, Boolean enabled, Double low, Double high)
154  {
155  if (_blocks.Contains(blockname.ToLower()))
156  {
157  _blocks[blockname.ToLower()].SetRunControlLimits(enabled, low, high);
158  return true;
159  }
160 
161  return false;
162  }
163 
170  public static Boolean ToggleRunContol(String blockname, Boolean enabled)
171  {
172  if (_blocks.Contains(blockname.ToLower()))
173  {
174  _blocks[blockname.ToLower()].SetRunControl(enabled);
175  return true;
176  }
177 
178  return false;
179  }
180 
181  public static bool BlockGroupExists(string name)
182  {
183  for (int i = 0; i < _groups.Count; ++i)
184  {
185  if (name.ToLower().Trim() == _groups[i].Name.ToLower().Trim())
186  {
187  return true;
188  }
189  }
190 
191  return false;
192  }
193 
194  public static void ReorderBlockGroups(List<string> neworder)
195  {
196  SerialisableList<BlockGroup> newList = new SerialisableList<BlockGroup>();
197  foreach (string name in neworder)
198  {
199  newList.Add(new BlockGroup(name));
200  }
201 
202  _groups = newList;
203  }
204 
205  #endregion
206 
211  internal static void UpdateBlockValues()
212  {
213  Boolean allInRange = true;
214 
215  for (int i = 0; i < Managers.BlockMgr.Blocks.Count; ++i)
216  {
217  //Check that block is enabled
218  if (Managers.BlockMgr.Blocks[i].BlockEnabled)
219  {
220  //Update value
221  try
222  {
223  Managers.BlockMgr.Blocks[i].CurrentValue = Seci.LabView.LabViewApp.GetValue(Managers.BlockMgr.Blocks[i].ParentVI, Managers.BlockMgr.Blocks[i].ReadControl)[0];
224  Managers.BlockMgr.Blocks[i].PrevReadError = false;
225  if (Managers.BlockMgr.Blocks[i].WriteControl != null)
226  {
227  try
228  {
229  Managers.BlockMgr.Blocks[i].SetPointValue = Seci.LabView.LabViewApp.GetValue(Managers.BlockMgr.Blocks[i].ParentVI, Managers.BlockMgr.Blocks[i].WriteControl)[0];
230  }
231  catch (Exception err)
232  {
233  Managers.BlockMgr.Blocks[i].SetPointValue = null;
234  //Seci.Helpers.ErrorLogger.SeciError("UpdateBlockValues GetSetpoint (" + Managers.BlockMgr.Blocks[i].BlockName + " ) ", err);
235  }
236  }
237 
238  if (Seci.Definitions.Status.LogToDatabase)
239  {
240  //Push into database
241  Seci.Helpers.DatabaseLogger.AddCurrentValue(Managers.BlockMgr.Blocks[i].BlockName, Managers.BlockMgr.Blocks[i].CurrentValue, Managers.BlockMgr.Blocks[i].SetPointValue);
242  }
243  }
244  catch (Exception err)
245  {
246  Managers.BlockMgr.Blocks[i].CurrentValue = Definitions.BlockInfo.ErrorString;
247  Managers.BlockMgr.Blocks[i].SetPointValue = null;
248  if (!Managers.BlockMgr.Blocks[i].PrevReadError)
249  {
250  //Log it only the first time it throws
251  Managers.BlockMgr.Blocks[i].PrevReadError = true;
252  Seci.Helpers.ErrorLogger.SeciError("UpdateBlockValues (" + Managers.BlockMgr.Blocks[i].BlockName + ")", err);
253  }
254  }
255  }
256 
257  //Check whether is in range
258  if (Managers.BlockMgr.Blocks[i].OutOfRange())
259  {
260  //Don't come out of waiting
261  allInRange = false;
262 
263  //Tell DAE to go into waiting state immediately, if not already
264  Standard.Dae.BeginWaiting();
265  }
266 
267  //Do we need to raise an alert?
268  AlertsMgr.CheckBlockInRange(Managers.BlockMgr.Blocks[i]);
269 
270  //Then log, if necessary
271  if (_logValuesEnabled)
272  {
273  Managers.BlockMgr.Blocks[i].LogValue(DateTime.Now, Definitions.Status.ShortInstrument, Standard.Dae.RunNumber);
274  }
275  }
276 
277  //See if should come out of waiting
278  if (allInRange)
279  {
280  //Tell DAE to come out of waiting
281  Standard.Dae.EndWaiting();
282  }
283 
284  if (Standard.Dae.RunStatus == "WAITING")
285  {
286  AlertsMgr.CheckInWaiting(true);
287  }
288  else
289  {
290  AlertsMgr.CheckInWaiting(false);
291  }
292  }
293 
294  }
295 }
static EnableLoggingChangedEventHandler OnEnableLoggingUpdated
Definition: BlockMgr.cs:25
static List< String > GetAllBlockValues(Boolean visibleOnly, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaitState)
Get all the current block values, including any in the components. Format of String is &quot;blockname: va...
Definition: BlockMgr.cs:92
object Clone()
Clone - Default method of ICloneable Used to clone (deep copy) the block dictionary. This allows for cancelling when editing the blocks.
static void SetBlocks(BlockDictionary blocks)
This method set the blocks in SECI after they have been created or modified
Definition: BlockMgr.cs:45
static String GetBlockValue(String blockname)
Gets the value of a specified block in the configuration or its components.
Definition: BlockMgr.cs:116
static String GetBlockSetpoint(String block)
Gets the set-point value of a specified block in the configuration or its components.
Definition: BlockMgr.cs:139
static Boolean ToggleRunContol(String blockname, Boolean enabled)
Toggle whether run-control is enabled - uses limits set previously.
Definition: BlockMgr.cs:170
The manager class for Block related stuff.
Definition: BlockMgr.cs:11
static String GetFormattedBlockValue(String blockname, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaiting)
Gets a formatted string of form &quot;BlockName: value units&quot;.
Definition: BlockMgr.cs:129
static void ReorderBlockGroups(List< string > neworder)
Definition: BlockMgr.cs:194
static BlockDictionary GetBlocksClone()
Creates a new BlockDictionary containing all the blocks, including any which are in a component...
Definition: BlockMgr.cs:59
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...
static bool BlockGroupExists(string name)
Definition: BlockMgr.cs:181
static Boolean SetBlockRunControl(String blockname, Boolean enabled, Double low, Double high)
Sets the run-control limits for a block, includes any blocks in components. This is the recommended m...
Definition: BlockMgr.cs:153