using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.News; using Muchinfo.MTPClient.Infrastructure.Cache; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.LinkProxy; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.NetworkCore; using Muchinfo.MTPClient.UI.Utilities; using Muchinfo.MTPClient.UI.Views; using Muchinfo.PC.Common.Extensions; using Muchinfo.PC.Common.Helpers; using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Threading; namespace Muchinfo.MTPClient.UI.ViewModels { public class StatusBarViewModel : ViewModelBase { private IGoodsService _goodsService; private DispatcherTimer _dispatcherTimer; private string _currentTime; private ImageSource _connectedImageSource; private ImageSource _disConnectedImageSource; private string _errorMessage = ""; private int _refrashQuoteDayCycle = 0; ////行情盘面定时更新周期 private int _checkTokenCycle = 0; ////CheckToken计时器 private const int c_checkTokenTime = 600; ////CheckToken周期时间 private const int c_showErrorSeconds = 5; ////错误消息时间 private int _showErrorSeconds = 15; ////错误消息时间 public StatusBarViewModel(IGoodsService goodsService) { _connectedImageSource = ResourceHelper.GetFromeResource("StatusbarConnected"); _disConnectedImageSource = ResourceHelper.GetFromeResource("StatusbarDisconnected"); NetStatusImageSource = _connectedImageSource; _goodsService = goodsService; _dispatcherTimer = new DispatcherTimer(); _dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background) { Interval = new TimeSpan(0, 0, 1) }; _dispatcherTimer.Tick += DispatcherTimer_Tick; _dispatcherTimer.Start(); MessengerHelper.DefaultRegister(this, MessengerTokens.ErrorMessage, ShowErrorMessage); MessengerHelper.DefaultRegister(this, MessengerTokens.TradeServerConnectMsg, TradeConnectChangeState); MessengerHelper.DefaultRegister(this, MessengerTokens.QuoteServerStateChange, QuoteConnectChangeState); MessengerHelper.DefaultRegister(this, MessengerTokens.CheckTokenMsg, ChenkToken); ShowChangeState(); } private void TradeConnectChangeState(bool state) { // TradeIsOk = state; ShowChangeState(); } private void QuoteConnectChangeState(bool state) { // QuoteIsOk = state; ShowChangeState(); } private void ShowChangeState() { GuestQuoteVisible = LinkManager.Instance.GuestQuoteTcpLinkProxy != null ; ///是否显示游客行情连接 if (UserManager.IsAccountLogin) { var linkProxy = LinkManager.Instance.TradeTcpLinkProxy; var quotelinkProxy = LinkManager.Instance.QuoteTcpLinkProxy; var guestlinkProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy; //if (linkProxy != null && quotelinkProxy != null && // linkProxy.TcpConnectState == TCPConnectState.Connected && // quotelinkProxy.TcpConnectState == TCPConnectState.Connected ) //{ // if (guestlinkProxy == null || guestlinkProxy.TcpConnectState == TCPConnectState.Connected) ////游客连接判断 // { // NetStatusImageSource = _connectedImageSource; // } // else // { // NetStatusImageSource = _disConnectedImageSource; // } //} //else //{ // NetStatusImageSource = _disConnectedImageSource; //} IsTradeConnect= linkProxy != null&& linkProxy.TcpConnectState == TCPConnectState.Connected; IsQuoteConnect = quotelinkProxy != null && quotelinkProxy.TcpConnectState == TCPConnectState.Connected; IsGuestConnect = guestlinkProxy != null && guestlinkProxy.TcpConnectState == TCPConnectState.Connected; } else { var linkProxy = LinkManager.Instance.GuestQuoteTcpLinkProxy; IsGuestConnect = linkProxy != null && linkProxy.TcpConnectState == TCPConnectState.Connected; } } private void ShowErrorMessage(string message) { ErrorMessage = message; _showErrorSeconds = c_showErrorSeconds; } #region 状态栏绑定属性 private bool _isQuoteConnect; /// /// 交易行情连接是否正常 /// public bool IsQuoteConnect { get { return _isQuoteConnect; } set { Set(() => IsQuoteConnect, ref _isQuoteConnect, value); } } private bool _isTradeConnect; /// /// 交易 连接是否正常 /// public bool IsTradeConnect { get { return _isTradeConnect; } set { Set(() => IsTradeConnect, ref _isTradeConnect, value); } } private bool _isGuestConnect; /// /// 游客连接是否正常 /// public bool IsGuestConnect { get { return _isGuestConnect; } set { Set(() => IsGuestConnect, ref _isGuestConnect, value); } } /// /// 当前时间 /// public string CurrentTime { get { return _currentTime; } set { Set(() => CurrentTime, ref _currentTime, value); } } /// /// 调用方法,一级或者二级返回的错误 /// public string ErrorMessage { get { return _errorMessage; } set { Set(() => ErrorMessage, ref _errorMessage, value); } } private ObservableCollection _indicatorsGoodses; /// /// 商品循环显示栏 /// public ObservableCollection IndicatorsGoodses { get { return _indicatorsGoodses; } set { Set(() => IndicatorsGoodses, ref _indicatorsGoodses, value); } } private QuoteGoods _currentGoods; /// /// 当前显示的指标 /// public QuoteGoods CurrentGoods { get { return _currentGoods; } set { Set(() => CurrentGoods, ref _currentGoods, value); } } private ObservableCollection _newsTitleList; /// /// 滚动的新闻 /// public ObservableCollection NewsTitleList { get { return _newsTitleList; } set { Set(() => NewsTitleList, ref _newsTitleList, value); } } private ObservableCollection _quoteGoodses; /// ///快捷精灵 /// public ObservableCollection QuikQuertyGoodses { get { if (_quoteGoodses != null) { return _quoteGoodses; } _quoteGoodses = _goodsService.GetAllGoods(); return _quoteGoodses; } } private ImageSource _netStatusImageSource; /// /// 网络状态图标资源 /// public ImageSource NetStatusImageSource { get { return _netStatusImageSource; } set { Set(() => NetStatusImageSource, ref _netStatusImageSource, value); } } //Visibility="{Binding TradeVisible, // Converter={StaticResource BooleanToVisibility}}" /> // // // // /// 登录账号信息 /// public string CustomerName { get { if (UserManager.CurrentTradeAccount == null) { return string.Empty; } return UserManager.CurrentTradeAccount.CustomerName; } } /// /// 登录账号信息 /// public ulong LoginID { get { if (UserManager.CurrentTradeAccount == null) { return uint.MaxValue; } return UserManager.CurrentTradeAccount.LoginID; } } /// /// 用户状态 /// public string AccountStatus { get { //if (UserManager.CurrentTradeAccount.FundsAccounts == null || !UserManager.CurrentTradeAccount.FundsAccounts.Any()) //{ // return string.Empty; //} //return UserManager.CurrentTradeAccount.FundsAccounts[0].AccountStatusDisplay; if (UserManager.CurrentTradeAccount == null) { return string.Empty; } return UserManager.CurrentTradeAccount.AccountStatusDisplay; } } /// /// 是否使用账号登录 /// public bool IsAccountVisible { get { return UserManager.IsAccountLogin; } } #region 私有方法 /// /// Handles the Tick event of the DispatcherTimer control. /// /// The source of the event. /// The instance containing the event data. private void DispatcherTimer_Tick(object sender, EventArgs e) { this.CurrentTime = ApplicationParameter.ServerTimeNow.ToString("yyyy-MM-dd HH:mm:ss"); #region 同步盘面 if (_refrashQuoteDayCycle <= 300) { _refrashQuoteDayCycle++; } else { try { Task.Factory.TryStartNew(() => { //更新盘面 //var goodReadTimeService = SimpleIoc.Default.GetInstance(); //goodReadTimeService.UpdateFromDayQuote(CacheManager.CacheGoodsBaseInfos); QuoteProxyHelper.QueryDayQuote(GoodsFromScr.All); }); } catch (Exception ex) { //todo:更新盘面出错提示 // throw; LogHelper.WriteError(typeof(StatusBarViewModel), ex.ToString()); } _refrashQuoteDayCycle = 0; } #endregion if (_checkTokenCycle < c_checkTokenTime) { _checkTokenCycle++; } else { ChenkToken(true); } #region 错误消息显示时长 if (_showErrorSeconds > 0) { _showErrorSeconds--; } else { ErrorMessage = string.Empty; _showErrorSeconds = 0; } #endregion RaisePropertyChanged(() => IsAccountVisible); //RaisePropertyChanged(() => LoginID); //RaisePropertyChanged(() => CustomerName); //RaisePropertyChanged(() => AccountStatus); } /// /// 令牌校验 /// private void ChenkToken(bool param) { var linkProxy = LinkManager.Instance.TradeTcpLinkProxy; if (linkProxy.TcpConnectState == TCPConnectState.Connected) { Task.Factory.TryStartNew(() => { _checkTokenCycle = 0; SimpleIoc.Default.GetInstance().TokenCheck((serverTime) => { }, (errormsg) => { MessengerHelper.DefaultSend(errormsg, MessengerTokens.ConnectCheckedToken); }); }); } } #endregion } }