2026-06-05 13:44:39 +09:00

348 lines
12 KiB
C#

using Aurora;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
namespace AuroraLaser.Trupulse.Views
{
public class UCTruPulseManualViewModel : NotifyPropertyChangedBase
{
private Laser _laser;
private TruPulse _laserCore;
private readonly DispatcherTimer _monitorTimer;
public ICommand CMD_RestartPulseGenerator { get; }
public ICommand CMD_LogClear { get; }
public ICommand CMD_GetStatusLines { get; }
public ICommand CMD_GetInterfaceMode { get; }
public ICommand CMD_GetBurstLength { get; }
public ICommand CMD_GetPumpDutyFactor { get; }
public ICommand CMD_GetAnalogSig { get; }
public ICommand CMD_GetTemperatures { get; }
public ICommand CMD_GetCurrents { get; }
public ICommand CMD_GetOperatingHours { get; }
public ICommand CMD_GetFirmware { get; }
public ICommand CMD_GetExternalTriggerRate { get; }
public string LogText { get => _logText; set => SetProperty(ref _logText, value); }
private string _logText;
public int Mode {
get => _Mode;
set {
if (SetProperty(ref _Mode, value)) {
try {
_laserCore.SetInterfaceMode((byte)_Mode);
Log($"interfaceMode: {_laserCore.GetInterfaceMode()}");
} catch (Exception ex) {
Log("InterfaceMode error: " + ex.Message);
}
}
}
}
private int _Mode;
public int WaveForm {
get => _WaveForm;
set {
if (SetProperty(ref _WaveForm, value)) {
try {
_laserCore.SetPulseWaveform((byte)_WaveForm);
Log($"waveForm: {_laserCore.GetPulseWaveform()}");
} catch (Exception ex) {
Log("waveForm error: " + ex.Message);
}
}
}
}
private int _WaveForm;
public bool CwPulse {
get => _CwPulse;
set {
if (SetProperty(ref _CwPulse, value)) {
try {
if (_CwPulse) {
_laserCore.CwPulseOn();
Log("CwPulseOn");
} else {
_laserCore.CwPulseOff();
Log("CwPulseOff");
}
} catch (Exception ex) {
Log("CwPulse error: " + ex.Message);
}
}
}
}
private bool _CwPulse;
public bool LaserDisable {
get => _LaserDisable;
set {
if (SetProperty(ref _LaserDisable, value)) {
try {
_laserCore.LaserDisableFunc(_LaserDisable);
Log($"Laser Disable {_LaserDisable}");
}
catch (Exception ex) {
Log("Laser error: " + ex.Message);
}
}
}
}
private bool _LaserDisable;
public bool LaserEnable {
get => _LaserEnable;
set {
if (SetProperty(ref _LaserEnable, value)) {
try {
if (_LaserEnable) {
_laserCore.LaserOn();
Log("LaserOn");
} else {
_laserCore.LaserOff();
Log("LaserOff");
}
} catch (Exception ex) {
Log("Laser error: " + ex.Message);
}
}
}
}
private bool _LaserEnable;
public bool PilotLaser {
get => _PilotLaser;
set {
if (SetProperty(ref _PilotLaser, value)) {
try {
if (_PilotLaser) {
_laserCore.PilotLaserOn();
Log("PilotOn");
} else {
_laserCore.PilotLaserOff();
Log("PilotOff");
}
} catch (Exception ex) {
Log("Pilot error: " + ex.Message);
}
}
}
}
private bool _PilotLaser;
public int ActiveCurrent {
get => _ActiveCurrent;
set {
if (SetProperty(ref _ActiveCurrent, value)) {
try {
_laserCore.SetAnalogueSetPoint(0, (ushort)_ActiveCurrent);
foreach (var kv in _laserCore.GetLaserCurrents())
Log($"{kv.Key}: {kv.Value:F1} A");
} catch (Exception ex) {
Log("Apply currents error: " + ex.Message);
}
}
}
}
private int _ActiveCurrent;
public int SimmerCurrent {
get => _SimmerCurrent;
set {
if (SetProperty(ref _SimmerCurrent, value)) {
try {
_laserCore.SetAnalogueSetPoint(1, (ushort)_SimmerCurrent);
Log($"Applied: Simmer={_SimmerCurrent} mA");
} catch (Exception ex) {
Log("Apply currents error: " + ex.Message);
}
}
}
}
private int _SimmerCurrent;
public int PulseRate {
get => _PulseRate;
set {
if (SetProperty(ref _PulseRate, value)) {
try {
_laserCore.SetPulseRate((ushort)_PulseRate);
Log($"Applied: PulseRate={_PulseRate} ");
} catch (Exception ex) {
Log("Apply PulseRate error: " + ex.Message);
}
}
}
}
private int _PulseRate;
public int PulseBurstLength {
get => _PulseBurstLength;
set {
if (SetProperty(ref _PulseBurstLength, value)) {
try {
_laserCore.SetPulseBurstLength((ushort)_PulseBurstLength);
Log($"Applied: _PulseBurstLength={_PulseBurstLength} ");
} catch (Exception ex) {
Log("Apply _PulseBurstLength error: " + ex.Message);
}
}
}
}
private int _PulseBurstLength;
public int PumpDutyFactor {
get => _PumpDutyFactor;
set {
if (SetProperty(ref _PumpDutyFactor, value)) {
try {
_laserCore.SetPumpDutyFactor((ushort)_PumpDutyFactor);
Log($"Applied: PumpDutyFactor={_PumpDutyFactor} ");
}
catch (Exception ex) {
Log("Apply PumpDutyFactor error: " + ex.Message);
}
}
}
}
private int _PumpDutyFactor;
public UCTruPulseManualViewModel(IAuroraEnvironment env, string deviceId) {
_laser = env.DeviceManager[deviceId] as Laser;
_laserCore = _laser.getCore() as TruPulse;
_laser.updateCore += (s, e) => _laserCore = _laser.getCore() as TruPulse;
CMD_RestartPulseGenerator = new DelegateCommand(OnResetPulseGenerator);
CMD_LogClear = new DelegateCommand((obj) => LogText = "");
CMD_GetStatusLines = new DelegateCommand(OnGetStatusLines);
CMD_GetBurstLength = new DelegateCommand((obj) => { fnGetParameterI(_laserCore.GetPulseBurstLength); });
CMD_GetPumpDutyFactor = new DelegateCommand((obj) => { fnGetParameterShort(_laserCore.GetPumpDutyFactor); });
CMD_GetAnalogSig = new DelegateCommand(OnGetAnalogSig);
CMD_GetInterfaceMode = new DelegateCommand((obj) => { fnGetParameterB(_laserCore.GetInterfaceMode); });
CMD_GetTemperatures = new DelegateCommand(OnGetTemperatures);
CMD_GetCurrents = new DelegateCommand(OnGetCurrents);
CMD_GetOperatingHours = new DelegateCommand((obj) => { fnGetParameterD(_laserCore.GetOperatingHours); });
CMD_GetFirmware = new DelegateCommand((obj) => { fnGetParameterS(_laserCore.GetFirmwareDetails); });
CMD_GetExternalTriggerRate = new DelegateCommand((obj) => { fnGetParameterI(_laserCore.GetExternalTriggerRateHz); });
}
private void OnGetAnalogSig(object obj)
{
try {
var temps = _laserCore.GetAnalogueSignal(0);
var channel = temps.channel == 0 ? "active" : "simmer";
Log($"{channel} Current: {temps.value} ");
} catch (Exception ex) {
Log("Temp error: " + ex.Message);
}
}
private void OnResetPulseGenerator(object obj)
{
try {
_laserCore.RestartPulseGenerator();
Log("Pulse Generator restarted");
} catch (Exception ex) {
Log("RestartPG error: " + ex.Message);
}
}
private void OnGetStatusLines(object obj)
{
foreach (var reason in _laserCore.AlarmReasons) {
Log($"AlarmReason={reason}");
}
}
private void OnGetTemperatures(object obj)
{
try {
var temps = _laserCore.GetLaserTemperatures();
if (temps == null) {
Log("Temp query failed"); return;
}
foreach (var kv in temps)
Log($"{kv.Key}: {kv.Value:F1} °C");
} catch (Exception ex) {
Log("Temp error: " + ex.Message);
}
}
private void OnGetCurrents(object obj)
{
try {
var currents = _laserCore.GetLaserCurrents();
if (currents != null) {
var sb = new StringBuilder();
foreach (var kv in currents)
Log($"{kv.Key}: {kv.Value:F1} ");
}
} catch (Exception ex) {
Log("Temp error: " + ex.Message);
}
}
private void Log(string msg) => LogText += $"[{DateTime.Now:HH:mm:ss}] {msg}\r\n";
public delegate uint GetParamInt();
public delegate ushort GetParamShort();
public delegate double GetParamDouble();
public delegate string GetParamString();
public delegate byte GetParamByte();
public void fnGetParameterI(GetParamInt getFunc)
{
try {
var ret = getFunc();
Log($"{getFunc.Method.Name}: {ret}");
} catch (Exception ex) {
Log($"{getFunc.Method.Name} error: " + ex.Message);
}
}
public void fnGetParameterShort(GetParamShort getFunc)
{
try {
var ret = getFunc();
Log($"{getFunc.Method.Name}: {ret}");
} catch (Exception ex) {
Log($"{getFunc.Method.Name} error: " + ex.Message);
}
}
public void fnGetParameterD(GetParamDouble getFunc)
{
try {
var ret = getFunc();
Log($"{getFunc.Method.Name}: {ret}");
} catch (Exception ex) {
Log($"{getFunc.Method.Name} error: " + ex.Message);
}
}
public void fnGetParameterS(GetParamString getFunc)
{
try {
var ret = getFunc();
Log($"{getFunc.Method.Name}: {ret}");
} catch (Exception ex) {
Log($"{getFunc.Method.Name} error: " + ex.Message);
}
}
public void fnGetParameterB(GetParamByte getFunc)
{
try {
var ret = getFunc();
Log($"{getFunc.Method.Name}: {ret}");
} catch (Exception ex) {
Log($"{getFunc.Method.Name} error: " + ex.Message);
}
}
}
}