..
This commit is contained in:
@@ -8,6 +8,8 @@ using System.Media;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
#if SPEECH
|
||||
using Microsoft.Speech.Synthesis;
|
||||
@@ -23,6 +25,14 @@ using System.Drawing;
|
||||
|
||||
namespace Project
|
||||
{
|
||||
public class LastPositionData
|
||||
{
|
||||
public string NodeId { get; set; }
|
||||
public AgvDirection Direction { get; set; }
|
||||
public AGVTurn Turn { get; set; }
|
||||
public DateTime SaveTime { get; set; }
|
||||
}
|
||||
|
||||
public static class PUB
|
||||
{
|
||||
//public static Device.CFlag flag;
|
||||
@@ -633,72 +643,8 @@ namespace Project
|
||||
if (rfidValue.isEmpty()) return null;
|
||||
return _mapNodes.Where(t => t.RfidId.Equals(rfidValue)).FirstOrDefault();
|
||||
}
|
||||
public static List<MapNode> FindByNodeAlias(string alias)
|
||||
{
|
||||
var _mapNodes = PUB._mapCanvas.Nodes;
|
||||
if (_mapNodes == null || _mapNodes.Any() == false) return null;
|
||||
if (alias.isEmpty()) return null;
|
||||
var lst = _mapNodes.Where(t => t.AliasName.Equals(alias));
|
||||
if (lst.Any() == false) return null;
|
||||
return lst.ToList();
|
||||
}
|
||||
public static List<MapNode> FindByNodeType(AGVNavigationCore.Models.MapNode type)
|
||||
{
|
||||
var _mapNodes = PUB._mapCanvas.Nodes;
|
||||
if (_mapNodes == null || _mapNodes.Any() == false) return null;
|
||||
var lst = _mapNodes.Where(t => t.Type.Equals(type));
|
||||
if (lst.Any() == false) return null;
|
||||
return lst.ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// RFID 읽기 시 해당 노드 위치로 AGV 업데이트
|
||||
/// </summary>
|
||||
/// <param name="rfidId">읽은 RFID ID</param>
|
||||
/// <param name="motorDirection">모터 방향 (Forward/Backward)</param>
|
||||
/// <returns>업데이트 성공 여부</returns>
|
||||
public static bool UpdateAGVFromRFID(ushort rfidId, AgvDirection motorDirection = AgvDirection.Forward)
|
||||
{
|
||||
var _mapNodes = PUB._mapCanvas.Nodes;
|
||||
if (_virtualAGV == null || _mapNodes == null) return false;
|
||||
|
||||
|
||||
// RFID에 해당하는 노드 찾기
|
||||
var node = _mapNodes.FirstOrDefault(n => n.RfidId == rfidId);
|
||||
if (node != null)
|
||||
{
|
||||
_virtualAGV.SetPosition(node, motorDirection);
|
||||
RefreshAGVCanvas();
|
||||
|
||||
log.Add($"[AGV] RFID {rfidId} 감지 → 노드 {node.Id} 위치 업데이트 (방향: {motorDirection})");
|
||||
return true;
|
||||
}
|
||||
|
||||
log.Add($"[AGV] RFID {rfidId}에 해당하는 노드를 찾을 수 없음");
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 노드ID로 AGV 위치 업데이트
|
||||
/// </summary>
|
||||
/// <param name="nodeId">노드 ID</param>
|
||||
/// <param name="motorDirection">모터 방향 (Forward/Backward)</param>
|
||||
/// <returns>업데이트 성공 여부</returns>
|
||||
public static bool UpdateAGVToNode(string nodeId, AgvDirection motorDirection = AgvDirection.Forward)
|
||||
{
|
||||
var _mapNodes = PUB._mapCanvas.Nodes;
|
||||
if (_virtualAGV == null || _mapNodes == null) return false;
|
||||
|
||||
var node = _mapNodes.FirstOrDefault(n => n.Id == nodeId);
|
||||
if (node != null)
|
||||
{
|
||||
_virtualAGV.SetPosition(node, motorDirection);
|
||||
RefreshAGVCanvas();
|
||||
|
||||
log.Add($"[AGV] 노드 {nodeId} 위치로 이동 (방향: {motorDirection})");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 모든 로그를 플러시 합니다.
|
||||
@@ -718,10 +664,14 @@ namespace Project
|
||||
/// <param name="direction">새로운 방향</param>
|
||||
public static void UpdateAGVDirection(AgvDirection direction)
|
||||
{
|
||||
if (_virtualAGV == null) return;
|
||||
|
||||
_virtualAGV.CurrentDirection = direction;
|
||||
RefreshAGVCanvas();
|
||||
if (_virtualAGV == null) return;
|
||||
if(_virtualAGV.CurrentDirection != direction)
|
||||
{
|
||||
PUB.log.Add($"[PUB] AGV Direction Change {_virtualAGV.CurrentDirection}->{direction}");
|
||||
_virtualAGV.CurrentDirection = direction;
|
||||
RefreshAGVCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -732,8 +682,13 @@ namespace Project
|
||||
{
|
||||
if (_virtualAGV == null) return;
|
||||
|
||||
_virtualAGV.CurrentState = state;
|
||||
RefreshAGVCanvas();
|
||||
if(_virtualAGV.CurrentState != state)
|
||||
{
|
||||
PUB.log.Add($"[PUB] AGV State Change {_virtualAGV.CurrentState}->{state}");
|
||||
_virtualAGV.CurrentState = state;
|
||||
RefreshAGVCanvas();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -760,6 +715,51 @@ namespace Project
|
||||
}
|
||||
|
||||
|
||||
#region "Starting Position Persistence"
|
||||
|
||||
private static string LastPosFilePath => Path.Combine(UTIL.CurrentPath, "Data", "last_pos.json");
|
||||
|
||||
public static void SaveLastPosition()
|
||||
{
|
||||
if (_virtualAGV == null || _virtualAGV.CurrentNode == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
var data = new LastPositionData
|
||||
{
|
||||
NodeId = _virtualAGV.CurrentNode.Id,
|
||||
Direction = _virtualAGV.CurrentDirection,
|
||||
Turn = _virtualAGV.Turn,
|
||||
SaveTime = DateTime.Now
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
|
||||
File.WriteAllText(LastPosFilePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.AddE($"[PUB] Failed to save last position: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static LastPositionData LoadLastPosition()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(LastPosFilePath)) return null;
|
||||
|
||||
string json = File.ReadAllText(LastPosFilePath);
|
||||
return JsonConvert.DeserializeObject<LastPositionData>(json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log.AddE($"[PUB] Failed to load last position: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user