using Aurora; using Aurora.Api.Manager; using Aurora.View; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Threading; namespace AuroraCellTracker.Views { public class UCCellTrackerDeviceViewModel : NotifyPropertyChangedBase { #region Property private string _tbReceiveData; public string tbReceiveData { get => _tbReceiveData; set => SetProperty(ref _tbReceiveData, value); } private string _tbSendToPLC; public string tbSendToPLC { get => _tbSendToPLC; set => SetProperty(ref _tbSendToPLC, value); } private string _tbSendToUpper; public string tbSendToUpper { get => _tbSendToUpper; set => SetProperty(ref _tbSendToUpper, value); } private string _tbStatus; public string tbStatus { get => _tbStatus; set => SetProperty(ref _tbStatus, value); } #endregion #region Event Command public ICommand CMD_VisibleChanged { get; } public ICommand CMD_ReceiveData { get; } public ICommand CMD_SendToPLC { get; } public ICommand CMD_SendToUpper { get; } public ICommand CMD_PopUpIPConfig { get; } //public ICommand CMD_Init { get; } #endregion private readonly IDeviceManager _deviceManger; private readonly CellTracker _CellTracker; private readonly CellTrackerConfig _CellTrackerConfig; private DispatcherTimer _CurrentValueUpdateTimer; public UCCellTrackerDeviceViewModel(IAuroraEnvironment aurora, string deviceId) { _deviceManger = aurora.DeviceManager; _CellTracker = _deviceManger[deviceId] as CellTracker; _CellTrackerConfig = _deviceManger.GetDeviceConfig(deviceId) as CellTrackerConfig; CMD_ReceiveData = new DelegateCommand(ReceiveData); CMD_SendToPLC = new DelegateCommand(SendToPLC); CMD_SendToUpper = new DelegateCommand(SendToUpper); CMD_VisibleChanged = new DelegateCommand(VisibleChanged); CMD_PopUpIPConfig = new DelegateCommand(IPConfigUIPopUp); //CMD_Init = new DelegateCommand(DLLInit); if (!_CellTracker.IsSimul) { _CurrentValueUpdateTimer = new DispatcherTimer(); _CurrentValueUpdateTimer.Tick += OnCurrentValueUpdateTimer; _CurrentValueUpdateTimer.Interval = TimeSpan.FromMilliseconds(100); } } private void VisibleChanged(object obj) { bool visible = (bool)obj; if (visible) { _CurrentValueUpdateTimer?.Start(); } else { _CurrentValueUpdateTimer?.Stop(); } } private void OnCurrentValueUpdateTimer(object sender, EventArgs e) { var cellInfoStrings = _CellTracker.GetCellInfoStrings(); if (_CellTrackerConfig.CellTrackerMode == eMode.Callback && !string.IsNullOrWhiteSpace(cellInfoStrings.ReceiveBufferStr)) { tbReceiveData = cellInfoStrings.ReceiveBufferStr; } else if (_CellTrackerConfig.CellTrackerMode == eMode.Queue) { tbReceiveData = cellInfoStrings.ReceiveStrInQueue; } tbStatus = cellInfoStrings.EventContentStr + cellInfoStrings.StatusStr + cellInfoStrings.ErrorCodeStr; } private void ReceiveData(object obj) { _CellTracker.ReceiveData(); } private void IPConfigUIPopUp(object obj) { _CellTracker.SetIPConfig(); } //private void DLLInit(object obj) //{ // _CellTracker.Initialize(_CellTrackerConfig); //} private void SendToPLC(object obj) { //_CellTracker._CellTrackerObj.SendData(tbSendToPLC, false); } private void SendToUpper(object obj) { //_CellTracker.SaveSendingData(tbSendToUpper); //test //_CellTracker._CellTrackerObj.SendData(tbSendToUpper, true); } } }