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
{
///
/// 龙门位置作业任务,用于读取并更新H和Z轴设备的位置信息
///
///
/// 1. 从JobParams中获取设备参数
/// 2. 读取H轴设备位置并更新到OPositions.HPositions字典
/// 3. 读取Z轴设备位置并更新到OPositions.ZPositions字典
/// 4. 处理过程中捕获并记录异常
///
[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 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(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 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(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;
}
}
}