삽질하다 도저히 문제 파악이 안돼서 opcUaManager로 분리 테스트 중

This commit is contained in:
2026-02-25 08:52:03 +09:00
parent 4ea351946a
commit e88ab87771
138 changed files with 1051971 additions and 351 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

View File

@@ -0,0 +1 @@
![alt text](ProjectSchema.png)

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
// using System.Linq;
using System.Text;
using System.Threading;
//using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
@@ -21,6 +21,139 @@ namespace OpcConnectionTest
public string Description { get; set; } = "";
}
public class TagMaster
{
public string TagName {get; set; } = string.Empty;
public string FullNodeId {get; set; }= string.Empty;
public string NodeClass {get; set; }= string.Empty;
public string DataType {get; set; }= string.Empty;
public int Level {get; set; }
}
public class HoneywellCrawler
{
private readonly Session _session;
private readonly List<TagMaster> _discoveredTags = []; // IDE0028 적용
public HoneywellCrawler(Session session)
{
// 의존성 주입 및 Null 체크
_session = session ?? throw new ArgumentNullException(nameof(session));
}
/// <summary>
/// 저인망 탐사 시작점
/// </summary>
public async Task RunAsync(string startNodeId)
{
Console.WriteLine($"\n🚀 비동기 저인망 탐사 시작: {startNodeId}");
try
{
NodeId rootNode = NodeId.Parse(startNodeId);
await BrowseRecursiveAsync(rootNode, 0);
Console.WriteLine("\n===============================================");
Console.WriteLine($"✅ 탐사 완료! 총 {_discoveredTags.Count}개의 항목 발견.");
Console.WriteLine("===============================================");
SaveToCsv();
}
catch (Exception ex)
{
Console.WriteLine($"❌ 실행 중 치명적 오류: {ex.Message}");
}
}
private async Task BrowseRecursiveAsync(NodeId nodeId, int level)
{
try
{
BrowseDescription description = new() {
NodeId = nodeId,
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask = (uint)(NodeClass.Variable | NodeClass.Object),
ResultMask = (uint)BrowseResultMask.All
};
// 1. BrowseAsync는 객체 방식 (에러 CS1061/CS8129 방지)
BrowseResponse response = await _session.BrowseAsync(null, null, 0, [description], default);
if (response?.Results == null || response.Results.Count == 0) return;
foreach (var result in response.Results)
{
await ProcessReferencesAsync(result.References, level);
byte[] cp = result.ContinuationPoint;
while (cp != null && cp.Length > 0)
{
// 2. BrowseNextAsync는 튜플 방식 (에러 CS0029 방지)
var (nextHeader, nextCp, nextRefs) = await _session.BrowseNextAsync(null, false, cp, default);
await ProcessReferencesAsync(nextRefs, level);
cp = nextCp;
}
}
}
catch (Exception)
{
// 특정 노드 접근 권한 에러 등은 무시하고 진행
// Console.WriteLine($"⚠️ [Level {level}] {nodeId} 탐색 건너뜀: {ex.Message}");
}
}
private async Task ProcessReferencesAsync(ReferenceDescriptionCollection references, int level)
{
if (references == null || references.Count == 0) return;
foreach (var rd in references)
{
// ExpandedNodeId를 실제 NodeId로 변환 (Namespace 관리용)
NodeId childId = ExpandedNodeId.ToNodeId(rd.NodeId, _session.NamespaceUris);
// 마스터 리스트에 추가
_discoveredTags.Add(new TagMaster
{
TagName = rd.BrowseName.Name ?? "Unknown",
FullNodeId = childId.ToString(),
NodeClass = rd.NodeClass.ToString(),
Level = level
});
// 콘솔 출력 (진행 상황 확인용)
string indent = new string(' ', level * 2);
Console.WriteLine($"{indent} [{rd.NodeClass}] {rd.BrowseName.Name} (ID: {childId})");
// 3. Object(폴더/태그)인 경우 재귀 탐색 (하니웰 구조에 맞춰 깊이 5단계 제한)
if (rd.NodeClass == NodeClass.Object && level < 5)
{
await BrowseRecursiveAsync(childId, level + 1);
}
}
}
private void SaveToCsv()
{
try
{
using StreamWriter sw = new("Honeywell_FullMap.csv");
sw.WriteLine("Level,Class,Name,NodeId");
foreach (var tag in _discoveredTags)
{
sw.WriteLine($"{tag.Level},{tag.NodeClass},{tag.TagName},{tag.FullNodeId}");
}
Console.WriteLine($"💾 CSV 저장 완료: {Path.GetFullPath("Honeywell_FullMap.csv")}");
}
catch (Exception ex)
{
Console.WriteLine($"❌ CSV 저장 실패: {ex.Message}");
}
}
}
class Program
{
static Dictionary<string, StatusCodeInfo> _statusCodeMap = new(StringComparer.OrdinalIgnoreCase);
@@ -117,11 +250,30 @@ namespace OpcConnectionTest
var endpoint = new ConfiguredEndpoint(null, selected, endpointConfig);
var identity = new UserIdentity(userName, Encoding.UTF8.GetBytes(password));
session = await Session.Create(config, endpoint, false, "OpcTestSession", 60000, identity, null);
#pragma warning disable CS0618
session = await Session.Create(
config,
endpoint,
false,
"OpcTestSession",
60000,
identity,
null
);
#pragma warning restore CS0618
Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");
// 3. 데이터 읽기 및 DB 저장 루프 (테스트용으로 5회 반복)
Console.WriteLine("\n🔍 하니웰 전체 노드 탐사(저인망) 시작...");
// ISession을 실제 Session 클래스로 형변환하여 전달
if (session is Session clientSession)
{
HoneywellCrawler crawler = new HoneywellCrawler(clientSession);
// 'shinam' 노드를 기점으로 바닥까지 훑고 CSV 저장까지 수행합니다.
await crawler.RunAsync("ns=1;s=$assetmodel");
}
// 3. 데이터 읽기 및 DB 저장 루프 (테스트용으로 5회 반복)
string nodeID = "ns=1;s=shinam:p-6102.hzset.fieldvalue";
for (int i = 0; i < 5; i++)
{

BIN
OpcConnectionTest/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

View File

@@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+20ee22ae0c85a902f6767918a8ebece87301c37f")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4ea351946aa5ffb76c8c693b768cb7f460f0cb79")]
[assembly: System.Reflection.AssemblyProductAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyTitleAttribute("OpcConnectionTest")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// MSBuild WriteCodeFragment 클래스에서 생성되었습니다.
// Generated by the MSBuild WriteCodeFragment class.

View File

@@ -1 +1 @@
f71e0f6a7ff186b47ef80c0b9a2fc70fce275730017c4f885ccba6c266a8ced1
5dc72724b0828bf3589118e6e5edcb9a4e4008e775a094b3a90862f8de1efba1

View File

@@ -1,3 +1,8 @@
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.csproj.AssemblyReference.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.GeneratedMSBuildEditorConfig.editorconfig
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.AssemblyInfoInputs.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.AssemblyInfo.cs
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.csproj.CoreCompileInputs.cache
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/OpcConnectionTest
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/OpcConnectionTest.deps.json
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/OpcConnectionTest.runtimeconfig.json
@@ -11,6 +16,7 @@
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Microsoft.Extensions.Options.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Microsoft.Extensions.Primitives.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Newtonsoft.Json.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Npgsql.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Opc.Ua.Client.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Opc.Ua.Configuration.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Opc.Ua.Core.dll
@@ -26,15 +32,9 @@
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/System.Text.Encodings.Web.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/System.Text.Json.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/runtimes/browser/lib/net8.0/System.Text.Encodings.Web.dll
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.csproj.AssemblyReference.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.GeneratedMSBuildEditorConfig.editorconfig
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.AssemblyInfoInputs.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.AssemblyInfo.cs
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.csproj.CoreCompileInputs.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.csproj.CopyComplete
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.dll
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/refint/OpcConnectionTest.dll
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.pdb
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/OpcConnectionTest.genruntimeconfig.cache
/home/pacer/projects/OpcConnectionTest/obj/Debug/net8.0/ref/OpcConnectionTest.dll
/home/pacer/projects/OpcConnectionTest/bin/Debug/net8.0/Npgsql.dll