Tjiny
7 天以前 6d40f7c8b19efc612f824ee7e778d5be9f8382f5
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using HH.WCS.Mobox3.RiDong.device;
using HH.WCS.Mobox3.RiDong.generalMethod;
using HH.WCS.Mobox3.RiDong.Quartz;
using HH.WCS.Mobox3.RiDong.util;
using Microsoft.Owin.Hosting;
using Quartz;
using Quartz.Impl;
using Topshelf;
 
namespace HH.WCS.Mobox3.RiDong
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            // 定时任务
            await QuartzJobCreate.CreateJob();
            
            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://192.168.1.87:8901";//{SettingHelper.port}
                var url = $"http://+:{Settings.port}";//
                Console.WriteLine(url);
                using (WebApp.Start<Startup>(url))
                {
                    Console.WriteLine("Running on {0}", url);
                    Console.ReadLine();
                }
            });
        }
 
        /// <summary>
        /// 启动TCP连接
        /// </summary>
        private static void StartTcp()
        {
            //new TcpServer("192.168.1.87");
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine($"ip= {ip}");
                    new TcpServer(ip.ToString());
                }
            }
        }
 
        /// <summary>
        /// 工作的线程
        /// </summary>
        public class  WorkThread
        {
            public void Start()
            {
                List<Task> tasks = new List<Task>
                {
                    // 创建任务(根据未执行的作业创建对应任务)
                    GetTask(ThreadMenthod.CreateTask),
                    // 创建出库单(MaterialOut)
                    GetTask(ThreadMenthod.CreateOutboundOrderFromMaterialOut),
                    // 创建出库单(DoOut) 
                    GetTask(ThreadMenthod.CreateOutboundOrderFromDoOut),
                    // 创建出库作业
                    GetTask(ThreadMenthod.CreateOutTaskFromDistributionCntrDetail),
                    // 作业完成以及错误修改
                    GetTask(ThreadMenthod.AccomplishOperation),
                    // Directory读取文件,并处理
                    GetTask(DirectoryHelper.GainData),
                    // 出库agv任务推送
                    GetTaskS(ThreadMenthod.DispatchFromAGV),
                    // 读取线体信息并做对应的处理
                    GetTask(ThreadMenthod.ReadConveyorlinesMessage),
                };
                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 GetTaskS(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
 
                        Thread.Sleep(600000);
                    }
                });
 
                return task;
            }
 
            /// <summary>
            /// ftp上传与下载
            /// </summary>
            /// <param name="action"></param>
            /// <returns></returns>
            private Task FtpDownOrUp(Action action)
            {
                var task = Task.Run(() =>
                {
                    while (true)
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex.Message, ex);
                        }
 
                        // 六十万秒(十分钟)
                        Thread.Sleep(10000);
                    }
                });
                return task;
            }
        }
    }
}