1
pulg
2025-05-14 5a640911f7e7ef3a003775993f077e1a0e9ac130
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using HH.WCS.Hexafluo.api;
using HH.WCS.Hexafluo.core;
using HH.WCS.Hexafluo.device;
using HH.WCS.Hexafluo.util;
using HH.WCS.SJML.Bll;
using HH.WCS.ZCQTJ.core;
using HH.WCS.ZCQTJ.util;
using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Topshelf;
 
namespace HH.WCS.Hexafluo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string currentProcessName = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(currentProcessName);
            if (processes.Length > 1)
            {
                Environment.Exit(0);
            }
            //1.0 开启api
            Startup();
            //2.0 开启tcp
            StartTcp();
            //初始化表名
            TableName();
            //初始化连接池
            SqlName();
            //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("hh");
                x.SetDisplayName("中策大江东WCS V1.03");
                x.SetServiceName("hh.wms");
            });
 
            var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
            Environment.ExitCode = exitCode;
        }
 
        /// <summary>
        /// 获取项目的表名
        /// </summary>
        private static void TableName()
        {
            STAttribute.GetValue(Settings.TableHelperList);
        }
        private static void SqlName()
        {
            BLLCreator.CreateSingleton<SqlServerList>().SqlConnectC();
        }
 
        private static void Startup()
        {
            Console.WriteLine("Startup ApiController");
 
            Task.Run(() =>
            {
                //var a  = Newtonsoft.Json.JsonConvert.DeserializeObject<SettingHelper>(SettingHelperList);
                var uri = Settings.SettingHelperList;
                Console.WriteLine(uri);
                //  var url = "http://+:8801";//{SettingHelper.port}
                try
                {
                    var url = uri;
                    using (WebApp.Start<Startup>(url))
                    {
                        Console.WriteLine("Running on {0}", url);
                        Console.ReadLine();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
 
            });
        }
        private static void StartTcp()
        {
            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(TaskCore.Dispatch));//正式
                //tasks.Add(GetTask(TaskCore.agvce));//测试
 
                //添加自定义线程
                tasks.Add(GetTask(MonitorZxc.CheckCamera));//总线程
                                                           //tasks.Add(GetTask(MonitorWtp.CheckCamera));//无托盘获取托盘信息
                                                           //tasks.Add(GetTask(Monitor.CheckCamera));//入库
                                                           //tasks.Add(GetTask(MonitorWtp.CheckCamera));//无托盘入库 获取托盘信息
                                                           //tasks.Add(GetTask(MonitorOut.CheckCamera));//出库
 
                // tasks.Add(GetTask(MonitorAgvPriy.CheckCamera));//调整优先级
 
                tasks.Add(GetTask(WcsSecurityRequest.CheckCamera));//1102/1104安全请求 等异步请求
                tasks.Add(GetTaskCv(UnBing.CheckCamera));//解锁接驳位
                tasks.Add(GetTaskCv2(TaskOver.CheckCameraTask));//钉钉
                tasks.Add(GetTaskCv5(TaskOver.CheckCameraTask2));//钉钉2 30分钟
                tasks.Add(GetTaskCv4(TaskOverItem.CheckCameraTask));//钉钉推送物料数据
                tasks.Add(GetTask2(OverTimeAgvTask.AgvOverTaskPir));//出库agv任务调整优先级
                tasks.Add(GetTask(MesKtTask.MesKtTaskIssued));//空托任务处理
 
                //任务状态更新线程
                // tasks.Add(GetTask(TaskCore.SeleTaskListTm));
                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(5000);
                    }
                });
                return task;
            }
            private Task GetTask2(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(30000);
                    }
                });
                return task;
            }
            private Task GetTaskCv(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(60000);
                    }
                });
                return task;
            }
 
            private Task GetTaskCv2(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(300000);
                    }
                });
                return task;
            }
 
            private Task GetTaskCv3(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(900000);
                    }
                });
                return task;
            }
 
            private Task GetTaskCv4(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(3600000);
                    }
                });
                return task;
            }
            private Task GetTaskCv5(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
                        Thread.Sleep(2400000);
                    }
                });
                return task;
            }
        }
 
    }
}