10133
2025-06-05 de050bac4823c99ff275b85cb0c80f292d1bbfb1
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
using HH.WCS.NongFuChaYuan.ApiService;
using HH.WCS.NongFuChaYuan.TaskController;
using HH.WCS.NongFuChaYuan.DeviceService;
using HH.WCS.NongFuChaYuan.OtherService;
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.NongFuChaYuan.TaskController.Monitor;
using HH.WCS.NongFuChaYuan.WmsService;
 
namespace HH.WCS.NongFuChaYuan
{
    internal class StartMain
    {
        static void Main(string[] args)
        {
            //0.0 项目标识
            ////Console.WriteLine($"Startup Server On {Settings.ProjectName}!");
            //3.0 开启外挂服务
            StartOther();
            //4.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.WmsService");
                x.SetServiceName("hh123.WmsService");
            });
 
            var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
            Environment.ExitCode = exitCode;
        }
 
        private static void Startup()
        {
            Task.Run(() =>
            {
                var url = "http://+:8001";//{SettingHelper.port}
                //var url = Settings.HttpUrlPort;//{SettingHelper.port}
                using (WebApp.Start<Startup>(url))
                {
                    Console.WriteLine("Startup ApiServer On {0}!", url);
                    Console.ReadLine();
                    while (true) { }
                }
            });
        }
 
        private static void StartTcp()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine($"Startup TcpServer On {ip.ToString()}:{Settings.TcpPort}!");
                    new TcpServer(ip.ToString());
                }
            }
        }
 
        private static void StartOther()
        {
            Console.WriteLine($"Startup OtherServer!");
            STAttribute.InitTableName();//初始化 MOBOX、SQLSERVER 数据库表名
            STAttribute.CreateMiddleTable();//初始化 标准数据中间表
 
            WCSHelper.CreateMiddleTable();//创建中间表
                                          //WCSHelper.InitRowLockTable();//初始化排锁定表
 
 
        }
 
        public class WorkThread
        {
 
            public void Start()
            {
                //1.0 开启api
                Startup();
                //2.0 开启tcp
                StartTcp();
                List<Task> tasks = new List<Task>();
                //添加任务推送线程
                tasks.Add(GetTask(TaskProcess.Dispatch));
                //添加自定义线程
                TaskAddFunc(tasks);
                //Task.WaitAll(tasks.ToArray());
            }
 
            private void TaskAddFunc(List<Task> tasks)
            {
                tasks.Add(GetTask(Monitor.AnalysisIntraoCular));
                tasks.Add(GetTask(Monitor.AnalysisMoveLib));
                //tasks.Add(GetTask(Monitor.AnalysisMoveLib1));
                //tasks.Add(GetTask(Monitor.PanKurealtime));
                //tasks.Add(GetTask(Monitor.AnalysisProductLine));
                //tasks.Add(GetTask(Monitor.AnalysisStockup));
            }
 
            public void Stop() { Console.WriteLine("Stopped Thread Server!"); }
            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;
            }
        }
    }
}