using HH.WCS.Mobox3.pinggao.api;
|
using HH.WCS.Mobox3.pinggao.core;
|
using HH.WCS.Mobox3.pinggao.device;
|
using HH.WCS.Mobox3.pinggao.process;
|
using HH.WCS.Mobox3.pinggao.util;
|
using HH.WCS.Mobox3.pinggao.wms;
|
using Microsoft.Owin.Hosting;
|
using NLog;
|
using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Http;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using Topshelf;
|
using Monitor = HH.WCS.Mobox3.pinggao.core.Monitor;
|
|
namespace HH.WCS.Mobox3.pinggao
|
{
|
internal class Program
|
{
|
private static Timer timer;
|
static void Main(string[] args) {
|
|
|
string currentProcessName = Process.GetCurrentProcess().ProcessName;
|
Process[] processes = Process.GetProcessesByName(currentProcessName);
|
if (processes.Length <= 1)
|
{
|
Settings.Init();
|
//1.0 开启api
|
Startup();
|
|
//定時器
|
SetTimer();
|
//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 SetTimer()
|
{
|
TimerCallback tcb = new TimerCallback(CallWebApiAsync);
|
timer = new Timer(tcb, null, 0, 30000); // 300000毫秒 = 5分钟
|
//timer = new Timer(300000); // 设置定时器间隔为5分钟
|
//timer.Elapsed += async (sender, e) => await CallWebApiAsync();
|
//timer.AutoReset = true;
|
//timer.Enabled = true;
|
}
|
private static async void CallWebApiAsync(object state)
|
{
|
|
int i = 0;
|
i++;
|
LogHelper.Info($"Error: 定時器" + i);
|
Console.WriteLine($"Error: 定時器1"+i);
|
|
|
}
|
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();
|
}
|
});
|
}
|
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.ToString()}");
|
new TcpServer(ip.ToString());
|
}
|
}
|
}
|
|
public class WorkThread
|
{
|
public void Start() {
|
List<Task> tasks = new List<Task>();
|
//添加任务推送线程
|
//tasks.Add(GetTask(() => RunAtSpecificTime(WMSCore.Start, "14:00")));
|
tasks.Add(GetTask(WMSCore.Start));
|
tasks.Add(GetTask(WMSCore.CheckDistributionCNTROrder));
|
//tasks.Add(GetTask(WMSCore.TransportTask));
|
tasks.Add(GetTask(WCSCore.Dispatch));
|
//定时重置
|
tasks.Add(GetTask(WCSCore.DSCZ));
|
//充电
|
tasks.Add(GetTask(WCSCore.Dispatch1));
|
//添加自定义线程
|
|
//tasks.Add(GetTask(Monitor.CheckDevice));
|
|
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;
|
}
|
}
|
}
|
}
|