版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

396 строки
21 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Newtonsoft.Json.Linq;
  10. using ProductionControl.Utils;
  11. namespace ProductionControl
  12. {
  13. public class Config
  14. {
  15. public static Models.User loginUser;
  16. public static bool HeightDevIOState;
  17. public static Dictionary<string, string> dicDevType = new Dictionary<string, string>();
  18. //
  19. public static string appBasePath = "";
  20. public static string CustomerVer = "A";
  21. //跳过检测设备
  22. public static bool SkipAxis0, SkipAxis1, SkipAxis2, SkipAxis3;
  23. public static bool SkipTension, SkipHeight, SkipLight, SkipScannerGL, SkipScannerCC, SkipSmallAxis;
  24. //启用功能 2023-1023
  25. public static bool OpenMarkErrorStop;
  26. //启用缺陷分布功能
  27. public static bool OpenFlawDistribution;
  28. //DEV
  29. public static int[] Axis_PulseOutMode=new int[4];
  30. public static int[] Axis_MM2PulseNum = new int[4] { 5000, 5000, 5000, 1000};//1mm对应脉冲数
  31. public static int[] Axis_HomeMode = new int[4];
  32. public static int[] Axis_HomeDir = new int[4];//回Home方向
  33. //回HOME速度
  34. public static JArray Axis_HomeVelLow = new JArray();
  35. public static JArray Axis_HomeVelHigh = new JArray();
  36. public static JArray Axis_HomeAcc = new JArray();
  37. public static JArray Axis_HomeDec = new JArray();
  38. public static string IOCard_DeviceNum = "PCI-1730,BID#0";
  39. public static LightDevNameEnum Light_Name= LightDevNameEnum.康仕达;
  40. public static int Light_PortNum = 1;//COM端口号
  41. public static int Tension_PortNum = 7;//COM端口号
  42. public static double Tension_Offset = 0;//张力偏移值
  43. public static string SmallAxis_ComName = "COM2";//COM端口号
  44. public static string HeightDev_IP = "192.168.3.35";
  45. public static int HeightDev_Port = 64000;
  46. public static decimal HeightDev_SafeValue = 0;//厚度传感器安全值(axis移动前的前提条件)
  47. public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP.cti";//板卡相机
  48. //public static string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCML.cti";//GE相机
  49. public static string SizeEnginePath;
  50. public static string SizeBmp_SavePath, SizeBmp_Compare_SavePath, SizeBmp_Zoom_Image_SavePath;
  51. public static double SizeBmp_SavePath_AutoClear = -1, SizeBmp_Compare_SavePath_AutoClear = -1, SizeBmp_Zoom_Image_SavePath_AutoClear = -1;
  52. //缺陷配置
  53. public static Size Defect_CutSize, Defect_ReSize;
  54. public static float Defect_Thresholds;
  55. public static string Defect_SavePath, Defect_Compress_SavePath, Defect_Small_SavePath;
  56. public static double Defect_SavePath_AutoClear =-1,Defect_Compress_SavePath_AutoClear=-1,Defect_Small_SavePath_AutoClear=-1;
  57. public static bool SaveAllDefectImg, MakeTag;
  58. //尺寸配置
  59. //public static string Size_LoadPath, Size_FileName;
  60. //
  61. public static Dictionary<CMDName, JObject> CMDProcess;
  62. //DB
  63. //有些服务器防火墙有问题需要加上 min pool size=1 避免认为是恶意请求
  64. //如果用到bulkCopy需要加: AllowLoadLocalInfile=true
  65. //public static string dbMysqlCon = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456;";
  66. public static string DBConStr = "server=localhost;Database=ProductionDB;Uid=root;Pwd=123456; AllowLoadLocalInfile=true";
  67. //LOG
  68. public static string LogPath;
  69. //WEBService
  70. public static string LocalIp="";
  71. public static int LocalHttpPort=18082, LocalSocketPort;
  72. public static int timeout_http_wait = -1;//webapi超时限制,-1为无限制一直等待
  73. //==========位置 初始位 上料位 下料位(下料位不存在时使用上料位)
  74. public static JObject joPTSetting = new JObject()
  75. {
  76. { "initPT", null },
  77. { "upPT", null},
  78. { "downPT", null}
  79. };
  80. //=========全局按钮
  81. //public static JObject joGButtonSetting = new JObject() {
  82. // { "start", null },
  83. // { "pause", null},
  84. // { "reset", null},
  85. // { "warning", null},
  86. // { "iodefault", null}
  87. //};
  88. public static void DelectPictureFile()
  89. {
  90. //删除文件
  91. Task.Factory.StartNew(() =>
  92. {
  93. // CreateSysFolder();
  94. //
  95. //DeleteFiles(appBasePath + "\\temp\\");
  96. //
  97. if (Defect_SavePath_AutoClear >= 0)
  98. DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear, false);
  99. if (Defect_Compress_SavePath_AutoClear >= 0)
  100. DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, false);
  101. if (Defect_Small_SavePath_AutoClear >= 0)
  102. DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, false);
  103. //
  104. if (SizeBmp_SavePath_AutoClear >= 0)
  105. DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, false);
  106. if (SizeBmp_Compare_SavePath_AutoClear >= 0)
  107. DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, false);
  108. if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0)
  109. DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, false);
  110. });
  111. }
  112. public static void LoadAllConfig()
  113. {
  114. if (string.IsNullOrWhiteSpace(appBasePath))
  115. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  116. //删除文件
  117. Task.Factory.StartNew(() =>
  118. {
  119. CreateSysFolder();
  120. //
  121. DeleteFiles(appBasePath+"\\temp\\");
  122. //
  123. if (Defect_SavePath_AutoClear>=0)
  124. DeleteFiles(Config.Defect_SavePath, Defect_SavePath_AutoClear,true);
  125. if (Defect_Compress_SavePath_AutoClear >= 0)
  126. DeleteFiles(Config.Defect_Compress_SavePath, Defect_Compress_SavePath_AutoClear, true);
  127. if (Defect_Small_SavePath_AutoClear>=0)
  128. DeleteFiles(Config.Defect_Small_SavePath, Defect_Small_SavePath_AutoClear, true);
  129. //
  130. if (SizeBmp_SavePath_AutoClear >= 0)
  131. DeleteFiles(Config.SizeBmp_SavePath, SizeBmp_SavePath_AutoClear, true);
  132. if (SizeBmp_Compare_SavePath_AutoClear >= 0)
  133. DeleteFiles(Config.SizeBmp_Compare_SavePath, SizeBmp_Compare_SavePath_AutoClear, true);
  134. if (SizeBmp_Zoom_Image_SavePath_AutoClear >= 0)
  135. DeleteFiles(Config.SizeBmp_Zoom_Image_SavePath, SizeBmp_Zoom_Image_SavePath_AutoClear, true);
  136. });
  137. InitDevDic();
  138. LoadSysConfig();
  139. LoadPTConfig();
  140. //
  141. string lsCmdPath = appBasePath + "\\CMDProcess\\";
  142. if(!Directory.Exists(lsCmdPath))
  143. Directory.CreateDirectory(lsCmdPath);
  144. string fileName;
  145. CMDProcess = new Dictionary<CMDName, JObject>();
  146. foreach (int item in Enum.GetValues(typeof(CMDName)))//item为key值
  147. {
  148. //string name = Enum.GetName(typeof(CMDName), item);//获取名称
  149. fileName = lsCmdPath + item + ".json";
  150. if (File.Exists(fileName))
  151. CMDProcess.Add((CMDName)item, JObject.Parse(File.ReadAllText(fileName)));
  152. //else
  153. // CMDProcess.Add((CMDName)item, null);
  154. }
  155. }
  156. public static void LoadSysConfig()
  157. {
  158. if (string.IsNullOrWhiteSpace(appBasePath))
  159. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  160. string configPath = appBasePath + "\\SysConfig.ini";
  161. string lsTmp = "";
  162. CustomerVer = Util.ReadIniValue(configPath, "VER", "CustomerVer");
  163. if (string.IsNullOrWhiteSpace(CustomerVer))
  164. CustomerVer = "A";
  165. //Skip
  166. SkipAxis0 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis0") =="1";
  167. SkipAxis1 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis1") == "1";
  168. SkipAxis2 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis2") == "1";
  169. SkipAxis3 = Util.ReadIniValue(configPath, "SKIP", "SkipAxis3") == "1";
  170. SkipTension = Util.ReadIniValue(configPath, "SKIP", "SkipTension") == "1";
  171. SkipHeight = Util.ReadIniValue(configPath, "SKIP", "SkipHeight") == "1";
  172. SkipLight = Util.ReadIniValue(configPath, "SKIP", "SkipLight") == "1";
  173. SkipScannerGL = Util.ReadIniValue(configPath, "SKIP", "SkipScannerGL") == "1";
  174. SkipScannerCC = Util.ReadIniValue(configPath, "SKIP", "SkipScannerCC") == "1";
  175. SkipSmallAxis = Util.ReadIniValue(configPath, "SKIP", "SkipSmallAxis") == "1";
  176. OpenMarkErrorStop = Util.ReadIniValue(configPath, "SKIP", "MarkErrorStop") == "1";
  177. OpenFlawDistribution = Util.ReadIniValue(configPath, "SKIP", "OpenFlawDistribution") == "1";
  178. //DEV
  179. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_PulseOutMode");
  180. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[0] = Convert.ToInt32(lsTmp);
  181. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_PulseOutMode");
  182. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[1] = Convert.ToInt32(lsTmp);
  183. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_PulseOutMode");
  184. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[2] = Convert.ToInt32(lsTmp);
  185. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_PulseOutMode");
  186. if (Util.IsNumber(lsTmp)) Config.Axis_PulseOutMode[3] = Convert.ToInt32(lsTmp);
  187. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeMode");
  188. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[0] = Convert.ToInt32(lsTmp);
  189. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeMode");
  190. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[1] = Convert.ToInt32(lsTmp);
  191. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeMode");
  192. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[2] = Convert.ToInt32(lsTmp);
  193. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeMode");
  194. if (Util.IsNumber(lsTmp)) Config.Axis_HomeMode[3] = Convert.ToInt32(lsTmp);
  195. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis0_HomeDir");
  196. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[0] = Convert.ToInt32(lsTmp);
  197. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis1_HomeDir");
  198. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[1] = Convert.ToInt32(lsTmp);
  199. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis2_HomeDir");
  200. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[2] = Convert.ToInt32(lsTmp);
  201. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis3_HomeDir");
  202. if (Util.IsNumber(lsTmp)) Config.Axis_HomeDir[3] = Convert.ToInt32(lsTmp);
  203. //
  204. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelLow");
  205. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelLow = JArray.Parse(lsTmp);
  206. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeVelHigh");
  207. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeVelHigh = JArray.Parse(lsTmp);
  208. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeAcc");
  209. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeAcc = JArray.Parse(lsTmp);
  210. lsTmp = Util.ReadIniValue(configPath, "DEV", "Axis_HomeDec");
  211. if (!string.IsNullOrWhiteSpace(lsTmp)) Axis_HomeDec = JArray.Parse(lsTmp);
  212. //
  213. lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_Name");
  214. if (Util.IsNumber(lsTmp)) Config.Light_Name = (LightDevNameEnum)Convert.ToInt32(lsTmp);
  215. lsTmp = Util.ReadIniValue(configPath, "DEV", "Light_PortNum");
  216. if (Util.IsNumber(lsTmp)) Config.Light_PortNum = Convert.ToInt32(lsTmp);
  217. lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_PortNum");
  218. if (Util.IsNumber(lsTmp)) Config.Tension_PortNum = Convert.ToInt32(lsTmp);
  219. Config.SmallAxis_ComName = Util.ReadIniValue(configPath, "DEV", "SmallAxis_ComName");
  220. lsTmp = Util.ReadIniValue(configPath, "DEV", "Tension_Offset");
  221. if (string.IsNullOrEmpty(lsTmp))
  222. Config.Tension_Offset = 0;
  223. else
  224. Config.Tension_Offset = double.Parse(lsTmp);
  225. HeightDev_IP = Util.ReadIniValue(configPath, "DEV", "HeightDev_IP");
  226. lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_Port");
  227. if (Util.IsNumber(lsTmp)) Config.HeightDev_Port = Convert.ToInt32(lsTmp);
  228. lsTmp = Util.ReadIniValue(configPath, "DEV", "HeightDev_SafeValue");
  229. if (Util.IsNumber(lsTmp)) Config.HeightDev_SafeValue = Convert.ToDecimal(lsTmp);
  230. IOCard_DeviceNum = Util.ReadIniValue(configPath, "DEV", "IOCard_DeviceNum");
  231. Defect_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath");
  232. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_SavePath_AutoClear");
  233. if (Util.IsNumber(lsTmp)) Defect_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  234. Defect_Compress_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath");
  235. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Compress_SavePath_AutoClear");
  236. if (Util.IsNumber(lsTmp)) Defect_Compress_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  237. Defect_Small_SavePath = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath");
  238. lsTmp = Util.ReadIniValue(configPath, "DEV", "Defect_Small_SavePath_AutoClear");
  239. if (Util.IsNumber(lsTmp)) Defect_Small_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  240. lsTmp = Util.ReadIniValue(configPath, "DEV", "SaveAllDefectImg");
  241. SaveAllDefectImg = (lsTmp == "1");
  242. MakeTag = Util.ReadIniValue(configPath, "DEV", "MakeTag") == "1";
  243. SizeEnginePath = Util.ReadIniValue(configPath, "DEV", "SizeEnginePath");
  244. SizeBmp_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath");
  245. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_SavePath_AutoClear");
  246. if (Util.IsNumber(lsTmp)) SizeBmp_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  247. SizeBmp_Compare_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath");
  248. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Compare_SavePath_AutoClear");
  249. if (Util.IsNumber(lsTmp)) SizeBmp_Compare_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  250. SizeBmp_Zoom_Image_SavePath = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath");
  251. lsTmp = Util.ReadIniValue(configPath, "DEV", "SizeBmp_Zoom_Image_SavePath_AutoClear");
  252. if (Util.IsNumber(lsTmp)) SizeBmp_Zoom_Image_SavePath_AutoClear = Convert.ToDouble(lsTmp);
  253. //DB
  254. DBConStr = Util.ReadIniValue(configPath, "DB", "DBConStr");
  255. //HTTP
  256. LocalIp= Util.ReadIniValue(configPath, "HTTP", "LocalIp");
  257. lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalHttpPort");
  258. if (Util.IsNumber(lsTmp)) Config.LocalHttpPort = Convert.ToInt32(lsTmp);
  259. lsTmp = Util.ReadIniValue(configPath, "HTTP", "LocalSocketPort");
  260. if (Util.IsNumber(lsTmp)) Config.LocalSocketPort = Convert.ToInt32(lsTmp);
  261. //LOG
  262. LogPath = Util.ReadIniValue(configPath, "LOG", "LogPath");
  263. //缺陷配置
  264. Defect_CutSize = new Size(592, 532);
  265. Defect_ReSize = new Size(224, 224);
  266. Defect_Thresholds = 0.4f;
  267. }
  268. public static void LoadPTConfig()
  269. {
  270. if (string.IsNullOrWhiteSpace(appBasePath))
  271. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  272. string configPath = appBasePath + "\\PTConfig.json";
  273. string lsTmp = File.ReadAllText(configPath);
  274. joPTSetting = JObject.Parse(lsTmp);
  275. }
  276. private static void InitDevDic()
  277. {
  278. //dicDevType.Add("CodeScanner", "扫码枪");//固定为必有开始
  279. if(!dicDevType.ContainsKey("IOCard")) dicDevType.Add("IOCard", "I/O控制");
  280. if (!dicDevType.ContainsKey("Tension")) dicDevType.Add("Tension", "张力测量");
  281. if (!dicDevType.ContainsKey("Height")) dicDevType.Add("Height", "厚度测量");
  282. if (!dicDevType.ContainsKey("Axis")) dicDevType.Add("Axis", "滑台电机");
  283. if (!dicDevType.ContainsKey("AxisTag")) dicDevType.Add("AxisTag", "滑台&测量");
  284. if (!dicDevType.ContainsKey("Light")) dicDevType.Add("Light", "光源");
  285. if (!dicDevType.ContainsKey("Scanner_GENTL")) dicDevType.Add("Scanner_GENTL", "相机GENTL");
  286. if (!dicDevType.ContainsKey("Scanner_CC")) dicDevType.Add("Scanner_CC", "相机CC");
  287. if (!dicDevType.ContainsKey("SmallAxis")) dicDevType.Add("SmallAxis", "镜头电机");
  288. if (!dicDevType.ContainsKey("Defect")) dicDevType.Add("Defect", "缺陷检测");
  289. if (!dicDevType.ContainsKey("Size")) dicDevType.Add("Size", "尺寸测量");
  290. if (!dicDevType.ContainsKey("For")) dicDevType.Add("For", "For循环");
  291. if (!dicDevType.ContainsKey("If")) dicDevType.Add("If", "If条件");
  292. }
  293. private static void CreateSysFolder()
  294. {
  295. appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  296. string dirPath = appBasePath + "\\Attachment\\";
  297. if(!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
  298. dirPath = appBasePath + "\\Temp\\";
  299. if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
  300. }
  301. /// <summary>
  302. ///
  303. /// </summary>
  304. /// <param name="dirPath"></param>
  305. /// <param name="days"></param>
  306. /// <param name="delDir">把日期文件夹也按days进行删除,格式:yyyyMMdd</param>
  307. private static void DeleteFiles(string dirPath,double days=0,bool delDir=false)
  308. {
  309. if (!Directory.Exists(dirPath)) return;
  310. DateTime delDate = DateTime.Now.AddDays(-days);
  311. API.OutputDebugString($"清除{delDate.ToString("yyyyMMdd")}前记录:{dirPath}。");
  312. string[] file_list = Directory.GetFiles(dirPath);
  313. if(days>0)
  314. file_list = file_list.Select(file => new FileInfo(file))
  315. .Where(f=>f.CreationTime<= delDate)
  316. .Select(x => x.FullName)
  317. .ToArray();
  318. foreach (string file in file_list)
  319. {
  320. try {
  321. API.OutputDebugString($"清除文件:{file}...");
  322. File.Delete(file);
  323. } catch(Exception ex) {
  324. API.OutputDebugString($"Ex1:{ex.Message}");
  325. }
  326. }
  327. //
  328. if (delDir)
  329. {
  330. int dateNum = Convert.ToInt32(delDate.ToString("yyyyMMdd"));
  331. string floderName;
  332. string[] dirList = Directory.GetDirectories(dirPath,"20*");
  333. foreach (string dir in dirList)
  334. {
  335. floderName= dir.Trim('\\');
  336. floderName = floderName.Substring(floderName.LastIndexOf('\\') + 1);
  337. if (floderName.Length == 8 && Util.IsNumber(floderName) && Convert.ToInt32(floderName) <= dateNum)
  338. {
  339. try
  340. {
  341. API.OutputDebugString($"清除目录:{dir}...");
  342. Directory.Delete(dir,true);
  343. }
  344. catch (Exception ex)
  345. {
  346. API.OutputDebugString($"Ex2:{ex.Message}");
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }