using JinYuan.DAL;
using JinYuan.Models;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using JinYuan.Helper;
using JinYuan.MES;
using PLC;
using PLC.PLCAlarm;
using System.Xml.Linq;
using JinYuan.MES.Models;
using PLCCommunication.Modbus;
using System.Collections.Concurrent;
namespace JinYuan.VirtualDataLibrary
{
public class CommonMethods
{
///
/// 本地数据库
///
public static ISugarHepler db = new SQLSugarService();
///
/// 远程服务段数据库
///
public static ISugarHepler dbServer = new SQLSugarService();
///
/// 远程服务段数据库2
///
public static ISugarHepler dbServer2 = new SQLSugarService();
///
/// 全局的PLC链接
///
public static List plcDevices;
///
/// plc状态
///
public static readonly Dictionary> plcStatuses = new Dictionary>();
///
/// 报警
///
public static AlarmManager alarmManager = new AlarmManager();
///
/// MES接口
///
public static IMes hbgMes = new MesDataProcess();
///
/// 参数
///
public static ProductModelParam parRange = new ProductModelParam();
///
/// 全局钩子
///
public static ScanerHook listener = new ScanerHook();
///
/// 创建个全局焊接功率队列数据存储
///
public static List> QWeldingPowerData = new List>();
///
/// 创建个全局入壳推力队列数据存储
///
public static List> QPlanenessData = new List>();
///
/// 创建全局的登录对象
///
public static SysAdmin currentAdmin = null;
///
/// 登录是否成功
///
public static bool IsLoginOk = false;
//创建一个全局的配置对象
public static SysConfig sysConfig = new SysConfig();
public static int InvadTime;
///
/// 智能电表
///
public static List listElectricEnergy = new List();
///
/// 电能表
///
public static EnergyMeterHelper energyMeter = new EnergyMeterHelper();
///
///是否开启刷新
///
public static bool isRefreshTimerWeeding = false;
///
/// SQL语句
///
public static DBSQL dBSQL { get; } = new DBSQL();
public static M_DBCtrl m_DBCtrl { get; } = new M_DBCtrl();
///
/// MES配置文件类
///
public static MesConfig mesConfig = new MesConfig();
///创建一个全局的连接状态
public static bool connectState = false;
///
/// 滴答次数
///
public static int tickCount = 0;
///
/// QIEHUAN
///
public static int tickHomeCount = 0;
///
/// 报警信息
///
public static string SlideAlarmText = "当前无报警";
///
/// 退出
///
public static bool IsExit = false;
///
/// 是否登出账户
///
public static bool LoginOut = false;
///
/// 是否上传MES
///
public static bool IsUpMes { get; set; }
///
/// 是否运行
///
public static bool IsRun { get; set; } = false;
///
/// 上一次设备状态
///
public static int DeviceStatus = 0;
///
/// 上一次Guid
///
public static string DeviceGuid = "";
///
///第一次读取
///
public static bool IsFirst = false;
///
/// 焊接功率波形图显示条数
///
public static int ChatLineCount = 3;
///
/// 员工号
///
public static string strWorker { get; set; }
///
/// 是否开启智能电表
///
public static bool IsStartZNDB { get; set; } = false;
///
/// 白夜班
///
public static string strClass { get; set; } = "白班";
///
/// 时间变更
///
public static string ChangeClassTime { get; set; } = "20:30:00";
///
/// 设备状态
///
public static List Statues { get; set; }
///
/// 服务器链接状态
///
public static bool EMCStatues { get; set; }
#region 小时产能
///
/// 上个时间点的总数
///
public static int LastTotalNum { get; set; } = 0;
///
/// 上个时间点良品数
///
public static int LastOkNum { get; set; } = 0;
///
/// 上一次重置计数的时间
///
public static string LastResetTime { get; set; }
///
/// 上一次的小时
///
public static int LastHour { get; set; }
#endregion
///
///
///
public static List ListOmronGroups { get; set; }
///
/// 12小时产能
///
public static List Lst_Prod12HourData = new List();
///
/// 不良选项(检测项不良)
///
public static List Lst_DefectTypeQty = new List();
///
/// PLC报警前10项
///
public static List Lst_AlarmTop10_Qty = new List();
///
/// 参数项设置
///
public static List paramlists { get; set; }
///
/// 线程控制
///
public static ManualResetEvent ResetEventRecv = new ManualResetEvent(true);
///
/// 电表连接
///
public static List EM_modbusRtus = new List();
///
/// 电表连接结果
///
public static List EM_ComResult = new List();
#region 计数项
///
/// 生产总数
///
public static int TotalQty = 0;
public static int ProOKQty = 0;
public static int Ppm = 0;
public static double JiaDL = 0.0;
public static List AntiDustAirSpeed = new List();
///
/// PLC开机/运行时间
///
public static List PlcOpenAndRunTime = new List();
///
/// PLC开机/运行时间地址
///
public static List PlcOpenAndRunTimeAdress = new List { "D50", "D52", "D54", "D56", "D58", "D60" };
public static int ProdGaoRFIDNgQty = 0;//高托杯 绑定卷心码
public static int ProdDiRFIDNgQty = 0;//低托杯 绑定钢壳码
public static int ProdGangKeRFIDNgQty = 0;//钢壳托杯 绑定出站钢壳码
public static int ProdJuanBarNgQty = 0;
public static int ProdGangKeBarNgQty = 0;
public static int ProdRuKeTuiliNgQty = 0;
public static int ProdCCDNgQty = 0;
public static int ProdMesOutNGQty = 0;
#endregion
#region 配置信息
public static string devPath = Application.StartupPath + "\\Config\\Device.ini";
public static string groupPath = Application.StartupPath + "\\Config\\Group.xlsx";
public static string variablePath = Application.StartupPath + "\\Config\\Variable.xlsx";
public static string settingPath = Application.StartupPath + "\\Config\\settings.json";
///
/// 错误日志路径
///
// public static string strErrorLogspath = System.Windows.Forms.Application.StartupPath + "\\Logs\\ErrorLogs";
public static string strErrorLogspath = @"D:\Logs\ErrorLogs";
// public static string strSystemLogspath = System.Windows.Forms.Application.StartupPath + "\\Logs\\SystemLogs";
public static string strSystemLogspath = @"D:\Logs\SystemLogs";
//public static string strAlarmLogspath = System.Windows.Forms.Application.StartupPath + "\\Logs\\AlarmLogs";
public static string strAlarmLogspath = @"D:\Logs\AlarmLogs";
public static string strsqlLogspath = "";
///
/// 删除路径
///
public static string strDeletepath = "";
///
/// 能耗保存路径
///
public static string strWattrMeterpath = System.Windows.Forms.Application.StartupPath + "\\智能电表数据";
///
/// MES路径日志
///
public static string strMesLogspath = System.Windows.Forms.Application.StartupPath + "\\Logs\\MesLogs";
///
/// 设备状态信息路径
///
public static string strDeviceStatusPath = System.Windows.Forms.Application.StartupPath + "\\Config\\DeviceStatus.cfg";
///
/// PLC读取寄存器配置文件路径
///
public static string ConfigPath = Path.Combine(System.Windows.Forms.Application.StartupPath, "ini\\PlcConfig.ini");
///
/// 系统程序配置文件路径
///
//public static string iniFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "ini\\Configure.ini");
public static string xmlFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, "Config\\Configure.xml");
///
/// 配置文件字典
///
public static Dictionary> Configxml = new Dictionary>();
#region 读取配置文件
// 读取 XML 配置文件
public static Dictionary> ReadXmlConfig(string filePath)
{
var config = new Dictionary>();
XDocument xmlDoc = XDocument.Load(filePath);
foreach (var sectionElement in xmlDoc.Root.Elements())
{
string sectionName = sectionElement.Name.LocalName;
config[sectionName] = new Dictionary();
foreach (var keyElement in sectionElement.Elements())
{
string key = keyElement.Name.LocalName;
string value = keyElement.Value;
config[sectionName][key] = value;
}
}
return config;
}
// 获取配置值
public static string GetConfigValue(Dictionary> config, string section, string key, string defaultValue)
{
if (config.ContainsKey(section) && config[section].ContainsKey(key))
{
return config[section][key];
}
return defaultValue;
}
// 写入 XML 文件
public static void WriteXmlFile(Dictionary> config, string filePath)
{
XDocument xmlDoc = new XDocument(new XElement("Configuration"));
foreach (var section in config)
{
var sectionElement = new XElement(section.Key);
foreach (var keyValue in section.Value)
{
sectionElement.Add(new XElement(keyValue.Key, keyValue.Value));
}
xmlDoc.Root.Add(sectionElement);
}
xmlDoc.Save(filePath);
}
///
/// 单个配置数据保存
///
///
///
///
public static void UpdateConfig(string INFO, string Type, string value)
{
// 更新 Configxml 中的 SteelshellCount
CommonMethods.Configxml[INFO][Type] = value;
// 将更新后的 Configxml 写入文件
CommonMethods.WriteXmlFile(CommonMethods.Configxml, CommonMethods.xmlFilePath);
}
///
///读取配置文件
///
public static SysConfig GetSysConfig()
{
try
{
#region 系统配置文件
return sysConfig = new SysConfig
{
AutoStart = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "AutoStart", "false")),
AutoLogin = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "AutoLogin", "false")),
AutoLock = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "AutoLockScreen", "false")),
LockPeriod = Convert.ToInt32(GetConfigValue(Configxml, "CONFIG_INFO", "LockIntervalTime", "0")),
AutoQieHuanM = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "AutoSwitchMonitor", "false")),
QieHuanMPeriod = Convert.ToInt32(GetConfigValue(Configxml, "CONFIG_INFO", "SwitchMonitorInterval", "0")),
ShowSeriesCount = Convert.ToInt32(GetConfigValue(Configxml, "CONFIG_INFO", "CurveDisplayCount", "0")),
SwipeCardMode = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "EnableCardMode", "false")),
MesModeSwitching = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "MESModeSwitch", "false")),
GetMesParam = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "FetchMESParams", "false")),
SaveThreadTime = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "SaveThreadTimeLog", "false")),
IsDebugMode = Convert.ToBoolean(GetConfigValue(Configxml, "CONFIG_INFO", "IsDebugMode", "false"))
};
#endregion
}
catch (Exception)
{
return sysConfig = null;
}
}
///
/// 获取MES配置文件
///
public static MesConfig GetMesConfig()
{
try
{
#region MES配置文件
return mesConfig = new MesConfig
{
siteCode = GetConfigValue(Configxml, "MES_CONFIG", "SiteCode", "49J"),
lineCode = GetConfigValue(Configxml, "MES_CONFIG", "LineCode", "PW-491"),
equipNum = GetConfigValue(Configxml, "MES_CONFIG", "EquipNum", "491VIM01"),
MESURLAddressIP = GetConfigValue(Configxml, "MES_CONFIG", "MESURLAddressIP", "10.61.135.254"),
MESURLAddressPort = GetConfigValue(Configxml, "MES_CONFIG", "MESURLAddressPort", "80"),
EmployeeAuthCheck = GetConfigValue(Configxml, "MES_CONFIG", "EmployeeAuthCheck", "491VIM01"),
MaterialInputInfo = GetConfigValue(Configxml, "MES_CONFIG", "MaterialInputInfo", "491VIM01"),
DeviceParamRequest = GetConfigValue(Configxml, "MES_CONFIG", "DeviceParamRequest", "491VIM01"),
DeviceParamChange = GetConfigValue(Configxml, "MES_CONFIG", "DeviceParamChange", "491VIM01"),
ProductInStation = GetConfigValue(Configxml, "MES_CONFIG", "ProductInStation", "491VIM01"),
ProductProcessParams = GetConfigValue(Configxml, "MES_CONFIG", "ProductProcessParams", "491VIM01"),
ProductResultParams = GetConfigValue(Configxml, "MES_CONFIG", "ProductResultParams", "491VIM01"),
ProductOutStation = GetConfigValue(Configxml, "MES_CONFIG", "ProductOutStation", "491VIM01"),
DeviceAlarm = GetConfigValue(Configxml, "MES_CONFIG", "DeviceAlarm", "491VIM01"),
DeviceStatus = GetConfigValue(Configxml, "MES_CONFIG", "DeviceStatus", "491VIM01"),
EnergyConsumption = GetConfigValue(Configxml, "MES_CONFIG", "EnergyConsumption", "491VIM01"),
Anemometer = GetConfigValue(Configxml, "MES_CONFIG", "Anemometer", "491VIM01"),
};
#endregion
}
catch (Exception)
{
return mesConfig = null;
}
}
#endregion
#region 保存配置文件
///
/// 保存启动自动登录
///
///
///
public static bool SaveAutoStart(bool value)
{
Configxml["CONFIG_INFO"]["AutoStart"] = value.ToString(); // 修改自动登录为 True
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存开机自动启动
///
///
///
public static bool SaveAutoLogin(bool value)
{
Configxml["CONFIG_INFO"]["AutoLogin"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存无操作自动锁屏
///
///
///
public static bool SaveAutoLock(bool value)
{
Configxml["CONFIG_INFO"]["AutoLockScreen"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存锁屏间隔时间
///
///
///
public static bool SaveLockPeriod(int value)
{
Configxml["CONFIG_INFO"]["LockIntervalTime"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存无操作自动切换监控界面
///
///
///
public static bool SaveAutoQieHuanM(bool value)
{
Configxml["CONFIG_INFO"]["AutoSwitchMonitor"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存无操作自动切换监控界面
///
///
///
public static bool SaveIsDebug(bool value)
{
Configxml["CONFIG_INFO"]["IsDebugMode"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存切换监控界面间隔时间
///
///
///
public static bool SaveQieHuanMPeriod(int value)
{
Configxml["CONFIG_INFO"]["SwitchMonitorInterval"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存曲线显示数量
///
///
///
public static bool SaveShowSeriesCount(int value)
{
Configxml["CONFIG_INFO"]["CurveDisplayCount"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 开启刷卡模式
///
///
///
public static bool SaveSwipeCardMode(bool value)
{
Configxml["CONFIG_INFO"]["EnableCardMode"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// MES/非MES模式
///
///
///
public static bool SaveOpenMesModel(bool value)
{
Configxml["CONFIG_INFO"]["MESModeSwitch"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 获取MES参数
///
///
///
public static bool SaveGetMesParam(bool value)
{
Configxml["CONFIG_INFO"]["FetchMESParams"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
///
/// 保存线程耗时操作
///
///
///
public static bool SaveSaveThreadTime(bool value)
{
Configxml["CONFIG_INFO"]["SaveThreadTimeLog"] = value.ToString();
WriteXmlFile(Configxml, xmlFilePath);
return true;
}
#endregion
#endregion
#region 委托信息
//创建委托
// 数据监视委托
public static Action AddDataMonitorDelegate;
///
/// 系统日志
///
public static Action AddLogDelegate;
///
/// 操作日志
///
public static Action AddOPLogDelegate;
///
/// 报警日志
///
public static Action AddAlarmDelegate;
///
/// 参数更新
///
public static Action ChangeParamDelegate;
///
///
///
public static Action AddHomeDelegate;
///
/// 显示上料信息
///
public static Action> ShowFeedingDelegate;
///
/// 显示钢壳信息
///
public static Action> ShowSteelShellDelegate;
///
/// 显示下料数据
///
public static Action> ShowBlankingDelegate;
///
/// 显示结构件料盘信息
///
public static Action> ShowMaterialDelegate;
///
/// 数据监视委托
///
///
///
public static void AddDataMonitorLog(int type, string info)
{
//调用委托
if (AddDataMonitorDelegate != null)
{
AddDataMonitorDelegate(type, info);
}
}
///
/// 写入系统日志
///
/// 是否为错误信息
/// 日志信息
public static void AddLog(bool isError, string info)
{
//调用委托
if (AddLogDelegate != null)
{
AddLogDelegate(isError, info);
}
}
///
/// 写入操作日志
///
///
///
public static void AddOPLog(bool isError, string info)
{
//调用委托
if (AddLogDelegate != null)
{
AddOPLogDelegate(isError, info);
}
}
public static void AddAlarmLog(SysLog sysLog)
{
//调用委托
if (AddAlarmDelegate != null)
{
AddAlarmDelegate(sysLog);
}
}
public static void ChangeParamData()
{
//调用委托
if (ChangeParamDelegate != null)
{
ChangeParamDelegate();
}
}
#endregion
///
/// TODO PLC变量集合
/// KEY = VarAddress
///
public static ConcurrentDictionary PLCVariable = new ConcurrentDictionary();
}
}