삽질하다 도저히 문제 파악이 안돼서 opcUaManager로 분리 테스트 중
This commit is contained in:
@@ -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++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user