SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ComponentMgr.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 
6 namespace Seci.Managers
7 {
11  public static class ComponentMgr
12  {
13  private static ComponentList _components = new ComponentList();
14  public static ComponentList Components { get { return _components; } set { _components = value; } }
15 
21  public static Boolean AddComponent(String fileName)
22  {
23  //load a component then add it to the current config
24  try
25  {
26  //Check component has not already been added
27  for (int i = 0; i < _components.Count; ++i)
28  {
29  if (fileName.ToLower().EndsWith("\\" + _components[i].ConfigName.ToLower()))
30  {
31  return false;
32  }
33  }
34 
35  //load component and add it to conf
36  _components.Add(Helpers.ConfigurationIO.DeserialiseConfig(fileName));
37 
38  String compName = _components[_components.Count - 1].ConfigName;
39 
40  //Set the owning name of the blocks then add to the block dictionary
41  addBlocks(compName);
42 
43  //Add groups here! First time the sub-config groups will be added at the end
44  //When the main config is saved the order will be saved there
45  var comp = _components[_components.Count - 1];
46  if (comp.Groups != null)
47  {
48  for (int i = 0; i < comp.Groups.Count; ++i)
49  {
50  if (!BlockMgr.BlockGroupExists(comp.Groups[i].Name))
51  {
52  BlockMgr.Groups.Add(new Definitions.BlockGroup(comp.Groups[i].Name));
53  }
54  }
55  }
56  //If the comp does not contain group list (i.e. it was saved using an older version of seci)
57  //then obtain the list from the blocks
58  if (comp.Blocks != null)
59  {
60  for (int i = 0; i < comp.Blocks.Count; ++i)
61  {
62  if (!BlockMgr.BlockGroupExists(comp.Blocks[i].Group))
63  {
64  BlockMgr.Groups.Add(new Definitions.BlockGroup(comp.Blocks[i].Group));
65  }
66  }
67  }
68 
69  //Add any component tabs
70  ConfigurationMgr.AddComponentTabs();
71 
72  //Add the alerts but with owning name set
73  AlertsMgr.AddComponentBox(null, _components[compName].Alerts, compName);
74 
75  //Load VIs
76  loadComponentVIs(compName);
77 
78  //Load Executables
79  loadComponentExes(compName);
80 
81  //Load associated files
82  loadAssocFiles(compName);
83 
84  //Add graphs
85  addGraphs(compName);
86 
87  return true;
88  }
89  catch (Exception e)
90  {
91  //throw error up to next level
92  throw new ArgumentException(e.Message);
93  }
94  }
95 
102  public static Boolean RemoveComponent(String compName)
103  {
104  try
105  {
106  for (int i = 0; i < _components.Count; ++i)
107  {
108  if (_components[i].ConfigName == compName)
109  {
110  //Stop the updating threads
111  Seci.Managers.SeciMgr.PauseThreads();
112  System.Threading.Thread.Sleep(1000);
113 
114  //Remove any blocks
115  if (BlockMgr.Blocks != null)
116  {
117  for (int j = BlockMgr.Blocks.Count - 1; j >= 0; --j)
118  {
119  if (!String.IsNullOrEmpty(BlockMgr.Blocks[j].OwningComponent) && BlockMgr.Blocks[j].OwningComponent.ToLower() == compName.ToLower())
120  {
121  BlockMgr.Blocks.Remove(BlockMgr.Blocks[j].BlockName);
122  }
123  }
124  }
125 
126  try
127  {
128  //Need to remove any VIs associated with the component to remove
129  var panels = Seci.Managers.LabViewMgr.GetAllPanelsInfo();
130 
131  for (int j = 0; j < panels.Count; ++j)
132  {
133  //Does not actually close the VIs, they will still be in memory
134  if (panels[j].OwningComponent == compName)
135  {
136  LabView.LabViewApp.CloseVI(panels[j].FilePath);
137  }
138  }
139  }
140  catch
141  {
142  //throw new Exception("VI");
143  }
144 
145  //remove any executables
146  for (int j = 0; j < _components[i].Executables.Count; ++j)
147  {
148  _components[i].Executables[j].Dispose();
149  ExecutableMgr.RemoveExecutable(_components[i].Executables[j].FilePath);
150  }
151 
152  //remove any assoc files
153  for (int j = 0; j < _components[i].AssociatedFiles.Count; ++j)
154  {
155  AssocFileMgr.RemoveFile(_components[i].AssociatedFiles[j].FileLocation);
156  }
157 
158  //remove any graphs
159  ConfigurationMgr.RemoveComponentGraphs(compName);
160 
161  //Then remove component
162  _components.Remove(_components[i]);
163 
164  //Restart threads
165  Seci.Managers.SeciMgr.ResumeThreads();
166 
167  return true;
168  }
169  }
170  }
171  catch (Exception err)
172  {
173  Seci.Helpers.ErrorLogger.SeciError("RemoveComponent", err);
174  }
175 
176  return false;
177  }
178 
183  public static List<String> GetNames()
184  {
185  if (_components != null)
186  {
187  List<String> names = new List<String>();
188 
189  for (int i = 0; i < _components.Count; ++i)
190  {
191  names.Add(_components[i].ConfigName);
192  }
193 
194  return names;
195  }
196 
197  return null;
198  }
199 
204  private static void addBlocks(String compName)
205  {
206  for (int i = 0; i < _components[compName].Blocks.Count; ++i)
207  {
208  _components[compName].Blocks[i].OwningComponent = _components[_components.Count - 1].ConfigName;
209  }
210  BlockMgr.Blocks.AddDictionary(_components[compName].Blocks);
211  //_components[compName].Blocks = null;
212  }
213 
217  public static void LoadComponentsVisExes()
218  {
219  //load VIs and Exes in components
220  if (_components != null)
221  {
222  for (int i = 0; i < _components.Count; ++i)
223  {
224  loadComponentVIs(_components[i].ConfigName);
225  loadComponentExes(_components[i].ConfigName);
226  loadAssocFiles(_components[i].ConfigName);
227  }
228  }
229  }
230 
236  private static void loadComponentVIs(String compName)
237  {
238  FileInfo fi;
239 
240  for (int i = 0; i < _components[compName].LVPanels.Count; ++i)
241  {
242  //Create a reference to the panel to load (just to make the code clearer)
243  Seci.LabView.LabViewPanelInfo Panel = _components[compName].LVPanels[i];
244 
245  //Might be a .llb, check for that
246  if (Panel.FilePath.Substring(0, Panel.FilePath.LastIndexOf('\\')).ToLower().EndsWith(".llb"))
247  {
248  //It is a .llb, so have to remove the .vi bit when checking the file exists
249  fi = new FileInfo(Panel.FilePath.Substring(0, Panel.FilePath.LastIndexOf('\\')));
250  }
251  else
252  {
253  //It is a .vi
254  fi = new FileInfo(Panel.FilePath);
255  }
256 
257  if (fi.Exists)
258  {
259 
260  Panel.OwningComponent = compName; //Set the name for the owning config
261  if (LabView.LabViewApp.LoadVI(Panel, true))
262  {
263  //Show if necessary
264  if (Panel.ShowPanel)
265  {
266  LabView.LabViewApp.ShowVI(Panel.FilePath);
267  }
268 
269  //Start running if necessary
270  if (Panel.StartRunning)
271  {
272  LabView.LabViewApp.StartVI(Panel.FilePath);
273  }
274  }
275  }
276  }
277  }
278 
283  private static void loadComponentExes(String compName)
284  {
285  for (int i = 0; i < _components[compName].Executables.Count; ++i)
286  {
287  _components[compName].Executables[i].OwningComponent = compName;
288  ExecutableMgr.AddExecutable(_components[compName].Executables[i]);
289  }
290  }
291 
297  private static void loadAssocFiles(String compName)
298  {
299  for (int i = 0; i < _components[compName].AssociatedFiles.Count; ++i)
300  {
301  _components[compName].AssociatedFiles[i].OwningComponent = compName;
302  AssocFileMgr.AddFile(_components[compName].AssociatedFiles[i]);
303  AssocFileMgr.RecreateFile(_components[compName].AssociatedFiles[i]);
304  }
305  }
306 
312  private static void addGraphs(String compName)
313  {
314  for (int i = 0; i < _components[compName].GraphDefinitions.Count; ++i)
315  {
316  _components[compName].GraphDefinitions[i].OwningComponent = compName; //Set the name for the owning config
317  }
318 
319  ConfigurationMgr.AddGraphDefinitions(_components[compName].GraphDefinitions);
320  }
321 
322  public static void SaveComponent(String compName)
323  {
324  String fileName = Definitions.Status.ConfigDir + _components[compName].ConfigName;
325 
326  //Clear out the owning config for the blocks temporarily
327  for (int i = 0; i < _components[compName].Blocks.Count; ++i)
328  {
329  _components[compName].Blocks[i].OwningComponent = null;
330  }
331 
332  //Clear out the owning config for the VIs temporarily
333  for (int i = 0; i < _components[compName].LVPanels.Count; ++i)
334  {
335  _components[compName].LVPanels[i].OwningComponent = null;
336  }
337 
338  //Saves the sub-config as xml
339  Helpers.ConfigurationIO.SaveConfig(_components[compName], false, fileName);
340 
341  //Pop the owning config back for the blocks
342  for (int i = 0; i < _components[compName].Blocks.Count; ++i)
343  {
344  _components[compName].Blocks[i].OwningComponent = _components[compName].ConfigName;
345  }
346 
347  //Pop the owning config back for the VIs
348  for (int i = 0; i < _components[compName].LVPanels.Count; ++i)
349  {
350  _components[compName].LVPanels[i].OwningComponent = _components[compName].ConfigName;
351  }
352  }
353 
354  }
355 }
The manager class for all things component related
Definition: ComponentMgr.cs:11
A serialisable list for containing objects of type SeciConfiguration. This class wraps a standard Lis...
static BlockDictionary Blocks
Definition: BlockMgr.cs:21
static List< String > GetNames()
Gets a list of the names of the components that are part of the current configuration.
static void loadComponentVIs(String compName)
When adding a component to a configuration this method loads all (if any) LabVIEW VIs in the componen...
static void SaveComponent(String compName)
static void LoadComponentsVisExes()
Loads the VIs and executables associated to the component
The manager class for Block related stuff.
Definition: BlockMgr.cs:11
static void loadAssocFiles(String compName)
When loading or adding a component this method loads any associated files that are part of the compon...
static void loadComponentExes(String compName)
When loading or adding a component this method loads any executables that are part of the ...
static void addGraphs(String compName)
When loading or adding a component this method adds any graphs that are part of the component...
static Boolean RemoveComponent(String compName)
Removes a component from the current configuration. Removes any VIs, blocks, or executables associate...
This class contains all the information about a LabVIEW VI that needs to be saved in the configuratio...
static void addBlocks(String compName)
Set the owning name of the blocks then add to the block dictionary.
static bool BlockGroupExists(string name)
Definition: BlockMgr.cs:181
static Boolean AddComponent(String fileName)
Open a component and add it to the current configuration.
Definition: ComponentMgr.cs:21