using GalaSoft.MvvmLight;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.LinkProxy;
using Muchinfo.MTPClient.NetworkCore;
using Muchinfo.MTPClient.Resources;
using Muchinfo.MTPClient.UI.Views;
using System;
using System.Collections.ObjectModel;
namespace Muchinfo.MTPClient.UI.ViewModels
{
///
/// 链路状态
///
public class LinkStateViewModel : ViewModelBase
{
private ObservableCollection _links = new ObservableCollection();
private LinkStateView View = null;
//private bool TradeIsOk = false; //交易服务状态
//private bool QuoteIsOk = false; //行情服务状态
///
/// 链路集合
///
public ObservableCollection Links
{
get
{
return _links;
}
}
System.Timers.Timer timer = new System.Timers.Timer(2000);
public LinkStateViewModel(LinkStateView view)
{
View = view;
//timer.Elapsed += timer_Elapsed;
//RefrushState();
//timer.Start();
MessengerHelper.DefaultRegister(this, MessengerTokens.TradeServerConnectMsg, TradeConnectChangeState);
MessengerHelper.DefaultRegister(this, MessengerTokens.QuoteServerStateChange, QuoteConnectChangeState);
RefrushState();
}
private void TradeConnectChangeState(bool state)
{
// TradeIsOk = state;
RefrushState();
}
private void QuoteConnectChangeState(bool state)
{
// QuoteIsOk = state;
RefrushState();
}
///
/// 刷新链路状态
///
public void RefrushState()
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
if (null != Links)
{
Links.Clear();
}
var linkProxy = LinkManager.Instance.TradeTcpLinkProxy;
if (null != linkProxy)
{
var temp = new LinkState();
temp.Name = Client_Resource.Link_Trade_Service; // "交易服务";
temp.Ip = linkProxy._host;
temp.Port = linkProxy._port;
if (linkProxy.TcpConnectState == TCPConnectState.Connected)
{
temp.IsEnable = true;
}
else
{
temp.IsEnable = false;
}
Links.Add(temp);
}
//--------------------
var QuotelinkProxy = LinkManager.Instance.QuoteTcpLinkProxy;
if (null != QuotelinkProxy)
{
var Quotetemp = new LinkState();
Quotetemp.Name = Client_Resource.Link_Quote_Service;// "行情服务";
Quotetemp.Ip = QuotelinkProxy._host;
Quotetemp.Port = QuotelinkProxy._port;
if (QuotelinkProxy.TcpConnectState == TCPConnectState.Connected)
{
Quotetemp.IsEnable = true;
}
else
{
Quotetemp.IsEnable = false;
}
Links.Add(Quotetemp);
}
var guestProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy;
if (null != guestProxy)
{
var guesttemp = new LinkState();
guesttemp.Name = Client_Resource.GuestQuoteSeivece; // "游客服务";
guesttemp.Ip = guestProxy._host;
guesttemp.Port = guestProxy._port;
if (guestProxy.TcpConnectState == TCPConnectState.Connected)
{
guesttemp.IsEnable = true;
}
else
{
guesttemp.IsEnable = false;
}
Links.Add(guesttemp);
}
}));
return;
//if (View != null)
// View.Dispatcher.BeginInvoke(new Action(() =>
// {
// _links.Clear();
// var links = LinkerManager.LinkerEntities;
// if (links == null) return;
// foreach (var item in links)
// {
// var temp = new LinkState();
// temp.LinkerEntity = item;
// temp.Name = item.LinkName;
// switch (item.LinkerType)
// {
// case ServiceProxyType.BizService:
// // temp.Name = "业务服务";
// // temp.Name = item.LinkName;
// break;
// //case ServiceProxyType.QuoteService:
// // // temp.Name = "历史行情服务";
// // // temp.Name = item.LinkName;
// // break;
// case ServiceProxyType.NewsService:
// // temp.Name = "新闻服务";
// //temp.Name = item.LinkName;
// break;
// case ServiceProxyType.QuoteHisRealTime:
// // temp.Name = "实时行情服务";
// // temp.Name = item.LinkName;
// break;
// case ServiceProxyType.OtherService:
// temp.Name = Client_Resource.Link_Other_Service;
// break;
// default:
// break;
// }
// try
// {
// string addr = item.ServiceProxy.IPAddress;
// string[] chars = addr.Split(':');
// temp.Ip = chars[0];
// temp.Port = int.Parse(chars[1].Replace("/", ""));
// }
// catch
// {
// temp.Ip = Client_Resource.Link_Unknow;
// temp.Port = 0;
// }
// if (item.ServiceProxy == null || item.ServiceProxy.ClientState == CommunicationState.Closed ||
// item.ServiceProxy.ClientState == CommunicationState.Closing ||
// item.ServiceProxy.ClientState == CommunicationState.Faulted)
// temp.IsEnable = false;
// else temp.IsEnable = true;
// _links.Add(temp);
// }
// }));
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
RefrushState();
timer.Start();
}
public override void Cleanup()
{
base.Cleanup();
if (timer != null) timer.Stop();
}
}
}