using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using DM_Weight.Finger; using DM_Weight.Views; using Unity; using DM_Weight.HIKVISION; using log4net.Repository.Hierarchy; using log4net; using DM_Weight.Models; using System.Windows; namespace DM_Weight.ViewModels { internal class MainWindowViewModel : BindableBase { private string _title = "Prism App"; //标题 private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3)); private SolidColorBrush _colorBrush; //private PortUtil _portUtil; //private ScreenUtil _screenUtil; public SolidColorBrush Background { get => _colorBrush; set => SetProperty(ref _colorBrush, value); } public ISnackbarMessageQueue SnackbarMessageQueue { get => _snackbarMessageQueue; set => SetProperty(ref _snackbarMessageQueue, value); } public string Title { get { return _title; } set { SetProperty(ref _title, value); } } IEventAggregator eventAggregator; //public MainWindowViewModel(IEventAggregator eventAggregator, PortUtil portUtil, ScreenUtil screenUtil) //{ // _portUtil = portUtil; // this.eventAggregator = eventAggregator; // this.eventAggregator.GetEvent().Subscribe(doMyPrismEvent2); // _screenUtil = screenUtil; //} private FingerprintUtil _fingerprintUtil; IRegionManager _regionManager; IUnityContainer _container; //private CHKFunction _cHKFunction; private readonly ILog logger = LogManager.GetLogger(typeof(PortUtil)); SocketHelper _socketHelper; private PortUtil _portUtil; public MainWindowViewModel(IRegionManager regionManager, IUnityContainer container, IEventAggregator eventAggregator, FingerprintUtil fingerprintUtil, SocketHelper socketHelper, PortUtil portUtil) { _portUtil = portUtil; this.eventAggregator = eventAggregator; this.eventAggregator.GetEvent().Subscribe(doMyPrismEvent2); _fingerprintUtil = fingerprintUtil; _regionManager = regionManager; _container = container; //_cHKFunction = cHKFunction; //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => //{ _container.RegisterType("LoginWindow"); _regionManager.RegisterViewWithRegion("MainRegion", "LoginWindow"); //})); _socketHelper = socketHelper; new PromiseUtil().taskAsyncLoop(1200000, 0, async (options, next, stop) => { _socketHelper.IsMultiThread = true; try { TemperatureHumidityInfo temp = _portUtil.GetWSD(); if (temp != null) { SqlSugarHelper.Db.Insertable(new TemperatureHumidityInfo() { GroupNo = temp.GroupNo, Temp = temp.Temp, Humi = temp.Humi, AddTime = DateTime.Now }).ExecuteCommand(); logger.Info($"保存温湿度信息:{temp.Temp},{temp.Humi}"); Task.Delay(1200000);//20分钟查一次 } else { logger.Info("温湿度信息返回空"); } next(); } catch (Exception ex) { logger.Info($"保存温湿度异常:{ex.Message}"); next(); } }); } void doMyPrismEvent2(AlertMsg msg) { Application.Current.Dispatcher.Invoke(new Action(() => { switch (msg.Type) { case MsgType.INFO: this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676")); break; case MsgType.ERROR: this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); break; default: this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00e676")); break; } SnackbarMessageQueue.Enqueue(msg.Message); })); } } }