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
162
163
164
165
166
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;
using WIDESEA_Common.DBHelper;
using WIDESEA_Common.Tools;
using WIDESEA_Entity.DomainModels;
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 StackerJob : JobBase, IJob
    {
        static List<AGVCenterEqDB> centerEqDBList;
        static FreeDB freeDB = new FreeDB();
 
        public StackerJob()
        {
            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)
        {
            //return null;//停用提升机调度
            try
            {
                ExecuteJob(context, DoAction);
            }
            catch { }
            return Task.FromResult(string.Empty);
        }
 
 
        private void DoAction(IJobExecutionContext context)
        {
            PLCClient client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
            if (client==null)
            {
                return;
            }
            var stationList = centerEqDBList.FirstOrDefault(t => t.tiShenJiName == client.PLCName);
            if (stationList != null)
            {
                foreach (var station in stationList.stationInfos)
                {
                    var clientData = (byte[])client.ReadDBValue(station.eqDBList[0], 100);
                    TransferData(station, client, clientData);
 
                    try
                    {
                        //入口无料
                        if (station.stationType == "in" && clientData[93] == 0x00)
                        {
                            //提升机入口无料不主动叫料
                            //WCSCommon.CreateTask(station, true);
                        }
                        //出口有料
                        else if (station.stationType == "out" && clientData[93] == 0x01)
                        {
                            //物料类型,传任务号
                            var taskNo = DataTrans.GetStr(clientData, 26) + "_T";
                            WriteLog.GetLog().Write($"【info】任务{taskNo}请求任务完成。" + "\n", client.PLCName);
 
                            if (!string.IsNullOrEmpty(taskNo))
                            {
                                FinshTiShenJiTask(taskNo);
                            }
                            WCSCommon.CreateTask(station, false);
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog.GetLog().Write($"【提升机任务请求异常】{client.PLCName},{station.stationCode}口。{ex.Message}" + "\n", client.PLCName);
                    }
                }
            }
        }
 
        /// <summary>
        /// 完成提升机任务
        /// </summary>
        /// <param name="taskNo">提升机任务号</param>
        private void FinshTiShenJiTask(string taskNo)
        {
            var task = freeDB.Select<dt_agvtask>().Where(t => t.agv_tasknum == taskNo).ToOne();
            if (task != null)
            {
                freeDB.Add(new dt_agvtask_hty()
                {
                    hty_pkid = Guid.NewGuid(),
                    agv_id = Guid.Empty,
                    agv_tasknum = task.agv_tasknum,
                    agv_materielid = task.agv_materielid,
                    agv_qty = task.agv_qty,
                    agv_createtime = task.agv_createtime,
                    agv_realesstime = task.agv_realesstime,
                    agv_executingBeginTime = task.agv_executingBeginTime,
                    agv_executingEndTime = task.agv_executingEndTime,
                    agv_completeBeginTime = task.agv_completeBeginTime,
                    agv_finishedtime = DateTime.Now,
                    agv_taskstate = AGVTaskStateEnum.Complete1.ToString(),
                    agv_tasktype = task.agv_tasktype,
                    agv_fromaddress = task.agv_fromaddress,
                    agv_toaddress = task.agv_toaddress,
                    agv_operatetype = string.Empty,
                    agv_compeletor = "wcs",
                    agv_completedate = DateTime.Now,
                    agv_grade = task.agv_grade,
                    agv_userid = "wcs",
                    agv_barcode = task.agv_barcode,
                    agv_code = task.agv_code,
                    agv_worktype = task.agv_worktype
                });
                freeDB.Remove(task);
            }
        }
 
        /// <summary>
        /// 中转AGV对接数据
        /// </summary>
        /// <param name="station"></param>
        /// <param name="tsj_Client"></param>
        /// <param name="tsjData"></param>
        private void TransferData(StationInfo station, PLCClient tsj_Client, byte[] tsjData)
        {
            var agv_Client = WIDESEA_WCS.WCSService.Clients.FirstOrDefault(t => t.PLCName == station.agvCenterName);
            if (agv_Client != null)
            {
                var agvData = (byte[])agv_Client.ReadDBValue(station.agvDBList[0], 200);
 
                //90,流程步骤非0,开始对接
                if (agvData[90 + 1] != 0x0 && station.stationType == "in")
                {
                    //目的地址
                    agvData[96] = (byte)station.tiShenJiEndUniCode;
                    //托盘码
                    OperateDB.WriteData(agvData, 58, "null");
                }
 
                ////tsj_Client.WriteDBValue(station.eqDBList[1], agvData);
                /// agv_Client.WriteDBValue(station.agvDBList[1], tsjData);
            }
            else
            {
                WriteLog.GetLog().Write($"【异常】{station.agvCenterName},读取调度中心PLC失败" + "\n", tsj_Client.PLCName);
            }
        }
    }
}