hudong
2025-05-12 0e2425d59ffe4c698182f134a47dc84585bc8439
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
237
238
239
240
241
242
using HH.WCS.Mobox3.pinggao.device;
using HH.WCS.Mobox3.pinggao.dispatch;
using HH.WCS.Mobox3.pinggao.util;
using HH.WCS.Mobox3.pinggao.wms;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
 
namespace HH.WCS.Mobox3.pinggao.process
{
    /// <summary>
    /// 设备信号处理,主要是tcp信号,我们做server被动接收信号来处理,根据项目定制的
    /// </summary>
    internal class DeviceProcess
    {
        internal static void Analysis(string data, string ip)
        {
            if (data.Length >= 6)
            {
                //去掉消息头3F 00
                data = data.Substring(4);
                //Console.WriteLine($"{ip}-{data}");
                var plc = Settings.deviceInfos.Where(a => a.address == ip && a.enable == 1).FirstOrDefault();
                if (plc != null)
                {
                    if (plc.deviceType == 1)
                    {
                        AnalysisBottleCapmolding(data, plc);
                    }
                }
                else
                {
                    Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
                }
            }
 
        }
 
        internal static void Traffic(string forkliftNo, string lockNo, bool v)
        {
        }
 
 
        /// <summary>
        /// 接收PLC注塑机信号
        /// </summary>
        /// <param name="data"></param>
        /// <param name="plc"></param>
        private static void AnalysisBottleCapmolding(string data, Settings.deviceInfo plc)
        {
            LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg={data}", "注塑机");
            //下满框信号生成下满框任务 待取货完成解绑起点货位 生成送空任务
            if (data.Substring(0, 2) == "11")
            {
                if (moldingFull(plc, plc.location[0]))
                {
                    LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机A口生成取满任务", "注塑机");
                }
            }
            if (data.Substring(2, 2) == "21")
            {
                if (moldingFull(plc, plc.location[1]))
                {
                    LogHelper.Info($"{plc.deviceName}--IP={plc.address}--msg=注塑机B口生成取满任务", "注塑机");
                }
            }
 
        }
 
