wanshenmean
4 天以前 64a2aa2301946f777659239247233e47ad1e3076
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using Quartz;
using StackExchange.Profiling.Internal;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class SerialPortJob : JobBase, IJob, IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
 
        public enum CommandType
        {
            None = 0,
            Get = 1,
            Set = 2,
        }
 
        public enum CommandResult
        {
            SetOK = 0,
            GetOK = 1,
            GetError = 2,
        }
 
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                SerialPortDevice serialPortDevice = (SerialPortDevice)context.JobDetail.JobDataMap.Get("JobParams");
                if (serialPortDevice != null)
                {
                    List<DeviceProDTO> deviceProDTOs = serialPortDevice.DeviceProDTOs;
                    foreach (var item in deviceProDTOs)
                    {
                        if (item.DeviceProParamName != CommandType.Get.ToString() && item.DeviceProParamName != CommandType.Set.ToString())
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
                            if (deviceProtocolDetail != null)
                            {
                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
                                item.DeviceProParamName = CommandType.Set.ToString();
                            }
                        }
 
                        if (CommandType.Get.ToString() == item.DeviceProParamName)
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Get));
                            if (deviceProtocolDetail != null)
                            {
                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
                            }
                        }
                        else if (CommandType.Set.ToString() == item.DeviceProParamName)
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
                            if (deviceProtocolDetail != null)
                            {
                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
                            }
                        }
 
                        if (serialPortDevice.Communicator.Buffers.Count > 0)
                        {
                            string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default);
                            if (!string.IsNullOrEmpty(receiveData))
                            {
                                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
 
                                if (item.DeviceProParamName == CommandType.Set.ToString())
                                {
                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK));
                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                    {
                                        item.DeviceProParamName = CommandType.Get.ToString();
                                    }
                                }
                                else if (item.DeviceProParamName == CommandType.Get.ToString())
                                {
                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetOK));
 
                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                    {
                                        item.DeviceProParamName = CommandType.Set.ToString();
                                    }
                                }
                                else if (item.DeviceProParamName == CommandType.Get.ToString())
                                {
                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                    {
                                        item.DeviceProParamName = CommandType.Set.ToString();
                                    }
                                }
                            }
                        }
                    }
 
                }
 
            }
            catch (Exception ex)
            {
                WriteError("CommonConveyorLineJob", "test", ex);
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            WriteDebug("CommonConveyorLineJob", "test");
            return Task.CompletedTask;
        }
    }
}