分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-14 73a926018601d9a5a5a3d3f4c051537f45a8eff4
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using HslCommunication;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Jobs;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WCS
{
    /// <summary>
    /// 链条机
    /// </summary>
    [DisallowConcurrentExecution]
    public class PipelineJob : JobBase, IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
                if (client == null)
                {
                    return Task.CompletedTask;
                }
 
                //自动重连
                if (!client.IsConnected)
                {
                    client.Connect();
                    return Task.CompletedTask;
                }
 
                //DoAction(client);
                ExecuteJob(context, DoAction);
            }
            catch { }
            return Task.CompletedTask;
        }
 
        private void DoAction(IJobExecutionContext context)
        {
            var client = context.JobDetail.JobDataMap.Get("JobParams") as PLCClient;
            //自动重连
            if (!client.IsConnected)
            {
                client.Connect();
                return;
            }
            Loadinglevel(client);
        }
        /// <summary>
        /// 上料区
        /// </summary>
        /// <param name="client"></param>
        private void Loadinglevel(PLCClient client, string number = "上料区")
        {
            try
            {
                VOLContext Context = new VOLContext();
                Idt_plcinfoheadRepository repository = new dt_plcinfoheadRepository(Context);
                Idt_plcinfodetailRepository plcRepository = new dt_plcinfodetailRepository(Context);
                Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                var plc = repository.FindFirst(x => x.plcinfo_name == client.PLCName);
                var Gantry_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "桁架");
                if (Gantry_client == null) throw new Exception("桁架调度服务未开启!");
                if (!Gantry_client.IsConnected) throw new Exception("与桁架连接超时!");
                var Gantryplc = repository.FindFirst(x => x.plcinfo_name == Gantry_client.PLCName);
 
 
                ///查找上料区的货位
                var Stations = stationinfoRepository.Find(x => x.area == area_code(number));
                //缓存架未启用禁止桁架进入
                foreach (var station in Stations)
                {
                    if (!station.enable || station.location_state != LocationStateEnum.Stroge.ToString())
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                }
 
                var Station = Stations?.Where(x => x.enable && x.location_state == LocationStateEnum.Stroge.ToString() && x.quantity > 0).OrderBy(x => x.quantity).FirstOrDefault();
                if (Station != null)
                {
                    var SNS = Station.bindSN.Split(",");
                    List<string> list = new List<string>();
                    foreach (var SN in SNS)
                    {
                        if (!string.IsNullOrEmpty(SN))
                            list.Add(SN);
                    }
                    //缓存架上车轮数量与SN号数量不一致
                    if (list.Count != Station.quantity)
                    {
                        Station.location_state = LocationStateEnum.Abnormal.ToString();
                        Station.remark = "车轮数量与SN号数量不一致";
                        stationinfoRepository.Update(Station, true);
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                        #region 日志记录
                        WriteDBLog.Error("上料区", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}车轮数量与SN号数量不一致", "PCS");
                        #endregion
                        return;
                        //throw new Exception("上料位车轮数量与SN号数量不一致,上料位编号:" + Station.stationCode);
                    }
                    var details = plcRepository.Find(x => x.plcdetail_iotype == plc.plcinfo_iotyep && x.plcdetail_number == Station.stationCode).ToList();
                    var PalletSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_PalletSignal").First(), client);//读取托盘信号:1:有,2无
                    var MaterialSignal = (Int16)DBExtension.Read(details.Where(x => x.plcdetail_name == "R_MaterialSignal").First(), client);//读取货物信号:1:有,2无
                    if (PalletSignal == 1 && MaterialSignal == 1)
                    {
                        var area = Convert.ToInt16(Station.stationCode.Substring(Station.stationCode.Length - 1, 1));
                        Gantry_client.WriteByOrder("W_AreaNr", (Int16)area, number);//区域货位号
                        Gantry_client.WriteByOrder("W_IndexNr", (Int16)SNS.Length, number);//托盘上的第几个车轮
                        Gantry_client.WriteByOrder("W_Storage_Type", (Int16)1, number); //托盘类型1-横放;2-竖放
                        Gantry_client.WriteByOrder("W_Wheel_Type", (Int16)Convert.ToInt16(QueryMateriel(Station.stationType).TypeId), number);//车轮类型
                        Gantry_client.WriteByOrder("W_Wheel_id", SNS[SNS.Length - 1], number);//车轮SN号
                        Gantry_client.WriteByOrder("W_RequestUnload", true, number);
                        #region 日志记录
                        WriteDBLog.Success("上料区申请", $"读取托盘光电信号:{PalletSignal}\n读取第一个车轮光电信号:{MaterialSignal}\n\n" +
                            $"写入桁架信息:\n区域货位号:{area}\n托盘上的第几个车轮:{SNS.Length}\n托盘类型:{1}\n车轮类型:{QueryMateriel(Station.stationType).TypeId}" +
                            $"\n车轮SN号:{SNS[SNS.Length - 1]}\nW_RequestUnload:true", "PCS");
                        #endregion
                    }
                    else
                    {
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);
                    }
 
                    var Gantrydetails = plcRepository.Find(x => x.plcdetail_iotype == Gantryplc.plcinfo_iotyep && x.plcdetail_number == number).ToList();
                    var Gantry_Out_of_Area = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Gantry_Out_of_Area").First(), Gantry_client);//桁架是否在区域内
 
                    var QueryDate = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_QueryDate").First(), Gantry_client);//信息查询
                    if (QueryDate)
                    {
                        var Date_Vaild = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Date_Vaild").First(), Gantry_client);//信息确认
                        if (!Date_Vaild)//信息有误,记录日志
                        {
                            Station.location_state = LocationStateEnum.Abnormal.ToString();
                            Station.remark = $"未查询到SN号:{SNS[SNS.Length - 1]}的订单";
                            stationinfoRepository.Update(Station, true);
                            Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                            //throw new Exception($"未查询到SN号:{SNS[SNS.Length - 1]}的订单,上料位编号:{Station.stationCode}");
                            WriteDBLog.Error("上料区信息查询", $"写入桁架信息:\nW_RequestUnload:false\n\n{Station.stationCode}未查询到SN号:{SNS[SNS.Length - 1]}的订单", "PCS");
                            return;
                        }
                    }
                    var finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);//夹取完成
                    var updatefinished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "W_Storage_update").First(), Gantry_client);//货位状态更新
                    if (!finished && updatefinished)
                        Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
                    if (finished && !updatefinished)
                    {
                        Station.quantity = Station.quantity - 1;
                        Station.bindSN = OperStr(SNS);
                        if (Station.quantity <= 0)
                        {
                            Station.stationType = string.Empty;
                            //Station.location_state = LocationStateEnum.Empty.ToString();
                            Station.Number = string.Empty;
                            Station.heatNumber = string.Empty;
                        }
                        var count = stationinfoRepository.Update(Station, true);
                        if (count < 1)
                        {
                            WriteDBLog.Error("取料完成", $"上料位信息更新失败!上料位编号:{Station.stationCode}", "PCS");
                            //throw new Exception($"上料位信息更新失败!上料位编号:{Station.stationCode}");
                            return;
                        }
                        Gantry_client.WriteByOrder("W_Storage_update", true, number);//货位状态更新
                        Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
                        #region 日志记录
                        WriteDBLog.Success("取料完成", $"写入桁架信息:\n货位状态更新:{true}\nW_RequestUnload:{false}", "PCS");
                        #endregion
                        //finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);
                        //while (finished)
                        //{
                        //    finished = (bool)DBExtension.Read(Gantrydetails.Where(x => x.plcdetail_name == "R_Unlod_finished").First(), Gantry_client);
                        //    Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
                        //}
                    }
                    //else if (!finished && updatefinished)
                    //    Gantry_client.WriteByOrder("W_Storage_update", false, number);//货位状态更新
 
                }
                else
                    Gantry_client.WriteByOrder("W_RequestUnload", false, number);//信号为false桁架停止进入
            }
            catch (Exception ex)
            {
                //WritePCSLog.LogAdd(requestin.AreaNr.ToString(), respone.success == 1 ? "成功 " : "失败", "WMS", "AGV", json, JsonConvert.SerializeObject(respone), remark, "检测线上料区", ex.Message);
            }
        }
        public static string area_code(string area_name)
        {
            FreeDB freeDB = new FreeDB();
            var areainfo = freeDB.Select<dt_areainfo>().Where(x => x.area_name == area_name).First();
            return areainfo.area_code.ToString();
        }
 
        private string OperStr(string[] strArrty)
        {
            string[] newstr = strArrty.RemoveLast(1);
            string Newsn = string.Join(",", newstr);
 
            return Newsn;
        }
        public static dt_geometry_data QueryMateriel(string type)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
            var materielinfo = dataRepository.Find(x => x.Description == type).OrderBy(x => x.TypeId).FirstOrDefault();
            return materielinfo;
        }
        public static string QueryMateriel(int typeId)
        {
            VOLContext Context = new VOLContext();
            Idt_geometry_dataRepository dataRepository = new dt_geometry_dataRepository(Context);
            var materielinfo = dataRepository.FindFirst(x => x.TypeId == typeId);
            return materielinfo.Description;
        }
    }
}