From 8a9310397b96cd5ab5d21c1890a16947f54099cc Mon Sep 17 00:00:00 2001 From: hhsung Date: Mon, 11 Aug 2025 12:16:52 +0900 Subject: [PATCH 1/3] =?UTF-8?q?file=20=EC=B2=98=EB=A6=AC=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataAnalisys/AquarDataControl.cs | 81 +++++++++++++++++++ SmartAquaViewer/DataAnalisys/IWaterQuality.cs | 27 +++++++ 2 files changed, 108 insertions(+) create mode 100644 SmartAquaViewer/DataAnalisys/IWaterQuality.cs diff --git a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs index 52cfb36..1c17ada 100644 --- a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs +++ b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs @@ -1,15 +1,96 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SmartAquaViewer.DataAnalisys { + + + internal class AquarDataControl { + private AQUA_DATA_CONTROL_STATE state = AQUA_DATA_CONTROL_STATE.IDLE; + private readonly IWaterQuality iwaterQuality; + private CancellationTokenSource? cts; + + public AquarDataControl(IWaterQuality iwaterQuality) + { + this.iwaterQuality = iwaterQuality; + } + + public AQUA_DATA_CONTROL_STATE GetState() => state; + + /// + /// 파일 파싱 시작 + /// + public void ParseFile(string filePath) + { + // --- 동기 처리 구간 --- + if (state != AQUA_DATA_CONTROL_STATE.IDLE && + state != AQUA_DATA_CONTROL_STATE.PROCESS_COMPLETED) + { + iwaterQuality.OnError("illegal state error : " + state); + return; + } + + state = AQUA_DATA_CONTROL_STATE.PROCESSING; + cts = new CancellationTokenSource(); + + // --- 비동기 처리 구간 --- + Task.Run(async () => + { + try + { + var fileInfo = new FileInfo(filePath); + long totalBytes = fileInfo.Length; + long processedBytes = 0; + + using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + using (var reader = new StreamReader(fs)) + { + string? line; + while ((line = await reader.ReadLineAsync()) != null) + { + cts.Token.ThrowIfCancellationRequested(); + + processedBytes += Encoding.UTF8.GetByteCount(line + Environment.NewLine); + double percent = (double)processedBytes / totalBytes * 100.0; + + iwaterQuality.OnProgress(percent); + + // TODO: 라인 파싱 로직 + } + } + state = AQUA_DATA_CONTROL_STATE.PROCESS_COMPLETED; + iwaterQuality.OnCompleted(); + } + catch (OperationCanceledException) + { + state = AQUA_DATA_CONTROL_STATE.CANCELLED; + iwaterQuality.OnCancelled(); + } + catch (Exception ex) + { + state = AQUA_DATA_CONTROL_STATE.ERROR; + iwaterQuality.OnError("파일 처리 중 오류 발생: " + ex.Message); + } + }); + } + /// + /// 처리 중단 요청 + /// + public void Cancel() + { + if (state == AQUA_DATA_CONTROL_STATE.PROCESSING && cts != null) + { + cts.Cancel(); + } + } } diff --git a/SmartAquaViewer/DataAnalisys/IWaterQuality.cs b/SmartAquaViewer/DataAnalisys/IWaterQuality.cs new file mode 100644 index 0000000..a76621e --- /dev/null +++ b/SmartAquaViewer/DataAnalisys/IWaterQuality.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SmartAquaViewer.DataAnalisys +{ + + internal enum AQUA_DATA_CONTROL_STATE + { + IDLE, + PROCESSING, + PROCESS_COMPLETED, + CANCELLED, + ERROR + } + + + internal interface IWaterQuality + { + void OnProgress(double percent); + void OnError(string message); + void OnCompleted(); + void OnCancelled(); + } +} From 14a46fbf908303f8a5b308137664f5099a029360 Mon Sep 17 00:00:00 2001 From: hhsung Date: Mon, 11 Aug 2025 12:20:30 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=9E=84=EC=9D=98=EC=9D=98=20vo=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SmartAquaViewer/DataAnalisys/AquarDataControl.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs index 1c17ada..59ba69c 100644 --- a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs +++ b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs @@ -59,9 +59,14 @@ namespace SmartAquaViewer.DataAnalisys processedBytes += Encoding.UTF8.GetByteCount(line + Environment.NewLine); double percent = (double)processedBytes / totalBytes * 100.0; - iwaterQuality.OnProgress(percent); - // TODO: 라인 파싱 로직 + WaterQualityVO vo = new(); + vo.PH = 10.5; + vo.Timestamp = DateTime.Now; + iwaterQuality.OnParsed(vo); + + + iwaterQuality.OnProgress(percent); } } From c007128dee4f8562754e62153e8ea996c670fd08 Mon Sep 17 00:00:00 2001 From: hhsung Date: Mon, 11 Aug 2025 13:06:10 +0900 Subject: [PATCH 3/3] =?UTF-8?q?EF=20framework=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SmartAquaViewer/DataAnalisys/AppDbContext.cs | 27 +++++++++++++++++++ .../DataAnalisys/AquarDataControl.cs | 5 +++- SmartAquaViewer/DataAnalisys/IWaterQuality.cs | 1 + .../DataAnalisys/WaterQualityVO.cs | 8 ++++++ SmartAquaViewer/SmartAquaViewer.csproj | 10 +++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 SmartAquaViewer/DataAnalisys/AppDbContext.cs diff --git a/SmartAquaViewer/DataAnalisys/AppDbContext.cs b/SmartAquaViewer/DataAnalisys/AppDbContext.cs new file mode 100644 index 0000000..6ad3331 --- /dev/null +++ b/SmartAquaViewer/DataAnalisys/AppDbContext.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; + +namespace SmartAquaViewer.DataAnalisys +{ + public class AppDbContext : DbContext + { + + //dotnet ef migrations add InitialCreate + //dotnet ef database update + + //dotnet ef migrations add AddMultipleTables + //dotnet ef database update + + + + public DbSet WaterQualities { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // SQL Server 예시 (필요에 맞게 변경) + optionsBuilder.UseSqlServer( + "Server=localhost;Database=SmartAquaDB;Trusted_Connection=True;TrustServerCertificate=True;"); + } + } +} diff --git a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs index 59ba69c..68de21b 100644 --- a/SmartAquaViewer/DataAnalisys/AquarDataControl.cs +++ b/SmartAquaViewer/DataAnalisys/AquarDataControl.cs @@ -9,13 +9,15 @@ namespace SmartAquaViewer.DataAnalisys { - internal class AquarDataControl { private AQUA_DATA_CONTROL_STATE state = AQUA_DATA_CONTROL_STATE.IDLE; private readonly IWaterQuality iwaterQuality; private CancellationTokenSource? cts; + AppDbContext db = new AppDbContext(); + + public AquarDataControl(IWaterQuality iwaterQuality) { this.iwaterQuality = iwaterQuality; @@ -65,6 +67,7 @@ namespace SmartAquaViewer.DataAnalisys vo.Timestamp = DateTime.Now; iwaterQuality.OnParsed(vo); + db.Add(vo); iwaterQuality.OnProgress(percent); } diff --git a/SmartAquaViewer/DataAnalisys/IWaterQuality.cs b/SmartAquaViewer/DataAnalisys/IWaterQuality.cs index a76621e..157ef0b 100644 --- a/SmartAquaViewer/DataAnalisys/IWaterQuality.cs +++ b/SmartAquaViewer/DataAnalisys/IWaterQuality.cs @@ -19,6 +19,7 @@ namespace SmartAquaViewer.DataAnalisys internal interface IWaterQuality { + void OnParsed(WaterQualityVO vo); void OnProgress(double percent); void OnError(string message); void OnCompleted(); diff --git a/SmartAquaViewer/DataAnalisys/WaterQualityVO.cs b/SmartAquaViewer/DataAnalisys/WaterQualityVO.cs index 1f88c8c..5af359b 100644 --- a/SmartAquaViewer/DataAnalisys/WaterQualityVO.cs +++ b/SmartAquaViewer/DataAnalisys/WaterQualityVO.cs @@ -9,9 +9,17 @@ namespace SmartAquaViewer.DataAnalisys using System; + using System.ComponentModel.DataAnnotations; + using System.ComponentModel.DataAnnotations.Schema; + [Table("WaterQuality")] public class WaterQualityVO { + + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // 자동 증가 + public int Id { get; set; } // PK, Auto Increment + /// /// 저수조 번호 /// diff --git a/SmartAquaViewer/SmartAquaViewer.csproj b/SmartAquaViewer/SmartAquaViewer.csproj index e3e33e3..4f73c26 100644 --- a/SmartAquaViewer/SmartAquaViewer.csproj +++ b/SmartAquaViewer/SmartAquaViewer.csproj @@ -8,4 +8,14 @@ true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + +