-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmDatabase.cs
123 lines (113 loc) · 4.4 KB
/
AlarmDatabase.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
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using CONTROLBUILDERLib;
namespace Fin
{
public class AlarmDatabase
{
//getapplicationcontrolmodules
public void getCM()
{
CBOpenIF cb = null;
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AlarmDatabase");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
Console.WriteLine($"Creating directory at {folderPath}");
}
try
{
Console.WriteLine("Initializing CONTROLBUILDERLib...");
cb = new CBOpenIF();
string whatareYouLookingfor = "Applications";
// Retrieve project tree and gather individual controller data
string projectTree = cb.GetProjectTree(whatareYouLookingfor, 1, true);
// Specify the file path where you want to save the XML
// Save the XML string directly to the file
File.WriteAllText(Path.Combine(folderPath, whatareYouLookingfor), projectTree);
Console.WriteLine($"XML saved to {whatareYouLookingfor}");
Console.WriteLine("Project tree retrieved successfully.");
ParseAndPrintApplications(projectTree);
}
catch (COMException ex)
{
Console.WriteLine($"COM Error: {ex.Message}");
Console.WriteLine($"Error Code: 0x{ex.ErrorCode:X}");
}
catch (Exception ex)
{
Console.WriteLine($"General Error: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
}
finally
{
if (cb != null)
{
Marshal.ReleaseComObject(cb);
cb = null;
Console.WriteLine("Released COM object.");
}
Console.WriteLine("\nProcess complete. Check log file for details.");
}
}
static void ParseAndPrintApplications(string xmlData)
{
List<string> applicationNames = new List<string>(); // List to store application names
try
{
// Load XML data
XDocument xDoc = XDocument.Parse(xmlData);
// Define Namespace
XNamespace ns = "CBOpenIFSchema3_0";
// Query for all application elements
var appls = xDoc.Descendants(ns + "Application");
foreach (var app in appls)
{
// Extract attributes from each application
string appName = app.Attribute("Name")?.Value;
// Add to the application names list
if (!string.IsNullOrEmpty(appName))
{
applicationNames.Add(appName); // Add the name to the list
}
// Write the information to the console
Console.WriteLine($"Application Name: {appName}");
}
// Save application names to a text file
string filePath = "application_names.txt"; // Specify the file path
SaveApplicationNamesToFile(applicationNames, filePath);
Console.WriteLine($"\nApplication names saved to: {filePath}");
}
catch (Exception ex)
{
Console.WriteLine("Error parsing XML: " + ex.Message);
}
}
static void SaveApplicationNamesToFile(List<string> applicationNames, string filePath)
{
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (var appName in applicationNames)
{
writer.WriteLine(appName); // Write each application name to the file
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error writing to file: " + ex.Message);
}
}
}
}