using LocalhostMES.Core; using LocalhostMES.DataBase; using Microsoft.Owin.Hosting; using Org.BouncyCastle.Asn1.Ocsp; using Owin; using Prism.Ioc; using System; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Web.Http; using Unity; using Unity.Lifetime; using Unity.WebApi; namespace LocalhostMES.Controller { public class WebApiService : IDisposable { private IDisposable _webApp; private CancellationTokenSource _cts = new CancellationTokenSource(); private int _port; private StartOptions _startOptions; private string _baseUrl="192.168.1.26:8081"; public WebApiService() { } public void Init(int port = 8081) { _port = port; _baseUrl = $"http://192.168.1.26:{port}"; _startOptions = new StartOptions { Urls = { $"http://192.168.1.26:{port}",$"http://10.157.15.51:{port}" } };//", } public async Task StartAsync() { try { // 创建 Startup 实例并传入容器提供者 var startup = new Startup(); // 启动 Web App _webApp = WebApp.Start(_startOptions, startup.Configuration); // 启动Web API服务 // 初始化测试数据 InitializeTestData(); LogHelper.WriteLogInfo($"Web API服务已启动: {_baseUrl}"); Console.WriteLine($"Web API服务已启动: {_baseUrl}"); } catch ( Exception ex ) { Console.WriteLine($"启动Web API服务失败: {ex.Message}"); LogHelper.WriteLogInfo($"启动Web API服务失败: {ex.Message}"); throw; } } private void InitializeTestData() { try { //// 添加一些测试工单 //var testWorkOrder = new Models.WorkOrderInfo //{ // WorkOrderNo = "GD-001", // MaterialCode = "T03GCD", // MaterialName = "T03贯穿灯", // PlannedQuantity = 100, // CompletedQuantity = 0, // Status = "2", // 已发布 // LineCode = "RSC01", // CreateTime = DateTime.Now.AddDays(-1), // StartTime = DateTime.Now.AddHours(-2), // EndTime = DateTime.Now.AddHours(1), //}; //DatabaseHelper.InsertWorkOrderInfo(testWorkOrder); //// 生成测试SN //for ( int i = 1; i <= 10; i++ ) //{ // DatabaseHelper.InsertSnInfo(new Models.SnInfo // { // Sn = $"P-T03GCD-{DateTime.Now:yyMMddHHmm}-{i:D4}-{Guid.NewGuid().ToString("N").Substring(0, 6).ToUpper()}", // GenerateTime = DateTime.Now.AddMinutes(-i * 5), // WorkOrderNo = "GD-001", // PrintType = "产品码", // IsUsed = i > 5 // 前5个已使用 // }); //} Console.WriteLine("测试数据初始化完成"); } catch ( Exception ex ) { Console.WriteLine($"初始化测试数据失败: {ex.Message}"); } } public Task StopAsync() { _webApp?.Dispose(); _webApp = null; Console.WriteLine("Web API服务已停止"); return Task.CompletedTask; } public void Dispose() { _cts?.Cancel(); _cts?.Dispose(); _webApp?.Dispose(); GC.SuppressFinalize(this); } public bool IsRunning => _webApp != null; public string BaseUrl => _baseUrl; } // OWIN Startup类 public class Startup { public void Configuration(IAppBuilder app) { // 创建 Unity 容器 var container = new UnityContainer(); // 启用CORS app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // 配置Web API var config = new HttpConfiguration(); // 启用属性路由 config.MapHttpAttributeRoutes(); // 配置默认路由 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; // 使用Web API app.UseWebApi(config); } } // 辅助类,用于跨控制器共享数据 //public static class ConcurrentStorage //{ // //public static readonly System.Collections.Concurrent.ConcurrentDictionary WorkOrders // // = new System.Collections.Concurrent.ConcurrentDictionary(); // //public static readonly System.Collections.Concurrent.ConcurrentDictionary> WorkOrderSns // // = new System.Collections.Concurrent.ConcurrentDictionary>(); // //public static readonly System.Collections.Concurrent.ConcurrentBag BindRecords // // = new System.Collections.Concurrent.ConcurrentBag(); // //public static readonly System.Collections.Concurrent.ConcurrentBag ProcessRecords // // = new System.Collections.Concurrent.ConcurrentBag(); //} }