You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.6 KiB
132 lines
5.6 KiB
|
4 weeks ago
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using leak_test_project.Models;
|
||
|
|
|
||
|
|
namespace leak_test_project.Utils
|
||
|
|
{
|
||
|
|
public class DioConfigParser
|
||
|
|
{
|
||
|
|
public string CompanyName { get; set; } = "ADLink";
|
||
|
|
public string BoardType { get; set; } = "PCI_7432";
|
||
|
|
public int BoardNumber { get; set; } = 0;
|
||
|
|
public List<DioPoint> InputPoints { get; private set; } = new List<DioPoint>();
|
||
|
|
public List<DioPoint> OutputPoints { get; private set; } = new List<DioPoint>();
|
||
|
|
|
||
|
|
public bool Load(string filePath)
|
||
|
|
{
|
||
|
|
if (!File.Exists(filePath)) return false;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var lines = File.ReadAllLines(filePath);
|
||
|
|
string currentSection = "";
|
||
|
|
InputPoints.Clear();
|
||
|
|
OutputPoints.Clear();
|
||
|
|
|
||
|
|
foreach (var rawLine in lines)
|
||
|
|
{
|
||
|
|
string line = rawLine.Split(new[] { "//" }, StringSplitOptions.None)[0].Trim();
|
||
|
|
if (string.IsNullOrEmpty(line)) continue;
|
||
|
|
|
||
|
|
if (line.StartsWith("[") && line.Contains("]"))
|
||
|
|
{
|
||
|
|
currentSection = line.Substring(1, line.IndexOf(']') - 1).ToUpper();
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
int eqIdx = line.IndexOf('=');
|
||
|
|
if (eqIdx < 0) continue;
|
||
|
|
|
||
|
|
string key = line.Substring(0, eqIdx).Trim();
|
||
|
|
string value = line.Substring(eqIdx + 1).Trim().TrimEnd(',');
|
||
|
|
|
||
|
|
switch (currentSection)
|
||
|
|
{
|
||
|
|
case "DIO":
|
||
|
|
if (key == "CompanyName") CompanyName = value;
|
||
|
|
break;
|
||
|
|
case "BOARD_1":
|
||
|
|
if (key == "BoardType") BoardType = value;
|
||
|
|
else if (key == "BoardNumber") { if (int.TryParse(value, out int bn)) BoardNumber = bn; }
|
||
|
|
break;
|
||
|
|
case "BOARD_1_IN":
|
||
|
|
ParsePointEntry(key, value, true);
|
||
|
|
break;
|
||
|
|
case "BOARD_1_OUT":
|
||
|
|
ParsePointEntry(key, value, false);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
catch { return false; }
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ParsePointEntry(string index, string value, bool isInput)
|
||
|
|
{
|
||
|
|
var parts = value.Split(',');
|
||
|
|
var point = new DioPoint
|
||
|
|
{
|
||
|
|
Name = parts[0].Trim(),
|
||
|
|
Description = parts.Length > 2 ? parts[2].Trim() : "",
|
||
|
|
IsInput = isInput
|
||
|
|
};
|
||
|
|
if (isInput) InputPoints.Add(point); else OutputPoints.Add(point);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Save(string filePath)
|
||
|
|
{
|
||
|
|
string dir = Path.GetDirectoryName(filePath);
|
||
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
|
||
|
|
|
||
|
|
using (var sw = new StreamWriter(filePath))
|
||
|
|
{
|
||
|
|
sw.WriteLine("[DIO]");
|
||
|
|
sw.WriteLine($"CompanyName={CompanyName}");
|
||
|
|
sw.WriteLine("BoardCnt=1");
|
||
|
|
sw.WriteLine("LogEnabled=False");
|
||
|
|
sw.WriteLine();
|
||
|
|
sw.WriteLine("[BOARD_1]");
|
||
|
|
sw.WriteLine($"BoardType={BoardType}");
|
||
|
|
sw.WriteLine($"BoardNumber={BoardNumber}");
|
||
|
|
sw.WriteLine($"InPortCnt={InputPoints.Count}");
|
||
|
|
sw.WriteLine($"OutPortCnt={OutputPoints.Count}");
|
||
|
|
sw.WriteLine();
|
||
|
|
sw.WriteLine("[BOARD_1_IN]");
|
||
|
|
for (int i = 0; i < InputPoints.Count; i++)
|
||
|
|
sw.WriteLine($"{i}={InputPoints[i].Name},1,{(string.IsNullOrEmpty(InputPoints[i].Description) ? "" : InputPoints[i].Description)}");
|
||
|
|
sw.WriteLine();
|
||
|
|
sw.WriteLine("[BOARD_1_OUT]");
|
||
|
|
for (int i = 0; i < OutputPoints.Count; i++)
|
||
|
|
sw.WriteLine($"{i}={OutputPoints[i].Name},1,{(string.IsNullOrEmpty(OutputPoints[i].Description) ? "" : OutputPoints[i].Description)}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static DioConfigParser LoadDefault()
|
||
|
|
{
|
||
|
|
var parser = new DioConfigParser();
|
||
|
|
string localPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings", "DioConfig.ini");
|
||
|
|
string legacyPath = @"C:\Users\COMPUTER1\Desktop\LeakTester\Settings\DioConfig.ini";
|
||
|
|
|
||
|
|
if (File.Exists(localPath)) parser.Load(localPath);
|
||
|
|
else if (File.Exists(legacyPath)) parser.Load(legacyPath);
|
||
|
|
else
|
||
|
|
{
|
||
|
|
// 파일 없을 때 기본 생성 (사용자 양식 반영)
|
||
|
|
parser.InputPoints.Add(new DioPoint { Name = "LEFT_START", Description = "좌측 시험 시작 신호" });
|
||
|
|
parser.InputPoints.Add(new DioPoint { Name = "RIGHT_START", Description = "우측 시험 시작 신호" });
|
||
|
|
|
||
|
|
parser.OutputPoints.Add(new DioPoint { Name = "LEFT_OK", Description = "좌측 합격 출력" });
|
||
|
|
parser.OutputPoints.Add(new DioPoint { Name = "RIGHT_OK", Description = "우측 합격 출력" });
|
||
|
|
parser.OutputPoints.Add(new DioPoint { Name = "PC_ON", Description = "PC 가동 신호" });
|
||
|
|
parser.OutputPoints.Add(new DioPoint { Name = "LEFT_NG", Description = "좌측 불합격 출력" });
|
||
|
|
parser.OutputPoints.Add(new DioPoint { Name = "RIGHT_NG", Description = "우측 불합격 출력" });
|
||
|
|
|
||
|
|
parser.Save(localPath);
|
||
|
|
}
|
||
|
|
return parser;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|