杨前锦
2025-07-01 a93b0e99036c24b9bd58c79bf5e7364b1ba28bae
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
using HH.WCS.Mobox3.TSSG.api;
using HH.WCS.Mobox3.TSSG.device;
using HH.WCS.Mobox3.TSSG.dispatch;
using HH.WCS.Mobox3.TSSG.models;
using HH.WCS.Mobox3.TSSG.util;
using HH.WCS.Mobox3.TSSG.wms;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Threading;
using static HH.WCS.Mobox3.TSSG.api.ApiModel;
 
namespace HH.WCS.Mobox3.TSSG.process
{
   
    /// <summary>
    /// 设备信号处理,主要是tcp信号,我们做server被动接收信号来处理,根据项目定制的
    /// </summary>
    internal class DeviceProcess
    {
 
        public static Dictionary<string,DateTime> lastReceiptTime = new Dictionary<string,DateTime>();
 
        internal static void Analysis(string data, string ip) {
 
           // 检查是否需要跳过处理
            if (lastReceiptTime.TryGetValue(ip, out DateTime lastTime) &&
                DateTime.Now < lastTime.AddMinutes(1))
            {
                return; // 1分钟内重复请求,跳过
            }
 
            // 更新接收时间并执行业务逻辑
            lastReceiptTime[ip] = DateTime.Now;
 
            LogHelper.Info($"下发入库请求,IP:{ip}", "TSSG");
            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();
                Console.WriteLine("plc:" + JsonConvert.SerializeObject(plc));
                if (plc != null) {
                    if (plc.deviceType == 1) {
                        
                        var db = new SqlHelper<object>().GetInstance();
                        var workOrder = db.Queryable<WorkOrder>().Where(a => a.S_STATUS.Equals("执行中") && a.S_PROD_LINE.Equals(plc.deviceName)).First();
                        Console.WriteLine("工单:" + JsonConvert.SerializeObject(workOrder));
                        if (workOrder != null) {
                            string endArea = LocationHelper.getAreaByAreaName(workOrder.S_END_AREA).S_CODE;
 
                            InstockInfo instockInfo = new InstockInfo()
                            {
                                start = plc.location[0],
                                item = workOrder.S_ITEM_CODE,
                                endArea = endArea,
                                ip = ip,
                                UDID = GenerateTaskNo()
                            };
                            LogHelper.Info("接收信号处理:"+ JsonConvert.SerializeObject(instockInfo),"TSSG");
                            ApiHelper.Instock(instockInfo);
                        }
                    }
                    else if (plc.deviceType == 2) {
                        //出库缓存位的光电信息
                        //如果有缓存位是空的状态,我们先判断有没有任务终点分配到这里,如果没有,就找一条出库任务,终点是虚拟点的任务,分配到这个空位
                        //修改任务终点
                    }
                }
                else {
                    Console.WriteLine($"TCP信号处理:未查询到IP为{ip}的数据,请检查deviceInfo配置中心是否存在该IP的数据!");
                }
            }
 
        }
 
        internal static string GenerateTaskNo()
        {
            var id = SYSHelper.GetSerialNumber("缓存信号识别码", "XH");
            var date = DateTime.Now.ToString("yyMMdd");
            return $"XH{date}{id.ToString().PadLeft(5, '0')}";
        }
    }
}