kazelee
2025-05-28 de8cd5585ba690902333cf4ce9aa5dbc7eb9acf6
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
using System;
using System.Collections.Generic;
using System.Threading;
using HH.WCS.Mobox3.DSZSH.device;
using HH.WCS.Mobox3.DSZSH.core;
 
using Microsoft.Owin.Hosting;
 
using Topshelf;
 
using Task = System.Threading.Tasks.Task;
using Monitor = HH.WCS.Mobox3.DSZSH.core.Monitor;
 
namespace HH.WCS.Mobox3.DSZSH {
    internal class Program
    {
        static void Main(string[] args) {
            // 基础设置信息初始化
            Settings.Init();
            // 1.0 开启api
            Startup();
            // 2.0 开启tcp
            StartTcp();
            // 3.0 开启S7
            //StartS7();
            // 4.0 开启Modbus
            //StartModbus();
 
            // TCP测试
            //TcpClientHelper.Link("127.0.0.1", 8550);
 
            // 5.0 开启线程
            var rc = HostFactory.Run(x => {
                x.Service<WorkThread>(s => {
                    s.ConstructUsing(name => new WorkThread());
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });
                x.RunAsLocalSystem();
 
                x.SetDescription("hh123");
                x.SetDisplayName("hh123.wms");
                x.SetServiceName("hh123.wms");
            });
 
            var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
            Environment.ExitCode = exitCode;
        }
 
        /// <summary>
        /// 开启API协议通讯
        /// </summary>
        private static void Startup()
        {
            Console.WriteLine("Startup ApiController");
            Task.Run(() =>
            {
                var url = Settings.WebApiUrl;
                Console.WriteLine(url);
                using (WebApp.Start<Startup>(url))
                {
                    Console.WriteLine("API,Running on {0}", url);
                    Console.ReadLine();
                }
            });
        }
        
        /// <summary>
        /// 开启TCP协议通讯,服务端
        /// </summary>
        private static void StartTcp()
        {
            var tcpServerIP = Settings.TcpServerIp;
            var tcpServerPort = Settings.TcpServerPort;
            new TcpServer(tcpServerIP, tcpServerPort);
            //var res = TcpClientHelper.Init(tcpServerIP, tcpServerPort);
            //var res = TcpClientHelper.Init(tcpServerIP, 8550);
            //LogHelper.Info($"TcpClient连接" + (res ? "成功" : "失败"));
        }
 
        /// <summary>
        /// 开启S7协议通讯
        /// </summary>
        private static void StartS7()
        {
            //所有的S7设备
            //var allPLCDevice = Settings.ProductionLines;
 
            //if (allPLCDevice.Count > 0)
            //{
            //    foreach (var item in allPLCDevice)
            //    {
            //        new S7Helper(item.ProductionLine_IP, (short)item.ProductionLine_Rack, (short)item.ProductionLine_Slot);
            //        Console.WriteLine("S7ProductionLineHelper," + item.ProductionLine_IP);
            //    }
            //}
        }
 
        /// <summary>
        /// 开启Modbus协议通讯
        /// </summary>
        private static void StartModbus()
        {
            // 所有的Modbus设备
            var allPLCDevice = Settings.ProductionLines;
            
            if (allPLCDevice.Count > 0) {
                foreach (var item in allPLCDevice) {
                    new ModbusHelper(item.PlcIp, item.PlcPort, (byte)item.SlaveId);
                    Console.WriteLine("ModbusHelper," + item.PlcIp);
                }
            }
        }
        
        public class WorkThread
        {
            public void Start()
            {
                List<Task> tasks = new List<Task>();
 
                tasks.Add(GetTask(WCSCore.Dispatch));
 
                // 测试:托盘下线
                //tasks.Add(GetTask(Monitor.CheckInbound));
 
                // 轮询:出库单状态
                tasks.Add(GetTask(Monitor.CheckOutboundOrder));
 
                // 轮询:抽检单状态
                tasks.Add(GetTask(Monitor.CheckCheckOrder));
 
                // 轮询:移库单状态
                tasks.Add(GetTask(Monitor.CheckShiftOrder));
 
                Task.WaitAll(tasks.ToArray());
            }
            public void Stop() { Console.WriteLine("work stopped"); }
 
            private Task GetTask(Action action, int intervalMs = 3000)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(intervalMs);
                    }
                });
                return task;
            }
        }
    }
}