-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHardwareExtractor.cs
129 lines (113 loc) · 4.8 KB
/
HardwareExtractor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using CONTROLBUILDERLib;
using System.Runtime.InteropServices;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
namespace Fin
{
public class HWExtractor
{
private string _logFilePath;
public HWExtractor()
{
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
_logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"HWExtractorLog_{timestamp}.log");
LogInfo("Log file created: " + _logFilePath);
}
public void GetAllHardware()
{
CBOpenIF cb = null;
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "xmlControllers");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
LogInfo($"Creating directory at {folderPath}");
}
try
{
LogInfo("Initializing CONTROLBUILDERLib...");
cb = new CBOpenIF();
// Retrieve project tree and gather individual controller data
string projectTree = cb.GetProjectTree("", 1, true);
LogInfo("Project tree retrieved successfully.");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(projectTree);
string projectTreePath = Path.Combine(folderPath, "ContProjectTree.xml");
xmlDoc.Save(projectTreePath);
LogInfo($"Saved project tree to: {projectTreePath}");
List<string> controllerNames = GetControllerNames(projectTreePath);
LogInfo($"Found {controllerNames.Count} controller(s).");
foreach (var name in controllerNames)
{
LogInfo($"Processing hardware unit for controller: {name}");
var hardwareData = cb.GetHardwareUnit(name, true); // Retrieve XML data for the controller
XElement controllerElement = XElement.Parse(hardwareData);
// Save each controller's data into its own XML file
string controllerFilePath = Path.Combine(folderPath, $"{name}.xml");
controllerElement.Save(controllerFilePath);
LogInfo($"Saved hardware data for {name} to: {controllerFilePath}");
}
}
catch (COMException ex)
{
LogError($"COM Error: {ex.Message}");
LogError($"Error Code: 0x{ex.ErrorCode:X}");
}
catch (Exception ex)
{
LogError($"General Error: {ex.Message}");
LogError($"Stack Trace: {ex.StackTrace}");
}
finally
{
if (cb != null)
{
Marshal.ReleaseComObject(cb);
cb = null;
LogInfo("Released COM object.");
}
LogInfo("\nProcess complete. Check log file for details.");
}
}
private List<string> GetControllerNames(string xmlPath)
{
List<string> controllerNames = new List<string>();
try
{
LogInfo($"Loading XML from path: {xmlPath}");
XDocument xdoc = XDocument.Load(xmlPath);
LogInfo("XML loaded successfully.");
XNamespace ns = "CBOpenIFSchema3_0"; // Adjust according to your XML
controllerNames = xdoc.Descendants(ns + "Controller")
.Select(c => (string)c.Attribute("Name"))
.Where(name => !string.IsNullOrEmpty(name))
.ToList();
LogInfo($"Extracted {controllerNames.Count} controller name(s).");
foreach (var name in controllerNames)
{
LogInfo($"Controller name: {name}");
}
}
catch (FileNotFoundException ex)
{
LogError($"File not found: {ex.Message}");
}
catch (XmlException ex)
{
LogError($"XML Error: {ex.Message}");
}
catch (Exception ex)
{
LogError($"Error while loading XML: {ex.Message}");
LogError($"Stack Trace: {ex.StackTrace}");
}
return controllerNames;
}
private void LogInfo(string message) => WriteLog("INFO: " + message);
private void LogError(string message) => WriteLog("ERROR: " + message);
private void WriteLog(string message) => File.AppendAllText(_logFilePath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}\n");
}
}