wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Tools;
using WIDESEA_Entity.DomainModels.Equipment;
using WIDESEA_WCS.Jobs;
using WIDESEA_WCS.JobsPart.Public;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS
{
 
    /// <summary>
    /// 涂布机调度【欧姆龙】
    /// </summary>
    [DisallowConcurrentExecution]
    public class PrintJob : JobBase, IJob
    {
 
        static List<AGVCenterEqDB> centerEqDBList;
        public PrintJob()
        {
            if (centerEqDBList == null || centerEqDBList.Count == 0)
            {
                string tsjJson = File.ReadAllText(AppContext.BaseDirectory + "/涂布机.json", Encoding.UTF8);
                centerEqDBList = JsonConvert.DeserializeObject<List<AGVCenterEqDB>>(tsjJson);
            }
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                ExecuteJob(context, DoAction);
            }
            catch { }
            return Task.FromResult(string.Empty);
        }
 
        private void DoAction(IJobExecutionContext context)
        {
            var clinet = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
 
            var stationList = centerEqDBList.FirstOrDefault(t => t.tiShenJiName == clinet.PLCName);
            if (stationList != null)
            {
                foreach (var station in stationList.stationInfos)
                {
                    if (station.stationType == "in")
                    {
                        // var hreat = clinet.OmronPLCClient.Read<bool>(station.dbData["R_hreat"]);
                        // var waring = clinet.OmronPLCClient.Read<bool>(station.dbData["R_waring"]);
                        var step = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_step"]);
                        //var requestType = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_requestType"]);
                        //var isAB = clinet.OmronPLCClient.Read<bool>(station.dbData["R_isAB"]);
                        //var isToRihgt = clinet.OmronPLCClient.Read<bool>(station.dbData["R_isToRihgt"]);
                        //var diffNum = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_diffNum"]);
 
                        //转换为调度中心地址
                        byte[] clientData = new byte[100];
                        //clientData[0] = (byte)(hreat ? 1 : 0);
                        clientData[90 + 1] = (byte)step;
                        //clientData[92] = (byte)(waring ? 1 : 0);
                        //int data_93 = 0;
                        //data_93 += isAB ? 2 : 0;
                        //data_93 += isToRihgt ? 4 : 0;
                        //clientData[93] = (byte)data_93;
                        //clientData[94] = (byte)diffNum;
 
                        TransferData(station, clinet, clientData);
                        //Create(clinet, station, requestType);
                    }
                    else if (station.stationType == "out")
                    {
                        //var materialtype = clinet.OmronPLCClient.Read<bool>(station.dbData["R_materialtype"]);
                        var waring = clinet.OmronPLCClient.Read<bool>(station.dbData["R_waring"]);
                        var step = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_step"]);
                        var requestType = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_requestType"]);
                        var isAB = clinet.OmronPLCClient.Read<bool>(station.dbData["R_isAB"]);
                        var isToRihgt = clinet.OmronPLCClient.Read<bool>(station.dbData["R_isToRihgt"]);
                        var diffNum = clinet.OmronPLCClient.Read<ushort>(station.dbData["R_diffNum"]);
 
                        //转换为调度中心地址
                        byte[] clientData = new byte[100];
                        clientData[90 + 1] = (byte)step;
                        clientData[92] = (byte)(waring ? 1 : 0);
                        int data_93 = 0;
                        data_93 += isAB ? 2 : 0;
                        data_93 += isToRihgt ? 4 : 0;
                        clientData[93] = (byte)data_93;
                        clientData[94] = (byte)diffNum;
 
                        TransferData(station, clinet, clientData);
                        Create(clinet, station, requestType);
                    }
                }
            }
        }
 
        private void Create(PLCClient clinet, StationInfo station, int requestType)
        {
            try
            {
                //入口上料
                if (station.stationType == "in" && requestType == 1)
                {
                    WCSCommon.CreateTask(station, true);
                }
                //出口卸料
                else if (station.stationType == "out" && requestType == 2)
                {
                    WCSCommon.CreateTask(station, false);
                }
            }
            catch (Exception ex)
            {
                WriteLog.GetLog().Write($"【任务请求异常】{clinet.PLCName},{station.stationCode}口。{ex.Message}" + "\n", clinet.PLCName);
            }
        }
 
        /// <summary>
        /// 中转AGV对接数据
        /// </summary>
        /// <param name="station">AGV的PLC</param>
        /// <param name="eq_Client">主机设备PLC</param>
        /// <param name="eqData">读的主机设备状态</param>
        private void TransferData(StationInfo station, PLCClient eq_Client, byte[] eqData)
        {
            var agv_Client = WIDESEA_WCS.WCSService.Clients.FirstOrDefault(t => t.PLCName == station.agvCenterName);
            if (agv_Client != null)
            {
                //读AGV
                var agvData = (byte[])agv_Client.ReadDBValue(station.agvDBList[0], 100);
                int hreat = (int)agvData[0];
                int step = (int)agvData[90 + 1];
                bool waring = agvData[92] == 0x1;
 
                //写涂布主机流程步骤
                eq_Client.OmronPLCClient.Write(station.dbData["W_step"], step);
                eq_Client.OmronPLCClient.Write(station.dbData["W_waring"], waring);
                if (station.stationType == "in")
                {
                    eq_Client.OmronPLCClient.Write(station.dbData["W_hreat"], hreat);
                    var material_Type = DataTrans.GetStr(agvData, 26);
                    eq_Client.OmronPLCClient.Write(station.dbData["W_materialtype"], material_Type);
                }
                //写给AGV
                agv_Client.WriteDBValue(station.agvDBList[1], eqData);
            }
            else
            {
                WriteLog.GetLog().Write($"【异常】{station.agvCenterName},读取调度中心PLC失败" + "\n", eq_Client.PLCName);
            }
        }
    }
}