using MaiMuControl.SysStatusMgr.UserMgr; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ToolKits.Ini; using ToolKits.mAPI; namespace GeBoShi.SysCtrl { /// /// 设备参数 /// public class ConfMgr { #region 实例化 /// /// 实例 /// private static ConfMgr _instance; private static readonly object _lock = new object(); public static ConfMgr Instance { get { if (_instance == null) { lock (_lock) { if (_instance == null) { _instance = new ConfMgr(); } } } return _instance; } } #endregion #region 私有字段 private readonly string _rootPath; private readonly string _configFile; private readonly string _projectDir; private readonly string _pointsPath; private readonly string _markPicPath; #endregion private ConfMgr() { _rootPath = Path.Combine(Directory.GetCurrentDirectory(), "ConfigFiles"); _configFile = Path.Combine(_rootPath, "SysParamsConfig.ini"); _devConfigPath = Path.Combine(_rootPath, "DevParamsConfig.ini"); _projectDir = Path.Combine(_rootPath, "Project"); _pointsPath = Path.Combine(_rootPath, "PointsConfig.ini"); _markPicPath = Path.Combine(_rootPath, "MarkPic"); _specificationPath = Path.Combine(_rootPath, "革博士软件操作说明.docx"); DBConStr = "server = localhost; Database = LeatherDB; Uid = root; Pwd = " + ConteolUserDB.GetSqlDBCode() + "; AllowLoadLocalInfile = true;"; } #region 公开字段 //DB //有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求 //如果用到bulkCopy需要加: AllowLoadLocalInfile=true public string DBConStr = "server=localhost;Database=LeatherDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true"; /// /// 系统配置参数 /// public SysConfigParams SysConfigParams { get; set; } private string _devConfigPath; /// /// 获取驱动配制文件路径 /// public string DevConfigPath { get { return _devConfigPath; } } //public string ProjectDir { get { return _projectDir; } } public string ProjectDir { get { return SysConfigParams.ProductPath; } } public string MarkPicPath { get { return _markPicPath; } } /// /// 说明书路径 /// public string SpecificationPath { get { return _specificationPath; } } private string _specificationPath; /// /// 上料位X1 /// public double LoadPoint_X1 { get; set; } /// /// 上料位X2 /// public double LoadPoint_X2 { get; set; } /// /// 上料位Y /// public double LoadPoint_Y { get; set; } /// /// 上料位Z /// public double LoadPoint_Z { get; set; } /// /// 测厚气缸到位等待时间 /// public int HeightDoWait { get; set; } /// /// 加紧气缸到位等待时间 /// public int ProductDoWait { get; set; } #endregion #region 公开文件操作 #region 系统配置参数 /// /// 写入ini /// /// public void WriteSysConfParams(SysConfigParams SysParams) { //XmlHelper.Serialize(_configFile, sysConfParams); IniHelper iniHelper = new IniHelper(_configFile); //iniHelper.WriteBool("SaveAllImg", "SaveAllImg", SysParams.SaveAllImg); iniHelper.WriteString("DefectSrcImag", "SavePath", SysParams.DefectSrcImag.SavePath); iniHelper.WriteBool("DefectSrcImag", "AutoSave", SysParams.DefectSrcImag.AutoSave); iniHelper.WriteBool("DefectSrcImag", "AutoDelete", SysParams.DefectSrcImag.AutoDelete); iniHelper.WriteDouble("DefectSrcImag", "AutoDeleteDays", SysParams.DefectSrcImag.AutoDeleteDays); iniHelper.WriteString("DefectSmallImag", "SavePath", SysParams.DefectSmallImag.SavePath); iniHelper.WriteBool("DefectSmallImag", "AutoSave", SysParams.DefectSmallImag.AutoSave); iniHelper.WriteBool("DefectSmallImag", "AutoDelete", SysParams.DefectSmallImag.AutoDelete); iniHelper.WriteDouble("DefectSmallImag", "AutoDeleteDays", SysParams.DefectSmallImag.AutoDeleteDays); iniHelper.WriteString("DefectSplicImag", "SavePath", SysParams.DefectSplicImag.SavePath); iniHelper.WriteBool("DefectSplicImag", "AutoSave", SysParams.DefectSplicImag.AutoSave); iniHelper.WriteBool("DefectSplicImag", "AutoDelete", SysParams.DefectSplicImag.AutoDelete); iniHelper.WriteDouble("DefectSplicImag", "AutoDeleteDays", SysParams.DefectSplicImag.AutoDeleteDays); iniHelper.WriteString("Log", "SavePath", SysParams.LogPath); iniHelper.WriteBool("Log", "AutoDelete", SysParams.AutoDeleteLog); iniHelper.WriteDouble("Log", "AutoDeleteDays", SysParams.AutoDeleteLogData); iniHelper.WriteBool("SysFunction", "OpenBuzzer", SysParams.OpenBuzzer); iniHelper.WriteBool("SysFunction", "OpenDoor", SysParams.OpenDoor); iniHelper.WriteBool("SysFunction", "OpenIO", SysParams.OpenIO); iniHelper.WriteBool("SysFunction", "OpenPLC", SysParams.OpenPLC); iniHelper.WriteString("Dev", "LightCom", SysParams.LightCom); iniHelper.WriteInteger("Dev", "LightComBaud", SysParams.LightComBaud); iniHelper.WriteString("Dev", "IODevName", SysParams.IODevName); iniHelper.WriteString("Dev", "IOCfgPath", SysParams.IOCfgPath); iniHelper.WriteString("Sys", "AIModelPath", SysParams.AIModelPath); iniHelper.WriteString("Sys", "ProductPath", SysParams.ProductPath); iniHelper.WriteBool("ShowMainWin", "ShowResetBtn", SysParams.ShowResetBtn); iniHelper.WriteBool("ShowMainWin", "ShowBuzz", SysParams.ShowBuzz); iniHelper.WriteBool("ShowMainWin", "ShowDoorAlm", SysParams.ShowDoorAlm); iniHelper.WriteBool("CloudAndMES", "OpenCloud", SysParams.OpenCloud); iniHelper.WriteString("CloudAndMES", "CloudThisName", SysParams.CloudThisName); iniHelper.WriteString("CloudAndMES", "CloudServerIP", SysParams.CloudServerIP); iniHelper.WriteInteger("CloudAndMES", "CloudServerPort", SysParams.CloudServerPort); iniHelper.WriteString("CloudAndMES", "CloudUser", SysParams.CloudUser); iniHelper.WriteString("CloudAndMES", "CloudPassword", SysParams.CloudPassword); iniHelper.WriteBool("CloudAndMES", "OpenPdtServer", SysParams.OpenPdtServer); iniHelper.WriteString("CloudAndMES", "PdtServerIP", SysParams.PdtServerIP); iniHelper.WriteInteger("CloudAndMES", "PdtServerPort", SysParams.PdtServerPort); iniHelper.WriteBool("CloudAndMES", "IsPdtServer", SysParams.IsPdtServer); iniHelper.WriteBool("LabelPrint", "EnableLabelPrint", SysParams.EnableLabelPrint); iniHelper.WriteString("LabelPrint", "LabelPrinterName", SysParams.LabelPrinterName); iniHelper.WriteBool("ExcelPrint", "EnableExcelPrint", SysParams.EnableExcelPrint); iniHelper.WriteString("ExcelPrint", "ExcelPrinterName", SysParams.ExcelPrinterName); SysConfigParams = SysParams; } /// /// 读取ini /// /// public SysConfigParams ReadSysConfParams() { //return XmlHelper.Deserialize(_configFile); SysConfigParams sysParams = new SysConfigParams(); IniHelper iniHelper = new IniHelper(_configFile); string sValue = ""; int iValue = 0; double dValue = 0; bool bValue = false; //iniHelper.ReadBool("SaveAllImg", "SaveAllImg", out bValue); //sysParams.SaveAllImg = bValue; iniHelper.ReadString("DefectSrcImag", "SavePath", out sValue); sysParams.DefectSrcImag.SavePath = sValue.Replace("\0", ""); iniHelper.ReadBool("DefectSrcImag", "AutoSave", out bValue); sysParams.DefectSrcImag.AutoSave = bValue; iniHelper.ReadBool("DefectSrcImag", "AutoDelete", out bValue); sysParams.DefectSrcImag.AutoDelete = bValue; iniHelper.ReadDouble("DefectSrcImag", "AutoDeleteDays", out dValue); sysParams.DefectSrcImag.AutoDeleteDays = dValue; iniHelper.ReadString("DefectSmallImag", "SavePath", out sValue); sysParams.DefectSmallImag.SavePath = sValue.Replace("\0", ""); iniHelper.ReadBool("DefectSmallImag", "AutoSave", out bValue); sysParams.DefectSmallImag.AutoSave = bValue; iniHelper.ReadBool("DefectSmallImag", "AutoDelete", out bValue); sysParams.DefectSmallImag.AutoDelete = bValue; iniHelper.ReadDouble("DefectSmallImag", "AutoDeleteDays", out dValue); sysParams.DefectSmallImag.AutoDeleteDays = dValue; iniHelper.ReadString("DefectSplicImag", "SavePath", out sValue); sysParams.DefectSplicImag.SavePath = sValue.Replace("\0", ""); iniHelper.ReadBool("DefectSplicImag", "AutoSave", out bValue); sysParams.DefectSplicImag.AutoSave = bValue; iniHelper.ReadBool("DefectSplicImag", "AutoDelete", out bValue); sysParams.DefectSplicImag.AutoDelete = bValue; iniHelper.ReadDouble("DefectSplicImag", "AutoDeleteDays", out dValue); sysParams.DefectSplicImag.AutoDeleteDays = dValue; iniHelper.ReadString("Log", "SavePath", out sValue); sysParams.LogPath = sValue.Replace("\0", ""); iniHelper.ReadBool("Log", "AutoDelete", out bValue); sysParams.AutoDeleteLog = bValue; iniHelper.ReadDouble("Log", "AutoDeleteDays", out dValue); sysParams.AutoDeleteLogData = dValue; iniHelper.ReadBool("SysFunction", "OpenBuzzer", out bValue); sysParams.OpenBuzzer = bValue; iniHelper.ReadBool("SysFunction", "OpenDoor", out bValue); sysParams.OpenDoor = bValue; iniHelper.ReadBool("SysFunction", "OpenIO", out bValue); sysParams.OpenIO = bValue; iniHelper.ReadBool("SysFunction", "OpenPLC", out bValue); sysParams.OpenPLC = bValue; iniHelper.ReadString("Dev", "LightCom", out sValue); sysParams.LightCom = sValue.Replace("\0", ""); iniHelper.ReadInteger("Dev", "LightComBaud", out iValue); sysParams.LightComBaud = iValue; iniHelper.ReadString("Dev", "IODevName", out sValue); sysParams.IODevName = sValue.Replace("\0", ""); iniHelper.ReadString("Dev", "IOCfgPath", out sValue); sysParams.IOCfgPath = sValue.Replace("\0", ""); iniHelper.ReadString("Sys", "AIModelPath", out sValue); sysParams.AIModelPath = sValue.Replace("\0", ""); iniHelper.ReadString("Sys", "ProductPath", out sValue); sysParams.ProductPath = sValue.Replace("\0", ""); iniHelper.ReadString("Sys", "HttpServerIP", out sValue); iniHelper.ReadBool("ShowMainWin", "ShowResetBtn", out bValue); sysParams.ShowResetBtn = bValue; iniHelper.ReadBool("ShowMainWin", "ShowBuzz", out bValue); sysParams.ShowBuzz = bValue; iniHelper.ReadBool("ShowMainWin", "ShowDoorAlm", out bValue); sysParams.ShowDoorAlm = bValue; iniHelper.ReadBool("CloudAndMES", "OpenCloud", out bValue); sysParams.OpenCloud = bValue; iniHelper.ReadString("CloudAndMES", "CloudThisName", out sValue); sysParams.CloudThisName = sValue.Replace("\0", string.Empty); iniHelper.ReadString("CloudAndMES", "CloudServerIP", out sValue); sysParams.CloudServerIP = sValue; iniHelper.ReadInteger("CloudAndMES", "CloudServerPort", out iValue); sysParams.CloudServerPort = iValue; iniHelper.ReadString("CloudAndMES", "CloudUser", out sValue); sysParams.CloudUser = sValue; iniHelper.ReadString("CloudAndMES", "CloudPassword", out sValue); sysParams.CloudPassword = sValue; iniHelper.ReadBool("CloudAndMES", "OpenPdtServer", out bValue); sysParams.OpenPdtServer = bValue; iniHelper.ReadString("CloudAndMES", "PdtServerIP", out sValue); sysParams.PdtServerIP = sValue; iniHelper.ReadInteger("CloudAndMES", "PdtServerPort", out iValue); sysParams.PdtServerPort = iValue; iniHelper.ReadBool("CloudAndMES", "IsPdtServer", out bValue); sysParams.IsPdtServer = bValue; iniHelper.ReadBool("LabelPrint", "EnableLabelPrint", out bValue); sysParams.EnableLabelPrint = bValue; iniHelper.ReadString("LabelPrint", "LabelPrinterName", out sValue); sysParams.LabelPrinterName = sValue; iniHelper.ReadBool("ExcelPrint", "EnableExcelPrint", out bValue); sysParams.EnableExcelPrint = bValue; iniHelper.ReadString("ExcelPrint", "ExcelPrinterName", out sValue); sysParams.ExcelPrinterName = sValue; SysConfigParams = sysParams; return sysParams; } /// /// 写入系统默认配置 /// public void WriteDefaultSystemConfig() { SysConfigParams = new SysConfigParams(); WriteSysConfParams(SysConfigParams); } /// /// 加载系统配置 /// /// public bool LoadSystemConfig() { bool ret = false; if (!Directory.Exists(_rootPath)) { Directory.CreateDirectory(_rootPath); } if (!Directory.Exists(_projectDir)) { Directory.CreateDirectory(_projectDir); } if (File.Exists(_configFile)) { SysConfigParams = ReadSysConfParams(); if (null != SysConfigParams) { ret = true; } } else { // write default value WriteDefaultSystemConfig(); ret = true; } return ret; } #endregion #region 文件选择 /// /// 选择目录 /// /// public static string SelectFolder(string defaultPath = "") { FolderBrowserDialog dlg = new FolderBrowserDialog(); dlg.SelectedPath = (defaultPath != "" ? defaultPath : Path.GetFullPath(".")); if (dlg.ShowDialog() == DialogResult.OK) return dlg.SelectedPath; return ""; } /// /// 选择文件 /// /// 如 图像文件|*.jpg|图像文件|*.png|所有文件|*.* /// public static string SelectFile(string filter = "所有文件|*.*", string defaultPath = "") { OpenFileDialog fie = new OpenFileDialog(); fie.Title = "选择文件"; //fie.InitialDirectory = (defaultPath != "" ? defaultPath : Path.GetFullPath(".")); fie.Filter = filter; //设置文件类型 if (fie.ShowDialog() == DialogResult.OK) return fie.FileName; return ""; } /// /// 删除指定目录下文件 /// /// /// /// public static bool DelFilesInFolder(string FolderPath, string fileExtend = "*") { if (!Directory.Exists(FolderPath)) return false; string[] files = Directory.GetFiles(FolderPath, fileExtend, SearchOption.TopDirectoryOnly); foreach (string file in files) API.DeleteFile(file); return true; } /// /// 另存为 /// /// 默认文件名 /// 如 图像文件|*.jpg|图像文件|*.png|所有文件|*.* /// public static string SaveAsFile(string defaultFileName, string filter = "所有文件|*.*") { SaveFileDialog s = new SaveFileDialog(); s.Title = "保存文件"; s.Filter = filter; //"图像文件|*.jpg|图像文件|*.png|所有文件|*.*"; //s.DefaultExt = "图像文件|*.jpg"; //默认扩展名 //s.InitialDirectory = @"C:\Users\Administrator\Desktop"; //保存的初始目录 s.FileName = defaultFileName;//默认文件名 if (s.ShowDialog() == DialogResult.OK) return s.FileName; return ""; } #endregion #region json操作 //public static void addKey(JObject obj, string key, JToken value) //{ // if (obj.ContainsKey(key)) // obj[key] = value; // else // obj.Add(key, value); //} #endregion #region enum名称 public static ArrayList GetArrayList() { ArrayList list = new ArrayList(); Type type = typeof(T); //list.Add(new DictionaryEntry("start", "启动按钮")); foreach (int value in Enum.GetValues(type)) { string strName = Enum.GetName(type, value);//获取名称 list.Add(new DictionaryEntry(value, strName)); } return list; } #endregion #region 生产数据记录 public bool SetDailyOutputAdd(out int tDailyOutput) { bool ret = true; try { //读取数据 string tempFile = Path.Combine(_rootPath, "DailyOutput.ini"); IniHelper iniHelper = new IniHelper(tempFile); int iValue = 0; iniHelper.ReadInteger("TempData", "DailyOutput", out iValue); tDailyOutput = iValue; iniHelper.ReadInteger("TempData", "Date", out iValue); int dateNum = iValue; int nowDateNum = int.Parse(DateTime.Now.ToString("yyyyMMdd")); if (dateNum == nowDateNum) { //同一天,生产数量+1 tDailyOutput++; } else if (dateNum < nowDateNum) { //昨天数据,为1 tDailyOutput = 1; } //重新记录 iniHelper.WriteInteger("TempData", "DailyOutput", tDailyOutput); iniHelper.WriteInteger("TempData", "Date", nowDateNum); } catch (Exception ex) { tDailyOutput = 0; ret = false; string tempFile = Path.Combine(_rootPath, "DailyOutput.ini"); IniHelper iniHelper = new IniHelper(tempFile); int nowDateNum = int.Parse(DateTime.Now.ToString("yyyyMMdd")); //重新记录 iniHelper.WriteInteger("TempData", "DailyOutput", tDailyOutput); iniHelper.WriteInteger("TempData", "Date", nowDateNum); } return ret; } public bool GetDailyOutput(out int tDailyOutput) { bool ret = true; try { //读取数据 string tempFile = Path.Combine(_rootPath, "DailyOutput.ini"); IniHelper iniHelper = new IniHelper(tempFile); int iValue = 0; iniHelper.ReadInteger("TempData", "DailyOutput", out iValue); tDailyOutput = iValue; iniHelper.ReadInteger("TempData", "Date", out iValue); int dateNum = iValue; int nowDateNum = int.Parse(DateTime.Now.ToString("yyyyMMdd")); if (dateNum == nowDateNum) { //同一天,生产数量+1 //tDailyOutput++; } else if (dateNum < nowDateNum) { //昨天数据,为1 tDailyOutput = 0; } } catch (Exception ex) { tDailyOutput = 0; ret = false; } return ret; } #endregion #endregion } #region 参数类型 /// /// 图片保存参数 /// [Serializable] public class ImageSaveParam { /// /// 保存路径 /// public string SavePath { get; set; } /// /// 自动保存 /// public bool AutoSave { get; set; } /// /// 自动删除 /// public bool AutoDelete { get; set; } /// /// 自动删除日期 /// public double AutoDeleteDays { get; set; } public ImageSaveParam() { SavePath = "D:\\AllImageSave"; AutoSave = true; AutoDelete = true; AutoDeleteDays = 3; } public ImageSaveParam(string path) { SavePath = path; AutoSave = true; AutoDelete = true; AutoDeleteDays = 3; } } [Serializable] public class SysConfigParams { #region 缺陷图 /// /// 缺陷原大图 /// public ImageSaveParam DefectSrcImag { get; set; } /// /// 缺陷合并图 /// public ImageSaveParam DefectSplicImag { get; set; } /// /// 缺陷小图 /// public ImageSaveParam DefectSmallImag { get; set; } #endregion #region 功能 /// /// 开启蜂鸣器 /// public bool OpenBuzzer { get; set; } /// /// 开启门磁 /// public bool OpenDoor { get; set; } /// /// 开启IO /// public bool OpenIO { get; set; } /// /// 开启PLC /// public bool OpenPLC { get; set; } #endregion #region 硬件传感器通信 /// /// 光源串口 /// //public LightParams LightCfg { get; set; } public string LightCom { get; set; } /// /// 光源波特率 /// public int LightComBaud { get; set; } #endregion #region IO/电机/相机 /// /// 西门子PLC IP /// public string PLC_IP { get; set; } /// /// 西门子PLC 端口 /// public int PLC_Rack { get; set; } /// /// 西门子PLC 端口 /// public int PLC_Solt { get; set; } /// /// IO驱动名称 /// //public IOParams IOCfg { get; set; } public string IODevName { get; set; } /// /// IO配置文件路径 /// public string IOCfgPath { get; set; } /// /// 相机序号 /// public int CamIndex_1 { get; set; } /// /// 相机板卡序号 /// public int CamDev_1 { get; set; } /// /// 相机配置文件 /// public string CamPath_1 { get; set; } /// /// 相机序号 /// public int CamIndex_2 { get; set; } /// /// 相机板卡序号 /// public int CamDev_2 { get; set; } /// /// 相机配置文件 /// public string CamPath_2 { get; set; } #endregion #region 系统 public string AIModelPath { get; set; } public string ProductPath { get; set; } public string LogPath { get; set; } public bool AutoDeleteLog { get; set; } public double AutoDeleteLogData { get; set; } /// /// 调试:主界面显示复位按钮 /// public bool ShowResetBtn { get; set; } /// /// 调试:主界面显示蜂鸣器控制 /// public bool ShowBuzz { get; set; } /// /// 调试:主界面显示门磁报警控制 /// public bool ShowDoorAlm { get; set; } /// /// 1cm X方向像素 /// public int Cm2px_x = 1; /// /// 1cm Y方向像素 /// public int Cm2px_y = 1; /// /// 对比度范围阔算 /// public int Expand_pixel = 50; /// /// 中间重合像素 /// public int MidCoin = 100; /// /// 孔位 /// public int HolePx = 100; /// /// 左侧裁剪 /// public int LCrop = 0; /// /// 右侧裁剪 /// public int RCrop = 0; #endregion #region Http/MES #endregion #region MQTT本地云/产品服务器 /// /// 开启局域网云端 /// public bool OpenCloud { get; set; } public string CloudThisName { get; set; } public string CloudServerIP { get; set; } public int CloudServerPort { get; set; } public string CloudUser { get; set; } public string CloudPassword { get; set; } /// /// 开启产品转发 /// public bool OpenPdtServer { get; set; } public string PdtServerIP { get; set; } public int PdtServerPort { get; set; } /// /// 是否作为服务器还是客户端 /// public bool IsPdtServer { get; set; } #endregion #region BarTender /// /// 开启标签打印 /// public bool EnableLabelPrint { get; set; } /// /// 打印机名称 /// public string LabelPrinterName { get; set; } #endregion #region Excel /// /// 开启Excel打印 /// public bool EnableExcelPrint { get; set; } /// /// 打印机名称 /// public string ExcelPrinterName { get; set; } #endregion public SysConfigParams() { DefectSrcImag = new ImageSaveParam("D:\\Image\\DefectSrcImag"); DefectSmallImag = new ImageSaveParam("D:\\Image\\DefectSmallImag"); DefectSplicImag = new ImageSaveParam("D:\\Image\\DefectSplicImag"); OpenBuzzer = true; OpenDoor = true; OpenIO = true; OpenPLC = true; IODevName = ""; IOCfgPath = Path.Combine(Directory.GetCurrentDirectory(), "ConfigFiles\\IOCard_default.xml"); ; LightCom = ""; LightComBaud = 9600; PLC_IP = "192.168.0.1"; PLC_Rack = 0; PLC_Solt = 1; CamIndex_1 = 0; CamIndex_2 = 0; CamDev_1 = 0; CamDev_2 = 1; CamPath_1 = ""; CamPath_2 = ""; AIModelPath = Path.Combine(Directory.GetCurrentDirectory(), "ConfigFiles\\Models"); ProductPath = Path.Combine(Directory.GetCurrentDirectory(), "ConfigFiles\\Project"); LogPath = Path.Combine(Directory.GetCurrentDirectory(), "LogFiles"); AutoDeleteLog = false; AutoDeleteLogData = 0; ShowResetBtn = true; ShowBuzz = false; ShowDoorAlm = false; EnableLabelPrint = false; LabelPrinterName = ""; EnableExcelPrint = false; ExcelPrinterName = ""; OpenCloud = false; CloudThisName = "Test"; CloudServerIP = "127.0.0.1"; CloudServerPort = 1883; CloudUser = ""; CloudPassword = ""; OpenPdtServer = false; PdtServerIP = "127.0.0.1"; PdtServerPort = 10573; IsPdtServer = true; } } #endregion }