StatusBarViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Command;
  3. using GalaSoft.MvvmLight.Ioc;
  4. using Muchinfo.MTPClient.Data.Enums;
  5. using Muchinfo.MTPClient.Data.Model;
  6. using Muchinfo.MTPClient.Data.Model.News;
  7. using Muchinfo.MTPClient.Infrastructure.Cache;
  8. using Muchinfo.MTPClient.Infrastructure.Helpers;
  9. using Muchinfo.MTPClient.Infrastructure.LinkProxy;
  10. using Muchinfo.MTPClient.Infrastructure.Utilities;
  11. using Muchinfo.MTPClient.IService;
  12. using Muchinfo.MTPClient.NetworkCore;
  13. using Muchinfo.MTPClient.UI.Utilities;
  14. using Muchinfo.MTPClient.UI.Views;
  15. using Muchinfo.PC.Common.Extensions;
  16. using Muchinfo.PC.Common.Helpers;
  17. using System;
  18. using System.Collections.ObjectModel;
  19. using System.Linq;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Media;
  23. using System.Windows.Threading;
  24. namespace Muchinfo.MTPClient.UI.ViewModels
  25. {
  26. public class StatusBarViewModel : ViewModelBase
  27. {
  28. private IGoodsService _goodsService;
  29. private DispatcherTimer _dispatcherTimer;
  30. private string _currentTime;
  31. private ImageSource _connectedImageSource;
  32. private ImageSource _disConnectedImageSource;
  33. private string _errorMessage = "";
  34. private int _refrashQuoteDayCycle = 0; ////行情盘面定时更新周期
  35. private int _checkTokenCycle = 0; ////CheckToken计时器
  36. private const int c_checkTokenTime = 600; ////CheckToken周期时间
  37. private const int c_showErrorSeconds = 5; ////错误消息时间
  38. private int _showErrorSeconds = 15; ////错误消息时间
  39. public StatusBarViewModel(IGoodsService goodsService)
  40. {
  41. _connectedImageSource = ResourceHelper.GetFromeResource<ImageSource>("StatusbarConnected");
  42. _disConnectedImageSource = ResourceHelper.GetFromeResource<ImageSource>("StatusbarDisconnected");
  43. NetStatusImageSource = _connectedImageSource;
  44. _goodsService = goodsService;
  45. _dispatcherTimer = new DispatcherTimer();
  46. _dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background) { Interval = new TimeSpan(0, 0, 1) };
  47. _dispatcherTimer.Tick += DispatcherTimer_Tick;
  48. _dispatcherTimer.Start();
  49. MessengerHelper.DefaultRegister<string>(this, MessengerTokens.ErrorMessage, ShowErrorMessage);
  50. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.TradeServerConnectMsg, TradeConnectChangeState);
  51. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.QuoteServerStateChange, QuoteConnectChangeState);
  52. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.CheckTokenMsg, ChenkToken);
  53. ShowChangeState();
  54. }
  55. private void TradeConnectChangeState(bool state)
  56. {
  57. // TradeIsOk = state;
  58. ShowChangeState();
  59. }
  60. private void QuoteConnectChangeState(bool state)
  61. {
  62. // QuoteIsOk = state;
  63. ShowChangeState();
  64. }
  65. private void ShowChangeState()
  66. {
  67. GuestQuoteVisible = LinkManager.Instance.GuestQuoteTcpLinkProxy != null ; ///是否显示游客行情连接
  68. if (UserManager.IsAccountLogin)
  69. {
  70. var linkProxy = LinkManager.Instance.TradeTcpLinkProxy;
  71. var quotelinkProxy = LinkManager.Instance.QuoteTcpLinkProxy;
  72. var guestlinkProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy;
  73. //if (linkProxy != null && quotelinkProxy != null &&
  74. // linkProxy.TcpConnectState == TCPConnectState.Connected &&
  75. // quotelinkProxy.TcpConnectState == TCPConnectState.Connected )
  76. //{
  77. // if (guestlinkProxy == null || guestlinkProxy.TcpConnectState == TCPConnectState.Connected) ////游客连接判断
  78. // {
  79. // NetStatusImageSource = _connectedImageSource;
  80. // }
  81. // else
  82. // {
  83. // NetStatusImageSource = _disConnectedImageSource;
  84. // }
  85. //}
  86. //else
  87. //{
  88. // NetStatusImageSource = _disConnectedImageSource;
  89. //}
  90. IsTradeConnect= linkProxy != null&& linkProxy.TcpConnectState == TCPConnectState.Connected;
  91. IsQuoteConnect = quotelinkProxy != null && quotelinkProxy.TcpConnectState == TCPConnectState.Connected;
  92. IsGuestConnect = guestlinkProxy != null && guestlinkProxy.TcpConnectState == TCPConnectState.Connected;
  93. }
  94. else
  95. {
  96. var linkProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy;
  97. IsGuestConnect = linkProxy != null && linkProxy.TcpConnectState == TCPConnectState.Connected;
  98. }
  99. }
  100. private void ShowErrorMessage(string message)
  101. {
  102. ErrorMessage = message;
  103. _showErrorSeconds = c_showErrorSeconds;
  104. }
  105. #region 状态栏绑定属性
  106. private bool _isQuoteConnect;
  107. /// <summary>
  108. /// 交易行情连接是否正常
  109. /// </summary>
  110. public bool IsQuoteConnect
  111. {
  112. get { return _isQuoteConnect; }
  113. set { Set(() => IsQuoteConnect, ref _isQuoteConnect, value); }
  114. }
  115. private bool _isTradeConnect;
  116. /// <summary>
  117. /// 交易 连接是否正常
  118. /// </summary>
  119. public bool IsTradeConnect
  120. {
  121. get { return _isTradeConnect; }
  122. set { Set(() => IsTradeConnect, ref _isTradeConnect, value); }
  123. }
  124. private bool _isGuestConnect;
  125. /// <summary>
  126. /// 游客连接是否正常
  127. /// </summary>
  128. public bool IsGuestConnect
  129. {
  130. get { return _isGuestConnect; }
  131. set { Set(() => IsGuestConnect, ref _isGuestConnect, value); }
  132. }
  133. /// <summary>
  134. /// 当前时间
  135. /// </summary>
  136. public string CurrentTime
  137. {
  138. get
  139. {
  140. return _currentTime;
  141. }
  142. set
  143. {
  144. Set(() => CurrentTime, ref _currentTime, value);
  145. }
  146. }
  147. /// <summary>
  148. /// 调用方法,一级或者二级返回的错误
  149. /// </summary>
  150. public string ErrorMessage
  151. {
  152. get
  153. {
  154. return _errorMessage;
  155. }
  156. set
  157. {
  158. Set(() => ErrorMessage, ref _errorMessage, value);
  159. }
  160. }
  161. private ObservableCollection<QuoteGoods> _indicatorsGoodses;
  162. /// <summary>
  163. /// 商品循环显示栏
  164. /// </summary>
  165. public ObservableCollection<QuoteGoods> IndicatorsGoodses
  166. {
  167. get { return _indicatorsGoodses; }
  168. set { Set(() => IndicatorsGoodses, ref _indicatorsGoodses, value); }
  169. }
  170. private QuoteGoods _currentGoods;
  171. /// <summary>
  172. /// 当前显示的指标
  173. /// </summary>
  174. public QuoteGoods CurrentGoods
  175. {
  176. get { return _currentGoods; }
  177. set { Set(() => CurrentGoods, ref _currentGoods, value); }
  178. }
  179. private ObservableCollection<NewsTitle> _newsTitleList;
  180. /// <summary>
  181. /// 滚动的新闻
  182. /// </summary>
  183. public ObservableCollection<NewsTitle> NewsTitleList
  184. {
  185. get { return _newsTitleList; }
  186. set { Set(() => NewsTitleList, ref _newsTitleList, value); }
  187. }
  188. private ObservableCollection<QuoteGoods> _quoteGoodses;
  189. /// <summary>
  190. ///快捷精灵
  191. /// </summary>
  192. public ObservableCollection<QuoteGoods> QuikQuertyGoodses
  193. {
  194. get
  195. {
  196. if (_quoteGoodses != null)
  197. {
  198. return _quoteGoodses;
  199. }
  200. _quoteGoodses = _goodsService.GetAllGoods();
  201. return _quoteGoodses;
  202. }
  203. }
  204. private ImageSource _netStatusImageSource;
  205. /// <summary>
  206. /// 网络状态图标资源
  207. /// </summary>
  208. public ImageSource NetStatusImageSource
  209. {
  210. get { return _netStatusImageSource; }
  211. set { Set(() => NetStatusImageSource, ref _netStatusImageSource, value); }
  212. }
  213. //Visibility="{Binding TradeVisible,
  214. // Converter={StaticResource BooleanToVisibility}}" />
  215. // <!-- 交易行情连接 -->
  216. // <Image Width="20"
  217. // Height="20"
  218. // Source="{DynamicResource Status_alarmImage_p}"
  219. // ToolTip="{Binding AlarmString}"
  220. // Visibility="{Binding TradeQuoteVisible,
  221. // Converter={StaticResource BooleanToVisibility}}" />
  222. // <!-- 游客行情连接 -->
  223. // <Image Width="20"
  224. // Height="20"
  225. // Source="{DynamicResource Status_alarmImage_p}"
  226. // ToolTip="{Binding AlarmString}"
  227. // Visibility="{Binding GuestQuoteVisible,
  228. private bool _guestQuoteVisible;
  229. /// <summary>
  230. /// 游客链路是否显示
  231. /// </summary>
  232. public bool GuestQuoteVisible
  233. {
  234. get { return _guestQuoteVisible; }
  235. set { Set(() => GuestQuoteVisible, ref _guestQuoteVisible, value); }
  236. }
  237. #endregion
  238. #region 绑定命令
  239. /// <summary>
  240. /// 关注商品单击命令
  241. /// </summary>
  242. public RelayCommand FocusGoodsSelectedCommand
  243. {
  244. get
  245. {
  246. return new RelayCommand(() =>
  247. {
  248. //粤国际:取消此功能
  249. //if (CurrentGoods == null) return;
  250. //WindowHelper.OpenQuoteChartWindow(new OpenQuoteChartWindowArg(CurrentGoods, CycleType.Minute));
  251. });
  252. }
  253. }
  254. /// <summary>
  255. /// 设置关注商品命令
  256. /// </summary>
  257. public RelayCommand FocusGoodsUpdateCommand
  258. {
  259. get
  260. {
  261. return new RelayCommand(() =>
  262. {
  263. //todo:添加关注商品后启用
  264. //var dialog = new FocusGoodsSelectView() { Owner = Application.Current.MainWindow };
  265. //var flag = dialog.ShowDialog();
  266. //if (flag.HasValue && flag.Value)
  267. //{
  268. // var items = dialog.SelectedListBox.Items;
  269. // if (items == null || items.Count == 0)
  270. // {
  271. // IndicatorsGoodses = null;
  272. // return;
  273. // };
  274. // var service = SimpleIoc.Default.GetInstance<IGoodsService>();
  275. // var codes = new List<string>();
  276. // foreach (var item in items)
  277. // {
  278. // var entity = item as TreeViewEntity;
  279. // if (entity == null) continue;
  280. // codes.Add(entity.Key);
  281. // }
  282. // IndicatorsGoodses = service.GetQuoteGoodsByCodes(codes);
  283. // ////订阅行情-获取商品后已订阅全部,此处代码注销
  284. // ////MessengerHelper.DefaultSend(string.Empty, MessengerTokens.QuoteSubscribe);
  285. //}
  286. });
  287. }
  288. }
  289. /// <summary>
  290. /// 打开新闻
  291. /// </summary>
  292. public RelayCommand<string> OpenNewsCommand
  293. {
  294. get
  295. {
  296. return new RelayCommand<string>((newsId) =>
  297. {
  298. NewsTitle newsTitle = this.NewsTitleList.FirstOrDefault(m => m.NewsID == newsId);
  299. if (newsTitle != null)
  300. {
  301. var newsCategory = CacheManager.CacheNewsCategory;
  302. if (newsCategory != null && newsCategory.Any())
  303. {
  304. var first = newsCategory.FirstOrDefault();
  305. if (first == null || string.IsNullOrWhiteSpace(first.ClassCode)) return;
  306. WindowHelper.OpenNewsWindow(first.ClassCode, newsTitle.NewsID);
  307. }
  308. }
  309. });
  310. }
  311. }
  312. /// <summary>
  313. /// 打开条件预警
  314. /// </summary>
  315. public RelayCommand OpenWarningCommand
  316. {
  317. get
  318. {
  319. //return new RelayCommand(WindowHelper.OpenWarningDialog);
  320. return new RelayCommand(() => { });
  321. }
  322. }
  323. /// <summary>
  324. /// 打开网络状态窗口
  325. /// </summary>
  326. public RelayCommand OpenNetStatusCommand
  327. {
  328. get
  329. {
  330. return new RelayCommand(() =>
  331. {
  332. //弹出网络状态窗口
  333. var view = new LinkStateView();
  334. view.Owner = Application.Current.MainWindow;
  335. view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  336. view.ShowDialog();
  337. });
  338. }
  339. }
  340. #endregion
  341. /// <summary>
  342. /// 客户端版本
  343. /// </summary>
  344. public string Version
  345. {
  346. get
  347. {
  348. return "v" + ApplicationParameter.Version;
  349. }
  350. }
  351. /// <summary>
  352. /// 登录账号信息
  353. /// </summary>
  354. public string CustomerName
  355. {
  356. get
  357. {
  358. if (UserManager.CurrentTradeAccount == null)
  359. {
  360. return string.Empty;
  361. }
  362. return UserManager.CurrentTradeAccount.CustomerName;
  363. }
  364. }
  365. /// <summary>
  366. /// 登录账号信息
  367. /// </summary>
  368. public ulong LoginID
  369. {
  370. get
  371. {
  372. if (UserManager.CurrentTradeAccount == null)
  373. {
  374. return uint.MaxValue;
  375. }
  376. return UserManager.CurrentTradeAccount.LoginID;
  377. }
  378. }
  379. /// <summary>
  380. /// 用户状态
  381. /// </summary>
  382. public string AccountStatus
  383. {
  384. get
  385. {
  386. //if (UserManager.CurrentTradeAccount.FundsAccounts == null || !UserManager.CurrentTradeAccount.FundsAccounts.Any())
  387. //{
  388. // return string.Empty;
  389. //}
  390. //return UserManager.CurrentTradeAccount.FundsAccounts[0].AccountStatusDisplay;
  391. if (UserManager.CurrentTradeAccount == null)
  392. {
  393. return string.Empty;
  394. }
  395. return UserManager.CurrentTradeAccount.AccountStatusDisplay;
  396. }
  397. }
  398. /// <summary>
  399. /// 是否使用账号登录
  400. /// </summary>
  401. public bool IsAccountVisible
  402. {
  403. get
  404. {
  405. return UserManager.IsAccountLogin;
  406. }
  407. }
  408. #region 私有方法
  409. /// <summary>
  410. /// Handles the Tick event of the DispatcherTimer control.
  411. /// </summary>
  412. /// <param name="sender">The source of the event.</param>
  413. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  414. private void DispatcherTimer_Tick(object sender, EventArgs e)
  415. {
  416. this.CurrentTime = ApplicationParameter.ServerTimeNow.ToString("yyyy-MM-dd HH:mm:ss");
  417. #region 同步盘面
  418. if (_refrashQuoteDayCycle <= 300)
  419. {
  420. _refrashQuoteDayCycle++;
  421. }
  422. else
  423. {
  424. try
  425. {
  426. Task.Factory.TryStartNew(() =>
  427. {
  428. //更新盘面
  429. //var goodReadTimeService = SimpleIoc.Default.GetInstance<IGoodsService>();
  430. //goodReadTimeService.UpdateFromDayQuote(CacheManager.CacheGoodsBaseInfos);
  431. QuoteProxyHelper.QueryDayQuote(GoodsFromScr.All);
  432. });
  433. }
  434. catch (Exception ex)
  435. {
  436. //todo:更新盘面出错提示
  437. // throw;
  438. LogHelper.WriteError(typeof(StatusBarViewModel), ex.ToString());
  439. }
  440. _refrashQuoteDayCycle = 0;
  441. }
  442. #endregion
  443. if (_checkTokenCycle < c_checkTokenTime)
  444. {
  445. _checkTokenCycle++;
  446. }
  447. else
  448. {
  449. ChenkToken(true);
  450. }
  451. #region 错误消息显示时长
  452. if (_showErrorSeconds > 0)
  453. {
  454. _showErrorSeconds--;
  455. }
  456. else
  457. {
  458. ErrorMessage = string.Empty;
  459. _showErrorSeconds = 0;
  460. }
  461. #endregion
  462. RaisePropertyChanged(() => IsAccountVisible);
  463. //RaisePropertyChanged(() => LoginID);
  464. //RaisePropertyChanged(() => CustomerName);
  465. //RaisePropertyChanged(() => AccountStatus);
  466. }
  467. /// <summary>
  468. /// 令牌校验
  469. /// </summary>
  470. private void ChenkToken(bool param)
  471. {
  472. var linkProxy = LinkManager.Instance.TradeTcpLinkProxy;
  473. if (linkProxy.TcpConnectState == TCPConnectState.Connected)
  474. {
  475. Task.Factory.TryStartNew(() =>
  476. {
  477. _checkTokenCycle = 0;
  478. SimpleIoc.Default.GetInstance<ILoginService>().TokenCheck((serverTime) =>
  479. {
  480. }, (errormsg) =>
  481. {
  482. MessengerHelper.DefaultSend(errormsg, MessengerTokens.ConnectCheckedToken);
  483. });
  484. });
  485. }
  486. }
  487. #endregion
  488. }
  489. }