diff --git a/DOAN.Admin.WebApi/Controllers/PBL/MESInteractionController.cs b/DOAN.Admin.WebApi/Controllers/PBL/MESInteractionController.cs index 948bfba..1135608 100644 --- a/DOAN.Admin.WebApi/Controllers/PBL/MESInteractionController.cs +++ b/DOAN.Admin.WebApi/Controllers/PBL/MESInteractionController.cs @@ -4,6 +4,8 @@ using DOAN.Model.PBL; using DOAN.Service.PBL.IService; using DOAN.Admin.WebApi.Filters; using DOAN.ServiceCore.Middleware; +using DOAN.Infrastructure.PLC; +using System.Collections.Generic; //创建时间:2024-09-23 namespace DOAN.Admin.WebApi.Controllers.PBL @@ -21,30 +23,51 @@ namespace DOAN.Admin.WebApi.Controllers.PBL this.mesInteraction = mesInteraction; } + //TODO 测试传感器地址 + [HttpGet("test_plc")] + [DoanPlcActionFilter] + public IActionResult TestPLc(string address, PLCTool pLCTool) + { + var response = mesInteraction.TestPLc(address, pLCTool); + return SUCCESS(response); + } + + //TODO 接受工单 亮灯 [HttpPost("mes_light_up")] [DoanPlcActionFilter] - public IActionResult MESLightUp([FromBody] LightUpDto light) + public IActionResult MESLightUp([FromBody] LightUpDto light, PLCTool pLCTool) { - var response= mesInteraction.MESLightUp(light); + try + { + var response = mesInteraction.MESLightUp(light, pLCTool); + return SUCCESS(response); + } + catch (Exception e) + { + return ToResponse(500, e.Message); + } - return SUCCESS(response); } //TODO 扫码灭灯 [HttpGet("mes_light_down")] [DoanPlcActionFilter] - public IActionResult MESLightDown(string scan_code) + public IActionResult MESLightDown(string scan_code, PLCTool pLCTool) { - var response = mesInteraction.MESLightDown(scan_code); + try + { + var response = mesInteraction.MESLightDown(scan_code, pLCTool); - return SUCCESS(response); + return SUCCESS(response); + + } + catch (Exception e) + { + return ToResponse(500, e.Message); + } } - - - - } } diff --git a/DOAN.Admin.WebApi/DOAN.Admin.WebApi.csproj b/DOAN.Admin.WebApi/DOAN.Admin.WebApi.csproj index f5d8e8e..5d1d6b0 100644 --- a/DOAN.Admin.WebApi/DOAN.Admin.WebApi.csproj +++ b/DOAN.Admin.WebApi/DOAN.Admin.WebApi.csproj @@ -10,6 +10,9 @@ 1701;1702;1591,8603,8602,8604,8600,8618 + + + diff --git a/DOAN.Admin.WebApi/appsettings.Development.json b/DOAN.Admin.WebApi/appsettings.Development.json index 6cf3328..170fe71 100644 --- a/DOAN.Admin.WebApi/appsettings.Development.json +++ b/DOAN.Admin.WebApi/appsettings.Development.json @@ -11,7 +11,7 @@ { - "Conn": "Data Source=139.224.232.211;User ID=root;Password=doantech123;Initial Catalog=pbl_huaxiang;Port=3308", + "Conn": "Data Source=10.72.80.161;User ID=root;Password=doantech123;Initial Catalog=pbl_huaxiang;Port=3306", "DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4 "ConfigId": "0", //多租户唯一标识 "IsAutoCloseConnection": true @@ -32,12 +32,12 @@ "Issuer": "DOANAdmin.NET", //即token的签发者。 "Audience": "DOANAdmin.NET", //指该token是服务于哪个群体的(群体范围) "SecretKey": "SecretKey-DOANADMIN.NET-202311281883838", - "Expire": 30, //jwt登录过期时间(分) + "Expire": 600000, //jwt登录过期时间(分) "RefreshTokenTime": 30, //分钟 "TokenType": "Bearer" }, "InjectClass": [ "DOAN.Repository", "DOAN.Service", "DOAN.Tasks", "DOAN.ServiceCore" ], //自动注入类 - "ShowDbLog": true, //是否打印db日志 + "ShowDbLog": false, //是否打印db日志 "InitDb": false, //是否初始化db "DemoMode": false, //是否演示模式 "SingleLogin": false, //是否允许多设备/浏览器登录 diff --git a/DOAN.Admin.WebApi/appsettings.Production.json b/DOAN.Admin.WebApi/appsettings.Production.json index b433de2..8334284 100644 --- a/DOAN.Admin.WebApi/appsettings.Production.json +++ b/DOAN.Admin.WebApi/appsettings.Production.json @@ -6,9 +6,12 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "dbConfigs": [ { - "Conn": "Data Source=127.0.0.1;User ID=root;Password=123456;Initial Catalog=pbl_huaxiang;", + + + "Conn": "Data Source=10.72.80.161;User ID=root;Password=doantech123;Initial Catalog=pbl_huaxiang;Port=3306", "DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4 "ConfigId": "0", //多租户唯一标识 "IsAutoCloseConnection": true @@ -18,23 +21,23 @@ //代码生成数据库配置 "CodeGenDbConfig": { //代码生成连接字符串,注意{dbName}为固定格式,不要填写数据库名 - "Conn": "Data Source=127.0.0.1;User ID=admin;Password=admin123;Initial Catalog={dbName};", + "Conn": "Data Source=192.168.0.58;User ID=root;Password=123456;Initial Catalog={dbName};", "DbType": 0, "IsAutoCloseConnection": true, "DbName": "pbl_huaxiang" //代码生成默认连接数据库,Oracle库是实例的名称 }, - "urls": "http://127.0.0.1:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改 + "urls": "http://0.0.0.0:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改 "corsUrls": [ "http://localhost:8887", "http://localhost:8886" ], //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开 "JwtSettings": { "Issuer": "DOANAdmin.NET", //即token的签发者。 "Audience": "DOANAdmin.NET", //指该token是服务于哪个群体的(群体范围) "SecretKey": "SecretKey-DOANADMIN.NET-202311281883838", - "Expire": 1440, //jwt登录过期时间(分) + "Expire": 600000, //jwt登录过期时间(分) "RefreshTokenTime": 30, //分钟 "TokenType": "Bearer" }, "InjectClass": [ "DOAN.Repository", "DOAN.Service", "DOAN.Tasks", "DOAN.ServiceCore" ], //自动注入类 - "ShowDbLog": true, //是否打印db日志 + "ShowDbLog": false, //是否打印db日志 "InitDb": false, //是否初始化db "DemoMode": false, //是否演示模式 "SingleLogin": false, //是否允许多设备/浏览器登录 @@ -104,7 +107,7 @@ //自动去除表前缀 "autoPre": true, //默认生成业务模块名 - "moduleName": "business", + "moduleName": "PBL", "author": "admin", "tablePrefix": "sys_", //"表前缀(生成类名不会包含表前缀,多个用逗号分隔)", "vuePath": "", //前端代码存储路径eg:D:\Work\DOANAdmin-Vue3 diff --git a/DOAN.Model/PBL/Dto/BillofmaterialsDto.cs b/DOAN.Model/PBL/Dto/BillofmaterialsDto.cs index 5fc0679..c11b5ce 100644 --- a/DOAN.Model/PBL/Dto/BillofmaterialsDto.cs +++ b/DOAN.Model/PBL/Dto/BillofmaterialsDto.cs @@ -39,6 +39,8 @@ namespace DOAN.Model.PBL.Dto public string MirrorbodyCode { get; set; } + public string Version { get; set; } + public string CreatedBy { get; set; } public DateTime? CreatedTime { get; set; } diff --git a/DOAN.Model/PBL/Light_Log.cs b/DOAN.Model/PBL/Light_Log.cs index afafda5..6ac7bfc 100644 --- a/DOAN.Model/PBL/Light_Log.cs +++ b/DOAN.Model/PBL/Light_Log.cs @@ -31,7 +31,13 @@ namespace DOAN.Model.PBL [SugarColumn(ColumnName = "shelf_code")] public string ShelfCode { get; set; } + /// + /// 层号 + /// + [SugarColumn(ColumnName = "layer_num")] + public int LayerNum { get; set; } + /// /// 操作者 /// diff --git a/DOAN.Service/PBL/IService/IMESInteractionServcie.cs b/DOAN.Service/PBL/IService/IMESInteractionServcie.cs index 7258fb6..147e39f 100644 --- a/DOAN.Service/PBL/IService/IMESInteractionServcie.cs +++ b/DOAN.Service/PBL/IService/IMESInteractionServcie.cs @@ -1,4 +1,5 @@ -using DOAN.Model.PBL.Dto; +using DOAN.Infrastructure.PLC; +using DOAN.Model.PBL.Dto; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; @@ -10,8 +11,9 @@ namespace DOAN.Service.PBL.IService { public interface IMESInteractionServcie { - - bool MESLightUp(LightUpDto light); - bool MESLightDown(string scan_code); + + bool TestPLc(string address, PLCTool pLCTool); + bool MESLightUp(LightUpDto light,PLCTool pLCTool); + bool MESLightDown(string scan_code,PLCTool pLCTool); } } diff --git a/DOAN.Service/PBL/InventorylogService.cs b/DOAN.Service/PBL/InventorylogService.cs index 2a5619b..97fa258 100644 --- a/DOAN.Service/PBL/InventorylogService.cs +++ b/DOAN.Service/PBL/InventorylogService.cs @@ -25,6 +25,7 @@ namespace DOAN.Service.PBL var response = Queryable() .Where(predicate.ToExpression()) + .OrderByDescending(it => it.CreatedTime) .ToPage(parm); return response; diff --git a/DOAN.Service/PBL/LightLogService.cs b/DOAN.Service/PBL/LightLogService.cs index 22903bf..12c0448 100644 --- a/DOAN.Service/PBL/LightLogService.cs +++ b/DOAN.Service/PBL/LightLogService.cs @@ -24,6 +24,7 @@ namespace DOAN.Service.PBL var response = Queryable() .Where(predicate.ToExpression()) + .OrderByDescending(it=>it.CreatedTime) .ToPage(parm); return response; @@ -72,8 +73,8 @@ namespace DOAN.Service.PBL private static Expressionable QueryExp(LightLogQueryDto parm) { var predicate = Expressionable.Create() - .AndIF(parm.TimeRange[0]>DateTime.MinValue, it => it.CreatedTime >=parm.TimeRange[0]) - .AndIF(parm.TimeRange[1]>DateTime.MinValue, it => it.CreatedTime <=parm.TimeRange[1]) + .AndIF(parm.TimeRange[0]>DateTime.MinValue, it => it.CreatedTime >= parm.TimeRange[0]) + .AndIF(parm.TimeRange[1]>DateTime.MinValue, it => it.CreatedTime <= parm.TimeRange[1].Value.Add(new TimeSpan(23, 59, 59))) ; return predicate; diff --git a/DOAN.Service/PBL/MESInteractionServcie.cs b/DOAN.Service/PBL/MESInteractionServcie.cs index a46a057..ac4dc26 100644 --- a/DOAN.Service/PBL/MESInteractionServcie.cs +++ b/DOAN.Service/PBL/MESInteractionServcie.cs @@ -1,14 +1,10 @@ -using Infrastructure.Attribute; -using Infrastructure.Extensions; -using DOAN.Model.PBL.Dto; +using DOAN.Infrastructure.PLC; using DOAN.Model.PBL; -using DOAN.Repository; +using DOAN.Model.PBL.Dto; using DOAN.Service.PBL.IService; +using Infrastructure.Attribute; using Mapster; -using System.Runtime.InteropServices; -using SqlSugar; -using System.Security.Cryptography.X509Certificates; -using DOAN.Infrastructure.PLC; +using Newtonsoft.Json.Linq; namespace DOAN.Service.PBL @@ -19,10 +15,22 @@ namespace DOAN.Service.PBL [AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)] public class MESInteractionServcie : BaseService, IMESInteractionServcie { - - - public bool MESLightUp(LightUpDto light) + public bool TestPLc(string address, PLCTool pLCTool) { + bool isSucesss = pLCTool.ReadBit(address); + return isSucesss; + + } + + /// + /// MES亮灯 + /// + /// + /// + /// + public bool MESLightUp(LightUpDto light, PLCTool pLCTool) + { + int result = 0; // 1.记录MES交互记录 MES_Interation_Log item = light.Adapt(); item.Id = XUEHUA; @@ -31,70 +39,80 @@ namespace DOAN.Service.PBL // 2.根据总成零件号 ,版本 查询对应零件号,使得对应料架亮灯 + // 同一个会有多个料架 //镜壳 料架 - Storagelocation MirrorshellShelf = Context.Queryable().Where(it => it.Partnumber == - SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorshellCode)).First(); + List MirrorshellShelfList = Context.Queryable().Where(it => it.Partnumber == + SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorshellCode)).ToList(); //镜体 料架 - Storagelocation MirrorshellBody = Context.Queryable().Where(it => it.Partnumber == - SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorbodyCode)).First(); - - // 3.对应料架亮灯 - - //TODO PLC 交互 - // 对应料架 亮灯字段 - // 通知PLC - bool isSucesss = PLCTool.WriteBit(MirrorshellShelf.PlcAddress, true); - if (isSucesss) + // Storagelocation MirrorshellBody = Context.Queryable().Where(it => it.Partnumber == + // SqlFunc.Subqueryable().Where(It => It.Productcode == light.AssemblyPartNumber && It.Version == light.Version).Select(it => it.MirrorbodyCode)).First(); + if (MirrorshellShelfList != null || MirrorshellShelfList.Count() > 0) { - MirrorshellShelf.IsLight = 1; - MirrorshellBody.IsLight = 1; - Context.Insertable(MirrorshellShelf).ExecuteCommand(); - Context.Insertable(MirrorshellBody).ExecuteCommand(); - } - - - //亮灯日志 - Light_Log light_Log = new Light_Log(); - light_Log.Id = XUEHUA; - light_Log.LightOperation = 1; - if (MirrorshellShelf != null) - { - light_Log.ShelfCode = MirrorshellShelf.RackCode; + + foreach (var item1 in MirrorshellShelfList) + { + // 3.对应料架亮灯 + + //TODO PLC 交互 + // 对应料架 亮灯字段 + // 通知PLC + bool isSucesss = pLCTool.WriteBit(item1.PlcAddress, true); + if (isSucesss) + { + item1.IsLight = 1; + + Context.Updateable(item1).ExecuteCommand(); + + } + + + //亮灯日志 + Light_Log light_Log = new Light_Log(); + light_Log.Id = XUEHUA; + light_Log.LightOperation = 1; + light_Log.ShelfCode = item1.RackCode; + light_Log.LayerNum = item1.LayerNum; + light_Log.Operationer = "PBL"; + light_Log.CreatedTime = DateTime.Now; + light_Log.IsSuccess = isSucesss; + + + //Light_Log light_Log2 = new Light_Log(); + //light_Log2.Id = XUEHUA; + //light_Log2.LightOperation = 1; + + //light_Log2.ShelfCode = item1.RackCode; + + + + //light_Log2.Operationer = "PBL"; + //light_Log2.CreatedTime = DateTime.Now; + //light_Log2.IsSuccess = isSucesss; + //Context.Insertable(light_Log2).ExecuteCommand(); + result += Context.Insertable(light_Log).ExecuteCommand(); + + } } - - light_Log.Operationer = "PBL"; - light_Log.CreatedTime = DateTime.Now; - light_Log.IsSuccess = isSucesss; - - - Light_Log light_Log2 = new Light_Log(); - light_Log2.Id = XUEHUA; - light_Log2.LightOperation = 1; - if (MirrorshellShelf != null) + else { - light_Log2.ShelfCode = MirrorshellShelf.RackCode; - + return false; } - light_Log2.Operationer = "PBL"; - light_Log2.CreatedTime = DateTime.Now; - light_Log2.IsSuccess = isSucesss; - Context.Insertable(light_Log2).ExecuteCommand(); - int result = Context.Insertable(light_Log).ExecuteCommand(); return result > 0; } /// - /// 灭灯 + /// MES灭灯 /// /// /// - public bool MESLightDown(string scan_code) + public bool MESLightDown(string scan_code, PLCTool pLCTool) { + int result = 0; // 1.记录MES交互记录 MES_Interation_Log item = new MES_Interation_Log(); item.Id = XUEHUA; @@ -102,35 +120,57 @@ namespace DOAN.Service.PBL item.CreatedTime = DateTime.Now; Context.Insertable(item).ExecuteCommand(); - //2 找到对应的料架 灭灯 - Storagelocation storagelocation = Context.Queryable().Where(it => it.Partnumber == scan_code).First(); - - //TODO PLC 交互 - bool isSuccess= PLCTool.WriteBit(storagelocation.PlcAddress, false); - - if (isSuccess) - { - storagelocation.IsLight = 0; - storagelocation.PackageNum =storagelocation.PackageNum - 1; - //3 扣减对应的库存 - Context.Insertable(storagelocation).ExecuteCommand(); - - } - //灭灯日志 - Light_Log light_Log = new Light_Log(); - light_Log.Id = XUEHUA; - light_Log.LightOperation = 2; + //2 找到对应的库存最大料架 灭灯 + Storagelocation storagelocation = Context.Queryable().Where(it => it.Partnumber == scan_code).OrderByDescending(it => it.PackageNum).First(); if (storagelocation != null) { + + // TODO PLC 交互 + bool isSuccess = pLCTool.WriteBit(storagelocation.PlcAddress, false); + + if (isSuccess) + { + storagelocation.IsLight = 0; + storagelocation.PackageNum -= 1; + if (storagelocation.PackageNum < 0) + { + storagelocation.PackageNum = 0; + } + + //3 扣减对应的库存 + // 合并货架的id + int[] ids = { 1, 2, 3, 4, 19, 20, 21, 22 }; + if (ids.Contains(storagelocation.Id)) + { + Context.Updateable().SetColumns(it=>it.IsLight == 0).Where(it=>it.Partnumber == storagelocation.Partnumber).ExecuteCommand(); + } + else + { + Context.Updateable(storagelocation).ExecuteCommand(); + } + + + } + //灭灯日志 + Light_Log light_Log = new Light_Log(); + light_Log.Id = XUEHUA; + light_Log.LightOperation = 2; + light_Log.LayerNum = 1; light_Log.ShelfCode = storagelocation.RackCode; + + light_Log.IsSuccess = isSuccess; + light_Log.Operationer = "PBL"; + light_Log.CreatedTime = DateTime.Now; + result += Context.Insertable(light_Log).ExecuteCommand(); + } + else + { + return false; } - light_Log.IsSuccess = isSuccess; - light_Log.Operationer = "PBL"; - light_Log.CreatedTime = DateTime.Now; - int result = Context.Insertable(light_Log).ExecuteCommand(); return result > 0; } - } + + } \ No newline at end of file diff --git a/DOAN.Service/PBL/MesInterationLogService.cs b/DOAN.Service/PBL/MesInterationLogService.cs index e5286b6..6886347 100644 --- a/DOAN.Service/PBL/MesInterationLogService.cs +++ b/DOAN.Service/PBL/MesInterationLogService.cs @@ -25,6 +25,7 @@ namespace DOAN.Service.PBL var response = Queryable() .Where(predicate.ToExpression()) + .OrderByDescending(it => it.CreatedTime) .ToPage(parm); return response; @@ -75,7 +76,7 @@ namespace DOAN.Service.PBL var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.Workorder),it=>it.Workorder.Contains(parm.Workorder)) .AndIF(parm.TimeRange[0]>DateTime.MinValue, it => it.CreatedTime >=parm.TimeRange[0]) - .AndIF(parm.TimeRange[1]>DateTime.MinValue, it => it.CreatedTime <=parm.TimeRange[1]) + .AndIF(parm.TimeRange[1]>DateTime.MinValue, it => it.CreatedTime <=parm.TimeRange[1].Value.Add(new TimeSpan(23, 59, 59))) ; return predicate; diff --git a/DOAN.ServiceCore/DoanBackgroundService.cs b/DOAN.ServiceCore/DoanBackgroundService.cs index 6334b1f..884d963 100644 --- a/DOAN.ServiceCore/DoanBackgroundService.cs +++ b/DOAN.ServiceCore/DoanBackgroundService.cs @@ -1,4 +1,5 @@ using System; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using DOAN.Infrastructure.PLC; @@ -14,10 +15,13 @@ namespace DOAN.ServiceCore { private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); private Task _executingTask; + private PLCTool pLCTool; public Task StartAsync(CancellationToken cancellationToken) { - PLCTool.ConnectPLC(); + pLCTool = new PLCTool(); + + pLCTool.ConnectPLC(); // 当服务开始时,启动后台任务 _executingTask = ExecuteAsync(_cancellationTokenSource.Token); @@ -37,79 +41,113 @@ namespace DOAN.ServiceCore private async Task ExecuteAsync(CancellationToken stoppingToken) { - // 获取所有传感器 地址 + try { + int index = 1; + int indexMax = await DbScoped.SugarScope.CopyNew() + .Queryable().CountAsync(); - int index = 1; - - - while (!stoppingToken.IsCancellationRequested) - { - Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew() - .Queryable().Where(it => it.Id == index).FirstAsync(); - - string[] address = storagelocation.PlcAddress2.Split(","); - for (int i = 0; i < address.Length; i++) + while (!stoppingToken.IsCancellationRequested) { - bool result = PLCTool.ReadBit(address[i]); - // 写补料日志 - Inventorylog inventorylog = new Inventorylog(); - inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString(); - inventorylog.RackCode = storagelocation.RackCode; - - if (result) + List storagelocationList = await DbScoped.SugarScope.CopyNew() + .Queryable().Where(it => it.Id == index).ToListAsync(); + if (storagelocationList.Count > 0) { - //缺料 - - storagelocation.PackageNum = 2; - inventorylog.Operation = 1; - inventorylog.PackageNum = 2; - inventorylog.CreatedBy = "PLC"; - inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); - await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); - await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); - - } - else - { - if (storagelocation.PackageNum <= 2) + foreach (var storagelocation in storagelocationList) { - //补料成功 - storagelocation.PackageNum = 4; - inventorylog.Operation = 2; - inventorylog.PackageNum = 4; - inventorylog.CreatedBy = "PLC"; - inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); - await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); - await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); + + //遮挡不亮 false + bool result = pLCTool.ReadBit(storagelocation.PlcAddress2); + // 写补料日志 + Inventorylog inventorylog = new Inventorylog(); + inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString(); + inventorylog.RackCode = storagelocation.RackCode; + // 合并货架的id + int[] ids = {1,2,3,4,19,20,21,22 }; + int PackageLine = 2; + //if (ids.Contains(storagelocation.Id)) + //{ + // PackageLine = 1; + //} + if (result) + { + //缺料,需要补料 + // 仓库库存修正 + if (storagelocation.PackageNum >= PackageLine) + { + storagelocation.PackageNum = PackageLine; + inventorylog.Operation = 1; + inventorylog.PackageNum = 1; + inventorylog.CreatedBy = "PLC"; + inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); + await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); + await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); + + + } + } + else + { + // 仓库库存修正 + if (storagelocation.PackageNum <= PackageLine) + { + //不需要补料 补料成功 + storagelocation.PackageNum = storagelocation.MaxCapacity; + inventorylog.Operation = 2; + inventorylog.PackageNum = storagelocation.MaxCapacity; + inventorylog.CreatedBy = "PLC"; + inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); + await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); + await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); + } + + } + + } + + + // Console.WriteLine("永驻线程,正在读取plc值 " + result + "地址:" + address[i]); + } + // await Task.Delay(500, stoppingToken); + + + index++; + if (index > indexMax) + { + index = 1; + } + } - - - - await Task.Delay(5000, stoppingToken); - Console.WriteLine("永驻线程,正在读取plc值 " + result); - - index++; - if (index > 16) - { - index = 1; - } - + } catch (Exception ex) { + Console.WriteLine("DoanBackGround线程ExecuteAsync异常:" + ex.Message); } + } public void Dispose() { - _cancellationTokenSource.Cancel(); - _executingTask.Wait(); - _cancellationTokenSource.Dispose(); + try + { + pLCTool.ConnectClose(); + _cancellationTokenSource.Cancel(); + _executingTask.Wait(); + _cancellationTokenSource.Dispose(); + } + catch (Exception ex) + { + Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message); + } + } + + + } } diff --git a/DOAN.ServiceCore/Filters/DoanPlcActionFilter.cs b/DOAN.ServiceCore/Filters/DoanPlcActionFilter.cs index ddf7c38..99f2cd7 100644 --- a/DOAN.ServiceCore/Filters/DoanPlcActionFilter.cs +++ b/DOAN.ServiceCore/Filters/DoanPlcActionFilter.cs @@ -6,22 +6,18 @@ namespace DOAN.ServiceCore.Middleware { public class DoanPlcActionFilter : ActionFilterAttribute { - ///// - ///// PLC地址 - ///// - //private readonly string addr; - //private readonly bool value; + private PLCTool pLCTool; + public DoanPlcActionFilter() + { + pLCTool = new PLCTool(); + } - //public DoanPlcActionFilter(string addr,string value) - //{ - // addr = addr; - // value = value; - //} public override void OnActionExecuting(ActionExecutingContext context) { //建立plC 连接 - PLCTool.ConnectPLC(); + pLCTool.ConnectPLC(); + context.ActionArguments["pLCTool"] = pLCTool; base.OnActionExecuting(context); @@ -30,7 +26,7 @@ namespace DOAN.ServiceCore.Middleware public override void OnActionExecuted(ActionExecutedContext context) { // 关闭pLc连接 - PLCTool.ConnectClose(); + pLCTool.ConnectClose(); base.OnActionExecuted(context); } diff --git a/Infrastructure/PLC/PLCTool.cs b/Infrastructure/PLC/PLCTool.cs index bb1394f..510fb6d 100644 --- a/Infrastructure/PLC/PLCTool.cs +++ b/Infrastructure/PLC/PLCTool.cs @@ -2,6 +2,7 @@ using HslCommunication.Profinet.Inovance; using HslCommunication.Profinet.Siemens; using Infrastructure; +using JinianNet.JNTemplate; using System; using System.Collections.Generic; using System.Linq; @@ -16,26 +17,40 @@ namespace DOAN.Infrastructure.PLC // private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - public static SiemensS7Net siemensTcpNet = null; - public static bool ConnectPLC() + public SiemensS7Net siemensTcpNet = null; + public PLCTool() { - siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1") + + this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1") { ConnectTimeOut = 5000 - }; - OperateResult connect = siemensTcpNet.ConnectServer(); - if (connect.IsSuccess) + }; + } + + public bool ConnectPLC() + { + try { - // 连接成功 - Console.WriteLine("connect success"); - return true; + OperateResult connect = siemensTcpNet.ConnectServer(); + if (connect.IsSuccess) + { + // 连接成功 + Console.WriteLine("PLC连接成功"); + return true; + } + else + { + // 连接失败,输出原因 + Console.WriteLine("PLC连接失败:" + connect.Message); + throw new CustomException("connect failed:" + connect.Message); + } } - else + catch (Exception e) { - // 连接失败,输出原因 - Console.WriteLine("connect failed:" + connect.Message); + Console.WriteLine("PLC连接失败:" + e.Message); return false; } + @@ -46,7 +61,7 @@ namespace DOAN.Infrastructure.PLC /// /// /// - public static bool WriteBit(string addr, bool value) + public bool WriteBit(string addr, bool value) { OperateResult write = siemensTcpNet.Write(addr, value); @@ -58,7 +73,7 @@ namespace DOAN.Infrastructure.PLC /// /// /// - public static bool ReadBit(string addr) + public bool ReadBit(string addr) { bool M100_7 = siemensTcpNet.ReadBool(addr).Content; @@ -67,7 +82,7 @@ namespace DOAN.Infrastructure.PLC - public static void ConnectClose() + public void ConnectClose() { siemensTcpNet.ConnectClose();