2026-03-30 17:09:29 +09:00

281 lines
11 KiB
C#

using Aurora;
using Aurora.Api;
using Aurora.Api.Manager;
using Aurora.View;
using System;
using System.Windows.Input;
using System.Windows.Threading;
namespace AuroraLaser
{
public class UCLaserDeviceViewModel : NotifyPropertyChangedBase
{
Logger Log = new Logger("Laser");
#region Xaml Property
// Get Bit Status
private bool _bInitComm;
public bool bInitComm { get => _bInitComm; set => SetProperty(ref _bInitComm, value); }
private bool _bEmission;
public bool bEmission { get => _bEmission; set => SetProperty(ref _bEmission, value); }
private bool _bPilotLaser;
public bool bPilotLaser { get => _bPilotLaser; set => SetProperty(ref _bPilotLaser, value); }
private bool _bLaserReady;
public bool bLaserReady { get => _bLaserReady; set => SetProperty(ref _bLaserReady, value); }
private bool _bLaserWarning;
public bool bLaserWarning { get => _bLaserWarning; set => SetProperty(ref _bLaserWarning, value); }
private bool _bLaserError;
public bool bLaserError { get => _bLaserError; set => SetProperty(ref _bLaserError, value); }
// Set Bit Status
private bool _bStartLaser;
public bool bStartLaser { get => _bStartLaser; set => SetProperty(ref _bStartLaser, value); }
private bool _bLaserOn;
public bool bLaserOn { get => _bLaserOn; set => SetProperty(ref _bLaserOn, value); }
private bool _bPilotLaserOn;
public bool bPilotLaserOn { get => _bPilotLaserOn; set => SetProperty(ref _bPilotLaserOn, value); }
private bool _bResetLaserOn;
public bool bResetLaserOn { get => _bResetLaserOn; set => SetProperty(ref _bResetLaserOn, value); }
// Control Mode
private bool _bGetModeLocal;
public bool bGetModeLocal { get => _bGetModeLocal; set => SetProperty(ref _bGetModeLocal, value); }
private bool _bGetModeRemote;
public bool bGetModeRemote { get => _bGetModeRemote; set => SetProperty(ref _bGetModeRemote, value); }
private bool _bSetModeLocal;
public bool bSetModeLocal { get => _bSetModeLocal; set => SetProperty(ref _bSetModeLocal, value); }
private bool _bSetModeRemote;
public bool bSetModeRemote { get => _bSetModeRemote; set => SetProperty(ref _bSetModeRemote, value); }
// Power
private string _strPowerCenter;
public string strPowerCenter { get => _strPowerCenter; set => SetProperty(ref _strPowerCenter, value); }
private string _strPowerRing;
public string strPowerRing { get => _strPowerRing; set => SetProperty(ref _strPowerRing, value); }
#endregion
#region Envent Command
public ICommand CMD_VisibleChanged { get; }
public ICommand CMD_Connect { get; }
public ICommand CMD_DisConnect { get; }
public ICommand CMD_LaserON { get; }
public ICommand CMD_LaserOFF { get; }
public ICommand CMD_PilotLaserON { get; }
public ICommand CMD_PilotLaserOFF { get; }
private readonly DispatcherTimer _CurrentStatusUpdateTimer;
#endregion
private readonly IDeviceManager _deviceManger;
private readonly Laser laser;
public UCLaserDeviceViewModel(IAuroraEnvironment aurora, string deviceId)
{
_deviceManger = aurora.DeviceManager;
laser = _deviceManger[deviceId] as Laser;
#region DelegateCommand
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
CMD_Connect = new DelegateCommand(Connect);
CMD_DisConnect = new DelegateCommand(DisConnect);
CMD_LaserON = new DelegateCommand(LaserON);
CMD_LaserOFF = new DelegateCommand(LaserOFF);
CMD_PilotLaserON = new DelegateCommand(PilotLaserON);
CMD_PilotLaserOFF = new DelegateCommand(PilotLaserOFF);
#endregion
#region Init Properties
bInitComm = false;
bEmission = false;
bPilotLaser = false;
bLaserReady = false;
bLaserWarning = false;
bLaserError = false;
bStartLaser = false;
bLaserOn = false;
bPilotLaserOn = false;
bResetLaserOn = false;
bGetModeLocal = false;
bGetModeRemote = false;
bSetModeLocal = false;
bSetModeRemote = false;
strPowerCenter = "0.000V";
strPowerRing = "0.000V";
#endregion
if (!laser.IsSimul) {
_CurrentStatusUpdateTimer = new DispatcherTimer();
_CurrentStatusUpdateTimer.Tick += OuCurrentStatusUpdateTimer;
_CurrentStatusUpdateTimer.Interval = TimeSpan.FromMilliseconds(100);
}
}
private void VisibleChanged(object obj)
{
bool visible = (bool)obj;
if (visible) {
_CurrentStatusUpdateTimer?.Start();
} else {
_CurrentStatusUpdateTimer?.Stop();
}
}
private void OuCurrentStatusUpdateTimer(object sender, EventArgs e)
{
if (laser.IsActivated && !laser.IsSimul) {
try {
////var laserUnit = laser._Laser as ILaser;
////bInitComm = laserUnit.GetInitCommunication();
////bEmission = laserUnit.GetEmissionStatus();
////bPilotLaser = laserUnit.GetPilotLaserStatus();
////bLaserReady = laserUnit.GetLaserReady();
////bLaserWarning = laserUnit.GetWariningStatus();
////bLaserError = laserUnit.GetLaserError();
////bStartLaser = laserUnit.GetStartLaserSetBitStatus();
////bLaserOn = laserUnit.GetLaserStartSetBitStatus();
////bPilotLaserOn = laserUnit.GetPilotLaserSetBitStatus();
////bResetLaserOn = laserUnit.GetLaserResetSetBitStatus();
////bGetModeLocal = laserUnit.GetControlMode() == "LOCAL" ? true : false;
////bGetModeRemote = laserUnit.GetControlMode() == "REMOTE" ? true : false;
////bSetModeLocal = !laserUnit.GetControlModeSetStatus(); //local == false, remote == true;
////bSetModeRemote = laserUnit.GetControlModeSetStatus(); //local == false, remote == true;
////strPowerCenter = laserUnit.GetPowerCenter();
////strPowerRing = laserUnit.GetPowerRing();
////var laserUnit = laser._Laser as ILaser;
//bInitComm = laser.GetInitCommunication();
//bEmission = laser.GetEmissionStatus();
//bPilotLaser = laser.GetPilotLaserStatus();
//bLaserReady = laser.GetLaserReady();
//bLaserWarning = laser.GetWariningStatus();
//bLaserError = laser.GetLaserError();
//bStartLaser = laser.GetStartLaserSetBitStatus();
//bLaserOn = laser.GetLaserStartSetBitStatus();
//bPilotLaserOn = laser.GetPilotLaserSetBitStatus();
//bResetLaserOn = laser.GetLaserResetSetBitStatus();
//bGetModeLocal = laser.GetControlMode() == "LOCAL" ? true : false;
//bGetModeRemote = laser.GetControlMode() == "REMOTE" ? true : false;
//bSetModeLocal = !laser.GetControlModeSetStatus(); //local == false, remote == true;
//bSetModeRemote = laser.GetControlModeSetStatus(); //local == false, remote == true;
//strPowerCenter = laser.GetPowerCenter();
//strPowerRing = laser.GetPowerRing();
} catch (Exception ex) {
Log.E(string.Format($"[OuCurrentStatusUpdateTimer]{ex.Message}"));
}
} else {
_CurrentStatusUpdateTimer?.Stop();
Log.D(string.Format("[OuCurrentStatusUpdateTimer]Check Scanner device Connect"));
}
}
private void Connect(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.Open(laser.Config);
laser.Open(laser.Config);
}
}
private void DisConnect(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.DisConnect();
laser.DisConnect();
}
}
private void LaserON(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.LaserOn();
laser.LaserOn();
}
}
private void LaserOFF(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.LaserOff();
laser.LaserOff();
}
}
private void PilotLaserON(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.PilotLaserOn();
laser.PilotLaserOn();
}
}
private void PilotLaserOFF(object obj)
{
if (laser.IsSimul)
return;
if (laser != null) {
//laser._Laser.PilotLaserOff();
laser.PilotLaserOff();
}
}
/* ///// OPCUA
/// Browser Test
ILaser iLaser = null;
laser.GetLaserDevice(ref iLaser);
LaserTruFiber laserTruFiber = iLaser as LaserTruFiber;
/// ReadNode 정리
var fields = typeof(csReadNodeNo).GetFields(BindingFlags.Public | BindingFlags.Static);
Dictionary<string, int> readNodeDictionary = new Dictionary<string, int>();
foreach (var field in fields) {
if (field.FieldType == typeof(int)) {
string name = field.Name;
int value = (int)field.GetValue(null);
Console.WriteLine($"{name} = {value}");
readNodeDictionary.Add(name, value);
}
}
/// WriteNode 정리
fields = typeof(csReadNodeNo).GetFields(BindingFlags.Public | BindingFlags.Static);
Dictionary<string, int> writeNodeDictionary = new Dictionary<string, int>();
foreach (var field in fields) {
if (field.FieldType == typeof(int)) {
string name = field.Name;
int value = (int)field.GetValue(null);
Console.WriteLine($"{name} = {value}");
writeNodeDictionary.Add(name, value);
}
}
/// Browse와 Node id를 적어주는 부분
List<string> strings = new List<string>();
NodeId startNode = new NodeId("ns=2;s=Production.MPI-Slot01.Output");
laserTruFiber.ExplorerNodes(startNode);
string str = null;
if (str.Contains('.')) {
strings.Add(str);
}
/// 매핑해주는 부분 (index와 Node Id를 매칭시켜주는 부분)
Dictionary<string, int> result = new Dictionary<string, int>();
// 매칭 및 저장
foreach (var listStr in strings) {
foreach (var kvp in readNodeDictionary) {
if (listStr.Contains(kvp.Key)) {
result[listStr] = kvp.Value;
}
}
}
*/
}
}