| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- 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<ImageSource>("StatusbarConnected");
- _disConnectedImageSource = ResourceHelper.GetFromeResource<ImageSource>("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<string>(this, MessengerTokens.ErrorMessage, ShowErrorMessage);
- MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.TradeServerConnectMsg, TradeConnectChangeState);
- MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.QuoteServerStateChange, QuoteConnectChangeState);
- MessengerHelper.DefaultRegister<bool>(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;
- /// <summary>
- /// 交易行情连接是否正常
- /// </summary>
- public bool IsQuoteConnect
- {
- get { return _isQuoteConnect; }
- set { Set(() => IsQuoteConnect, ref _isQuoteConnect, value); }
- }
- private bool _isTradeConnect;
- /// <summary>
- /// 交易 连接是否正常
- /// </summary>
- public bool IsTradeConnect
- {
- get { return _isTradeConnect; }
- set { Set(() => IsTradeConnect, ref _isTradeConnect, value); }
- }
- private bool _isGuestConnect;
- /// <summary>
- /// 游客连接是否正常
- /// </summary>
- public bool IsGuestConnect
- {
- get { return _isGuestConnect; }
- set { Set(() => IsGuestConnect, ref _isGuestConnect, value); }
- }
-
- /// <summary>
- /// 当前时间
- /// </summary>
- public string CurrentTime
- {
- get
- {
- return _currentTime;
- }
- set
- {
- Set(() => CurrentTime, ref _currentTime, value);
- }
- }
- /// <summary>
- /// 调用方法,一级或者二级返回的错误
- /// </summary>
- public string ErrorMessage
- {
- get
- {
- return _errorMessage;
- }
- set
- {
- Set(() => ErrorMessage, ref _errorMessage, value);
- }
- }
- private ObservableCollection<QuoteGoods> _indicatorsGoodses;
- /// <summary>
- /// 商品循环显示栏
- /// </summary>
- public ObservableCollection<QuoteGoods> IndicatorsGoodses
- {
- get { return _indicatorsGoodses; }
- set { Set(() => IndicatorsGoodses, ref _indicatorsGoodses, value); }
- }
- private QuoteGoods _currentGoods;
- /// <summary>
- /// 当前显示的指标
- /// </summary>
- public QuoteGoods CurrentGoods
- {
- get { return _currentGoods; }
- set { Set(() => CurrentGoods, ref _currentGoods, value); }
- }
- private ObservableCollection<NewsTitle> _newsTitleList;
- /// <summary>
- /// 滚动的新闻
- /// </summary>
- public ObservableCollection<NewsTitle> NewsTitleList
- {
- get { return _newsTitleList; }
- set { Set(() => NewsTitleList, ref _newsTitleList, value); }
- }
- private ObservableCollection<QuoteGoods> _quoteGoodses;
- /// <summary>
- ///快捷精灵
- /// </summary>
- public ObservableCollection<QuoteGoods> QuikQuertyGoodses
- {
- get
- {
- if (_quoteGoodses != null)
- {
- return _quoteGoodses;
- }
- _quoteGoodses = _goodsService.GetAllGoods();
- return _quoteGoodses;
- }
- }
- private ImageSource _netStatusImageSource;
- /// <summary>
- /// 网络状态图标资源
- /// </summary>
- public ImageSource NetStatusImageSource
- {
- get { return _netStatusImageSource; }
- set { Set(() => NetStatusImageSource, ref _netStatusImageSource, value); }
- }
- //Visibility="{Binding TradeVisible,
- // Converter={StaticResource BooleanToVisibility}}" />
- // <!-- 交易行情连接 -->
- // <Image Width="20"
- // Height="20"
- // Source="{DynamicResource Status_alarmImage_p}"
- // ToolTip="{Binding AlarmString}"
- // Visibility="{Binding TradeQuoteVisible,
- // Converter={StaticResource BooleanToVisibility}}" />
- // <!-- 游客行情连接 -->
- // <Image Width="20"
- // Height="20"
- // Source="{DynamicResource Status_alarmImage_p}"
- // ToolTip="{Binding AlarmString}"
- // Visibility="{Binding GuestQuoteVisible,
- private bool _guestQuoteVisible;
- /// <summary>
- /// 游客链路是否显示
- /// </summary>
- public bool GuestQuoteVisible
- {
- get { return _guestQuoteVisible; }
- set { Set(() => GuestQuoteVisible, ref _guestQuoteVisible, value); }
- }
-
- #endregion
- #region 绑定命令
- /// <summary>
- /// 关注商品单击命令
- /// </summary>
- public RelayCommand FocusGoodsSelectedCommand
- {
- get
- {
- return new RelayCommand(() =>
- {
- //粤国际:取消此功能
- //if (CurrentGoods == null) return;
- //WindowHelper.OpenQuoteChartWindow(new OpenQuoteChartWindowArg(CurrentGoods, CycleType.Minute));
- });
- }
- }
- /// <summary>
- /// 设置关注商品命令
- /// </summary>
- public RelayCommand FocusGoodsUpdateCommand
- {
- get
- {
- return new RelayCommand(() =>
- {
- //todo:添加关注商品后启用
- //var dialog = new FocusGoodsSelectView() { Owner = Application.Current.MainWindow };
- //var flag = dialog.ShowDialog();
- //if (flag.HasValue && flag.Value)
- //{
- // var items = dialog.SelectedListBox.Items;
- // if (items == null || items.Count == 0)
- // {
- // IndicatorsGoodses = null;
- // return;
- // };
- // var service = SimpleIoc.Default.GetInstance<IGoodsService>();
- // var codes = new List<string>();
- // foreach (var item in items)
- // {
- // var entity = item as TreeViewEntity;
- // if (entity == null) continue;
- // codes.Add(entity.Key);
- // }
- // IndicatorsGoodses = service.GetQuoteGoodsByCodes(codes);
- // ////订阅行情-获取商品后已订阅全部,此处代码注销
- // ////MessengerHelper.DefaultSend(string.Empty, MessengerTokens.QuoteSubscribe);
- //}
- });
- }
- }
- /// <summary>
- /// 打开新闻
- /// </summary>
- public RelayCommand<string> OpenNewsCommand
- {
- get
- {
- return new RelayCommand<string>((newsId) =>
- {
- NewsTitle newsTitle = this.NewsTitleList.FirstOrDefault(m => m.NewsID == newsId);
- if (newsTitle != null)
- {
- var newsCategory = CacheManager.CacheNewsCategory;
- if (newsCategory != null && newsCategory.Any())
- {
- var first = newsCategory.FirstOrDefault();
- if (first == null || string.IsNullOrWhiteSpace(first.ClassCode)) return;
- WindowHelper.OpenNewsWindow(first.ClassCode, newsTitle.NewsID);
- }
- }
- });
- }
- }
- /// <summary>
- /// 打开条件预警
- /// </summary>
- public RelayCommand OpenWarningCommand
- {
- get
- {
- //return new RelayCommand(WindowHelper.OpenWarningDialog);
- return new RelayCommand(() => { });
- }
- }
- /// <summary>
- /// 打开网络状态窗口
- /// </summary>
- public RelayCommand OpenNetStatusCommand
- {
- get
- {
- return new RelayCommand(() =>
- {
- //弹出网络状态窗口
- var view = new LinkStateView();
- view.Owner = Application.Current.MainWindow;
- view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
- view.ShowDialog();
- });
- }
- }
- #endregion
- /// <summary>
- /// 客户端版本
- /// </summary>
- public string Version
- {
- get
- {
- return "v" + ApplicationParameter.Version;
- }
- }
- /// <summary>
- /// 登录账号信息
- /// </summary>
- public string CustomerName
- {
- get
- {
- if (UserManager.CurrentTradeAccount == null)
- {
- return string.Empty;
- }
- return UserManager.CurrentTradeAccount.CustomerName;
- }
- }
- /// <summary>
- /// 登录账号信息
- /// </summary>
- public ulong LoginID
- {
- get
- {
- if (UserManager.CurrentTradeAccount == null)
- {
- return uint.MaxValue;
- }
- return UserManager.CurrentTradeAccount.LoginID;
- }
- }
- /// <summary>
- /// 用户状态
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 是否使用账号登录
- /// </summary>
- public bool IsAccountVisible
- {
- get
- {
- return UserManager.IsAccountLogin;
- }
- }
- #region 私有方法
- /// <summary>
- /// Handles the Tick event of the DispatcherTimer control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
- 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<IGoodsService>();
- //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);
- }
- /// <summary>
- /// 令牌校验
- /// </summary>
- private void ChenkToken(bool param)
- {
- var linkProxy = LinkManager.Instance.TradeTcpLinkProxy;
- if (linkProxy.TcpConnectState == TCPConnectState.Connected)
- {
- Task.Factory.TryStartNew(() =>
- {
- _checkTokenCycle = 0;
- SimpleIoc.Default.GetInstance<ILoginService>().TokenCheck((serverTime) =>
- {
- }, (errormsg) =>
- {
- MessengerHelper.DefaultSend(errormsg, MessengerTokens.ConnectCheckedToken);
- });
- });
- }
- }
- #endregion
- }
- }
|