Files
CleanMachine/CleanMachineHMI/Utils/GlobalHelper.cs
gongxh a7afd29d82 1
2022-06-01 14:33:10 +08:00

379 lines
13 KiB
C#

using CleanMachine.Model;
using CleanMachine.Model.BAS;
using CleanMachine.Model.CM;
using CleanMachineHMI.Domain.Enums;
using CleanMachineHMI.Domain.Models.ViewModels;
using CleanMachineHMI.forms;
using CleanMachineHMI.frmChild;
using CleanMachineHMI.Utils;
using Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace CleanMachineHMI
{
/// <summary>
/// 应用配置
/// </summary>
public static class Global
{
/// <summary>
/// 主窗体_New
/// </summary>
public static frmMainNew MainFormNew { get; set; }
/// <summary>
/// 登陆窗体
/// </summary>
public static frmLogin LoginForm { get; set; }
/// <summary>
/// 基础信息窗体容器
/// </summary>
public static frmBase BaseForm { get; set; }
public static frmIoMonitorNew IoMonitor_2 { get; set; }
/// <summary>
/// 所有窗体模型
/// </summary>
public static List<FormModel<Type>> FrmModelList { get; set; }
/// <summary>
/// 语言编码
/// </summary>
public static string LanguageCode { get; set; } = AppConfigHelper.Get("LanguageCode");
/// <summary>
/// 语言字典
/// </summary>
public static Dictionary<string, string> ThemeLanguage = null;
/// <summary>
/// 用户信息
/// </summary>
public static UserInfo UserInfo { get; set; } = null;
/// <summary>
/// 参数列表
/// </summary>
// public static DataTable ParameterList { get; set; }
public static List<CleanMachine.Model.SYS.Parameter> ParameterList { get; set; }
/// <summary>
/// 设备编号
/// </summary>
public static string MacCode
{
get
{
return ParameterList.First(m => m.ParameterCode == "CleanMachineName").ParameterValue;
}
}
public static IEnumerable<CleanMachine.Model.SYS.Parameter> Parameters;
public static void LoadParameters()
{
Parameters = new CleanMachine.BLL.SYS.ParameterBLL().GetList("");
}
public static int MaintainAlarmTime
{
get
{
if (Parameters == null)
{
LoadParameters();
}
return int.Parse(Parameters.FirstOrDefault(m => m.ParameterCode == "MaintainAlarmTime").ParameterValue);
}
}
public static int MaintainTime
{
get
{
if (Parameters == null)
{
LoadParameters();
}
return int.Parse(Parameters.FirstOrDefault(m => m.ParameterCode == "MaintainTime").ParameterValue);
}
}
//int maintainAlarmTime = int.Parse(sysParameters.FirstOrDefault(m => m.ParameterCode == "MaintainAlarmTime").ParameterValue);
//int maintainTime = int.Parse(sysParameters.FirstOrDefault(m => m.ParameterCode == "MaintainTime").ParameterValue);
/// <summary>
/// 角色菜单权限数据
/// </summary>
// public static DataTable RoleMenuPermission { get; set; }
public static List<CleanMachine.Model.SYS.RoleMenuPermission> RoleMenuPermission { get; set; }
public static bool CheckRole(string FormName, string ControlName)
{
//DataRow[] drs = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{this.Name}' AND MENU_PID = '-1'");
//DataRow[] drs1 = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{item.Name}' AND MENU_PID = '{drs[0]["MENU_ID"]}'");
var drs = RoleMenuPermission.FirstOrDefault(m => m.MenuUrl == FormName && m.MenuPid == -1);
if (drs == null)
{
return false;
}
var drs1 = RoleMenuPermission.FirstOrDefault(m => m.MenuUrl == ControlName && m.MenuPid == drs.MenuId);
return drs1 != null;
}
public static bool CheckRole(string FormName)
{
//DataRow[] drs = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{this.Name}' AND MENU_PID = '-1'");
//DataRow[] drs1 = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{item.Name}' AND MENU_PID = '{drs[0]["MENU_ID"]}'");
var drs = RoleMenuPermission.FirstOrDefault(m => m.MenuUrl == FormName && m.MenuPid == -1);
return drs != null;
}
/// <summary>
///
/// </summary>
//public static List<PlcItem> PlcItemMdList { get; set; } = new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_D_List { get; set; } = new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_M_List { get; set; }=new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_X_List { get; set; }=new List<PlcItem>();
//public static List<PlcItem> PlcItemMd_Y_List { get; set; }=new List<PlcItem>();
public static List<WarningLog> CheckWarng(ushort id, string PlcItem)
{
var result = new List<WarningLog>();
var list = BasWarningMd_List.FindAll(m => m.PlcItemCode == PlcItem);
foreach (var wag in list)
{
if ((id & wag.bitCode) > 0)
{
result.Add(new WarningLog()
{
WarningCode = wag.WarningCode,
WarningName = wag.WarningName,
WarningType = wag.WarningType,
WarningMessage = wag.WarningMessage,
WarningTime = DateTime.Now,
PlcItemCode = PlcItem,
WaringStatus = 0,
WarningHandleResult = 0,
CreateBy = 1,
CreateTime = DateTime.Now,
UpdateBy = 1,
UpdateTime = DateTime.Now,
DataVersion = 0,
});
}
}
return result;
}
public static List<Warning> BasWarningMd_List { get; set; }
// public static Dictionary<string, WarningLog> CmWarningLogMd_List { get; set; } = new Dictionary<string, WarningLog>();
public static List<WarningLog> CmWarningLogMd_List_1 { get; set; }
/// <summary>
/// 参数列表
/// </summary>
public static bool CanOperate { get; set; }
/// <summary>
/// 系统是否运行
/// </summary>
public static bool SysThIsRunFlag { get; set; }
#region 委托事件
//定义一个委托类型的事件
public static bool eventLock = false;
//public static event EventHandler<EventArgs> OnShowX_Y_Status;
//public static void ShowX_Y_Status(object sender, EventArgs e)
//{
// if (!eventLock)
// {
// OnShowX_Y_Status?.Invoke(sender, e);
// }
//}
//定义一个委托类型的事件
public static event EventHandler<EventArgs> OnShowSensorStatus;
public static void ShowSensorStatus(object sender, EventArgs e)
{
if (!eventLock)
{
OnShowSensorStatus?.Invoke(sender, e);
}
}
// 定义一个委托类型的事件
public static frmPopupWarningMsg PopupWarningMsgForm { get; set; }
//定义一个委托类型的事件
public static event EventHandler<EventArgs> OnSyncFinishPalletData;
public static void SyncFinishPalletData(object sender, EventArgs e)
{
OnSyncFinishPalletData?.Invoke(sender, e);
}
#endregion
/// <summary>
/// 获取拖盘号
/// </summary>
/// <returns></returns>
public static string GetPalletCode()
{
string currDateStr = DateTime.Now.ToString("yyyyMMdd");
string PalletIndexPrefix = AppConfigHelper.Get("PalletIndexPrefix");
string PalletIndexDateMsg = AppConfigHelper.Get("PalletIndexDateMsg");
int PalletIndexLen = Convert.ToInt32(AppConfigHelper.Get("PalletIndexLen"));
int PalletIndexValue = Convert.ToInt32(AppConfigHelper.Get("PalletIndexValue"));
if (currDateStr == PalletIndexDateMsg)
{
PalletIndexValue++;
string tmpPalletCode = PalletIndexPrefix + currDateStr + PalletIndexValue.ToString().PadLeft(PalletIndexLen, '0');
AppConfigHelper.Update("PalletIndexValue", PalletIndexValue.ToString());
return tmpPalletCode;
}
else
{
PalletIndexValue = 1;
string tmpPalletCode = PalletIndexPrefix + currDateStr + PalletIndexValue.ToString().PadLeft(PalletIndexLen, '0');
AppConfigHelper.Update("PalletIndexValue", PalletIndexValue.ToString());
AppConfigHelper.Update("PalletIndexDateMsg", currDateStr);
return tmpPalletCode;
}
}
/// <summary>
/// 根据自定义特性和窗体的绑定原理,
/// 自动加载所有的窗体数据。
/// </summary>
public static void LoadAllFormData()
{
var types = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetCustomAttributes(typeof(FormTypeAttribute), false).Count() > 0);
FrmModelList = new List<FormModel<Type>>();
foreach (var item in types)
{
FormTypeAttribute tmpAttribute = (FormTypeAttribute)item.GetCustomAttribute(typeof(FormTypeAttribute));
FrmModelList.Add(new FormModel<Type>
{
//Form = (UserControl)Activator.CreateInstance(item),
Form = item,
FormType = tmpAttribute.Value
});
}
//MainForm = (frmMain)FrmModelList.Find(x => x.FormType == EnumFormType.Main).Form;
//MainFormNew = (frmMainNew)FrmModelList.Find(x => x.FormType == EnumFormType.Main).Form;
//LoginForm = (frmLogin)FrmModelList.Find(x => x.FormType == EnumFormType.Login).Form;
LoginForm = new frmLogin();
CanOperate = false;
}
/// <summary>
/// 加载主题
/// </summary>
public static void LoadTheme()
{
if (ThemeLanguage == null || ThemeLanguage.Count == 0)
{
// var appPath = System.Windows.Forms.Application.StartupPath.Split(new string[] { @"bin\Debug" }, StringSplitOptions.RemoveEmptyEntries)[0];
var filePath = $"Lang\\{Global.LanguageCode}.json";
var str = File.ReadAllText(filePath);
var lstLanguage = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ThemeLanguage>>(str);
var tmpThemeLanguage = new Dictionary<string, string>();
lstLanguage.ForEach(item =>
{
var key = item.formCode + "|" + item.controlCode;
var value = "";
if (item.options != null && item.options.Count > 0)
{
value = string.Join(",", item.options);
}
else
{
value = item.controlName;
}
if (tmpThemeLanguage.ContainsKey(key))
{
tmpThemeLanguage[key] = value;
}
else
{
tmpThemeLanguage.Add(key, value);
}
//if (item.options != null && item.options.Count > 0)
//{
// tmpThemeLanguage.Add(item.formCode + "|" + item.controlCode, string.Join(",", item.options));
//}
//else
//{
// tmpThemeLanguage.Add(item.formCode + "|" + item.controlCode, item.controlName);
//}
});
ThemeLanguage = tmpThemeLanguage;
}
}
//public static void LoadzhCN()
//{
// if (Utils.frmUtil.znCNLang == null || Utils.frmUtil.znCNLang.Count == 0)
// {
// var appPath = System.Windows.Forms.Application.StartupPath.Split(new string[] { @"bin\Debug" }, StringSplitOptions.RemoveEmptyEntries)[0];
// var filePath = appPath + $"Lang\\zh-CN.json";
// if (!File.Exists(filePath))
// {
// return;
// }
// var str = File.ReadAllText(filePath);
// Utils.frmUtil.znCNLang = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ThemeLanguage>>(str);
// }
//}
}
}