using Quartz; using System.Text.RegularExpressions; using WIDESEAWCS_QuartzJob; namespace WIDESEAWCS_Tasks { internal class CommonRoastJob : JobBase, IJob, IDisposable { public CommonRoastJob() { } public void Dispose() { GC.SuppressFinalize(this); } public Task Execute1(IJobExecutionContext context) { CommonRoast roast = (CommonRoast)context.JobDetail.JobDataMap.Get("JobParams"); AGV agv = null; if (roast.DeviceName.Contains("正极")) { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "正极AGV"); if (device != null) { agv = (AGV)device; } else throw new Exception("No such device found."); MatchCollection matches = Regex.Matches(roast.DeviceName, @"\d+"); int number = int.Parse(matches[0].Value); // 创建一个包含所有 double 值的集合 List doubleValues = new List(); if (number < 3) { doubleValues = new List { 8093.6, 8093.7, 3493.6, 3493.7, 8094.0, 8095.0, 8096.0, 8097.0, 8098.0, 8194.0, 8195.0, 8196.0, 8197.0, 3494.0, 3495.0, 3496.0, 3594.0, 3595.0, 3596.0, 3597.0, 3598.0, 3599.0, 8192.2, 3592.2 }; } else { doubleValues = new List { 1693.6, 1693.7, 293.6, 293.6, 1694.0, 1695.0, 1696.0, 1697.0, 1698.0, 1794.0, 1795.0, 1796.0, 1797.0, 294.0, 295.0, 296.0, 394.0, 395.0, 396.0, 397.0,398.0,399.0,1792.2,392.2 }; } if (number < 3) { // 根据 number 的值调整集合中的每个元素 for (int i = 0; i < doubleValues.Count; i++) { doubleValues[i] += (number - 1) * 400; } } else {// 根据 number 的值调整集合中的每个元素 for (int i = 0; i < doubleValues.Count; i++) { doubleValues[i] += (number - 3) * 400; } } } else if (roast.DeviceName.Contains("负极")) { IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == "负极AGV"); if (device != null) { agv = (AGV)device; } else throw new Exception("No such device found."); MatchCollection matches = Regex.Matches(roast.DeviceName, @"\d+"); int number = int.Parse(matches[0].Value); List doubleValues = new List(); if (number < 3) { doubleValues = new List { 8893.6, 8893.7, 9093.6, 9093.7, 8894.0, 8895.0, 8896.0, 8897.0, 8898.0, 8994.0, 8995.0, 8996.0, 8997.0, 9094.0, 9095.0, 9096.0, 9194.0, 9195.0, 9196.0, 9197.0, 9198.0, 9199.0, 8992.2, 9192.2 }; } else { doubleValues = new List { 4893.6, 4893.7, 5093.6, 5093.6, 4894.0, 4895.0, 4896.0, 4897.0, 4898.0, 4994.0, 4995.0, 4996.0, 4997.0, 5094.0, 5095.0, 5096.0, 5194.0, 5195.0, 5196.0, 5197.0, 5198.0, 5199.0, 4992.2, 5192.2 }; } if (number < 3) { // 根据 number 的值调整集合中的每个元素 for (int i = 0; i < doubleValues.Count; i++) { doubleValues[i] += (number - 1) * 400; } } else { // 根据 number 的值调整集合中的每个元素 for (int i = 0; i < doubleValues.Count; i++) { doubleValues[i] += (number - 3) * 400; } } } else throw new Exception("No such device found."); throw new NotImplementedException(); } public Task Execute(IJobExecutionContext context) { try { // 从上下文中获取 JobParams 并转换为 Roast 类型 CommonRoast roast = (CommonRoast)context.JobDetail.JobDataMap.Get("JobParams"); // 初始化 AGV 变量 AGV agv = null; if (roast == null) { throw new Exception("未找到烘烤设备参数。"); } // 根据设备名称决定是正极还是负极 AGV string devicePrefix = roast.DeviceName.Contains("正极") ? "正极" : "负极"; string agvName = $"{devicePrefix}AGV"; // 从存储中查找指定名称的设备 IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceName == agvName); if (device != null) { agv = (AGV)device; } else { throw new Exception("未找到AGV设备参数。"); } // 使用正则表达式从设备名称中提取数字 MatchCollection matches = Regex.Matches(roast.DeviceName, @"\d+"); if (matches.Count == 0) { throw new Exception("No number found in device name."); } int number = int.Parse(matches[0].Value); // 根据number的值选择相应的double集合并调整值 List doubleValues = GetDoubleValues(roast.DeviceName.Contains("正极"), number); DeviceInteraction(roast, agv, doubleValues); } catch (Exception ex) { Console.WriteLine(ex.Message); } return Task.CompletedTask; } public static void DeviceInteraction(CommonRoast roast, AGV agv, List doubleValues) { #region ////读烘烤滚筒是否转动 //bool RHKupFGTZ1 = roast.Communicator.Read("YC"); //bool RHKupFGTZ2 = roast.Communicator.Read("YD"); //bool RHKupFGTZ3 = roast.Communicator.Read("YE"); //bool RHKupFGTZ4 = roast.Communicator.Read("YF"); ////写AGV滚筒转动 //agv.Communicator.Write("DB1002." + doubleValues[0], RHKupFGTZ1); //agv.Communicator.Write("DB1002." + doubleValues[1], RHKupFGTZ2); //agv.Communicator.Write("DB1002." + doubleValues[2], RHKupFGTZ3); //agv.Communicator.Write("DB1002." + doubleValues[3], RHKupFGTZ4); ////读烘烤入料 //var RFHKupa = roast.Communicator.Read("D1960"); //var RFHKupb = roast.Communicator.Read("D15"); //var RFHKupc = roast.Communicator.Read("D1961"); //var RFHKupd = roast.Communicator.Read("D1950"); //var RFHKupe = roast.Communicator.Read("D1951"); ////写AGV入料 //agv.Communicator.Write("DB1002." + doubleValues[4], byte.Parse(RFHKupa.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[5], byte.Parse(RFHKupb.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[6], byte.Parse(RFHKupc.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[7], byte.Parse(RFHKupd.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[8], byte.Parse(RFHKupe.ToString())); ////读AGV入料 //var RFAGVa = agv.Communicator.Read("DB1002." + doubleValues[9]); //var RFAGVb = agv.Communicator.Read("DB1002." + doubleValues[10]); //var RFAGVc = agv.Communicator.Read("DB1002." + doubleValues[11]); //var RFAGVd = agv.Communicator.Read("DB1002." + doubleValues[12]); //var RFAGVe = agv.Communicator.Read("DB1002." + doubleValues[22]); ////写烘烤入料 //roast.Communicator.Write("D1940", RFAGVa); //roast.Communicator.Write("D1941", RFAGVb); //roast.Communicator.Write("D1955", RFAGVc); //roast.Communicator.Write("D1956", RFAGVd); //roast.Communicator.Write("M691", RFAGVe); ////读烘烤出料 //var RFHKdowna = roast.Communicator.Read("D1960"); //var RFHKdownb = roast.Communicator.Read("D35"); //var RFHKdownc = roast.Communicator.Read("D1962"); ////写AGV出料 //agv.Communicator.Write("DB1002." + doubleValues[13], byte.Parse(RFHKdowna.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[14], byte.Parse(RFHKdownb.ToString())); //agv.Communicator.Write("DB1002." + doubleValues[15], byte.Parse(RFHKdownc.ToString())); ////读AGV出料 //var RFAGVdowna = agv.Communicator.Read("DB1002." + doubleValues[16]); //var RFAGVdownb = agv.Communicator.Read("DB1002." + doubleValues[17]); //var RFAGVdownc = agv.Communicator.Read("DB1002." + doubleValues[18]); //var RFAGVdownd = agv.Communicator.Read("DB1002." + doubleValues[19]); //var RFAGVdowne = agv.Communicator.Read("DB1002." + doubleValues[20]); //var RFAGVdownf = agv.Communicator.Read("DB1002." + doubleValues[21]); //var RFAGVdowng = agv.Communicator.Read("DB1002." + doubleValues[23]); ////写烘烤出料 //roast.Communicator.Write("D1940", RFAGVdowna); //roast.Communicator.Write("D1941", RFAGVdownb); //roast.Communicator.Write("D1957", RFAGVdownc); //roast.Communicator.Write("D1952", RFAGVdownd); //roast.Communicator.Write("D1953", RFAGVdowne); //roast.Communicator.Write("D1958", RFAGVdownf); //roast.Communicator.Write("M692", RFAGVdowng); #endregion // 读烘烤滚筒是否转动 bool[] RHKupFGTZ = new bool[] { roast.Communicator.Read("YC"), roast.Communicator.Read("YD"), roast.Communicator.Read("YE"), roast.Communicator.Read("YF") }; // 写AGV滚筒转动 for (int i = 0; i < RHKupFGTZ.Length; i++) { agv.Communicator.Write($"DB1002.{doubleValues[i]}", RHKupFGTZ[i]); } // 读烘烤入料 short[] RFHKup = new short[] { roast.Communicator.Read("D1960"), roast.Communicator.Read("D15"), roast.Communicator.Read("D1961"), roast.Communicator.Read("D1950"), roast.Communicator.Read("D1951") }; // 写AGV入料 for (int i = 0; i < RFHKup.Length; i++) { agv.Communicator.Write($"DB1002.{doubleValues[i + 4]}", (byte)RFHKup[i]); } // 读AGV入料 byte[] RFAGVup = new byte[] { agv.Communicator.Read($"DB1002.{doubleValues[9]}"), agv.Communicator.Read($"DB1002.{doubleValues[10]}"), agv.Communicator.Read($"DB1002.{doubleValues[11]}"), agv.Communicator.Read($"DB1002.{doubleValues[12]}"), agv.Communicator.Read($"DB1002.{doubleValues[22]}") }; // 写烘烤入料 string[] writeAddressesUp = new string[] { "D1940", "D1941", "D1955", "D1956", "M691" }; for (int i = 0; i < RFAGVup.Length; i++) { roast.Communicator.Write(writeAddressesUp[i], RFAGVup[i]); } // 读烘烤出料 short[] RFHKdown = new short[] { roast.Communicator.Read("D1960"), roast.Communicator.Read("D35"), roast.Communicator.Read("D1962") }; // 写AGV出料 for (int i = 0; i < RFHKdown.Length; i++) { agv.Communicator.Write($"DB1002.{doubleValues[i + 13]}", (byte)RFHKdown[i]); } // 读AGV出料 byte[] RFAGVdown = new byte[] { agv.Communicator.Read($"DB1002.{doubleValues[16]}"), agv.Communicator.Read($"DB1002.{doubleValues[17]}"), agv.Communicator.Read($"DB1002.{doubleValues[18]}"), agv.Communicator.Read($"DB1002.{doubleValues[19]}"), agv.Communicator.Read($"DB1002.{doubleValues[20]}"), agv.Communicator.Read($"DB1002.{doubleValues[21]}") }; bool RFAGVdowng = agv.Communicator.Read($"DB1002.{doubleValues[23]}"); // 写烘烤出料 string[] writeAddressesDown = new string[] { "D1940", "D1941", "D1957", "D1952", "D1953", "D1958", "M692" }; for (int i = 0; i < RFAGVdown.Length; i++) { roast.Communicator.Write(writeAddressesDown[i], RFAGVdown[i]); } roast.Communicator.Write(writeAddressesDown[6], RFAGVdowng); } // 根据设备类型和number的值获取并调整double值集合 private List GetDoubleValues(bool isPositivePole, int number) { List doubleValues; if (isPositivePole) { doubleValues = number < 3 ? new List { 8093.6, 8093.7, 3493.6, 3493.7, 8094.0, 8095.0, 8096.0, 8097.0, 8098.0, 8194.0, 8195.0, 8196.0, 8197.0, 3494.0, 3495.0, 3496.0, 3594.0, 3595.0, 3596.0, 3597.0, 3598.0, 3599.0, 8192.2, 3592.2 } : new List { 1693.6, 1693.7, 293.6, 293.6, 1694.0, 1695.0, 1696.0, 1697.0, 1698.0, 1794.0, 1795.0, 1796.0, 1797.0, 294.0, 295.0, 296.0, 394.0, 395.0, 396.0, 397.0, 398.0, 399.0, 1792.2, 392.2 }; } else // 负极 { doubleValues = number < 3 ? new List { 8893.6, 8893.7, 9093.6, 9093.7, 8894.0, 8895.0, 8896.0, 8897.0, 8898.0, 8994.0, 8995.0, 8996.0, 8997.0, 9094.0, 9095.0, 9096.0, 9194.0, 9195.0, 9196.0, 9197.0, 9198.0, 9199.0, 8992.2, 9192.2 } : new List { 4893.6, 4893.7, 5093.6, 5093.6, 4894.0, 4895.0, 4896.0, 4897.0, 4898.0, 4994.0, 4995.0, 4996.0, 4997.0, 5094.0, 5095.0, 5096.0, 5194.0, 5195.0, 5196.0, 5197.0, 5198.0, 5199.0, 4992.2, 5192.2 }; } // 调整double集合中的每个元素 int adjustmentFactor = (number < 3 ? (number - 1) : (number - 3)) * 400; return doubleValues.Select(val => val + adjustmentFactor).ToList(); } } }