hhsung_work
HyungJune Kim 10 months ago
commit ea88b86186

@ -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<WaterQualityVO> WaterQualities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// SQL Server 예시 (필요에 맞게 변경)
optionsBuilder.UseSqlServer(
"Server=localhost;Database=SmartAquaDB;Trusted_Connection=True;TrustServerCertificate=True;");
}
}
}

@ -1,15 +1,104 @@
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;
AppDbContext db = new AppDbContext();
public AquarDataControl(IWaterQuality iwaterQuality)
{
this.iwaterQuality = iwaterQuality;
}
public AQUA_DATA_CONTROL_STATE GetState() => state;
/// <summary>
/// 파일 파싱 시작
/// </summary>
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;
// TODO: 라인 파싱 로직
WaterQualityVO vo = new();
vo.PH = 10.5;
vo.Timestamp = DateTime.Now;
iwaterQuality.OnParsed(vo);
db.Add(vo);
iwaterQuality.OnProgress(percent);
}
}
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);
}
});
}
/// <summary>
/// 처리 중단 요청
/// </summary>
public void Cancel()
{
if (state == AQUA_DATA_CONTROL_STATE.PROCESSING && cts != null)
{
cts.Cancel();
}
}
}

@ -0,0 +1,28 @@
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 OnParsed(WaterQualityVO vo);
void OnProgress(double percent);
void OnError(string message);
void OnCompleted();
void OnCancelled();
}
}

@ -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
/// <summary>
/// 저수조 번호
/// </summary>

Loading…
Cancel
Save