using Aurora;
using Aurora.Api;
using Aurora.View;
using AuroraChiller;
using AuroraChiller.SMC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace Scriber.Views.Devices
{
public class UCChillerMonitoringViewModel : NotifyPropertyChangedBase
{
private bool _IsRunningChiller = false;
public bool IsRunningChiller { get => _IsRunningChiller; set => SetProperty(ref _IsRunningChiller, value); }
private double _TargetTemperature;
public double TargetTemperature { get => _TargetTemperature; set => SetProperty(ref _TargetTemperature, value); }
private double _CurrentTemperature;
public double CurrentTemperature { get => _CurrentTemperature; set => SetProperty(ref _CurrentTemperature, value); }
private eTemperatureStatus _TemperatureStatus;
public eTemperatureStatus TemperatureStatus { get => _TemperatureStatus; set => SetProperty(ref _TemperatureStatus, value); }
private int _LastCoolantRemainTime = 0;
public int LastCoolantRemainTime { get => _LastCoolantRemainTime; set => SetProperty(ref _LastCoolantRemainTime, value); }
private int _MainBodyRemainTime = 0;
public int MainBodyRemainTime { get => _MainBodyRemainTime; set => SetProperty(ref _MainBodyRemainTime, value); }
private int _LastCoolantActualWidth;
public int LastCoolantActualWidth { get => _LastCoolantActualWidth; set => SetProperty(ref _LastCoolantActualWidth, value); }
private int _LastCoolantRecWidth;
public int LastCoolantRecWidth { get => _LastCoolantRecWidth; set => SetProperty(ref _LastCoolantRecWidth, value); }
private SolidColorBrush _LastCoolantColor;
public SolidColorBrush LastCoolantColor { get => _LastCoolantColor; set => SetProperty(ref _LastCoolantColor, value); }
private int _MainBodyActualWidth;
public int MainBodyActualWidth { get => _MainBodyActualWidth; set => SetProperty(ref _MainBodyActualWidth, value); }
private int _MainBodyRecWidth;
public int MainBodyRecWidth { get => _MainBodyRecWidth; set => SetProperty(ref _MainBodyRecWidth, value); }
private SolidColorBrush _MainBodyColor;
public SolidColorBrush MainBodyColor { get => _MainBodyColor; set => SetProperty(ref _MainBodyColor, value); }
private bool _Alarm1St1;
private bool _Alarm1St2;
private bool _Alarm1St3;
private bool _Alarm1St4;
private bool _Alarm1St5;
private bool _Alarm1St6;
private bool _Alarm1St7;
private bool _Alarm1St8;
private bool _Alarm1St9;
private bool _Alarm1St10;
private bool _Alarm1St11;
private bool _Alarm1St12;
private bool _Alarm1St13;
private bool _Alarm1St14;
private bool _Alarm1St15;
private bool _Alarm1St16;
private bool _Alarm2St1;
private bool _Alarm2St2;
private bool _Alarm2St3;
private bool _Alarm2St4;
private bool _Alarm2St5;
private bool _Alarm2St6;
private bool _Alarm2St7;
private bool _Alarm2St8;
private bool _Alarm2St9;
private bool _Alarm2St10;
private bool _Alarm2St11;
private bool _Alarm2St12;
private bool _Alarm2St13;
private bool _Alarm2St14;
private bool _Alarm2St15;
private bool _Alarm2St16;
private bool _Alarm3St1;
private bool _Alarm3St2;
private bool _Alarm3St3;
private bool _Alarm3St4;
private bool _Alarm3St5;
private bool _Alarm3St6;
private bool _Alarm3St7;
private bool _Alarm3St8;
private bool _Alarm3St9;
private bool _Alarm3St10;
private bool _Alarm3St11;
private bool _Alarm3St12;
private bool _Alarm3St13;
private bool _Alarm3St14;
private bool _Alarm3St15;
private bool _Alarm3St16;
private bool _Alarm4St1;
private bool _Alarm4St2;
private bool _Alarm4St3;
private bool _Alarm4St4;
private bool _Alarm4St5;
private bool _Alarm4St6;
private bool _Alarm4St7;
private bool _Alarm4St8;
private bool _Alarm4St9;
private bool _Alarm4St10;
private bool _Alarm4St11;
private bool _Alarm4St12;
private bool _Alarm4St13;
private bool _Alarm4St14;
private bool _Alarm4St15;
private bool _Alarm4St16;
///
/// ///////////////////////////////////////////////////////////////////////////////////////
///
public bool Alarm1St1 { get => _Alarm1St1; set => SetProperty(ref _Alarm1St1, value); }
public bool Alarm1St2 { get => _Alarm1St2; set => SetProperty(ref _Alarm1St2, value); }
public bool Alarm1St3 { get => _Alarm1St3; set => SetProperty(ref _Alarm1St3, value); }
public bool Alarm1St4 { get => _Alarm1St4; set => SetProperty(ref _Alarm1St4, value); }
public bool Alarm1St5 { get => _Alarm1St5; set => SetProperty(ref _Alarm1St5, value); }
public bool Alarm1St6 { get => _Alarm1St6; set => SetProperty(ref _Alarm1St6, value); }
public bool Alarm1St7 { get => _Alarm1St7; set => SetProperty(ref _Alarm1St7, value); }
public bool Alarm1St8 { get => _Alarm1St8; set => SetProperty(ref _Alarm1St8, value); }
public bool Alarm1St9 { get => _Alarm1St9; set => SetProperty(ref _Alarm1St9, value); }
public bool Alarm1St10 { get => _Alarm1St10; set => SetProperty(ref _Alarm1St10, value); }
public bool Alarm1St11 { get => _Alarm1St11; set => SetProperty(ref _Alarm1St11, value); }
public bool Alarm1St12 { get => _Alarm1St12; set => SetProperty(ref _Alarm1St12, value); }
public bool Alarm1St13 { get => _Alarm1St13; set => SetProperty(ref _Alarm1St13, value); }
public bool Alarm1St14 { get => _Alarm1St14; set => SetProperty(ref _Alarm1St14, value); }
public bool Alarm1St15 { get => _Alarm1St15; set => SetProperty(ref _Alarm1St15, value); }
public bool Alarm1St16 { get => _Alarm1St16; set => SetProperty(ref _Alarm1St16, value); }
public bool Alarm2St1 { get => _Alarm2St1; set => SetProperty(ref _Alarm2St1, value); }
public bool Alarm2St2 { get => _Alarm2St2; set => SetProperty(ref _Alarm2St2, value); }
public bool Alarm2St3 { get => _Alarm2St3; set => SetProperty(ref _Alarm2St3, value); }
public bool Alarm2St4 { get => _Alarm2St4; set => SetProperty(ref _Alarm2St4, value); }
public bool Alarm2St5 { get => _Alarm2St5; set => SetProperty(ref _Alarm2St5, value); }
public bool Alarm2St6 { get => _Alarm2St6; set => SetProperty(ref _Alarm2St6, value); }
public bool Alarm2St7 { get => _Alarm2St7; set => SetProperty(ref _Alarm2St7, value); }
public bool Alarm2St8 { get => _Alarm2St8; set => SetProperty(ref _Alarm2St8, value); }
public bool Alarm2St9 { get => _Alarm2St9; set => SetProperty(ref _Alarm2St9, value); }
public bool Alarm2St10 { get => _Alarm2St10; set => SetProperty(ref _Alarm2St10, value); }
public bool Alarm2St11 { get => _Alarm2St11; set => SetProperty(ref _Alarm2St11, value); }
public bool Alarm2St12 { get => _Alarm2St12; set => SetProperty(ref _Alarm2St12, value); }
public bool Alarm2St13 { get => _Alarm2St13; set => SetProperty(ref _Alarm2St13, value); }
public bool Alarm2St14 { get => _Alarm2St14; set => SetProperty(ref _Alarm2St14, value); }
public bool Alarm2St15 { get => _Alarm2St15; set => SetProperty(ref _Alarm2St15, value); }
public bool Alarm2St16 { get => _Alarm2St16; set => SetProperty(ref _Alarm2St16, value); }
public bool Alarm3St1 { get => _Alarm3St1; set => SetProperty(ref _Alarm3St1, value); }
public bool Alarm3St2 { get => _Alarm3St2; set => SetProperty(ref _Alarm3St2, value); }
public bool Alarm3St3 { get => _Alarm3St3; set => SetProperty(ref _Alarm3St3, value); }
public bool Alarm3St4 { get => _Alarm3St4; set => SetProperty(ref _Alarm3St4, value); }
public bool Alarm3St5 { get => _Alarm3St5; set => SetProperty(ref _Alarm3St5, value); }
public bool Alarm3St6 { get => _Alarm3St6; set => SetProperty(ref _Alarm3St6, value); }
public bool Alarm3St7 { get => _Alarm3St7; set => SetProperty(ref _Alarm3St7, value); }
public bool Alarm3St8 { get => _Alarm3St8; set => SetProperty(ref _Alarm3St8, value); }
public bool Alarm3St9 { get => _Alarm3St9; set => SetProperty(ref _Alarm3St9, value); }
public bool Alarm3St10 { get => _Alarm3St10; set => SetProperty(ref _Alarm3St10, value); }
public bool Alarm3St11 { get => _Alarm3St11; set => SetProperty(ref _Alarm3St11, value); }
public bool Alarm3St12 { get => _Alarm3St12; set => SetProperty(ref _Alarm3St12, value); }
public bool Alarm3St13 { get => _Alarm3St13; set => SetProperty(ref _Alarm3St13, value); }
public bool Alarm3St14 { get => _Alarm3St14; set => SetProperty(ref _Alarm3St14, value); }
public bool Alarm3St15 { get => _Alarm3St15; set => SetProperty(ref _Alarm3St15, value); }
public bool Alarm3St16 { get => _Alarm3St16; set => SetProperty(ref _Alarm3St16, value); }
public bool Alarm4St1 { get => _Alarm4St1; set => SetProperty(ref _Alarm4St1, value); }
public bool Alarm4St2 { get => _Alarm4St2; set => SetProperty(ref _Alarm4St2, value); }
public bool Alarm4St3 { get => _Alarm4St3; set => SetProperty(ref _Alarm4St3, value); }
public bool Alarm4St4 { get => _Alarm4St4; set => SetProperty(ref _Alarm4St4, value); }
public bool Alarm4St5 { get => _Alarm4St5; set => SetProperty(ref _Alarm4St5, value); }
public bool Alarm4St6 { get => _Alarm4St6; set => SetProperty(ref _Alarm4St6, value); }
public bool Alarm4St7 { get => _Alarm4St7; set => SetProperty(ref _Alarm4St7, value); }
public bool Alarm4St8 { get => _Alarm4St8; set => SetProperty(ref _Alarm4St8, value); }
public bool Alarm4St9 { get => _Alarm4St9; set => SetProperty(ref _Alarm4St9, value); }
public bool Alarm4St10 { get => _Alarm4St10; set => SetProperty(ref _Alarm4St10, value); }
public bool Alarm4St11 { get => _Alarm4St11; set => SetProperty(ref _Alarm4St11, value); }
public bool Alarm4St12 { get => _Alarm4St12; set => SetProperty(ref _Alarm4St12, value); }
public bool Alarm4St13 { get => _Alarm4St13; set => SetProperty(ref _Alarm4St13, value); }
public bool Alarm4St14 { get => _Alarm4St14; set => SetProperty(ref _Alarm4St14, value); }
public bool Alarm4St15 { get => _Alarm4St15; set => SetProperty(ref _Alarm4St15, value); }
public bool Alarm4St16 { get => _Alarm4St16; set => SetProperty(ref _Alarm4St16, value); }
///
/// ///////////////////////////////////////////////////////////////////////////////////////
///
public ICommand CMD_VisibleChanged { get; }
public ICommand CMD_GridLastCoolantActualWidthChanged { get; }
public ICommand CMD_GridMainBodyActualWidthChanged { get; }
public int DeviceNo => _SmcChiller.DeviceNo;
private readonly DispatcherTimer MonitorTimer;
private readonly SmcChiller _SmcChiller;
private SolidColorBrush _LightGreen = new SolidColorBrush(Colors.LightGreen);
private SolidColorBrush _Yellow = new SolidColorBrush(Colors.Yellow);
private SolidColorBrush _Orange = new SolidColorBrush(Colors.Orange);
private SolidColorBrush _Red = new SolidColorBrush(Colors.Red);
private readonly IAuroraEnvironment _Aurora;
private ChillerSetting _LaserManageSetting;
private readonly string _DeviceId;
private readonly string _SettingId;
public UCChillerMonitoringViewModel(IAuroraEnvironment aurora, string deviceId, string settingId)
{
this._Aurora = aurora;
_DeviceId = deviceId;
_SmcChiller = aurora.DeviceManager[_DeviceId] as SmcChiller;
_SettingId = settingId;
MonitorTimer = new DispatcherTimer();
MonitorTimer.Tick += OnMonitoring;
MonitorTimer.Interval = TimeSpan.FromMilliseconds(1000);
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
CMD_GridLastCoolantActualWidthChanged = new DelegateCommand(GridLastCoolantActualWidthChanged);
CMD_GridMainBodyActualWidthChanged = new DelegateCommand(GridMainBodyActualWidthChanged);
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_LANGUAGE_CHANGED).
Select(msg => msg.Param.ToString().Split('-').First().ToLower()).//Where(lan => lan != _currLanguage).
ObserveOnDispatcher().Subscribe(lan => UpdateLanguage(lan));
Observable.Merge(
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED),
aurora.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_DEACTIVATED)
).Select(msg => msg.Param as string
).Where(x => x == deviceId
).ObserveOnDispatcher(
).Subscribe(
x => OnDeviceActiveChanged()
);
}
private void VisibleChanged(object obj)
{
bool visible = (bool)obj;
if (visible) {
MonitorTimer.Start();
} else {
MonitorTimer.Stop();
}
}
private void UpdateLanguage(string lan)
{
// 반대로 바꿔줌
IsRunningChiller = !(IsRunningChiller);
// 원래대로 바꿔줌
IsRunningChiller = !(IsRunningChiller);
}
private void GridLastCoolantActualWidthChanged(object obj)
{
double actualWidth = (double)obj;
LastCoolantActualWidth = (int)actualWidth;
}
private void GridMainBodyActualWidthChanged(object obj)
{
double actualWidth = (double)obj;
MainBodyActualWidth = (int)actualWidth;
}
private void OnDeviceActiveChanged()
{
if (_SmcChiller.IsActivated == true) {
IsRunningChiller = _SmcChiller.IsConnected();
} else {
IsRunningChiller = false;
}
}
private void OnMonitoring(object sender, EventArgs e)
{
SetFilterRemainDate();
if (!_SmcChiller.IsActivated) {
return;
}
IsRunningChiller = _SmcChiller.IsConnected();
var monitoringItems = _SmcChiller.GetMonitoringItems() as SmcChillerMonitoringItems;
TargetTemperature = monitoringItems.TargetTemp;
CurrentTemperature = monitoringItems.CurrentTemp;
//TargetTemperature = monitoringItems.TargetTempCH1;
//CurrentTemperature = monitoringItems.CurrentTempCH1;
TemperatureStatus = monitoringItems.TempStatus;
Alarm1St1 = ((monitoringItems.AlamSt1 & (1 << 0)) > 0) ? true : false;
Alarm1St2 = ((monitoringItems.AlamSt1 & (1 << 1)) > 0) ? true : false;
Alarm1St3 = ((monitoringItems.AlamSt1 & (1 << 2)) > 0) ? true : false;
Alarm1St4 = ((monitoringItems.AlamSt1 & (1 << 3)) > 0) ? true : false;
Alarm1St5 = ((monitoringItems.AlamSt1 & (1 << 4)) > 0) ? true : false;
Alarm1St6 = ((monitoringItems.AlamSt1 & (1 << 5)) > 0) ? true : false;
Alarm1St7 = ((monitoringItems.AlamSt1 & (1 << 6)) > 0) ? true : false;
Alarm1St8 = ((monitoringItems.AlamSt1 & (1 << 7)) > 0) ? true : false;
Alarm1St9 = ((monitoringItems.AlamSt1 & (1 << 8)) > 0) ? true : false;
Alarm1St10 = ((monitoringItems.AlamSt1 & (1 << 9)) > 0) ? true : false;
Alarm1St11 = ((monitoringItems.AlamSt1 & (1 << 10)) > 0) ? true : false;
Alarm1St12 = ((monitoringItems.AlamSt1 & (1 << 11)) > 0) ? true : false;
Alarm1St13 = ((monitoringItems.AlamSt1 & (1 << 12)) > 0) ? true : false;
Alarm1St14 = ((monitoringItems.AlamSt1 & (1 << 13)) > 0) ? true : false;
Alarm1St15 = ((monitoringItems.AlamSt1 & (1 << 14)) > 0) ? true : false;
Alarm1St16 = ((monitoringItems.AlamSt1 & (1 << 15)) > 0) ? true : false;
Alarm2St1 = ((monitoringItems.AlamSt2 & (1 << 0)) > 0) ? true : false;
Alarm2St2 = ((monitoringItems.AlamSt2 & (1 << 1)) > 0) ? true : false;
Alarm2St3 = ((monitoringItems.AlamSt2 & (1 << 2)) > 0) ? true : false;
Alarm2St4 = ((monitoringItems.AlamSt2 & (1 << 3)) > 0) ? true : false;
Alarm2St5 = ((monitoringItems.AlamSt2 & (1 << 4)) > 0) ? true : false;
Alarm2St6 = ((monitoringItems.AlamSt2 & (1 << 5)) > 0) ? true : false;
Alarm2St7 = ((monitoringItems.AlamSt2 & (1 << 6)) > 0) ? true : false;
Alarm2St8 = ((monitoringItems.AlamSt2 & (1 << 7)) > 0) ? true : false;
Alarm2St9 = ((monitoringItems.AlamSt2 & (1 << 8)) > 0) ? true : false;
Alarm2St10 = ((monitoringItems.AlamSt2 & (1 << 9)) > 0) ? true : false;
Alarm2St11 = ((monitoringItems.AlamSt2 & (1 << 10)) > 0) ? true : false;
Alarm2St12 = ((monitoringItems.AlamSt2 & (1 << 11)) > 0) ? true : false;
Alarm2St13 = ((monitoringItems.AlamSt2 & (1 << 12)) > 0) ? true : false;
Alarm2St14 = ((monitoringItems.AlamSt2 & (1 << 13)) > 0) ? true : false;
Alarm2St15 = ((monitoringItems.AlamSt2 & (1 << 14)) > 0) ? true : false;
Alarm2St16 = ((monitoringItems.AlamSt2 & (1 << 15)) > 0) ? true : false;
Alarm3St1 = ((monitoringItems.AlamSt3 & (1 << 0)) > 0) ? true : false;
Alarm3St2 = ((monitoringItems.AlamSt3 & (1 << 1)) > 0) ? true : false;
Alarm3St3 = ((monitoringItems.AlamSt3 & (1 << 2)) > 0) ? true : false;
Alarm3St4 = ((monitoringItems.AlamSt3 & (1 << 3)) > 0) ? true : false;
Alarm3St5 = ((monitoringItems.AlamSt3 & (1 << 4)) > 0) ? true : false;
Alarm3St6 = ((monitoringItems.AlamSt3 & (1 << 5)) > 0) ? true : false;
Alarm3St7 = ((monitoringItems.AlamSt3 & (1 << 6)) > 0) ? true : false;
Alarm3St8 = ((monitoringItems.AlamSt3 & (1 << 7)) > 0) ? true : false;
Alarm3St9 = ((monitoringItems.AlamSt3 & (1 << 8)) > 0) ? true : false;
Alarm3St10 = ((monitoringItems.AlamSt3 & (1 << 9)) > 0) ? true : false;
Alarm3St11 = ((monitoringItems.AlamSt3 & (1 << 10)) > 0) ? true : false;
Alarm3St12 = ((monitoringItems.AlamSt3 & (1 << 11)) > 0) ? true : false;
Alarm3St13 = ((monitoringItems.AlamSt3 & (1 << 12)) > 0) ? true : false;
Alarm3St14 = ((monitoringItems.AlamSt3 & (1 << 13)) > 0) ? true : false;
Alarm3St15 = ((monitoringItems.AlamSt3 & (1 << 14)) > 0) ? true : false;
Alarm3St16 = ((monitoringItems.AlamSt3 & (1 << 15)) > 0) ? true : false;
Alarm4St1 = ((monitoringItems.AlamSt4 & (1 << 0)) > 0) ? true : false;
Alarm4St2 = ((monitoringItems.AlamSt4 & (1 << 1)) > 0) ? true : false;
Alarm4St3 = ((monitoringItems.AlamSt4 & (1 << 2)) > 0) ? true : false;
Alarm4St4 = ((monitoringItems.AlamSt4 & (1 << 3)) > 0) ? true : false;
Alarm4St5 = ((monitoringItems.AlamSt4 & (1 << 4)) > 0) ? true : false;
Alarm4St6 = ((monitoringItems.AlamSt4 & (1 << 5)) > 0) ? true : false;
Alarm4St7 = ((monitoringItems.AlamSt4 & (1 << 6)) > 0) ? true : false;
Alarm4St8 = ((monitoringItems.AlamSt4 & (1 << 7)) > 0) ? true : false;
Alarm4St9 = ((monitoringItems.AlamSt4 & (1 << 8)) > 0) ? true : false;
Alarm4St10 = ((monitoringItems.AlamSt4 & (1 << 9)) > 0) ? true : false;
Alarm4St11 = ((monitoringItems.AlamSt4 & (1 << 10)) > 0) ? true : false;
Alarm4St12 = ((monitoringItems.AlamSt4 & (1 << 11)) > 0) ? true : false;
Alarm4St13 = ((monitoringItems.AlamSt4 & (1 << 12)) > 0) ? true : false;
Alarm4St14 = ((monitoringItems.AlamSt4 & (1 << 13)) > 0) ? true : false;
Alarm4St15 = ((monitoringItems.AlamSt4 & (1 << 14)) > 0) ? true : false;
Alarm4St16 = ((monitoringItems.AlamSt4 & (1 << 15)) > 0) ? true : false;
///
/// ///////////////////////////////////////////////////////////////////////////////////////
///
}
private void SetFilterRemainDate()
{
_LaserManageSetting = _Aurora.SettingEntry[_SettingId] as ChillerSetting;
DateTime now = DateTime.Now;
int diffTime = (int)(now - _LaserManageSetting.GetChillerCurrentValue(_SmcChiller.DeviceNo).CHILLER_LAST_COOLANT_CHANGED).TotalDays;
LastCoolantRemainTime = _LaserManageSetting.WATER_TIME_MAX - diffTime;
LastCoolantColor = SetRemainDayColor(diffTime, _LaserManageSetting.WATER_TIME_1
, _LaserManageSetting.WATER_TIME_2, _LaserManageSetting.WATER_TIME_MAX);
LastCoolantRecWidth = SetRemainDayWidth(diffTime, _LaserManageSetting.WATER_TIME_MAX, LastCoolantActualWidth);
diffTime = (int)(now - _LaserManageSetting.GetChillerCurrentValue(_SmcChiller.DeviceNo).CHILLER_MAIN_BODY_CHANGED).TotalDays;
MainBodyRemainTime = _LaserManageSetting.CHILLER_TIME_MAX - diffTime;
MainBodyColor = SetRemainDayColor(diffTime, _LaserManageSetting.CHILLER_TIME_1
, _LaserManageSetting.CHILLER_TIME_2, _LaserManageSetting.CHILLER_TIME_MAX);
MainBodyRecWidth = SetRemainDayWidth(diffTime, _LaserManageSetting.CHILLER_TIME_MAX, MainBodyActualWidth);
}
private SolidColorBrush SetRemainDayColor(int diffTime, int warningTime1, int warningTime2, int maxTime)
{
// max 를 넘어서서 음수 이면, 전체 빨간색으로 경고 함.
if (diffTime > maxTime) {
return _Red;
} else if (diffTime > warningTime2) {
return _Orange;
} else if (diffTime > warningTime1) {
return _Yellow;
}
return _LightGreen;
}
private int SetRemainDayWidth(int diffTime, int maxTime, int actualWidth)
{
if (diffTime > maxTime) {
return actualWidth;
}
double percent = (double)(maxTime - diffTime) / (double)maxTime;
return (int)(actualWidth * percent);
}
}
}