using System; using System.Collections.Generic; using System.Threading; using HH.WCS.Mobox3.DSZSH.device; using HH.WCS.Mobox3.DSZSH.core; using Microsoft.Owin.Hosting; using Topshelf; using Task = System.Threading.Tasks.Task; using Monitor = HH.WCS.Mobox3.DSZSH.core.Monitor; using System.Net.Sockets; namespace HH.WCS.Mobox3.DSZSH { internal class Program { static void Main(string[] args) { // 基础设置信息初始化 Settings.Init(); // 1.0 开启api Startup(); // 2.0 开启tcp //StartTcp(); // 3.0 开启S7 //StartS7(); // 4.0 开启Modbus //StartModbus(); // TCP测试 //TcpClientHelper.Link("127.0.0.1", 8550); Task.Run(() => { WCSCore.StartServer(); }); // 5.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; } /// /// 开启API协议通讯 /// private static void Startup() { Console.WriteLine("Startup ApiController"); Task.Run(() => { var url = Settings.WebApiUrl; Console.WriteLine(url); using (WebApp.Start(url)) { Console.WriteLine("API,Running on {0}", url); Console.ReadLine(); } }); } /// /// 开启TCP协议通讯,服务端 /// private static void StartTcp() { //var tcpServerIP = Settings.TcpServerIp; //var tcpServerPort = Settings.TcpServerPort; //new TcpServer(tcpServerIP, tcpServerPort); //var res = TcpClientHelper.Init(tcpServerIP, tcpServerPort); //var res = TcpClientHelper.Init(tcpServerIP, 8550); //LogHelper.Info($"TcpClient连接" + (res ? "成功" : "失败")); } /// /// 开启S7协议通讯 /// private static void StartS7() { //所有的S7设备 //var allPLCDevice = Settings.ProductionLines; //if (allPLCDevice.Count > 0) //{ // foreach (var item in allPLCDevice) // { // new S7Helper(item.ProductionLine_IP, (short)item.ProductionLine_Rack, (short)item.ProductionLine_Slot); // Console.WriteLine("S7ProductionLineHelper," + item.ProductionLine_IP); // } //} } /// /// 开启Modbus协议通讯 /// private static void StartModbus() { // 所有的Modbus设备 var allPLCDevice = Settings.ProductionLines; if (allPLCDevice.Count > 0) { foreach (var item in allPLCDevice) { new ModbusHelper(item.PlcIp, item.PlcPort, (byte)item.SlaveId); Console.WriteLine("ModbusHelper," + item.PlcIp); } } } public class WorkThread { public void Start() { List tasks = new List(); tasks.Add(GetTask(WCSCore.Dispatch)); // 测试:托盘下线 //tasks.Add(GetTask(Monitor.CheckInbound)); // 轮询:出库单状态 tasks.Add(GetTask(Monitor.CheckOutboundOrder)); // 轮询:抽检单状态 tasks.Add(GetTask(Monitor.CheckCheckOrder)); // 轮询:移库单状态 tasks.Add(GetTask(Monitor.CheckShiftOrder)); Task.WaitAll(tasks.ToArray()); } public void Stop() { Console.WriteLine("work stopped"); } private Task GetTask(Action action, int intervalMs = 3000) { var task = Task.Run(() => { while (true) { try { action(); } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } Thread.Sleep(intervalMs); } }); return task; } } } }