You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Aucma.Scada/Aucma.Scada.UI/viewModel/MainViewModel.cs

635 lines
19 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
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<string, IPlc> _plcDictionary = new Dictionary<string, IPlc>();
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();
// MvCodeHelper.RefreshStateEvent += RefreshScannerState;
Scanner1State(false);
Scanner2State(false);
TouchSocketBusiness.RefreshStateEvent += RefreshScannerState;
// 创建一个DispatcherTimer对象
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1); //间隔1秒
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
ControlOnClickCommand = new RelayCommand<object>(obj => ControlOnClick(obj));
OpenSystemKeyboardCommand = new RelayCommand(OpenSystemKeyboard);
FormControlCommand = new RelayCommand<object>(obj => FormControl(obj));
CloseCommand = new RelayCommand<object>(obj => CloseWindow(obj));
MinCommand = new RelayCommand<object>(obj => MinWindow(obj));
UserContent = inStoreInfoControl;
// InitHikRobot();
init();
}
/// <summary>
/// 开启海康扫码器
/// </summary>
public void InitHikRobot()
{
try
{
Task.Run(() =>
{
Thread.Sleep(2000);
MvCodeHelper.Shell();
});
Task.Run(() =>
{
Thread.Sleep(4000);
MvCodeHelper.Liner();
});
}
catch (Exception ex)
{
Console.WriteLine("InitHikRobotAndGun()开启海康扫码器和扫码枪方法异常" + ex.Message.ToString());
}
}
#region 参数定义
/// <summary>
/// PLC设备状态
/// </summary>
private int _PlcStatus = 0;
public int PlcStatus
{
get { return _PlcStatus; }
set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); }
}
/// <summary>
/// 箱壳扫码器状态
/// </summary>
private int _ShellScannerStatus = 0;
public int ShellScannerStatus
{
get { return _ShellScannerStatus; }
set { _ShellScannerStatus = value; RaisePropertyChanged(nameof(ShellScannerStatus)); }
}
/// <summary>
/// 内胆扫码器状态
/// </summary>
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 事件定义
/// <summary>
/// 界面跳转按钮事件
/// </summary>
public RelayCommand<object> ControlOnClickCommand { get; set; }
/// <summary>
/// 打开系统键盘
/// </summary>
public RelayCommand OpenSystemKeyboardCommand { get; set; }
/// <summary>
/// 窗体控制
/// </summary>
public RelayCommand<object> FormControlCommand { get; set; }
/// <summary>
/// 最小化
/// </summary>
public RelayCommand<object> MinCommand { get; set; }
/// <summary>
/// 退出
/// </summary>
public RelayCommand<object> CloseCommand { get; set; }
#endregion
/// <summary>
/// 界面跳转
/// </summary>
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);
}
}
/// <summary>
/// 打开系统键盘
/// </summary>
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);
}
}
/// <summary>
/// 窗体控制
/// </summary>
/// <param name="obj"></param>
private void FormControl(object obj)
{
try
{
string controlType = obj as string;
switch (controlType)
{
// 还原 或者 最大化当前窗口
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;
default:
break;
}
}
catch (Exception ex)
{
logHelper.Error("窗体控制逻辑异常", ex);
}
}
#region 最小化界面
private void MinWindow(object parameter)
{
var window = parameter as Window;
if (window == null) return;
window.WindowState = WindowState.Minimized;
}
#endregion
#region 关闭当前界面
public void CloseWindow(object parameter)
{
var window = parameter as Window;
if (window == null) return;
if (MessageBox.Show("确定要退出系统吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
appConfig.TeamCode = "";
appConfig.TeamName = "";
appConfig.Account = "";
window.Hide();
//跳转到登录页
LoginPageView login = LoginPageView.Instance;
login.Show();
// window.Close();
}
}
#endregion
public void init()
{
TeamName = $"班组:{appConfig.TeamName} 用户:{appConfig.Account}";
// 设备状态刷新定时器
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 班组信息
/// <summary>
/// 班组信息
/// </summary>
public string _teamName;
public string TeamName
{
get => _teamName;
set { _teamName = value; RaisePropertyChanged(nameof(TeamName)); }
}
#endregion
#region 设备状态刷新
/// <summary>
/// 设备状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e)
{
RefreshMesDb();
RefreshPlc();
}
/// <summary>
/// 数据库状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshMesDb()
{
MesDbState(true);
}
/// <summary>
/// plc状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshPlc()
{
_plcDictionary = _pool.keyValuePairs;
// 箱壳状态
_plcDictionary.TryGetValue(appConfig.foamStoreCode, out IPlc _plc);
if (_plc != null)
{
PlcState(true);
}
else
{
PlcState(false);
}
}
#endregion
#region MES数据库状态
/// <summary>
/// MES数据库-文字
/// </summary>
public string _mesDbUIStatusWb;
public string MesDbUIStatusWb
{
get => _mesDbUIStatusWb;
set { _mesDbUIStatusWb = value; RaisePropertyChanged(nameof(MesDbUIStatusWb)); }
}
/// <summary>
/// MES数据库-颜色
/// </summary>
public string _mesDbUIColor;
public string MesDbUIColor
{
get => _mesDbUIColor;
set { _mesDbUIColor = value; RaisePropertyChanged(nameof(MesDbUIColor)); }
}
/// <summary>
/// MES数据库-图标
/// </summary>
public string _mesUIIcon;
public string MesUIIcon
{
get => _mesUIIcon;
set { _mesUIIcon = value; RaisePropertyChanged(nameof(MesUIIcon)); }
}
/// <summary>
/// MES数据库连接状态
/// </summary>
/// <param name="type"></param>
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 状态
/// <summary>
/// UI plc 展示状态-文字
/// </summary>
public string _plcUIStatusWb;
public string PlcUIStatusWb
{
get => _plcUIStatusWb;
set { _plcUIStatusWb = value; RaisePropertyChanged(nameof(PlcUIStatusWb)); }
}
/// <summary>
/// UI plc 展示状态-颜色
/// </summary>
public string _plcUIColor;
public string PlcUIColor
{
get => _plcUIColor;
set { _plcUIColor = value; RaisePropertyChanged(nameof(PlcUIColor)); }
}
/// <summary>
/// UI plc 展示状态-图标
/// </summary>
public string _plcUIIcon;
public string PlcUIIcon
{
get => _plcUIIcon;
set { _plcUIIcon = value; RaisePropertyChanged(nameof(PlcUIIcon)); }
}
/// <summary>
/// PLC连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
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 扫码器1状态
/// <summary>
/// UI 展示状态-文字
/// </summary>
public string _scanner1UIStatusWb;
public string Scanner1UIStatusWb
{
//get { return plcUIStatusWb; }
//set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); }
get => _scanner1UIStatusWb;
set { _scanner1UIStatusWb = value; RaisePropertyChanged(nameof(Scanner1UIStatusWb)); }
}
/// <summary>
/// UI 展示状态-颜色
/// </summary>
public string _scanner1UIColor;
public string Scanner1UIColor
{
//get { return plcUIColor; }
//set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); }
get => _scanner1UIColor;
set { _scanner1UIColor = value; RaisePropertyChanged(nameof(Scanner1UIColor)); }
}
/// <summary>
/// UI 展示状态-图标
/// </summary>
public string _scanner1UIIcon;
public string Scanner1UIIcon
{
//get { return plcUIIcon; }
//set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); }
get => _scanner1UIIcon;
set { _scanner1UIIcon = value; RaisePropertyChanged(nameof(Scanner1UIIcon)); }
}
public void RefreshScannerState(string ip, bool flag)
{
if (ip == appConfig.foamHikRobotIp)
{
Scanner1State(flag);
}
else if (ip == appConfig.foamOutHikRobotIp)
{
Scanner2State(flag);
}
}
/// <summary>
/// 扫码器1连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
public void Scanner1State(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
Scanner1UIStatusWb = "入库扫码器";
Scanner1UIColor = "Green";
Scanner1UIIcon = "templates/image/Green.png";
}
else
{
Scanner1UIStatusWb = "入库扫码器";
Scanner1UIColor = "Red";
Scanner1UIIcon = "templates/image/Red.png";
}
});
}
#endregion
#region 扫码器2状态
/// <summary>
/// UI 展示状态-文字
/// </summary>
public string _scanner2UIStatusWb;
public string Scanner2UIStatusWb
{
get => _scanner2UIStatusWb;
set { _scanner2UIStatusWb = value; RaisePropertyChanged(nameof(Scanner2UIStatusWb)); }
}
/// <summary>
/// UI 展示状态-颜色
/// </summary>
public string _scanner2UIColor;
public string Scanner2UIColor
{
get => _scanner2UIColor;
set { _scanner2UIColor = value; RaisePropertyChanged(nameof(Scanner2UIColor)); }
}
/// <summary>
/// UI 展示状态-图标
/// </summary>
public string _scanner2UIIcon;
public string Scanner2UIIcon
{
get => _scanner2UIIcon;
set { _scanner2UIIcon = value; RaisePropertyChanged(nameof(Scanner2UIIcon)); }
}
/// <summary>
/// 扫码器1连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
public void Scanner2State(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
Scanner2UIStatusWb = "出库扫码器";
Scanner2UIColor = "Green";
Scanner2UIIcon = "templates/image/Green.png";
}
else
{
Scanner2UIStatusWb = "出库扫码器";
Scanner2UIColor = "Red";
Scanner2UIIcon = "templates/image/Red.png";
}
});
}
#endregion
public string _currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
public string CurrentDateTime
{
get => _currentDateTime;
set { _currentDateTime = value; RaisePropertyChanged(nameof(CurrentDateTime)); }
}
#region 定时刷新时间
void timer_Tick(object sender, EventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
{
TeamName = $"班组:{appConfig.TeamName} 用户:{appConfig.Account}";
CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}));
}
#endregion
}
}