From f7a39845b18ec85d69f05db5615cb821a6ab3ec7 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 4 Sep 2024 14:27:42 +0800 Subject: [PATCH] =?UTF-8?q?add=20-=20=E7=89=A9=E6=96=99=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=8C?= =?UTF-8?q?=E5=BE=85=E6=B5=8B=E8=AF=95=EF=BC=8C=E9=85=8D=E6=96=B9=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=BE=85=E5=AE=8C=E5=96=84=E6=8E=A5=E5=8F=A3=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Admin.Core.Api/Admin.Core.Api.xml | 21 ++ .../Business/MaterialController.cs | 2 +- Admin.Core.Api/Log4net.config | 14 +- .../PLTBusiness/Entity/MaterialData.cs | 4 + Admin.Core.Api/PLTBusiness/HttpService.cs | 200 +++++++++++++++--- Admin.Core.Api/PLTBusiness/XlBusiness.cs | 57 +++-- .../IService_New/Ixl_materialServices.cs | 30 +++ Admin.Core.PlcServer/Impl/SiemensPlc.cs | 2 +- .../Service_New/xl_materialServices.cs | 27 ++- 9 files changed, 303 insertions(+), 54 deletions(-) diff --git a/Admin.Core.Api/Admin.Core.Api.xml b/Admin.Core.Api/Admin.Core.Api.xml index 806a89e..5a5eb24 100644 --- a/Admin.Core.Api/Admin.Core.Api.xml +++ b/Admin.Core.Api/Admin.Core.Api.xml @@ -1753,6 +1753,11 @@ 起始页编号 + + + 物料id + + 物料名称 @@ -1798,6 +1803,22 @@ Token失效时间 + + + 分页拉取所有Mes物料信息 + + + + + + 获取Token + + + + + 刷新Token + + 发起POST异步请求 diff --git a/Admin.Core.Api/Controllers/Business/MaterialController.cs b/Admin.Core.Api/Controllers/Business/MaterialController.cs index eb5401c..c788cf4 100644 --- a/Admin.Core.Api/Controllers/Business/MaterialController.cs +++ b/Admin.Core.Api/Controllers/Business/MaterialController.cs @@ -250,7 +250,7 @@ namespace Admin.Core.Api { try { - var result = await _xl_materialService.xlInsertMaterial(materialCode, materialName, isEnable, createDateTime); + var result = await _xl_materialService.xlInsertMaterial(Guid.NewGuid().ToString("N"), materialCode, materialName, isEnable, createDateTime); if (result) { return Success(result); diff --git a/Admin.Core.Api/Log4net.config b/Admin.Core.Api/Log4net.config index d7e7064..627f877 100644 --- a/Admin.Core.Api/Log4net.config +++ b/Admin.Core.Api/Log4net.config @@ -6,7 +6,7 @@ - + @@ -26,8 +26,6 @@ - - @@ -52,6 +50,15 @@ + + + + + + + + + @@ -61,5 +68,6 @@ + \ No newline at end of file diff --git a/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs index 17cd9e6..05ffac0 100644 --- a/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs +++ b/Admin.Core.Api/PLTBusiness/Entity/MaterialData.cs @@ -41,6 +41,10 @@ namespace Admin.Core.Api.PLTBusiness.Entity public class MaterialRecordListItem { + /// + /// 物料id + /// + public int id { get; set; } /// /// 物料名称 /// diff --git a/Admin.Core.Api/PLTBusiness/HttpService.cs b/Admin.Core.Api/PLTBusiness/HttpService.cs index 271f77e..9016d90 100644 --- a/Admin.Core.Api/PLTBusiness/HttpService.cs +++ b/Admin.Core.Api/PLTBusiness/HttpService.cs @@ -1,8 +1,12 @@ using Admin.Core.Api.PLTBusiness.Entity; +using Admin.Core.Common; using Admin.Core.IService; +using Admin.Core.PlcServer; using Admin.Core.Service; +using log4net; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using NPOI.HSSF.Record; using RabbitMQ.Client; using System; using System.Collections.Generic; @@ -19,22 +23,17 @@ namespace Admin.Core.Api.PLTBusiness public class HttpService : BackgroundService { - private readonly IServiceScopeFactory _scopeFactory; + private readonly log4net.ILog log = LogManager.GetLogger(typeof(HttpService)); - private string _token; + private readonly IServiceScopeFactory _scopeFactory; - private int _tokenExpirationTime; + private readonly IHttpClientFactory _httpClientFactory; - private Dictionary _tokenDictionary = new Dictionary() - { - {"authCode", "QT_XXX"}, - {"appid", "q7cloud_XXX"}, - {"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"} - }; + private readonly string _host; - private string _host; + private string _token; - private readonly IHttpClientFactory _httpClientFactory; + private int _tokenExpirationTime = 0; public HttpService(IHttpClientFactory httpClientFactory, IServiceScopeFactory scopeFactory) { @@ -44,25 +43,128 @@ namespace Admin.Core.Api.PLTBusiness protected async override Task ExecuteAsync(CancellationToken stoppingToken) { + int second = 1000 * 10; using var scope = _scopeFactory.CreateScope(); var services = scope.ServiceProvider; var xlBusiness = services.GetService(); - while (!stoppingToken.IsCancellationRequested) { //执行任务 - Console.WriteLine($"{DateTime.Now}"); + //log.Info($"{DateTime.Now}"); + + if (_tokenExpirationTime == 0) + { + if (await GetToken()) + { + log.Info("Token Get! Value:" + _token); + } + else + { + log.Error("Token Get Falied"); + } + + } + else if (_tokenExpirationTime <= 86400) + { + RefreshToken(); + } + else + { + _tokenExpirationTime = _tokenExpirationTime - (second / 1000); + } - await xlBusiness.InsertNewMaterialData(null); + try + { + if (_token.IsNotEmptyOrNull()) + { + await xlBusiness.InsertNewMaterialData(await GetMaterialDataPageList()); + } + else + { + log.Error("No Token!!!"); + } + } + catch (Exception ex) + { + log.Error("Error: " + ex); + } + + await Task.Delay(second); + } - await Task.Delay(1000*10); + } + + #region Mes信息拉取 + /// + /// 分页拉取所有Mes物料信息 + /// + /// + private async Task> GetMaterialDataPageList() + { + int pageSize = 200; + int pageIndex = 0; + int pageCount = 1; + string orgCode = "Mesnac"; + List allMaterials = new List(); + Dictionary _pageInfo = new Dictionary() + { + {"pageIndex", 200}, + {"pageSize", 0}, + {"orgCode", "Mesnac"}, + }; + try + { + do + { + string body = JsonSerializer.Serialize(_pageInfo); + string materialJson = await PostAsync(_host + "/qtn/api/q7/com/inventory/page", + body, "application/json", "application/json", + new Dictionary { { "token", _token } }, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(materialJson); + if (responseRoot == null) + { + log.Error("物料信息拉取失败!"); + return null; + } + if (responseRoot.code != 200) + { + log.Error("ERROR 状态码:" + responseRoot.code); + return null; + } + pageCount = responseRoot.Data.pageCount; + pageIndex = responseRoot.Data.pageIndex; + allMaterials.AddRange(responseRoot.Data.recordList); + pageIndex = pageIndex + 1; + } + while (pageIndex < pageSize); } + catch (Exception ex) + { + log.Error("Mes物料信息拉取失败 Error: " + ex); + return null; + } + + return allMaterials; } - private async void GetToken() + #endregion + + #region Token获取 + + /// + /// 获取Token + /// + private async Task GetToken() { + Dictionary _tokenDictionary = new Dictionary() + { + {"authCode", "QT_XXX"}, + {"appid", "q7cloud_XXX"}, + {"secret", "f434eab8b9652524dc719e33f4ff255a45570XXX"} + }; + try { string body = JsonSerializer.Serialize(_tokenDictionary); @@ -70,22 +172,63 @@ namespace Admin.Core.Api.PLTBusiness ResponseRoot responseRoot = JsonSerializer.Deserialize>(tokenJson); if (responseRoot == null) { - Console.WriteLine("Token获取失败!"); - return; + log.Error("Token获取失败!"); + return false; + } + if (responseRoot.code != 200) + { + log.Error("ERROR 状态码:" + responseRoot.code); + return false; + } + _token = responseRoot.Data.token; + _tokenExpirationTime = responseRoot.Data.expiration; + log.Info("Token获取成功:" + _token + "超时时间:" + _tokenExpirationTime); + return true; + } + catch (Exception ex) + { + log.Error("Token获取失败 ERROR: " + ex.ToString()); + return false; + } + } + + /// + /// 刷新Token + /// + private async Task RefreshToken() + { + try + { + string tokenJson = await PostAsync(_host + "/qtn/api/auth/token/refresh", "" + , "application/json", "application/x-www-form-urlencoded" + , new Dictionary() { { "token", _token } }, 10); + ResponseRoot responseRoot = JsonSerializer.Deserialize>(tokenJson); + if (responseRoot == null) + { + log.Error("Token更新失败!"); + return false; } - if(responseRoot.code != 200) + if (responseRoot.code != 200) { - Console.WriteLine("ERROR 状态码:" + responseRoot.code); - return; + log.Error("ERROR 状态码:" + responseRoot.code); + return false; } _token = responseRoot.Data.token; _tokenExpirationTime = responseRoot.Data.expiration; + log.Info("Token更新成功:" + _token + "超时时间:" + _tokenExpirationTime); + return true; } catch (Exception ex) { - Console.WriteLine("ERROR: " + ex.ToString()); + log.Error("Token更新失败 ERROR: " + ex.ToString()); + return false; } } + + #endregion + + #region HTTP操作 + /// /// 发起POST异步请求 /// @@ -136,8 +279,8 @@ namespace Admin.Core.Api.PLTBusiness } catch (Exception ex) { - Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); - return string.Empty; + log.Error($"Post请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}"); + throw; } } @@ -182,8 +325,8 @@ namespace Admin.Core.Api.PLTBusiness } catch (Exception ex) { - Console.WriteLine($"ERROR: {ex.ToString()} Time: {DateTime.Now}"); - return string.Empty; + log.Error($"Get请求失败 URL:{url} ERROR: {ex.ToString()} Time: {DateTime.Now}"); + throw; } } @@ -217,14 +360,15 @@ namespace Admin.Core.Api.PLTBusiness { // 读取响应内容 var responseBody = await response.Content.ReadAsStringAsync(); - Console.WriteLine($"Response: {responseBody}"); + log.Info($"Response: {responseBody}"); } else { - Console.WriteLine($"Error: {response.StatusCode}"); + log.Error($"Error: {response.StatusCode}"); } } + #endregion #region 私有函数 diff --git a/Admin.Core.Api/PLTBusiness/XlBusiness.cs b/Admin.Core.Api/PLTBusiness/XlBusiness.cs index 95ec0f8..67ef605 100644 --- a/Admin.Core.Api/PLTBusiness/XlBusiness.cs +++ b/Admin.Core.Api/PLTBusiness/XlBusiness.cs @@ -26,26 +26,51 @@ namespace Admin.Core.Api.PLTBusiness _xl_materialService = xl_materialService; } + /// + /// 向物料数据库中动态同步mes数据,没有就插入,不同就修改 + /// + /// + /// public async Task InsertNewMaterialData(List list) { - List mlist = new List(); - mlist = await _xl_materialService.xlMaterialList(); - bool state = await _xl_materialService.xlUpdateMaterial("4cbdaa3913a54092bff72e09f13e633f" - , "123412421567" - , "更新213214测试测试" - , 1 - , DateTime.Now); - //foreach (var mesItem in list) - //{ - // foreach(var mItem in mlist) - // { + try + { + foreach (var mesMaterial in list) + { + // Step 3: 在B表中查询是否存在相同ID的记录 + var material = await _xl_materialService.SelectXlMaterialById(mesMaterial.id.ToString()); - // } - //} - - return true; + if (material == null) + { + // Step 4: 如果B表中不存在该物料,插入新物料 + await _xl_materialService.xlInsertMaterial( + mesMaterial.id.ToString(), + mesMaterial.code, mesMaterial.name, mesMaterial.status, + Convert.ToDateTime(mesMaterial.createTime)); + } + else + { + // Step 5: 如果B表中存在该物料且有更新,更新物料信息 + if (material.Material_code != mesMaterial.code || + material.Material_name != mesMaterial.name || + material.IsEnable != (mesMaterial.status == 1 ? "是" : "否") || + material.CreateDateTime != Convert.ToDateTime(mesMaterial.createTime)) + { + await _xl_materialService.xlUpdateMaterial( + mesMaterial.id.ToString(), + mesMaterial.code, mesMaterial.name, mesMaterial.status, + Convert.ToDateTime(mesMaterial.createTime)); + } + } + } + return true; + } + catch (Exception ex) + { + throw; + } } - + } } diff --git a/Admin.Core.IService/IService_New/Ixl_materialServices.cs b/Admin.Core.IService/IService_New/Ixl_materialServices.cs index dae081c..c559437 100644 --- a/Admin.Core.IService/IService_New/Ixl_materialServices.cs +++ b/Admin.Core.IService/IService_New/Ixl_materialServices.cs @@ -32,12 +32,31 @@ namespace Admin.Core.IService Task> MaterialVerification(string barCode); + /// + /// 物料插入 + /// + /// + /// + /// + /// + /// + /// Task xlInsertMaterial( + string id, string materialCode, string materialName, int isEnable, DateTime createDatetime); + /// + /// 物料更新 + /// + /// + /// + /// + /// + /// + /// Task xlUpdateMaterial( string id, string materialCode, @@ -45,6 +64,17 @@ namespace Admin.Core.IService int isEnable, DateTime createDatetime); + /// + /// 物料查询 + /// + /// Task> xlMaterialList(); + + /// + /// 根据ID查询 + /// + /// + /// + Task SelectXlMaterialById(string id); } } \ No newline at end of file diff --git a/Admin.Core.PlcServer/Impl/SiemensPlc.cs b/Admin.Core.PlcServer/Impl/SiemensPlc.cs index 393ce05..042b6bf 100644 --- a/Admin.Core.PlcServer/Impl/SiemensPlc.cs +++ b/Admin.Core.PlcServer/Impl/SiemensPlc.cs @@ -507,7 +507,7 @@ namespace Admin.Core.PlcServer { siemensS7Net.ReceiveTimeOut = 2000; OperateResult read = await siemensS7Net.ReadBoolAsync(address); - log.Info($"心跳方法是否成功:{read.IsSuccess};反馈:{read.Message}"); + //log.Info($"心跳方法是否成功:{read.IsSuccess};反馈:{read.Message}"); if (read.IsSuccess) { IsConnected = true; diff --git a/Admin.Core.Service/Service_New/xl_materialServices.cs b/Admin.Core.Service/Service_New/xl_materialServices.cs index 40b3bbc..ec54597 100644 --- a/Admin.Core.Service/Service_New/xl_materialServices.cs +++ b/Admin.Core.Service/Service_New/xl_materialServices.cs @@ -695,6 +695,7 @@ namespace Admin.Core.Service /// /// public async Task xlInsertMaterial( + string id, string materialCode, string materialName, int isEnable, @@ -704,7 +705,7 @@ namespace Admin.Core.Service { xl_material xl_m = new xl_material() { - ID = Guid.NewGuid().ToString("N"), + ID = id, Material_code = materialCode, Material_name = materialName, CreateDateTime = createDatetime, @@ -728,7 +729,7 @@ namespace Admin.Core.Service } catch (Exception ex) { - return false; + throw; } } @@ -768,7 +769,7 @@ namespace Admin.Core.Service } catch (Exception e) { - return false; + throw; } } @@ -789,10 +790,26 @@ namespace Admin.Core.Service } catch (Exception ex) { - return null; + throw; } } - + /// + /// 根据ID查询物料信息 + /// + /// + /// + /// + public async Task SelectXlMaterialById(string id) + { + try + { + return await _xl_materialRepository.QueryByIdAsync(id); + } + catch (Exception ex) + { + throw; + } + } } } \ No newline at end of file