| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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<string, Models.WorkOrderInfo> WorkOrders
- // // = new System.Collections.Concurrent.ConcurrentDictionary<string, Models.WorkOrderInfo>();
- // //public static readonly System.Collections.Concurrent.ConcurrentDictionary<string, System.Collections.Generic.List<Models.SnInfo>> WorkOrderSns
- // // = new System.Collections.Concurrent.ConcurrentDictionary<string, System.Collections.Generic.List<Models.SnInfo>>();
- // //public static readonly System.Collections.Concurrent.ConcurrentBag<Models.BindRecord> BindRecords
- // // = new System.Collections.Concurrent.ConcurrentBag<Models.BindRecord>();
- // //public static readonly System.Collections.Concurrent.ConcurrentBag<Models.ProcessRecord> ProcessRecords
- // // = new System.Collections.Concurrent.ConcurrentBag<Models.ProcessRecord>();
- //}
- }
|