using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json.Linq; using ProductionControl.Utils; namespace ProductionControl { public class Config { public static Models.User loginUser; public static bool HeightDevIOState; public static Dictionary dicDevType = new Dictionary(); // public static string appBasePath = ""; public static string CustomerVer = "A"; //跳过检测设备 public static bool SkipAxis0, SkipAxis1, SkipAxis2, SkipAxis3; public static bool SkipTension, SkipHeight, SkipLight, SkipScannerGL, SkipScannerCC, SkipSmallAxis; //启用功能 2023-1023 public static bool OpenMarkErrorStop; //启用缺陷分布功能 public static bool OpenFlawDistribution; //启用X轴校准 2023-12-20 public static bool OpenAxisXCalibration; //DEV public static int[] Axis_PulseOutMode=new int[4]; public static int[] Axis_MM2PulseNum = new int[4] { 5000, 5000, 5000, 1000};//1mm对应脉冲数 public static int[] Axis_HomeMode = new int[4]; public static int[] Axis_HomeDir = new int[4];//回Home方向 //回HOME速度 public static JArray Axis_HomeVelLow = new JArray(); public static JArray Axis_HomeVelHigh = new JArray(); public static JArray Axis_HomeAcc = new JArray(); public static JArray Axis_HomeDec = new JArray(); public static string IOCard_DeviceNum = "PCI-1730,BID#0"; public static LightDevNameEnum Light_Name= LightDevNameEnum.康仕达; public static int Light_PortNum = 1;//COM端口号 public static int Tension_PortNum = 7;//COM端口号 public static double Tension_Offset = 0;//张力偏移值 public static string SmallAxis_ComName = "COM2";//COM端口号 public static string HeightDev_IP = "192.168.3.35"; public static int HeightDev_Port = 64000; public static decimal HeightDev_SafeValue = 0;//厚度传感器安全值(axis移动前的前提条件) public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP.cti";//板卡相机 //public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCML.cti";//GE相机 public static string SizeEnginePath; public static string SizeBmp_SavePath, SizeBmp_Compare_SavePath, SizeBmp_Zoom_Image_SavePath; public static double SizeBmp_SavePath_AutoClear = -1, SizeBmp_Compare_SavePath_AutoClear = -1, SizeBmp_Zoom_Image_SavePath_AutoClear = -1; //缺陷配置 public static Size Defect_CutSize, Defect_ReSize; public static float Defect_Thresholds; public static string Defect_SavePath, Defect_Compress_SavePath, Defect_Small_SavePath; public static double Defect_SavePath_AutoClear =-1,Defect_Compress_SavePath_AutoClear=-1,Defect_Small_SavePath_AutoClear=-1; public static bool SaveAllDefectImg, MakeTag; public static double PT_Offset = 0;//PT偏移值 public static double LineWidth_Offset = 0;//线宽偏移值 //尺寸配置 //public static string Size_LoadPath, Size_FileName; // public static Dictionary CMDProcess; //DB //有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求 //如果用到bulkCopy需要加: AllowLoadLocalInfile=true //public static string dbMysqlCon = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456;"; public static string DBConStr = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true"; //LOG public static string LogPath; //WEBService public static string LocalIp=""; public static int LocalHttpPort=18082, LocalSocketPort; public static int timeout_http_wait = -1;//webapi超时限制,-1为无限制一直等待 //==========位置 初始位 上料位 下料位(下料位不存在时使用上料位) public static JObject joPTSetting = new JObject() { { "initPT", null }, { "upPT", null}, { "downPT", null} }; //=========全局按钮 //public static JObject joGButtonSetting = new JObject() { // { "start", null }, // { "pause", null}, // { "reset", null}, // { "warning", null}, // { "iodefault", null} //}; public static void DelectPictureFile() { //删除文件 Task.Factory.StartNew(() => { // CreateSysFolder(); // //DeleteFiles(appBasePath + "\\temp\\"); // if (Defect_SavePath_AutoClear >= 0) DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear, true); if (Defect_Compress_SavePath_AutoClear >= 0) DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, true); if (Defect_Small_SavePath_AutoClear >= 0) DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, true); // if (SizeBmp_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, true); if (SizeBmp_Compare_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, true); if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, true); }); } public static void LoadAllConfig() { if (string.IsNullOrWhiteSpace(appBasePath)) appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //删除文件 Task.Factory.StartNew(() => { CreateSysFolder(); // DeleteFiles(appBasePath+"\\temp\\"); // if (Defect_SavePath_AutoClear>=0) DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear,true); if (Defect_Compress_SavePath_AutoClear >= 0) DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, true); if (Defect_Small_SavePath_AutoClear>=0) DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, true); // if (SizeBmp_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, true); if (SizeBmp_Compare_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, true); if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0) DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, true); }); InitDevDic(); LoadSysConfig(); LoadPTConfig(); // string lsCmdPath = appBasePath + "\\CMDProcess\\"; if(!Directory.Exists(lsCmdPath)) Directory.CreateDirectory(lsCmdPath); string fileName; CMDProcess = new Dictionary(); foreach (int item in Enum.GetValues(typeof(CMDName)))//item为key值 { //string name = Enum.GetName(typeof(CMDName), item);//获取名称 fileName = lsCmdPath + item + ".json"; if (File.Exists(fileName)) CMDProcess.Add((CMDName)item, JObject.Parse(File.ReadAllText(fileName))); //else // CMDProcess.Add((CMDName)item, null); } } public static void LoadSysConfig() { if (string.IsNullOrWhiteSpace(appBasePath)) appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string configPath = appBasePath + "\\SysConfig.ini"; string lsTmp = ""; CustomerVer = Util.ReadIniValue(configPath, "VER", "CustomerVer"); if (string.IsNullOrWhiteSpace(CustomerVer)) CustomerVer = "A"; //Skip SkipAxis0 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") =="1"; SkipAxis1 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1"; SkipAxis2 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1"; SkipAxis3 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1"; SkipTension = Util.ReadIniValue(configPath, "SKIP", "SkipTension") == "1"; SkipHeight = Util.ReadIniValue(configPath, "SKIP", "SkipHeight") == "1"; SkipLight = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1"; SkipScannerGL = Util.ReadIniValue(configPath, "SKIP", "SkipScannerGL") == "1"; SkipScannerCC = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1"; SkipSmallAxis = Util.ReadIniValue(configPath, "SKIP", "SkipSmallAxis") == "1"; OpenMarkErrorStop = Util.ReadIniValue(configPath, "SKIP", "MarkErrorStop") == "1"; OpenFlawDistribution = Util.ReadIniValue(configPath, "SKIP", "OpenFlawDistribution") == "1"; OpenAxisXCalibration = Util.ReadIniValue(configPath, "SKIP", "OpenAxisXCalibration") == "1"; //DEV lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode"); if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[0] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode"); if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[1] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode"); if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[2] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode"); if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[3] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[0] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[1] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[2] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[3] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[0] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[1] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[2] = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir"); if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[3] = Convert.ToInt32(lsTmp); // lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow"); if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelLow = JArray.Parse(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh"); if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelHigh = JArray.Parse(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc"); if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeAcc = JArray.Parse(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec"); if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeDec = JArray.Parse(lsTmp); // lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_Name"); if (Util.IsNumber(lsTmp)) Config.Light_Name = (LightDevNameEnum)Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_PortNum"); if (Util.IsNumber(lsTmp)) Config.Light_PortNum = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_PortNum"); if (Util.IsNumber(lsTmp)) Config.Tension_PortNum = Convert.ToInt32(lsTmp); Config.SmallAxis_ComName = Util.ReadIniValue(configPath, "DEV", "SmallAxis_ComName"); lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_Offset"); if (string.IsNullOrEmpty(lsTmp)) Config.Tension_Offset = 0; else Config.Tension_Offset = double.Parse(lsTmp); HeightDev_IP = Util.ReadIniValue(configPath, "DEV", "HeightDev_IP"); lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_Port"); if (Util.IsNumber(lsTmp)) Config.HeightDev_Port = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_SafeValue"); if (Util.IsNumber(lsTmp)) Config.HeightDev_SafeValue = Convert.ToDecimal(lsTmp); IOCard_DeviceNum = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum"); Defect_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) Defect_SavePath_AutoClear = Convert.ToDouble(lsTmp); Defect_Compress_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) Defect_Compress_SavePath_AutoClear = Convert.ToDouble(lsTmp); Defect_Small_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) Defect_Small_SavePath_AutoClear = Convert.ToDouble(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "SaveAllDefectImg"); SaveAllDefectImg = (lsTmp == "1"); MakeTag = Util.ReadIniValue(configPath, "DEV", "MakeTag") == "1"; SizeEnginePath = Util.ReadIniValue(configPath, "DEV", "SizeEnginePath"); SizeBmp_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) SizeBmp_SavePath_AutoClear = Convert.ToDouble(lsTmp); SizeBmp_Compare_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) SizeBmp_Compare_SavePath_AutoClear = Convert.ToDouble(lsTmp); SizeBmp_Zoom_Image_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath"); lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath_AutoClear"); if (Util.IsDecimal(lsTmp)) SizeBmp_Zoom_Image_SavePath_AutoClear = Convert.ToDouble(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "PT_Offset"); if (string.IsNullOrEmpty(lsTmp)) Config.PT_Offset = 0; else Config.PT_Offset = double.Parse(lsTmp); lsTmp = Util.ReadIniValue(configPath, "DEV", "LineWidth_Offset"); if (string.IsNullOrEmpty(lsTmp)) Config.LineWidth_Offset = 0; else Config.LineWidth_Offset = double.Parse(lsTmp); //DB DBConStr = Util.ReadIniValue(configPath, "DB", "DBConStr"); //HTTP LocalIp= Util.ReadIniValue(configPath, "HTTP", "LocalIp"); lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalHttpPort"); if (Util.IsNumber(lsTmp)) Config.LocalHttpPort = Convert.ToInt32(lsTmp); lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalSocketPort"); if (Util.IsNumber(lsTmp)) Config.LocalSocketPort = Convert.ToInt32(lsTmp); //LOG LogPath = Util.ReadIniValue(configPath, "LOG", "LogPath"); //缺陷配置 Defect_CutSize = new Size(592, 532); Defect_ReSize = new Size(224, 224); Defect_Thresholds = 0.4f; } public static void LoadPTConfig() { if (string.IsNullOrWhiteSpace(appBasePath)) appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string configPath = appBasePath + "\\PTConfig.json"; string lsTmp = File.ReadAllText(configPath); joPTSetting = JObject.Parse(lsTmp); } private static void InitDevDic() { //dicDevType.Add("CodeScanner", "扫码枪");//固定为必有开始 if(!dicDevType.ContainsKey("IOCard")) dicDevType.Add("IOCard", "I/O控制"); if (!dicDevType.ContainsKey("Tension")) dicDevType.Add("Tension", "张力测量"); if (!dicDevType.ContainsKey("Height")) dicDevType.Add("Height", "厚度测量"); if (!dicDevType.ContainsKey("Axis")) dicDevType.Add("Axis", "滑台电机"); if (!dicDevType.ContainsKey("AxisTag")) dicDevType.Add("AxisTag", "滑台&测量"); if (!dicDevType.ContainsKey("Light")) dicDevType.Add("Light", "光源"); if (!dicDevType.ContainsKey("Scanner_GENTL")) dicDevType.Add("Scanner_GENTL", "相机GENTL"); if (!dicDevType.ContainsKey("Scanner_CC")) dicDevType.Add("Scanner_CC", "相机CC"); if (!dicDevType.ContainsKey("SmallAxis")) dicDevType.Add("SmallAxis", "镜头电机"); if (!dicDevType.ContainsKey("Defect")) dicDevType.Add("Defect", "缺陷检测"); if (!dicDevType.ContainsKey("Size")) dicDevType.Add("Size", "尺寸测量"); if (!dicDevType.ContainsKey("For")) dicDevType.Add("For", "For循环"); if (!dicDevType.ContainsKey("If")) dicDevType.Add("If", "If条件"); } private static void CreateSysFolder() { appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string dirPath = appBasePath + "\\Attachment\\"; if(!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath); dirPath = appBasePath + "\\Temp\\"; if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath); } /// /// /// /// /// /// 把日期文件夹也按days进行删除,格式:yyyyMMdd private static void DeleteFiles(string dirPath, double days = 0, bool delDir = false) { if (!Directory.Exists(dirPath)) return; DateTime delDate = DateTime.Now.AddDays(-days); API.OutputDebugString($"清除{delDate.ToString("yyyyMMdd")}前记录:{dirPath}。"); //删除dirPath中的过期文件 string[] files_list = Directory.GetFiles(dirPath); if (days > 0) files_list = files_list.Select(file => new FileInfo(file)) .Where(f => f.CreationTime <= delDate) .Select(x => x.FullName) .ToArray(); foreach (string file in files_list) { try { API.OutputDebugString($"清除文件:{file}..."); File.Delete(file); } catch (Exception ex) { API.OutputDebugString($"Ex1:{ex.Message}"); } } string[] dirsList = Directory.GetDirectories(dirPath, "20*"); foreach (string dir in dirsList) { string[] file_list = Directory.GetFiles(dir); if (days > 0) file_list = file_list.Select(file => new FileInfo(file)) .Where(f => f.CreationTime <= delDate) .Select(x => x.FullName) .ToArray(); foreach (string file in file_list) { try { API.OutputDebugString($"清除文件:{file}..."); File.Delete(file); } catch (Exception ex) { API.OutputDebugString($"Ex1:{ex.Message}"); } } } // if (delDir) { int dateNum = 0; if (days > 1) dateNum = Convert.ToInt32(delDate.ToString("yyyyMMdd")); else if (days > 0) dateNum = Convert.ToInt32(delDate.ToString("yyyyMMdd")) - 1; string floderName; string[] dirList = Directory.GetDirectories(dirPath,"20*"); foreach (string dir in dirList) { floderName= dir.Trim('\\'); floderName = floderName.Substring(floderName.LastIndexOf('\\') + 1); if (floderName.Length == 8 && Util.IsNumber(floderName) && Convert.ToInt32(floderName) <= dateNum) { try { API.OutputDebugString($"清除目录:{dir}..."); Directory.Delete(dir,true); } catch (Exception ex) { API.OutputDebugString($"Ex2:{ex.Message}"); } } } } } public static long GetHardDiskSpace(string str_HardDiskName) { long result = 0L; try { str_HardDiskName += ":\\"; DriveInfo[] drives = DriveInfo.GetDrives(); DriveInfo[] array = drives; foreach (DriveInfo driveInfo in array) { if (driveInfo.Name == str_HardDiskName) { result = driveInfo.TotalFreeSpace / 1073741824; } } } catch { result = 0; } return result; } public static bool CheckDisk(int max = 10) { string path = Config.Defect_SavePath; string volume = path.Substring(0, path.IndexOf(':')); long freespace = GetHardDiskSpace(volume); if (freespace < max) { string tip = $"当前{volume}硬盘容量:{freespace}GB,小于{max}GB。注意清理!!"; MessageBox.Show(tip, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } return true; } } }