LinkStateViewModel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using GalaSoft.MvvmLight;
  2. using Muchinfo.MTPClient.Data;
  3. using Muchinfo.MTPClient.Data.Enums;
  4. using Muchinfo.MTPClient.Infrastructure.Helpers;
  5. using Muchinfo.MTPClient.Infrastructure.LinkProxy;
  6. using Muchinfo.MTPClient.NetworkCore;
  7. using Muchinfo.MTPClient.Resources;
  8. using Muchinfo.MTPClient.UI.Views;
  9. using System;
  10. using System.Collections.ObjectModel;
  11. namespace Muchinfo.MTPClient.UI.ViewModels
  12. {
  13. /// <summary>
  14. /// 链路状态
  15. /// </summary>
  16. public class LinkStateViewModel : ViewModelBase
  17. {
  18. private ObservableCollection<LinkState> _links = new ObservableCollection<LinkState>();
  19. private LinkStateView View = null;
  20. //private bool TradeIsOk = false; //交易服务状态
  21. //private bool QuoteIsOk = false; //行情服务状态
  22. /// <summary>
  23. /// 链路集合
  24. /// </summary>
  25. public ObservableCollection<LinkState> Links
  26. {
  27. get
  28. {
  29. return _links;
  30. }
  31. }
  32. System.Timers.Timer timer = new System.Timers.Timer(2000);
  33. public LinkStateViewModel(LinkStateView view)
  34. {
  35. View = view;
  36. //timer.Elapsed += timer_Elapsed;
  37. //RefrushState();
  38. //timer.Start();
  39. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.TradeServerConnectMsg, TradeConnectChangeState);
  40. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.QuoteServerStateChange, QuoteConnectChangeState);
  41. RefrushState();
  42. }
  43. private void TradeConnectChangeState(bool state)
  44. {
  45. // TradeIsOk = state;
  46. RefrushState();
  47. }
  48. private void QuoteConnectChangeState(bool state)
  49. {
  50. // QuoteIsOk = state;
  51. RefrushState();
  52. }
  53. /// <summary>
  54. /// 刷新链路状态
  55. /// </summary>
  56. public void RefrushState()
  57. {
  58. System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  59. {
  60. if (null != Links)
  61. {
  62. Links.Clear();
  63. }
  64. var linkProxy = LinkManager.Instance.TradeTcpLinkProxy;
  65. if (null != linkProxy)
  66. {
  67. var temp = new LinkState();
  68. temp.Name = Client_Resource.Link_Trade_Service; // "交易服务";
  69. temp.Ip = linkProxy._host;
  70. temp.Port = linkProxy._port;
  71. if (linkProxy.TcpConnectState == TCPConnectState.Connected)
  72. {
  73. temp.IsEnable = true;
  74. }
  75. else
  76. {
  77. temp.IsEnable = false;
  78. }
  79. Links.Add(temp);
  80. }
  81. //--------------------
  82. var QuotelinkProxy = LinkManager.Instance.QuoteTcpLinkProxy;
  83. if (null != QuotelinkProxy)
  84. {
  85. var Quotetemp = new LinkState();
  86. Quotetemp.Name = Client_Resource.Link_Quote_Service;// "行情服务";
  87. Quotetemp.Ip = QuotelinkProxy._host;
  88. Quotetemp.Port = QuotelinkProxy._port;
  89. if (QuotelinkProxy.TcpConnectState == TCPConnectState.Connected)
  90. {
  91. Quotetemp.IsEnable = true;
  92. }
  93. else
  94. {
  95. Quotetemp.IsEnable = false;
  96. }
  97. Links.Add(Quotetemp);
  98. }
  99. var guestProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy;
  100. if (null != guestProxy)
  101. {
  102. var guesttemp = new LinkState();
  103. guesttemp.Name = Client_Resource.GuestQuoteSeivece; // "游客服务";
  104. guesttemp.Ip = guestProxy._host;
  105. guesttemp.Port = guestProxy._port;
  106. if (guestProxy.TcpConnectState == TCPConnectState.Connected)
  107. {
  108. guesttemp.IsEnable = true;
  109. }
  110. else
  111. {
  112. guesttemp.IsEnable = false;
  113. }
  114. Links.Add(guesttemp);
  115. }
  116. }));
  117. return;
  118. //if (View != null)
  119. // View.Dispatcher.BeginInvoke(new Action(() =>
  120. // {
  121. // _links.Clear();
  122. // var links = LinkerManager.LinkerEntities;
  123. // if (links == null) return;
  124. // foreach (var item in links)
  125. // {
  126. // var temp = new LinkState();
  127. // temp.LinkerEntity = item;
  128. // temp.Name = item.LinkName;
  129. // switch (item.LinkerType)
  130. // {
  131. // case ServiceProxyType.BizService:
  132. // // temp.Name = "业务服务";
  133. // // temp.Name = item.LinkName;
  134. // break;
  135. // //case ServiceProxyType.QuoteService:
  136. // // // temp.Name = "历史行情服务";
  137. // // // temp.Name = item.LinkName;
  138. // // break;
  139. // case ServiceProxyType.NewsService:
  140. // // temp.Name = "新闻服务";
  141. // //temp.Name = item.LinkName;
  142. // break;
  143. // case ServiceProxyType.QuoteHisRealTime:
  144. // // temp.Name = "实时行情服务";
  145. // // temp.Name = item.LinkName;
  146. // break;
  147. // case ServiceProxyType.OtherService:
  148. // temp.Name = Client_Resource.Link_Other_Service;
  149. // break;
  150. // default:
  151. // break;
  152. // }
  153. // try
  154. // {
  155. // string addr = item.ServiceProxy.IPAddress;
  156. // string[] chars = addr.Split(':');
  157. // temp.Ip = chars[0];
  158. // temp.Port = int.Parse(chars[1].Replace("/", ""));
  159. // }
  160. // catch
  161. // {
  162. // temp.Ip = Client_Resource.Link_Unknow;
  163. // temp.Port = 0;
  164. // }
  165. // if (item.ServiceProxy == null || item.ServiceProxy.ClientState == CommunicationState.Closed ||
  166. // item.ServiceProxy.ClientState == CommunicationState.Closing ||
  167. // item.ServiceProxy.ClientState == CommunicationState.Faulted)
  168. // temp.IsEnable = false;
  169. // else temp.IsEnable = true;
  170. // _links.Add(temp);
  171. // }
  172. // }));
  173. }
  174. void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  175. {
  176. timer.Stop();
  177. RefrushState();
  178. timer.Start();
  179. }
  180. public override void Cleanup()
  181. {
  182. base.Cleanup();
  183. if (timer != null) timer.Stop();
  184. }
  185. }
  186. }