using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using HH.WCS.Mobox3.AnGang.AppStart; using HH.WCS.Mobox3.AnGang.Devices; using Microsoft.Owin.Hosting; using Topshelf; namespace HH.WCS.Mobox3.AnGang { internal class Program { [STAThread] static void Main(string[] args) { //基础设置信息初始化 Settings.Init(); //1.0 开启api Startup(); //2.0 开启tcp StartTcp(); //3.0 开启S7 //StartS7(); //4.0 开启Modbus //StartModbus(); //开启相机 StartSnap(); //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.Config.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.Config.TCPServerIP; var tcpServerPort = Settings.Config.TCPServerPort; new TcpServer(tcpServerIP, tcpServerPort); } /// /// 开启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); // Console.WriteLine("ModbusHelper," + item.PlcIp); // } //} } private static void StartSnap() { //new SnapManager(Settings.Snap); new SnapManager(Settings.Config.Snap[0]); } public class WorkThread { public void Start() { List tasks = new List(); // 添加任务推送线程 //tasks.Add(GetTask(WCSCore.Dispatch)); ////添加自定义线程 //tasks.Add(GetTask(Monitor.CheckEmptyCnt));//检测空托盘 ////根据S7/Modbus协议判断输送线的信号 原材料产线库区=>满托缓存库区,空托缓存库区=>原材料产线库区 //tasks.Add(GetTask(Monitor.CheckS7Devices)); 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(3000); } }); return task; } } } }