lss
2025-07-07 ba528c7a15058967ee3af7364035209aa0048025
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
using HH.WCS.DaYang.device;
using HH.WCS.DaYang.dispatch;
using HH.WCS.DaYang.util;
using HH.WCS.DaYang.wms;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Threading;
using static HH.WCS.DaYang.api.ApiModel;
using static HH.WCS.DaYang.util.Settings;
 
namespace HH.WCS.DaYang.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)
                    {
 
                    }
                    else if (plc.deviceType == 2)
                    {
                        //出库缓存位的光电信息
                        //如果有缓存位是空的状态,我们先判断有没有任务终点分配到这里,如果没有,就找一条出库任务,终点是虚拟点的任务,分配到这个空位
                        //修改任务终点
                    }
 
 
                }
                else
                {
                    Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
                }
            }
 
        }
        /// <summary>
        /// 自动门
        /// </summary>
        /// <param name="data"></param>
        /// <param name="plc"></param>
        /// 
        private static Dictionary<string, signalInfo> doorStatus = new Dictionary<string, signalInfo>();
        internal static void AnalysisDoor(string xh, string data, Settings.deviceInfo plc)
        {
            LogHelper.Info($"自动门状态:{xh},地址为:{plc.address},门号:{plc.deviceName}", "自动门");
            if (data.Length / 2 == plc.deviceNo.Length)//2 2
            {
                for (int i = 0; i < plc.deviceNo.Length; i++)
                {
                    var state = data.Substring(i * 2 + 1, 1);
                    //Console.WriteLine($"门{plc.deviceNo[i]}的状态{state}");
                    if (doorStatus.Keys.Contains(plc.deviceNo[i]))
                    {
                        doorStatus[plc.deviceNo[i]].info = state;
                        doorStatus[plc.deviceNo[i]].modify = DateTime.Now;
                    }
                    else
                    {
                        doorStatus.Add(plc.deviceNo[i], new signalInfo { info = state, modify = DateTime.Now });
                    }
                }
            }
        }
        public class signalInfo
        {
            public string info { get; set; }
            public DateTime modify { get; set; }
        }
        internal static void Traffic(string forkliftNo, string zone, bool occupy)
        {
            var plc = Settings.deviceInfos.Where(a => a.deviceNo.Contains(zone)).FirstOrDefault();
            if (plc != null)
            {
 
 
                var index = 1;
                for (int i = 0; i < plc.deviceNo.Length; i++)
                {
                    if (plc.deviceNo[i] == zone)
                    {
                        index = i + 1;
                        break;
                    }
                }
                LogHelper.Info($"安全门开门请求,门号:{zone},index:{index}");
                if (occupy)
                {
 
                    //开门请求 3F 00 11 20 0d 0a( 3F 00 10 21 0d 0a)
 
                    var req = $"3f 00 {index}1 0d 0a";
                    PlcHelper.SendHex(plc.address, req);
                    var msg = $"安全门开门请求,门号:{zone},index:{index},ip={plc.address}, data={req}";
                    LogHelper.Info(msg, "发送信号");
 
                    //车子请求一次就发一次开门请求
                    if (doorStatus.Keys.Contains(zone) && DateTime.Now.Subtract(doorStatus[zone].modify).TotalSeconds < 5)
                    {
                        if (doorStatus[zone].info == "1")
                        {
                            LogHelper.Info($"安全门已经打开:门号:{zone},index:{index}", "自动门");
                            NDCHelper.Traffic(zone, forkliftNo);
                        }
                    }
 
 
                }
                else
                {
                    //关门信号 3F 00 10 20 0d 0a
                    var req = $"3f 00 {index}0 0d 0a";
                    PlcHelper.SendHex(plc.address, req);
                    var msg = $"安全门关门请求,门号:{zone},index:{index},ip={plc.address}, data={req}";
                    LogHelper.Info(msg, "发送信号");
                }
 
 
            }
        }
        /// <summary>
        /// 获取叠盘机状态
        /// </summary>
        internal static void DijStatus()
        {
            var db = new SqlHelper<object>().GetInstance();
            HttpHelper httpHelper = new HttpHelper();
            TableUrl tableUrl = Settings.tableUrls.Find(a => a.id == 3);
            string DiePanLoc = "";
            Location startloc = null;
            if (tableUrl != null)
            {
                Result<DeviceStatusModel> DeviceStatu = httpHelper.GetDeviceStatus(new string[] { DiePanLoc }, tableUrl.url);
                if (DeviceStatu.Data != null)
                {
                    if (int.Parse(DeviceStatu.Data.CntrQty) > 0 && int.Parse(DeviceStatu.Data.CntrQty) < 4)
                    {
                        startloc = LocationHelper.GetLocByItemCodeOrderyTime("YCLLKQ", "");
 
                    }
                    else
                    {
 
                        startloc = db.Queryable<Location>().Where(a => a.N_CURRENT_NUM == 4).OrderByDescending(a => a.N_POS).First();
 
                    }
                    //根据托盘获取起点信息
                    var endloc = db.Queryable<Location>().Where(a => a.S_CODE == DiePanLoc).First();
                    if (endloc == null)
                    {
                        return;
                    }
                    #region 创建任务
                    if (startloc != null && endloc != null)
                    {
                        //获取托盘
                        var Cntr = db.Queryable<LocCntrRel>().Where(a => a.S_LOC_CODE.Trim() == startloc.S_CODE).ToList();
                        if (Cntr == null)
                        {
                            return;
                        }
                        var wcsTask = new WCSTask
                        {
                            S_CODE = WCSHelper.GenerateTaskNo(),
                            S_TYPE = "空托出库",
                            S_START_LOC = startloc.S_CODE,
                            S_END_LOC = endloc.S_CODE,
                            S_START_WH = startloc.S_WH_CODE,
                            S_START_AREA = startloc.S_AREA_CODE,
                            S_END_WH = endloc.S_WH_CODE,
                            S_END_AREA = endloc.S_AREA_CODE,
                            N_CNTR_COUNT = 1,
                            N_SCHEDULE_TYPE = 2,
                            S_CNTR_CODE = JsonConvert.SerializeObject(Cntr.Select(a => a.S_CNTR_CODE).ToList())
                        };
                        LogHelper.Info("创建入平库任务:" + JsonConvert.SerializeObject(wcsTask));
                        if (WCSHelper.CreateTask(wcsTask))
                        {
                            LocationHelper.LockLoc(startloc.S_CODE, 2);
                            LocationHelper.LockLoc(endloc.S_CODE, 1);
                            LogHelper.Info("创建任务成功");
                        }
 
                        #endregion
                    }
                }
            }
        }
    }
}