1
z8018
2025-06-10 e46aa927d231af83724683c7286d9db503e24cf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using WIDESEAWCS_Common;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 龙门位置作业任务,用于读取并更新H和Z轴设备的位置信息
    /// </summary>
    /// <remarks>
    /// 1. 从JobParams中获取设备参数 <br/>
    /// 2. 读取H轴设备位置并更新到OPositions.HPositions字典 <br/>
    /// 3. 读取Z轴设备位置并更新到OPositions.ZPositions字典 <br/>
    /// 4. 处理过程中捕获并记录异常
    /// </remarks>
    [DisallowConcurrentExecution]
    public class GantryPositionJob : JobBase, IJob
    {
        public GantryPositionJob()
        {
 
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null && value is OtherDevice otherDevice)
            {
                try
                {
                    {
                        List<string> hDeviceChildCodes = otherDevice.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(OPositions.HPositions)).GroupBy(x => x.DeviceChildCode).Select(x => x.Key).ToList();
                        DeviceProDTO deviceProDTOH = otherDevice.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(OPositions.HPositions)).OrderBy(x => x.DeviceProOffset).First();
                        if (deviceProDTOH != null)
                        {
                            int[] hPositions = otherDevice.Communicator.Read<int>(deviceProDTOH.DeviceProAddress, (ushort)(hDeviceChildCodes.Count * 4));
 
                            for (int i = 0; i < hDeviceChildCodes.Count; i++)
                            {
                                Position position = new Position()
                                {
                                    PositionX = hPositions[i * 4],
                                    PositionY = hPositions[i * 4 + 1],
                                    PositionZ = hPositions[i * 4 + 2],
                                    PositionR = hPositions[i * 4 + 3]
                                };
 
                                if(!OPositions.HPositions.TryAdd(hDeviceChildCodes[i], position))
                                {
                                    OPositions.HPositions[hDeviceChildCodes[i]] = position;
                                }
                            }
                        }
                    }
 
                    {
                        List<string> zDeviceChildCodes = otherDevice.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(OPositions.ZPositions)).GroupBy(x => x.DeviceChildCode).Select(x => x.Key).ToList();
                        DeviceProDTO deviceProDTOZ = otherDevice.DeviceProDTOs.Where(x => x.DeviceProParamType == nameof(OPositions.ZPositions)).OrderBy(x => x.DeviceProOffset).First();
                        if (deviceProDTOZ != null)
                        {
                            int[] zPositions = otherDevice.Communicator.Read<int>(deviceProDTOZ.DeviceProAddress, (ushort)(zDeviceChildCodes.Count * 4));
 
                            for (int i = 0; i < zDeviceChildCodes.Count; i++)
                            {
                                Position position = new Position()
                                {
                                    PositionX = zPositions[i * 4],
                                    PositionY = zPositions[i * 4 + 1],
                                    PositionZ = zPositions[i * 4 + 2],
                                    PositionR = zPositions[i * 4 + 3]
                                };
 
                               if(! OPositions.ZPositions.TryAdd(zDeviceChildCodes[i], position))
                                {
                                    OPositions.ZPositions[zDeviceChildCodes[i]] = position;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", ex.Message, ex);
                }
            }
            else
            {
                WriteError(nameof(CommonConveyorLightJob), "参数错误,未传递设备参数或设备类型错误");
            }
            return Task.CompletedTask;
        }
    }
 
 
}