        private static bool moldingFull(Settings.deviceInfo plc, string location)
        {
            //通过站点编号查询工单
            //通过工单获取物料编码、批号(前期富勒没上线批号自行编写)
            var result = false;
 
            var db = new SqlHelper<object>().GetInstance();
            if (location != "")
            {
                if (LocationHelper.CheckLocFree(location))
                {
                    LogHelper.Info($"2{plc.deviceName}--IP={location})");
                    
                    var workOrder = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == plc.deviceName).First();
                    if (workOrder != null)
                    {
                        LogHelper.Info($"查到了工单{workOrder.S_WOB_CODE} 工单状态为: {workOrder.S_WOB_STATE} 即产即用={workOrder.S_USING_NOW} 连接区域={workOrder.S_LINKLINENO}", "瓶盖机");
                        if (workOrder.S_WOB_STATE.Trim() == "执行中")
                        {
                            LogHelper.Info($"查询当前货位 托盘数量 货位编码={location}");
                            var trayInfo = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE == location).ToList();
                            if (trayInfo.Count() > 0)
                            {
                                LogHelper.Info($"当前货位托盘数量大于0");
                                var trayNo = trayInfo[0].S_CNTR_CODE;
                                ProduceTask(plc, location, db, workOrder, trayNo);
                            }
                            else
                            {
                                LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 该站点没有托盘无法取满", "瓶盖机");
                            }
                        }
                    }
                    else
                    {
                        LogHelper.Info($"瓶盖机:{plc.deviceName} 下线信号:{location} 未找到工单", "瓶盖机");
                    }
                }
                else
                {
                    LogHelper.Info($"瓶盖机:{plc.deviceName} 当前位置{location}有任务,不可触发满托下线", "瓶盖机");
                }
            }
            return result;
        }
 
        internal static void TaskState(WCSTask mst, int state)
        {
            LogHelper.Info($"车辆回报完后调用空框上线");
            if (mst.S_OP_NAME == "注塑机下线" && state == 6)
            {
                KKoutTask(mst);
            }
        }
 
        private static void KKoutTask(WCSTask mst)
        {
            var db = new SqlHelper<object>().GetInstance();
            try
            {
                //卸货完成生成上空任务
                var deviceInfo = Settings.deviceInfos.Where(a => a.location.Contains(mst.S_START_LOC)).FirstOrDefault();
                if (deviceInfo != null)
                {
                    //查找对应产线的工单是否执行
                    var workInfo = db.Queryable<WorkOrder>().Where(a => a.S_BP_CODE == deviceInfo.deviceName && a.S_WOB_STATE == "执行中").First();
                    if (workInfo != null)
                    {
                        //查找产线对应的空框库区配置
                        var prodectArea = Settings.connectAreas.Where(a => a.deviceName == deviceInfo.deviceName && a.locAttribute == "空").FirstOrDefault();
                        if (prodectArea != null)
                        {
                            //查询库区是否有空框使用
                            Location startLoc = null;
                            string trayNo = "";
                            for (int i = 0; i < prodectArea.connectAreaList.Length; i++)
                            {
                                LogHelper.Info($"伪长度{i}");
                                var startArea = prodectArea.connectAreaList[i];
                                startLoc = db.Queryable<Location>().Where(a => a.S_AREA_CODE == startArea && a.N_CURRENT_NUM > 0 && a.N_LOCK_STATE == 0).Includes(a => a.LocCntrRel).First();//a.S_ATTRIBUTE=="空" &&
                                if (startLoc!=null)
                                {
                                    LogHelper.Info($"S_CODE{startLoc.S_CODE}");
                                    if (startLoc.LocCntrRel != null)
                                    {
                                        trayNo = startLoc.LocCntrRel.S_CNTR_CODE;
                                        break;
                                    }
                                }
                                LogHelper.Info($"trayNo{trayNo}");
                               
                            }
                            LogHelper.Info($"进来了");
                            if (startLoc != null && !string.IsNullOrEmpty(trayNo))
                            {
                                var endLoc = db.Queryable<Location>().Where(a => a.S_CODE == mst.S_START_LOC).First();
                                WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机上线", 2);
                            }
                        }
                    }
                }
            }
            catch (Exception ex) 
            {
                LogHelper.Error($"注塑上线异常 任务号{mst.S_CODE} 异常信息{ex.Message}", ex);
            }
        }
 
        private static void ProduceTask(Settings.deviceInfo plc, string location, SqlSugar.SqlSugarClient db, WorkOrder workOrder, string trayNo)
        {
            var startLoc = db.Queryable<Location>().Where(a => a.S_CODE == location).First();
            Location endLoc = null;
            try
            {
                var connectArea = Settings.connectAreas.Where(a => a.deviceName == plc.deviceName && a.locAttribute == "满").FirstOrDefault();
                if (connectArea != null)
                {
                    for (int i = 0; i < connectArea.connectAreaList.Length; i++)
                    {
                        var endArea = connectArea.connectAreaList[i];
 
                        LogHelper.Info($"库区:{endArea} ");
                        endLoc = db.Queryable<Location>()
                            .Where(a =>
                            a.S_AREA_CODE == endArea &&
                            //a.S_ATTRIBUTE == "满" &&
                            a.N_CURRENT_NUM == 0 &&
                            a.N_LOCK_STATE == 0)
                            .First();
                        if (endLoc != null)
                        {
                            break;
                        }
                        else
                        {
                            LogHelper.Info($"库区:{endArea}没有找到可用库位");
                        }
                    }
 
                    if (startLoc != null && endLoc != null)
                    {
                        //创建任务
                        var res = WMSHelper.CreatOpenation(startLoc, endLoc, trayNo, "注塑机下线", 1);
                        if (res)
                        {
                            //将托盘绑定物料
                            var itemInfo = db.Queryable<CntrItemRel>()
                                .Where(a =>
                                a.S_ITEM_CODE == workOrder.S_ITEM_CODE &&
                                a.S_BP_CODE == workOrder.S_BP_CODE &&
                                a.S_BATCH_NO==workOrder.S_BATCH_NO &&
                                a.S_CNTR_CODE == trayNo)
                                .First();
                            if (itemInfo == null)
                            {
                                //新增
                                itemInfo = new CntrItemRel { S_ITEM_CODE = workOrder.S_ITEM_CODE, S_BP_CODE = workOrder.S_BP_CODE, S_CNTR_CODE = trayNo ,S_BATCH_NO=workOrder.S_BATCH_NO};
                                db.Insertable(itemInfo).ExecuteCommand();
                            }
                        }
                    }else
                        LogHelper.Info($"生成任务失败,没有找到可用库位");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"注塑机下线算取满框缓存位异常 异常信息{ex.Message}", ex);
            }
        }
    }
}