using Aucma.Core.Scanner; using Aucma.Scada.UI.Page.AssemblyPlan; using Aucma.Scada.UI.Page.InStoreInfo; using Aucma.Scada.UI.Page.InventoryInfo; using Aucma.Scada.UI.Page.OutStoreInfo; using Aucma.Scada.UI.Page.TaskInfo; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HighWayIot.Config; using HighWayIot.Log4net; using HighWayIot.Plc; using HighWayIot.TouchSocket; using SqlSugar.DistributedSystem.Snowflake; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Threading; namespace Aucma.Scada.UI.viewModel { public class MainViewModel : ViewModelBase { private AppConfig appConfig = AppConfig.Instance; private PlcPool _pool = PlcPool.Instance; private Dictionary _plcDictionary = new Dictionary(); private LogHelper logHelper = LogHelper.Instance; private readonly LogInfoControl logInfoControl = new LogInfoControl(); private readonly InStoreInfoControl inStoreInfoControl = new InStoreInfoControl(); private readonly OutStoreInfoControl outStoreInfoControl = new OutStoreInfoControl(); private readonly TaskInfoControl taskInfoControl = new TaskInfoControl(); private readonly InventoryInfoControl inventoryInfoControl = new InventoryInfoControl(); private readonly AssemblyPlanControl assemblyPlanControl = new AssemblyPlanControl(); private readonly TouchSocketBusiness touchSocket = TouchSocketBusiness.Instance; public MainViewModel() { // 启动扫码器socket通信代替sdk touchSocket.StartTouchSocket(); // 创建一个DispatcherTimer对象 DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += Timer_Tick; timer.Start(); // MvCodeHelper.RefreshStateEvent += RefreshScanner; TouchSocketBusiness.RefreshStateEvent += RefreshScanner; ControlOnClickCommand = new RelayCommand(obj => ControlOnClick(obj)); OpenSystemKeyboardCommand = new RelayCommand(OpenSystemKeyboard); FormControlCommand = new RelayCommand(obj => FormControl(obj)); UserContent = inStoreInfoControl; init(); } public void RefreshScanner(string ip, bool flag) { if (ip == appConfig.shellHikRobotIp) { ShellScannerState(flag); ; } else if (ip == appConfig.linerHikRobotIp) { LinerScannerState(flag); } } #region 参数定义 /// /// PLC设备状态 /// private int _PlcStatus = 0; public int PlcStatus { get { return _PlcStatus; } set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); } } /// /// 箱壳扫码器状态 /// private int _ShellScannerStatus = 0; public int ShellScannerStatus { get { return _ShellScannerStatus; } set { _ShellScannerStatus = value; RaisePropertyChanged(nameof(ShellScannerStatus)); } } /// /// 内胆扫码器状态 /// private int _BoldScannerStatus = 0; public int BoldScannerStatus { get { return _BoldScannerStatus; } set { _BoldScannerStatus = value; RaisePropertyChanged(nameof(BoldScannerStatus)); } } public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get { return _content; } set { _content = value; RaisePropertyChanged(nameof(UserContent)); } } #endregion #region 事件定义 /// /// 界面跳转按钮事件 /// public RelayCommand ControlOnClickCommand { get; set; } /// /// 打开系统键盘 /// public RelayCommand OpenSystemKeyboardCommand { get; set; } /// /// 窗体控制 /// public RelayCommand FormControlCommand { get; set; } #endregion /// /// 界面跳转 /// private void ControlOnClick(object obj) { try { string info = obj as string; switch (info) { case "instoreInfo": UserContent = inStoreInfoControl; break; case "outstoreInfo": UserContent = outStoreInfoControl; break; case "taskInfo": UserContent = taskInfoControl; break; case "inventoryInfo": UserContent = inventoryInfoControl; break; case "assemblyPlan": UserContent = assemblyPlanControl; break; case "logInfo": UserContent = logInfoControl; break; default: UserContent = inStoreInfoControl; break; } } catch (Exception ex) { logHelper.Error("界面跳转逻辑异常", ex); } } /// /// 打开系统键盘 /// private void OpenSystemKeyboard() { try { Process proc = new Process(); proc.StartInfo.FileName = Path.Combine(Directory.GetCurrentDirectory(), "osk.exe"); proc.StartInfo.UseShellExecute = true; proc.StartInfo.Verb = "runas"; proc.Start(); } catch (Exception ex) { logHelper.Error("打开系统键盘逻辑异常", ex); MessageBox.Show($"系统键盘打开异常:{ex.Message}", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } } /// /// 窗体控制 /// /// private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 关闭当前窗口 case "Exit": //Environment.Exit(0); Application.Current.Shutdown(); break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; // 最小化当前窗口 case "Minimized": Application.Current.MainWindow.WindowState = WindowState.Minimized; break; default: break; } } catch (Exception ex) { logHelper.Error("窗体控制逻辑异常", ex); } } public void init() { ShellScannerState(false); LinerScannerState(false); // 设备状态刷新定时器 System.Timers.Timer timer = new System.Timers.Timer(1000 * 5); timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshStatus); timer.AutoReset = true; timer.Enabled = true; timer.Start(); } #region 设备状态刷新 /// /// 设备状态刷新 /// /// /// public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e) { try { RefreshMesDb(); RefreshPlc(); } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } } /// /// 数据库状态刷新 /// /// /// public void RefreshMesDb() { MesDbState(true); } /// /// plc状态刷新 /// /// /// public void RefreshPlc() { _plcDictionary = _pool.keyValuePairs; // 箱壳状态 _plcDictionary.TryGetValue(appConfig.shellStoreCode, out IPlc _plc); if (_plc != null) { PlcState(true); } else { PlcState(false); } // 内胆状态 _plcDictionary.TryGetValue(appConfig.linerStoreCode, out IPlc _ndPlc); if (_ndPlc != null) { NdPlcState(true); } else { NdPlcState(false); } } /// /// 扫码器状态刷新 /// /// /// public void RefreshScanner() { string ip1 = appConfig.shellHikRobotIp; string ip2 = appConfig.linerHikRobotIp; //bool flag1 = MvCodeHelper.ConnectionStatus(ip1); //bool flag2 = MvCodeHelper.ConnectionStatus(ip2); ShellScannerState(false); LinerScannerState(false); } #endregion #region MES数据库状态 /// /// MES数据库-文字 /// public string _mesDbUIStatusWb; public string MesDbUIStatusWb { get => _mesDbUIStatusWb; set { _mesDbUIStatusWb = value; RaisePropertyChanged(nameof(MesDbUIStatusWb)); } } /// /// MES数据库-颜色 /// public string _mesDbUIColor; public string MesDbUIColor { get => _mesDbUIColor; set { _mesDbUIColor = value; RaisePropertyChanged(nameof(MesDbUIColor)); } } /// /// MES数据库-图标 /// public string _mesUIIcon; public string MesUIIcon { get => _mesUIIcon; set { _mesUIIcon = value; RaisePropertyChanged(nameof(MesUIIcon)); } } /// /// MES数据库连接状态 /// /// public void MesDbState(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { MesDbUIStatusWb = "MES数据库连接成功"; MesDbUIColor = "Green"; MesUIIcon = "templates/image/Green.png"; } else { MesDbUIStatusWb = "MES数据库异常"; MesDbUIColor = "Red"; MesUIIcon = "templates/image/Red.png"; } }); } #endregion #region 箱壳plc 状态 /// /// UI plc 展示状态-文字 /// public string _plcUIStatusWb; public string PlcUIStatusWb { get => _plcUIStatusWb; set { _plcUIStatusWb = value; RaisePropertyChanged(nameof(PlcUIStatusWb)); } } /// /// UI plc 展示状态-颜色 /// public string _plcUIColor; public string PlcUIColor { get => _plcUIColor; set { _plcUIColor = value; RaisePropertyChanged(nameof(PlcUIColor)); } } /// /// UI plc 展示状态-图标 /// public string _plcUIIcon; public string PlcUIIcon { get => _plcUIIcon; set { _plcUIIcon = value; RaisePropertyChanged(nameof(PlcUIIcon)); } } /// /// PLC连接状态-true:连接成功;false:失败 /// /// public void PlcState(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { PlcUIStatusWb = "箱壳PLC连接成功"; PlcUIColor = "Green"; PlcUIIcon = "templates/image/Green.png"; } else { PlcUIStatusWb = "箱壳PLC状态异常"; PlcUIColor = "Red"; PlcUIIcon = "templates/image/Red.png"; } }); } #endregion #region 内胆plc 状态 /// /// UI plc 展示状态-文字 /// public string _ndPlcUIStatusWb; public string NdPlcUIStatusWb { get => _ndPlcUIStatusWb; set { _ndPlcUIStatusWb = value; RaisePropertyChanged(nameof(NdPlcUIStatusWb)); } } /// /// UI plc 展示状态-颜色 /// public string _ndPlcUIColor; public string NdPlcUIColor { get => _ndPlcUIColor; set { _ndPlcUIColor = value; RaisePropertyChanged(nameof(NdPlcUIColor)); } } /// /// UI plc 展示状态-图标 /// public string _ndPlcUIIcon; public string NdPlcUIIcon { get => _ndPlcUIIcon; set { _ndPlcUIIcon = value; RaisePropertyChanged(nameof(NdPlcUIIcon)); } } /// /// PLC连接状态-true:连接成功;false:失败 /// /// public void NdPlcState(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { NdPlcUIStatusWb = "内胆PLC连接成功"; NdPlcUIColor = "Green"; NdPlcUIIcon = "templates/image/Green.png"; } else { NdPlcUIStatusWb = "内胆PLC状态异常"; NdPlcUIColor = "Red"; NdPlcUIIcon = "templates/image/Red.png"; } }); } #endregion #region 扫码器1状态 /// /// UI 展示状态-文字 /// public string _scanner1UIStatusWb; public string Scanner1UIStatusWb { //get { return plcUIStatusWb; } //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } get => _scanner1UIStatusWb; set { _scanner1UIStatusWb = value; RaisePropertyChanged(nameof(Scanner1UIStatusWb)); } } /// /// UI 展示状态-颜色 /// public string _scanner1UIColor; public string Scanner1UIColor { //get { return plcUIColor; } //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } get => _scanner1UIColor; set { _scanner1UIColor = value; RaisePropertyChanged(nameof(Scanner1UIColor)); } } /// /// UI 展示状态-图标 /// public string _scanner1UIIcon; public string Scanner1UIIcon { //get { return plcUIIcon; } //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } get => _scanner1UIIcon; set { _scanner1UIIcon = value; RaisePropertyChanged(nameof(Scanner1UIIcon)); } } /// /// 扫码器1连接状态-true:连接成功;false:失败 /// /// public void ShellScannerState(bool type) { try { Application.Current.Dispatcher.Invoke(() => { if (type) { Scanner1UIStatusWb = "箱壳扫码器"; Scanner1UIColor = "Green"; Scanner1UIIcon = "templates/image/Green.png"; } else { Scanner1UIStatusWb = "箱壳扫码器"; Scanner1UIColor = "Red"; Scanner1UIIcon = "templates/image/Red.png"; } }); } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } } #endregion #region 扫码器2状态 /// /// UI 展示状态-文字 /// public string _scanner2UIStatusWb; public string Scanner2UIStatusWb { //get { return plcUIStatusWb; } //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } get => _scanner2UIStatusWb; set { _scanner2UIStatusWb = value; RaisePropertyChanged(nameof(Scanner2UIStatusWb)); } } /// /// UI 展示状态-颜色 /// public string _scanner2UIColor; public string Scanner2UIColor { //get { return plcUIColor; } //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } get => _scanner2UIColor; set { _scanner2UIColor = value; RaisePropertyChanged(nameof(Scanner2UIColor)); } } /// /// UI 展示状态-图标 /// public string _scanner2UIIcon; public string Scanner2UIIcon { //get { return plcUIIcon; } //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } get => _scanner2UIIcon; set { _scanner2UIIcon = value; RaisePropertyChanged(nameof(Scanner2UIIcon)); } } /// /// 扫码器2连接状态-true:连接成功;false:失败 /// /// public void LinerScannerState(bool type) { try { Application.Current.Dispatcher.Invoke(() => { if (type) { Scanner2UIStatusWb = "内胆扫码器"; Scanner2UIColor = "Green"; Scanner2UIIcon = "templates/image/Green.png"; } else { Scanner2UIStatusWb = "内胆扫码器"; Scanner2UIColor = "Red"; Scanner2UIIcon = "templates/image/Red.png"; } }); } catch (Exception ex) { logHelper.Error(ex.Message.ToString()); } } #endregion public string _currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); public string CurrentDateTime { get => _currentDateTime; set { _currentDateTime = value; RaisePropertyChanged(nameof(CurrentDateTime)); } } public string _shiftStr = string.Empty; public string ShiftStr { get => _shiftStr; set { _shiftStr = value; RaisePropertyChanged(nameof(ShiftStr)); } } private void Timer_Tick(object sender, EventArgs e) { DateTime now = DateTime.Now; // 判断当前是否是白班时间段 if (now.Hour >= 8 && now.Hour < 20) { ShiftStr = $"白班 08点-20点"; } else { ShiftStr = $"夜班 20点-08点"; } CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } } }