杨前锦
2025-05-30 e4b92180e4336d777ac323c0395a043bde1285d3
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
using HH.WCS.Mobox3.YNJT_BZP.api;
using HH.WCS.Mobox3.YNJT_BZP.core;
using HH.WCS.Mobox3.YNJT_BZP.device;
using HH.WCS.Mobox3.YNJT_BZP.process;
using HH.WCS.Mobox3.YNJT_BZP.util;
using HH.WCS.Mobox3.YNJT_BZP.wms;
using Microsoft.Owin.Hosting;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;
using Monitor = HH.WCS.Mobox3.YNJT_BZP.core.Monitor;
 
namespace HH.WCS.Mobox3.YNJT_BZP
{
    internal class Program
    {
        static void Main(string[] args) {
 
            Settings.Init();
            // 设置控制台编码为 UTF-8
            Console.OutputEncoding = Encoding.UTF8;
 
            Settings.Init();
            //1.0 开启api
            Startup();
            //2.0 开启tcp
            StartTcp();
            //3.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;
        }
 
        private static void Startup() {
            Console.WriteLine("Startup ApiController");
            Task.Run(() => {
                var url = $"http://127.0.0.1:{Settings.port}";
                Console.WriteLine(url);
                using (WebApp.Start<Startup>(url)) {
                    Console.WriteLine("Running on {0}", url);
                    Console.ReadLine();
                }
            });
        }
        private static void StartTcp() {
           /* Console.WriteLine("开启tcp:" + Settings.listenAddress);
            new TcpServer(Settings.listenAddress);*/
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine($"ip= {ip.ToString()}");
                    new TcpServer(ip.ToString());
                }
            }
        }
 
        public class WorkThread
        {
            public void Start() {
                List<Task> tasks = new List<Task>();
                //添加任务推送线程
                tasks.Add(GetTask(WCSCore.Dispatch));
                //自动补充母拖
                tasks.Add(GetTask(Monitor.AutoEmptyTrayOutStock));
 
                Task.WaitAll(tasks.ToArray());
            }
            public void Stop() { Console.WriteLine("work stopped"); }
            private Task GetTask(Action action) {
                var task = Task.Run(() => {
                    while (true) {
                        try {
                            action();
                        }
                        catch (Exception ex) {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(3000);
                    }
                });
                return task;
            }
        }
    }
}