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 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.DeviceCode.Contains("FJ") ? "负极" : "正极"; 
 | 
                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<double> 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<double> doubleValues) 
 | 
        { 
 | 
            #region 
 | 
            ////读烘烤滚筒是否转动 
 | 
            //bool RHKupFGTZ1 = roast.Communicator.Read<bool>("YC"); 
 | 
            //bool RHKupFGTZ2 = roast.Communicator.Read<bool>("YD"); 
 | 
            //bool RHKupFGTZ3 = roast.Communicator.Read<bool>("YE"); 
 | 
            //bool RHKupFGTZ4 = roast.Communicator.Read<bool>("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<short>("D1960"); 
 | 
            //var RFHKupb = roast.Communicator.Read<short>("D15"); 
 | 
            //var RFHKupc = roast.Communicator.Read<short>("D1961"); 
 | 
            //var RFHKupd = roast.Communicator.Read<short>("D1950"); 
 | 
            //var RFHKupe = roast.Communicator.Read<short>("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<byte>("DB1002." + doubleValues[9]); 
 | 
            //var RFAGVb = agv.Communicator.Read<byte>("DB1002." + doubleValues[10]); 
 | 
            //var RFAGVc = agv.Communicator.Read<byte>("DB1002." + doubleValues[11]); 
 | 
            //var RFAGVd = agv.Communicator.Read<byte>("DB1002." + doubleValues[12]); 
 | 
            //var RFAGVe = agv.Communicator.Read<byte>("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<short>("D1960"); 
 | 
            //var RFHKdownb = roast.Communicator.Read<short>("D35"); 
 | 
            //var RFHKdownc = roast.Communicator.Read<short>("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<byte>("DB1002." + doubleValues[16]); 
 | 
            //var RFAGVdownb = agv.Communicator.Read<byte>("DB1002." + doubleValues[17]); 
 | 
            //var RFAGVdownc = agv.Communicator.Read<byte>("DB1002." + doubleValues[18]); 
 | 
            //var RFAGVdownd = agv.Communicator.Read<byte>("DB1002." + doubleValues[19]); 
 | 
            //var RFAGVdowne = agv.Communicator.Read<byte>("DB1002." + doubleValues[20]); 
 | 
            //var RFAGVdownf = agv.Communicator.Read<byte>("DB1002." + doubleValues[21]); 
 | 
            //var RFAGVdowng = agv.Communicator.Read<bool>("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<bool>("YC"), 
 | 
                roast.Communicator.Read<bool>("YD"), 
 | 
                roast.Communicator.Read<bool>("YE"), 
 | 
                roast.Communicator.Read<bool>("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<short>("D1960"), 
 | 
                roast.Communicator.Read<short>("D15"), 
 | 
                roast.Communicator.Read<short>("D1961"), 
 | 
                roast.Communicator.Read<short>("D1950"), 
 | 
                roast.Communicator.Read<short>("D1951") 
 | 
            }; 
 | 
  
 | 
            // 写AGV入料 
 | 
            for (int i = 0; i < RFHKup.Length; i++) 
 | 
            { 
 | 
                agv.Communicator.Write($"DB1002.{doubleValues[i + 4]}", (byte)RFHKup[i]); 
 | 
            } 
 | 
  
 | 
            // 读AGV入料 
 | 
  
 | 
            short RFAGVa = agv.Communicator.Read<byte>($"DB1002.{doubleValues[9]}"); 
 | 
            short RFAGVb = agv.Communicator.Read<byte>($"DB1002.{doubleValues[10]}"); 
 | 
            short RFAGVc = agv.Communicator.Read<byte>($"DB1002.{doubleValues[11]}"); 
 | 
            short RFAGVd = agv.Communicator.Read<byte>($"DB1002.{doubleValues[12]}"); 
 | 
            bool RFAGVe = agv.Communicator.Read<bool>($"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]); 
 | 
            //} 
 | 
            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); 
 | 
            // 读烘烤出料 
 | 
            short[] RFHKdown = new short[] 
 | 
            { 
 | 
                roast.Communicator.Read<short>("D1960"), 
 | 
                roast.Communicator.Read<short>("D35"), 
 | 
                roast.Communicator.Read<short>("D1962") 
 | 
            }; 
 | 
  
 | 
            // 写AGV出料 
 | 
            for (int i = 0; i < RFHKdown.Length; i++) 
 | 
            { 
 | 
                agv.Communicator.Write($"DB1002.{doubleValues[i + 13]}", (byte)RFHKdown[i]); 
 | 
            } 
 | 
  
 | 
            // 读AGV出料 
 | 
            short RFAGVdowna = agv.Communicator.Read<byte>($"DB1002.{doubleValues[16]}"); 
 | 
            short RFAGVdownb = agv.Communicator.Read<byte>($"DB1002.{doubleValues[17]}"); 
 | 
            short RFAGVdownc = agv.Communicator.Read<byte>($"DB1002.{doubleValues[18]}"); 
 | 
            short RFAGVdownd = agv.Communicator.Read<byte>($"DB1002.{doubleValues[19]}"); 
 | 
            short RFAGVdowne = agv.Communicator.Read<byte>($"DB1002.{doubleValues[20]}"); 
 | 
            short RFAGVdownf = agv.Communicator.Read<byte>($"DB1002.{doubleValues[21]}"); 
 | 
            bool RFAGVdowng = agv.Communicator.Read<bool>($"DB1002.{doubleValues[23]}"); 
 | 
  
 | 
            // 写烘烤出料 
 | 
            //string[] writeAddressesDown = new string[] { "D1940", "D1941", "D1957", "D1952", "D1953", "D1958", "M692" }; 
 | 
            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); 
 | 
        } 
 | 
  
 | 
  
 | 
        // 根据设备类型和number的值获取并调整double值集合 
 | 
        private List<double> GetDoubleValues(bool isPositivePole, int number) 
 | 
        { 
 | 
            List<double> doubleValues; 
 | 
  
 | 
            if (isPositivePole) 
 | 
            { 
 | 
                doubleValues = number < 3 
 | 
                    ? new List<double> { 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<double> { 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<double> { 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<double> { 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(); 
 | 
  
 | 
        } 
 | 
    } 
 | 
} 
 |