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.util; using Microsoft.Owin.Hosting; using Topshelf; namespace HH.WCS.Mobox3.RiDong { internal class Program { static void Main(string[] args) { Settings.Init(); //1.0 开启api Startup(); //2.0 开启tcp StartTcp(); //3.0 开启线程 var rc = HostFactory.Run(x => { x.Service(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(url)) { Console.WriteLine("Running on {0}", url); Console.ReadLine(); } }); } /// /// 启动TCP连接 /// 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()); } } } /// /// 工作的线程 /// public class WorkThread { public void Start() { List tasks = new List { // 创建任务(根据未执行的作业创建对应任务) 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; } /// /// ftp上传与下载 /// /// /// 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; } } } }