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.

395 lines
14 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 AutoUpdaterDotNET;
using MaterialTraceability.Business;
using MaterialTraceability.Entity.DTO;
using MaterialTraceability.SqlSugar;
using MaterialTraceabilityUI.Common;
using SqlSugar;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
namespace MaterialTraceabilityUI
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region 界面引用
//凹版页面
private intaglioPage intaglioPage = new intaglioPage();
//涂布界面
private CoatingProcess coatingProcess = new CoatingProcess();
//冷压页面
private firstPage firstPage = new firstPage();
//模切页面
private DieCuttingProcess dieCuttingProcess = new DieCuttingProcess();
//记录报表
private secondPage secondPage = new secondPage();
private ConfigInfo configInfo = new ConfigInfo();
private SqlSugarClient _db = SqlGenerator.GetMySqlInstance();
private TBAlarmRecord alarmRecordControl = new TBAlarmRecord();
#endregion
private readonly NotifyIcon notifyIcon = new NotifyIcon();
private AppConfigDto appConfig = AppConfigDto.Instance;
public MainWindow()
{
try
{
InitializeComponent();
AutoUpdater.Start("file://172.21.19.24/temp每天凌晨2点清理/临时文件/BHL/RFID物料追溯系统-现场更新/溧阳五程序/update.xml");
LogHelperBusiness.LogInfo("版本获取地址file://172.21.19.24/temp每天凌晨2点清理/临时文件/BHL/RFID物料追溯系统-现场更新/溧阳五程序/update.xml");
string info = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string resource = appConfig.resource;
this.titleText.Text = resource;
UpLoadBusiness loadBusiness = UpLoadBusiness.Instance;
if (loadBusiness.JudgeResourceIsUplpadData())
{
this.titleTextEngilsh.Text = "RFID material tracking system - Data Upload LY5 v" + info;
}
else
{
this.titleTextEngilsh.Text = "RFID material tracking system LY5 v" + info;
}
DataContext = this;
MainBusiness.ViewModelRefreshEvent += PlcStatus;
//WriteIniByConfig();
new MainBusiness();
} catch (Exception ex)
{
LogHelperBusiness.LogError("MainWindow构造函数初始化异常", ex);
}
}
private void WriteIniByConfig()
{
//initFlag
IniFileHelper inifile = new IniFileHelper(System.Environment.CurrentDirectory + "/App.InI");
if (inifile.IniReadValue("SystemConfig", "initFlag") == "YES")
{
inifile.IniWriteValue("SystemConfig", "plcAddress", ConfigHelper.GetConfig("plcAddress"));
inifile.IniWriteValue("SystemConfig", "plcPort", ConfigHelper.GetConfig("plcPort"));
inifile.IniWriteValue("SystemConfig", "machineId", ConfigHelper.GetConfig("machineId"));
inifile.IniWriteValue("SystemConfig", "processId", ConfigHelper.GetConfig("processId"));
inifile.IniWriteValue("SystemConfig", "resource", ConfigHelper.GetConfig("resource"));
inifile.IniWriteValue("SystemConfig", "operation", ConfigHelper.GetConfig("operation"));
inifile.IniWriteValue("SystemConfig", "operationByA", ConfigHelper.GetConfig("operationByA"));
inifile.IniWriteValue("SystemConfig", "operationByB", ConfigHelper.GetConfig("operationByB"));
inifile.IniWriteValue("SystemConfig", "isMesFlag", ConfigHelper.GetConfig("isMesFlag"));
inifile.IniWriteValue("SystemConfig", "plcSingalReadTime", ConfigHelper.GetConfig("plcSingalReadTime"));
inifile.IniWriteValue("SystemConfig", "initFlag","NO");
}
}
private void show_Click(object Sender,EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
WindowState = WindowState.Normal;
}
Show();
Activate();
}
private void exit_Clikc(object Sender, EventArgs e)
{
if("2".Equals(ConfigHelper.GetConfig("roleId")))
{
ConfigHelper.UpdateAppConfig("userName", null);
ConfigHelper.UpdateAppConfig("roleId", null);
LogHelperBusiness.LogInfo("程序正常退出");
Environment.Exit(0);
}
else
{
System.Windows.MessageBox.Show("无权限退出,请登录系统管理账号进行操作!");
}
}
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
Hide();
base.OnClosing(e);
}
/// <summary>
/// 窗体初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
notifyIcon.Visible = true;
notifyIcon.Text = "RFID物料追溯系统";
notifyIcon.Icon = new Icon("amn06-tnywh-001.ico");
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(show_Click);
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem show = new System.Windows.Forms.MenuItem("显示");
show.Click += new EventHandler(show_Click);
notifyIcon.ContextMenu.MenuItems.Add(show);
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
exit.Click += new EventHandler(exit_Clikc);
notifyIcon.ContextMenu.MenuItems.Add(exit);
string processId = appConfig.processId;
if (processId == "TB")
{
UserContent = coatingProcess;
this.alarmButton.Visibility = Visibility.Visible;
}
else if (processId.Contains("LY"))
{
UserContent = firstPage;
}
else if (processId.Contains("AB"))
{
UserContent = intaglioPage;
}
else
{
UserContent = dieCuttingProcess;
}
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面Load异常", ex);
}
}
/// <summary>
/// PLC连接状态
/// </summary>
/// <param name="viewModelDto"></param>
public void PlcStatus(ViewModelDto viewModelDto)
{
Action action;
//viewModelDto.plcStatus = true;
if (viewModelDto.plcStatus)
{
action = () =>
{
this.TestTB.Text = "PLC连接成功";
this.TestTB.Foreground = System.Windows.Media.Brushes.SpringGreen;
};
TestTB.Dispatcher.BeginInvoke(action);
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/正常.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.PlcStatusImage.ImageSource = bitmapImage;
};
PlcStatusImage.Dispatcher.BeginInvoke(action);
}
else
{
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/失败-01.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.PlcStatusImage.ImageSource = bitmapImage;
};
PlcStatusImage.Dispatcher.BeginInvoke(action);
action = () =>
{
this.TestTB.Text = "PLC连接失败";
this.TestTB.Foreground = System.Windows.Media.Brushes.Red;
};
TestTB.Dispatcher.BeginInvoke(action);
}
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void IndexPage_Click(object sender, RoutedEventArgs e)
{
//string processId = sysClients.Where(x => x.paramKey.Equals("processId")).FirstOrDefault().paramValue;
string processId = appConfig.processId;
if (processId == "TB")
{
UserContent = coatingProcess;
}
else if (processId.Contains("AB"))
{
UserContent = intaglioPage;
}
else if (processId.Contains("LY"))
{
UserContent = firstPage;
}
else
{
UserContent = dieCuttingProcess;
}
}
/// <summary>
/// 记录报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RecordPage_Click(object sender, RoutedEventArgs e)
{
UserContent = secondPage;
}
/// <summary>
/// 参数配置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ConfigPage_Click(object sender, RoutedEventArgs e)
{
if (!"2".Equals(ConfigHelper.GetConfig("roleId")))
{
System.Windows.MessageBox.Show("请登录系统管理账号进行操作");
}
else
{
UserContent = configInfo;
}
}
public System.Windows.Controls.UserControl _content;
public System.Windows.Controls.UserControl UserContent
{
get { return _content; }
set
{
_content = value;
OnPropertyChanged("UserContent");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private System.Timers.Timer timer = new System.Timers.Timer(1000 * 60 * 5);
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
UserLogin login = new UserLogin();
login.ShowDialog();
if ((bool)login.DialogResult)
{
this.loginUser.Text = ConfigHelper.GetConfig("userName");
timer = new System.Timers.Timer(1000 * 60 * 3);
if (!timer.Enabled)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(ReadRfidByTimer);
timer.AutoReset = true;
timer.Enabled = false;
timer.Start();
}
}
}
private async void ReadRfidByTimer(object source, System.Timers.ElapsedEventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
timer.Close();
timer.Dispose();
}
App.Current.Dispatcher.Invoke(() =>
{
try
{
ConfigHelper.UpdateAppConfig("userName", null);
ConfigHelper.UpdateAppConfig("roleId", null);
this.loginUser.Text = "请登录";
}
catch (Exception ex)
{
LogHelperBusiness.LogError("定时退出用户信息异常", ex);
}
});
}
/// <summary>
/// 注销
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RadioButton_Click_1(object sender, RoutedEventArgs e)
{
try
{
ConfigHelper.UpdateAppConfig("userName", null);
ConfigHelper.UpdateAppConfig("roleId", null);
System.Windows.MessageBox.Show("注销成功");
this.loginUser.Text = "请登录";
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("注销异常");
}
}
private void alarmButton_Click(object sender, RoutedEventArgs e)
{
UserContent = alarmRecordControl;
}
//获取新版本更新
private void Get_NewVersion(object sender, RoutedEventArgs e)
{
AutoUpdater.Start("file://172.21.19.24/temp每天凌晨2点清理/临时文件/BHL/RFID物料追溯系统-现场更新/溧阳五程序/update.xml");
}
}
}