SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
BlockDictionary.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Xml;
5 using System.Xml.Serialization;
6 using System.Collections;
7 using System.Collections.Specialized;
8 using Seci.Definitions;
9 
10 namespace Seci
11 {
18  public class BlockDictionary : IXmlSerializable, ICloneable
19  {
20  OrderedDictionary _dict = new OrderedDictionary();
21 
28  public BlockInfo this[string name]
29  {
30  get
31  {
32  if (!String.IsNullOrEmpty(name))
33  {
34  return _dict[name.ToLower()] as BlockInfo;
35  }
36  else
37  {
38  return null;
39  }
40  }
41  set
42  {
43  if (!String.IsNullOrEmpty(name))
44  {
45  _dict[name.ToLower()] = value;
46  }
47  }
48  }
49 
57  public BlockInfo this[int index]
58  {
59  get
60  {
61  String[] keys = this.GetKeys();
62 
63  if (keys != null)
64  {
65  return ((BlockInfo)_dict[keys[index]]);
66  }
67  else
68  {
69  return null;
70  }
71  }
72  set
73  {
74  String[] keys = this.GetKeys();
75 
76  _dict[keys[index]] = value;
77  }
78  }
79 
88  public Boolean Add(String blockName, BlockInfo block)
89  {
90  if (!String.IsNullOrEmpty(blockName))
91  {
92  if (!String.IsNullOrEmpty(block.Alias) && _dict.Count > 0)
93  {
94  //Check alias is unique
95  if (!IsAliasUnique(block.Alias))
96  {
97  throw new Exception("Alias is not unique");
98  }
99  }
100 
101  try
102  {
103  _dict.Add(blockName.ToLower(), block);
104  }
105  catch
106  {
107  throw new Exception("Block name already exists");
108  }
109 
110  return true;
111  }
112 
113  return false;
114  }
115 
116  public void Insert(int index, String blockName, BlockInfo block)
117  {
118  if (!String.IsNullOrEmpty(block.Alias) && _dict.Count > 0)
119  {
120  //Check alias is unique
121  if (!IsAliasUnique(block.Alias))
122  {
123  throw new Exception("Alias is not unique");
124  }
125  }
126 
127  try
128  {
129  _dict.Insert(index, blockName.ToLower(), block);
130  }
131  catch
132  {
133  throw new Exception("Block name already exists");
134  }
135 
136  }
137 
146  public Boolean Replace(String oldName, String newName, BlockInfo newBlock)
147  {
148  try
149  {
150  int index = -1;
151  List<String> keys = new List<string>(GetKeys());
152  for (int i = 0; i < keys.Count; ++i)
153  {
154  if (keys[i] == oldName.ToLower())
155  {
156  index = i;
157  break;
158  }
159  }
160 
161  BlockInfo oldBlock = (BlockInfo)_dict[oldName.ToLower()];
162 
163  Remove(oldName.ToLower());
164 
165  //Check new block name and alias are unique
166  if (this.Contains(newBlock.BlockName))
167  {
168  //Reinsert the old block and throw
169  _dict.Insert(index, oldName.ToLower(), oldBlock);
170  throw new ArgumentException("Block name already exists.");
171  }
172 
173  if (!this.IsAliasUnique(newBlock.Alias))
174  {
175  //Reinsert the old block and throw
176  _dict.Insert(index, oldName.ToLower(), oldBlock);
177  throw new ArgumentException("Alias name already exists");
178  }
179 
180  _dict.Insert(index, newName.ToLower(), newBlock);
181  return true;
182  }
183  catch
184  {
185  throw;
186  }
187  }
188 
189  public int Count { get { return _dict.Count; } }
190 
197  public Boolean Contains(String blockName)
198  {
199  if (!String.IsNullOrEmpty(blockName))
200  {
201  return _dict.Contains(blockName.ToLower());
202  }
203 
204  return false;
205  }
206 
213  public Boolean IsAliasUnique(String alias)
214  {
215  if (String.IsNullOrEmpty(alias))
216  {
217  return true;
218  }
219 
220  foreach (BlockInfo block in _dict.Values)
221  {
222  if (block.Alias != null && block.Alias.ToLower() == alias.ToLower())
223  {
224  return false;
225  }
226  }
227 
228  return true;
229  }
230 
234  public void Clear()
235  {
236  _dict.Clear();
237  }
238 
245  internal void AddDictionary(BlockDictionary newDictionary)
246  {
247  //As it is impossible to add a block with same name as one already exisiting
248  //we rename the block
249  int count = 0;
250  String newName = "renamed_";
251 
252  for (int i = 0; i < newDictionary.Count; ++i)
253  {
254  try
255  {
256  //Set alias to "" for new blocks - easiest solution
257  //newDictionary[i].Alias = "";
258 
259  //Does a block already exists with this name
260  if (!_dict.Contains(newDictionary[i].BlockName.ToLower()))
261  {
262  _dict.Add(newDictionary[i].BlockName.ToLower(), newDictionary[i]);
263  }
264  else
265  {
266  //Generate new name
267  newDictionary[i].BlockName = newName + newDictionary[i].BlockName + "_" + count.ToString();
268  _dict.Add(newDictionary[i].BlockName.ToLower(), newDictionary[i]);
269  }
270  }
271  catch(Exception err)
272  {
273  //If it fails to be added then don't add - just log and move on...
274  Helpers.ErrorLogger.SeciError("AddDictionary", err);
275  }
276  }
277  }
278 
283  public void Remove(String blockName)
284  {
285  _dict.Remove(blockName.ToLower());
286  }
287 
293  public void RemoveAt(int index)
294  {
295  String[] keys = this.GetKeys();
296 
297  _dict.Remove(keys[index]);
298  }
299 
305  public void Swap(int index1, int index2)
306  {
307  if (index2 < index1)
308  {
309  int temp = index1;
310  index1 = index2;
311  index2 = temp;
312  }
313 
314  if (index1 < _dict.Count && index2 < _dict.Count && index1 != index2)
315  {
316  BlockInfo temp1 = (BlockInfo)_dict[index1];
317  BlockInfo temp2 = (BlockInfo)_dict[index2];
318  _dict.RemoveAt(index2);
319  _dict.RemoveAt(index1);
320  _dict.Insert(index1, temp2.BlockName.ToLower(), temp2);
321  _dict.Insert(index2, temp1.BlockName.ToLower(), temp1);
322  }
323  }
324 
330  public void RemoveByVI(String fileName)
331  {
332  String[] keys = this.GetKeys();
333 
334  List<String> blocksToRemove = new List<String>();
335 
336  //Find the keys of the blocks which belong to the VI
337  if (keys != null)
338  {
339  foreach (String key in keys)
340  {
341  if (this[key].ParentVI.ToLower() == fileName.ToLower())
342  {
343  blocksToRemove.Add(key);
344  }
345  }
346 
347  //Then delete them
348  for (int i = 0; i < blocksToRemove.Count; ++i)
349  {
350  this.Remove(blocksToRemove[i]);
351  }
352  }
353  }
354 
359  public String[] GetKeys()
360  {
361  if (_dict.Keys.Count != 0)
362  {
363  String[] keys = new String[_dict.Keys.Count];
364 
365  _dict.Keys.CopyTo(keys, 0);
366 
367  return keys;
368  }
369  else
370  {
371  return null;
372  }
373  }
374 
379  internal List<String> GetEnabledBlocks()
380  {
381  if (_dict.Keys.Count != 0)
382  {
383  //Get only enabled blocks
384  List<String> keys = new List<String>();
385 
386  keys.AddRange(GetKeys());
387 
388  List<String> blocks = new List<String>();
389 
390  for (int i = 0; i < keys.Count; ++i)
391  {
392  if (this[keys[i]].BlockEnabled)
393  {
394  blocks.Add(this[keys[i]].BlockName);
395  }
396  }
397 
398  return blocks;
399  }
400  else
401  {
402  return null;
403  }
404  }
405 
410  internal String[] GetAliases()
411  {
412  if (_dict.Keys.Count != 0)
413  {
414  //Get only enabled blocks
415  List<String> keys = new List<String>();
416 
417  keys.AddRange(GetKeys());
418 
419  List<String> aliases = new List<String>();
420 
421  for (int i = 0; i < keys.Count; ++i)
422  {
423  aliases.Add(this[keys[i]].Alias);
424  }
425 
426  return aliases.ToArray();
427  }
428  else
429  {
430  return null;
431  }
432  }
433 
439  internal String[,] GetLabVIEWInfo()
440  {
441  if (_dict.Keys.Count != 0)
442  {
443  //Get only enabled blocks
444  List<String> keys = new List<String>();
445 
446  keys.AddRange(GetKeys());
447 
448  String[,] results = new String[keys.Count, 7];
449 
450  for (int i = 0; i < keys.Count; ++i)
451  {
452  results[i, 0] = keys[i];
453  results[i, 1] = this[keys[i]].Alias;
454  results[i, 2] = this[keys[i]].ParentVI;
455  results[i, 3] = this[keys[i]].ReadControl;
456  results[i, 4] = this[keys[i]].WriteControl;
457  results[i, 5] = this[keys[i]].GoButton;
458  results[i, 6] = this[keys[i]].WaitForControl;
459  }
460 
461  return results;
462  }
463  else
464  {
465  return null;
466  }
467  }
468 
478  internal String[] GetFormattedBlockValues(Boolean visibleOnly, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaitState)
479  {
480  if (_dict.Keys.Count != 0)
481  {
482  List<String> keys = new List<String>();
483 
484  keys.AddRange(GetKeys());
485 
486  List<String> values = new List<String>();
487 
488  for (int i = 0; i < keys.Count; ++i)
489  {
490  BlockInfo temp = this[keys[i]];
491 
492  //Check block enabled
493  if (temp.BlockEnabled && (!visibleOnly || temp.Visible))
494  {
495  values.Add(GetFormattedBlockValue(temp.BlockName, includeSetpoint, includeGroup, includeWaitState));
496  }
497  }
498 
499  return values.ToArray();
500  }
501  else
502  {
503  return null;
504  }
505  }
506 
515  internal String GetFormattedBlockValue(String blockName, Boolean includeSetpoint, Boolean includeGroup, Boolean includeWaitState)
516  {
517  BlockInfo temp = this[blockName];
518 
519  if (temp != null)
520  {
521  String result = temp.BlockName + ": " + temp.FormattedCurrentValue + " " + temp.BlockUnits;
522 
523  if (includeSetpoint && temp.WriteControl != null)
524  {
525  result += " [Setpoint = " + temp.FormattedSetPointValue + "]";
526  }
527 
528  if (includeWaitState && !temp.InRange)
529  {
530  result += " OUT OF RANGE";
531  }
532 
533  if (includeGroup && !String.IsNullOrEmpty(temp.Group))
534  {
535  result += " <Group=" + temp.Group + ">";
536  }
537 
538  return result;
539  }
540  else
541  {
542  return "";
543  }
544  }
545 
552  internal String GetBlockValue(String blockName)
553  {
554  if (!String.IsNullOrEmpty(blockName))
555  {
556  if (_dict.Keys.Count != 0)
557  {
558  String lower = blockName.ToLower();
559 
560  if (_dict.Contains(lower))
561  {
562  BlockInfo temp = (BlockInfo)_dict[lower];
563 
564  //Check block enabled
565  if (temp.BlockEnabled)
566  {
567  return temp.CurrentValue;
568  }
569  }
570  }
571  }
572 
573  return null;
574  }
575 
582  internal String GetSetpointValue(String blockName)
583  {
584  if (!String.IsNullOrEmpty(blockName))
585  {
586  if (_dict.Keys.Count != 0)
587  {
588  String lower = blockName.ToLower();
589 
590  if (_dict.Contains(lower))
591  {
592  BlockInfo temp = (BlockInfo)_dict[lower];
593 
594  //Check block enabled
595  if (temp.BlockEnabled)
596  {
597  return temp.SetPointValue;
598  }
599  }
600  }
601  }
602 
603  return null;
604  }
605 
606 
607  #region IXmlSerializable Members
608 
614  public System.Xml.Schema.XmlSchema GetSchema()
615  {
616  return null;
617  }
618 
625  public void ReadXml(System.Xml.XmlReader reader)
626  {
627  if (reader != null)
628  {
629  XmlSerializer Serializer = new XmlSerializer(typeof(BlockInfo));
630 
631  // Move past container
632  reader.Read();
633 
634  if (reader.Name == "BlockInfo")
635  {
636  // Deserialize and add the objects
637  while (reader.NodeType != XmlNodeType.EndElement)
638  {
639  BlockInfo entity;
640 
641  entity = Serializer.Deserialize(reader) as BlockInfo;
642  _dict.Add(entity.BlockName.ToLower(), entity);
643  reader.MoveToContent();
644  }
645 
646  // Move on container a step further to get past </Blocks>
647  reader.Read();
648  }
649  }
650  }
651 
659  public void WriteXml(System.Xml.XmlWriter writer)
660  {
661  if (writer != null)
662  {
663  XmlSerializer Serializer = new XmlSerializer(typeof(BlockInfo));
664 
665  foreach (string key in _dict.Keys)
666  {
667  if (((BlockInfo)_dict[key]).OwningComponent == null)
668  {
669  Serializer.Serialize(writer, _dict[key]);
670  }
671  }
672  }
673  }
674 
675  #endregion
676 
677  #region ICloneable Members
678 
685  public object Clone()
686  {
687  if (_dict.Keys.Count > 0)
688  {
689  BlockInfo blockclone;
690  BlockDictionary deepCopy = new BlockDictionary();
691  for (int i = 0; i < _dict.Count; ++i )
692  {
693  blockclone = (BlockInfo)((BlockInfo)_dict[i]).Clone();
694  deepCopy.Add(blockclone.BlockName.ToLower(), blockclone);
695  }
696 
697  return deepCopy;
698  }
699  else
700  {
701  return null;
702  }
703  }
704 
705  #endregion
706 
707  }
708 
709 }
Boolean Contains(String blockName)
Wrapper for standard Dictionary &quot;Contains&quot; All keys are lowercase.
void RemoveByVI(String fileName)
Remove block(s) by VI - may be more than one block. Used when a VI is removed from a configuration...
Boolean Replace(String oldName, String newName, BlockInfo newBlock)
Method for replacing a block in the dictionary. Keeps the existing key. First removes the old block...
Boolean Add(String blockName, BlockInfo block)
Wrapper for standard Dictionary &quot;Add&quot;, but with additional checks: i) the key is not &quot;&quot; or null...
This class is used for storing the information for any blocks that are created and provides methods f...
Definition: BlockInfo.cs:18
void Insert(int index, String blockName, BlockInfo block)
void Clear()
Wrapper for standard Dictionary &quot;Clear&quot;
void ReadXml(System.Xml.XmlReader reader)
ReadXml - Default method of IXmlSerializable Used to read the block information from the configuratio...
object Clone()
Clone - Default method of ICloneable Used to clone (deep copy) the block dictionary. This allows for cancelling when editing the blocks.
void RemoveAt(int index)
Remove a block by index number, like a standard List. Looks up the key then calls remove by key...
void WriteXml(System.Xml.XmlWriter writer)
WriteXml - Default method of IXmlSerializable Used to write the block information to the configuratio...
void Remove(String blockName)
Wrapper for standard Dictionary &quot;Remove&quot; which removes by key.
System.Xml.Schema.XmlSchema GetSchema()
GetSchema - Default method of IXmlSerializable Not used.
OrderedDictionary _dict
Boolean IsAliasUnique(String alias)
Checks that the alias for the Block is unique. Note: Ignores case.
String[] GetKeys()
Gets a list of all the keys in the dictionary.
Specialised version of the Dictionary class which can be converted to XML. Also has some additional s...
void Swap(int index1, int index2)
Swap the order of two blocks