using Aitex.Core.Util;
using MECF.Framework.Common.CommonData.TemperatureControl;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.Device.TemperatureController;
using MECF.Framework.Common.Utilities;
using CyberX8_MainPages.Model;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Threading;
using MECF.Framework.Common.Persistent.Temperature;
using MECF.Framework.Common.Persistent.Prewet;
using MECF.Framework.Common.Persistent.Reservoirs;
namespace CyberX8_MainPages.ViewModels
{
public class TemperatureControllerViewModel : BindableBase
{
#region 常量
private const string TEMPERATURE_DEVICELIST = "TemperatureDeviceList";
private const string TEMPERATURE_DATA = "TemperatureData";
private const string IS_CONNECTED = "IsConnected";
private const string PERSISTENT_VALUE = "PersistentValue";
#endregion
#region 内部变量
///
/// Temperature设备配置
///
private ObservableCollection _temperatureUIDatas;
///
/// 持久化数据
///
private TCPersistentValue _tCPersistent;
///
/// 模块名称
///
private string _moduleName;
///
/// 查询后台数据集合
///
private List _rtDataKeys = new List();
///
/// 查询后台的数据
///
private Dictionary _rtDataValues;
///
/// 定时器
///
private DispatcherTimer _timer;
///
///当前模式是否可用
///
private bool _isEnabled;
///
/// Persistent
///
private ReservoirsPersistentValue _reservoirsPersistent;
#endregion
#region 属性
///
/// 模块名称
///
public string ModuleName { get { return _moduleName; } set { SetProperty(ref _moduleName, value); } }
///
/// Temperature设备数据集合
///
public ObservableCollection TemperatureUIDatas { get { return _temperatureUIDatas; } set { SetProperty(ref _temperatureUIDatas, value); } }
public TCPersistentValue TCPersistent
{
get { return _tCPersistent; }
set { SetProperty(ref _tCPersistent, value); }
}
public bool IsEnabled
{
get { return _isEnabled; }
set { SetProperty(ref _isEnabled, value); }
}
public ReservoirsPersistentValue ReservoirsPersistent
{
get { return _reservoirsPersistent; }
set { SetProperty(ref _reservoirsPersistent, value); }
}
#endregion
///
/// 构造函数
///
public TemperatureControllerViewModel()
{
}
///
/// 加载数据
///
public void LoadData(string systemName)
{
ModuleName = systemName;
List lst = new List();
lst.Add($"{ModuleName}.{TEMPERATURE_DEVICELIST}");
_rtDataValues = QueryDataClient.Instance.Service.PollData(lst);
if (_rtDataValues != null)
{
TemperatureDeviceConfig config = CommonFunction.GetValue(_rtDataValues, $"{ModuleName}.{TEMPERATURE_DEVICELIST}");
TemperatureUIDatas = new ObservableCollection();
foreach(TemperatureDevice item in config.TemperatureDevices)
{
TemperatureUIData temp = new TemperatureUIData();
temp.ReserviorName = item.ReserviorName;
temp.Name = item.Name;
TemperatureUIDatas.Add(temp);
}
InitialKeys(config);
}
if (_timer == null)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(200);
_timer.Tick += Timer_Tick;
}
_timer.Start();
}
///
/// 定时器执行
///
///
///
private void Timer_Tick(object sender, EventArgs e)
{
if (_rtDataKeys.Count != 0)
{
_rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
if (_rtDataValues != null && _rtDataValues.Count != 0 && TemperatureUIDatas != null)
{
foreach (TemperatureUIData item in TemperatureUIDatas)
{
TemperatureControllerData data = CommonFunction.GetValue(_rtDataValues, $"{item.Name}.{TEMPERATURE_DATA}");
if (data != null)
{
data.IsConnected = CommonFunction.GetValue(_rtDataValues, $"{item.Name}.{IS_CONNECTED}");
TCPersistent = CommonFunction.GetValue(_rtDataValues, $"{item.Name}.{PERSISTENT_VALUE}");
ReservoirsPersistent = CommonFunction.GetValue(_rtDataValues, $"{item.ReserviorName}.{PERSISTENT_VALUE}");
data.AutoStatus = ReservoirsPersistent.OperatingMode;
data.SetPoint = TCPersistent.SetPoint;
data.SetPointMaxValue = TCPersistent.SetPointHighLimit;
data.SetPointMinValue = TCPersistent.SetPointLowLimit;
item.SetValue(data);
if ("Auto".Equals(item.AutoStatus) || (item.Alarm!=null && (item.Alarm.Substring(0, 10) + item.Alarm.Substring(13, 1) + item.Alarm.Substring(15)).Contains("1")))
{
item.IsEnable = false;
}
else
{
item.IsEnable = true;
}
if ("Auto".Equals(item.AutoStatus) || (item.Alarm != null && (item.Alarm.Substring(0, 10) + item.Alarm.Substring(13, 1) + item.Alarm.Substring(15)).Contains("1")) || "Disable".Equals(item.ControlOperationModel))
{
item.IsApplyEnable = false;
}
else
{
item.IsApplyEnable = true;
}
}
}
}
}
}
///
/// 隐藏
///
public void Hide()
{
if (_timer != null)
{
_timer.Stop();
}
}
///
/// 初始化Keys
///
private void InitialKeys(TemperatureDeviceConfig config)
{
_rtDataKeys.Clear();
foreach (TemperatureDevice item in config.TemperatureDevices)
{
_rtDataKeys.Add($"{item.Name}.{TEMPERATURE_DATA}");
_rtDataKeys.Add($"{item.Name}.{IS_CONNECTED}");
_rtDataKeys.Add($"{item.Name}.{PERSISTENT_VALUE}");
_rtDataKeys.Add($"{item.ReserviorName}.{PERSISTENT_VALUE}");
}
}
}
}