1
0

2 Ревизии 2122279874 ... 8dedccd13d

Автор SHA1 Съобщение Дата
  DFS_Shuo_Chen 8dedccd13d no message преди 2 седмици
  DFS_Shuo_Chen 28cd2dd072 no message преди 2 седмици
променени са 3 файла, в които са добавени 189 реда и са изтрити 54 реда
  1. 1 0
      .gitignore
  2. 56 8
      EasyTemplate.Blazor.Web/Common/TheRegister.cs
  3. 132 46
      EasyTemplate.Page/Pages/EngineSetting/UnifiedSetting.razor

+ 1 - 0
.gitignore

@@ -20,3 +20,4 @@ bin-release/
 /EasyTemplate.Blazor.Web/wwwroot/image
 EasyTemplate.Blazor.Web/publish/
 tmptest/.vs/
+文档/~$重构时间评估.xlsx

+ 56 - 8
EasyTemplate.Blazor.Web/Common/TheRegister.cs

@@ -170,14 +170,14 @@ public static class TheRegister
         //builder.Services.AddSingleton<UdpListenerService>();
         //builder.Services.AddHostedService(sp => sp.GetRequiredService<UdpListenerService>());
 
-        for (int i = 0; i < 40; i++)
-        {
-            NozzleState ns = new NozzleState();
-            ns.noz = i + 1;
-            ns.warnstate = i % 3;
-            ns.oil = i % 2 == 0 ? "92#" : "95#";
-            GlobalTool.g_mNozzleState.Add(ns.noz, ns);
-        }
+        //for (int i = 0; i < 40; i++)
+        //{
+        //    NozzleState ns = new NozzleState();
+        //    ns.noz = i + 1;
+        //    ns.warnstate = i % 3;
+        //    ns.oil = i % 2 == 0 ? "92#" : "95#";
+        //    GlobalTool.g_mNozzleState.Add(ns.noz, ns);
+        //}
 
         builder.Services.AddHostedService<UdpListenerService>();
 
@@ -292,6 +292,9 @@ public static class TheRegister
         using (var scope = app.Services.CreateScope())
         {
             var myService = scope.ServiceProvider.GetRequiredService<Init>();
+            
+            // 初始化数据库中的油枪状态
+            InitializeNozzleStates(scope.ServiceProvider);
         }
 
         app.Run();
@@ -299,6 +302,51 @@ public static class TheRegister
 
         return builder;
     }
+    
+    /// <summary>
+    /// 从数据库查询所有油枪并初始化状态
+    /// </summary>
+    private static void InitializeNozzleStates(IServiceProvider serviceProvider)
+    {
+        try
+        {
+            var nozzleService = serviceProvider.GetRequiredService<NozzleService>();
+            
+            // 异步获取油枪数据
+            var task = Task.Run(async () => await nozzleService.GetNozzlesAsync());
+            var nozzles = task.Result;
+            
+            if (nozzles != null && nozzles.Count > 0)
+            {
+                foreach (var nozzle in nozzles)
+                {
+                    if (!GlobalTool.g_mNozzleState.ContainsKey(nozzle.NozzleId))
+                    {
+                        var nozzleState = new NozzleState
+                        {
+                            noz = nozzle.NozzleId,
+                            VLR = "0",
+                            oil = nozzle.product.ToString()+"#", // 可以根据需要设置默认油品
+                            nozzlestate = GlobalTool.NozzleState_Offline, // 初始状态设为离线
+                            warnstate = GlobalTool.WarningState_Normal // 初始警告状态设为正常
+                        };
+                        
+                        GlobalTool.g_mNozzleState.Add(nozzle.NozzleId, nozzleState);
+                    }
+                }
+                
+                Console.WriteLine($"已初始化 {nozzles.Count} 个油枪状态");
+            }
+            else
+            {
+                Console.WriteLine("数据库中没有找到油枪数据");
+            }
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine($"初始化油枪状态时发生错误:{ex.Message}");
+        }
+    }
 
     private static FileExtensionContentTypeProvider GetFileExtensionContentTypeProvider()
     {

+ 132 - 46
EasyTemplate.Page/Pages/EngineSetting/UnifiedSetting.razor

@@ -16,7 +16,7 @@
                     <i class="fas fa-plus"></i>
                 </button>
             </div>
-            
+
             <!-- 显示所有油机选项 -->
             @* <div class="list-group mb-3">
                 <button class="list-group-item list-group-item-action @(selectedEngineId == 0 ? "active" : "")" 
@@ -24,14 +24,14 @@
                     显示所有油机
                 </button>
             </div> *@
-            
+
             <!-- 油机列表 -->
             <div class="list-group flex-grow-1" style="max-height: 400px; overflow-y: auto; margin-bottom: 50px;">
                 @if (paginatedEngines != null)
                 {
                     @foreach (var engine in paginatedEngines)
                     {
-						<div @onclick="() => SelectEngine(engine.EngineId)" class="list-group-item d-flex justify-content-between align-items-center py-1 @(selectedEngineId == engine.EngineId ? "active" : "")">
+                        <div @onclick="() => SelectEngine(engine.EngineId)" class="list-group-item d-flex justify-content-between align-items-center py-1 @(selectedEngineId == engine.EngineId ? "active" : "")">
                             <div style="cursor: pointer;">
                                 <div><strong>油机 @engine.EngineId</strong></div>
                             </div>
@@ -47,7 +47,7 @@
                     }
                 }
             </div>
-            
+
             <!-- 油机分页控件 -->
             @if (engines != null && engines.Count > pageSize)
             {
@@ -79,8 +79,8 @@
                     <i class="fas fa-plus"></i>
                 </button>
             </div>
-            
-            
+
+
             @* <!-- 显示所有主板选项 -->
             <div class="list-group mb-3">
                 <button class="list-group-item list-group-item-action @(selectedBoardId == 0 ? "active" : "")" 
@@ -88,7 +88,7 @@
                     显示所有主板
                 </button>
             </div> *@
-            
+
             <!-- 主板列表 -->
             <div class="list-group flex-grow-1" style="max-height: 400px; overflow-y: auto; margin-bottom: 50px;">
                 @if (paginatedBoards != null)
@@ -111,7 +111,7 @@
                     }
                 }
             </div>
-            
+
             <!-- 主板分页控件 -->
             @if (filteredBoards != null && filteredBoards.Count > pageSize)
             {
@@ -143,7 +143,7 @@
                     <i class="fas fa-plus"></i>
                 </button>
             </div>
-            
+
             <!-- 油枪列表 -->
             <div class="list-group flex-grow-1" style="margin-bottom: 50px;">
                 @if (paginatedNozzles != null)
@@ -167,7 +167,7 @@
                     }
                 }
             </div>
-            
+
             <!-- 油枪分页控件 -->
             @if (filteredNozzles != null && filteredNozzles.Count > pageSize)
             {
@@ -336,37 +336,37 @@
     private List<TNozzle> allNozzles = new();
     private List<TNozzle> filteredNozzles = new();
     private List<TBoard> boards = new(); // 用于下拉选择
-    
+
     // 分页相关
     private const int pageSize = 8; // 每页显示条数
     private int currentPage_Engine = 1;
     private int totalPages_Engine => engines == null ? 0 : (int)Math.Ceiling((double)engines.Count / pageSize);
     private List<TEngine> paginatedEngines => engines?.Skip((currentPage_Engine - 1) * pageSize).Take(pageSize).ToList() ?? new List<TEngine>();
-    
+
     private int currentPage_Board = 1;
     private int totalPages_Board => filteredBoards == null ? 0 : (int)Math.Ceiling((double)filteredBoards.Count / pageSize);
     private List<TBoard> paginatedBoards => filteredBoards?.Skip((currentPage_Board - 1) * pageSize).Take(pageSize).ToList() ?? new List<TBoard>();
-    
+
     private int currentPage_Nozzle = 1;
     private int totalPages_Nozzle => filteredNozzles == null ? 0 : (int)Math.Ceiling((double)filteredNozzles.Count / pageSize);
     private List<TNozzle> paginatedNozzles => filteredNozzles?.Skip((currentPage_Nozzle - 1) * pageSize).Take(pageSize).ToList() ?? new List<TNozzle>();
-    
+
     // 当前选中项
     private int selectedEngineId = 0;
     private int selectedBoardId = 0;
-    
+
     // 油机相关
     private TEngine currentEngine = new();
     private string engineModalTitle = "";
     private bool isEngineEditMode = false;
     private bool showEngineModal = false;
-    
+
     // 主板相关
     private TBoard currentBoard = new();
     private string boardModalTitle = "";
     private bool isBoardEditMode = false;
     private bool showBoardModal = false;
-    
+
     // 油枪相关
     private TNozzle currentNozzle = new();
     private string nozzleModalTitle = "";
@@ -387,16 +387,16 @@
             allBoards = await nozzleService.GetBoardsAsync();
             allNozzles = await nozzleService.GetNozzlesAsync();
             boards = new List<TBoard>(allBoards); // 用于下拉选择
-            
+
             // 默认显示所有数据
             //filteredBoards = new List<TBoard>(allBoards);
             //filteredNozzles = new List<TNozzle>(allNozzles);
-            
+
             // 重置分页
             currentPage_Engine = 1;
             currentPage_Board = 1;
             currentPage_Nozzle = 1;
-            
+
             StateHasChanged();
         }
         catch (Exception ex)
@@ -414,7 +414,7 @@
             StateHasChanged();
         }
     }
-    
+
     private void ChangePage_Board(int page)
     {
         if (page >= 1 && page <= totalPages_Board)
@@ -423,7 +423,7 @@
             StateHasChanged();
         }
     }
-    
+
     private void ChangePage_Nozzle(int page)
     {
         if (page >= 1 && page <= totalPages_Nozzle)
@@ -438,7 +438,7 @@
     {
         selectedEngineId = engineId;
         selectedBoardId = 0; // 重置主板选择
-        
+
         if (engineId == 0)
         {
             // 显示所有主板和油枪
@@ -453,11 +453,11 @@
             var boardIds = filteredBoards.Select(b => b.BoardId).ToList();
             filteredNozzles = allNozzles.Where(n => boardIds.Contains(n.BoardId)).ToList();
         }
-        
+
         // 重置分页
         currentPage_Board = 1;
         currentPage_Nozzle = 1;
-        
+
         StateHasChanged();
     }
 
@@ -465,7 +465,7 @@
     private void SelectBoard(int boardId)
     {
         selectedBoardId = boardId;
-        
+
         if (boardId == 0)
         {
             // 如果选择了"显示所有主板",则显示当前筛选条件下的所有油枪
@@ -484,10 +484,10 @@
             // 只显示选定主板的油枪
             filteredNozzles = allNozzles.Where(n => n.BoardId == boardId).ToList();
         }
-        
+
         // 重置油枪分页
         currentPage_Nozzle = 1;
-        
+
         StateHasChanged();
     }
 
@@ -557,7 +557,7 @@
             {
                 await nozzleService.DeleteEngineAsync(id);
                 await LoadAllData();
-                
+
                 // 如果删除的是当前选中的油机,重置选择
                 if (selectedEngineId == id)
                 {
@@ -579,7 +579,7 @@
             JSRuntime.InvokeVoidAsync("alert", "请先选择一个油机");
             return;
         }
-        
+
         currentBoard = new TBoard { EngineId = selectedEngineId };
         boardModalTitle = "添加主板";
         isBoardEditMode = false;
@@ -649,7 +649,7 @@
             {
                 await nozzleService.DeleteBoardAsync(id);
                 await LoadAllData();
-                
+
                 // 如果删除的是当前选中的主板,重置选择
                 if (selectedBoardId == id)
                 {
@@ -671,7 +671,7 @@
             JSRuntime.InvokeVoidAsync("alert", "请先选择一个主板");
             return;
         }
-        
+
         currentNozzle = new TNozzle { BoardId = selectedBoardId };
         nozzleModalTitle = "添加油枪";
         isNozzleEditMode = false;
@@ -722,6 +722,17 @@
             }
 
             CloseNozzleModal();
+
+            // 更新全局状态
+            if (isNozzleEditMode)
+            {
+                UpdateNozzleInGlobalState(currentNozzle);
+            }
+            else
+            {
+                AddNozzleToGlobalState(currentNozzle);
+            }
+
             await LoadAllData();
             // 重新应用当前筛选
             if (selectedBoardId > 0)
@@ -747,8 +758,12 @@
             try
             {
                 await nozzleService.DeleteNozzleAsync(id);
+
+                // 从全局状态中移除
+                RemoveNozzleFromGlobalState(id);
+
                 await LoadAllData();
-                
+
                 // 重新应用当前筛选
                 if (selectedBoardId > 0)
                 {
@@ -765,31 +780,31 @@
             }
         }
     }
-    
+
     // ID选择器相关状态
     private bool showEngineIdSelector = false;
     private bool showBoardIdSelector = false;
     private bool showNozzleIdSelector = false;
-    
+
     // 显示ID选择器
     private void ShowEngineIdSelector()
     {
         showEngineIdSelector = true;
         StateHasChanged();
     }
-    
+
     private void ShowBoardIdSelector()
     {
         showBoardIdSelector = true;
         StateHasChanged();
     }
-    
+
     private void ShowNozzleIdSelector()
     {
         showNozzleIdSelector = true;
         StateHasChanged();
     }
-    
+
     // ID选择回调
     private void OnEngineIdSelected(int id)
     {
@@ -797,55 +812,126 @@
         showEngineIdSelector = false;
         StateHasChanged();
     }
-    
+
     private void OnBoardIdSelected(int id)
     {
         currentBoard.BoardId = id;
         showBoardIdSelector = false;
         StateHasChanged();
     }
-    
+
     private void OnNozzleIdSelected(int id)
     {
         currentNozzle.NozzleId = id;
         showNozzleIdSelector = false;
         StateHasChanged();
     }
-    
+
     // 取消选择
     private void CancelEngineIdSelection()
     {
         showEngineIdSelector = false;
         StateHasChanged();
     }
-    
+
     private void CancelBoardIdSelection()
     {
         showBoardIdSelector = false;
         StateHasChanged();
     }
-    
+
     private void CancelNozzleIdSelection()
     {
         showNozzleIdSelector = false;
         StateHasChanged();
     }
-    
+
     // 获取已使用的ID列表
     private List<int> GetUsedEngineIds()
     {
         return engines?.Select(e => e.EngineId).ToList() ?? new List<int>();
     }
-    
+
     private List<int> GetUsedBoardIds()
     {
         return allBoards?.Select(b => b.BoardId).ToList() ?? new List<int>();
     }
-    
+
     private List<int> GetUsedNozzleIds()
     {
         return allNozzles?.Select(n => n.NozzleId).ToList() ?? new List<int>();
     }
+
+    // ===== 全局油枪状态管理 =====
+
+    /// <summary>
+    /// 添加油枪到全局状态
+    /// </summary>
+    private void AddNozzleToGlobalState(TNozzle nozzle)
+    {
+        try
+        {
+            if (!Service.GlobalTool.g_mNozzleState.ContainsKey(nozzle.NozzleId))
+            {
+                var nozzleState = new NozzleState
+                {
+                    noz = nozzle.NozzleId,
+                    VLR = "0",
+                    oil = nozzle.product.ToString()+"#",
+                    nozzlestate = Service.GlobalTool.NozzleState_Offline,
+                    warnstate = Service.GlobalTool.WarningState_Normal
+                };
+                Service.GlobalTool.g_mNozzleState.Add(nozzle.NozzleId, nozzleState);
+                Console.WriteLine($"已添加油枪 {nozzle.NozzleId} 到全局状态");
+            }
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine($"添加油枪到全局状态失败:{ex.Message}");
+        }
+    }
+
+    /// <summary>
+    /// 更新全局状态中的油枪信息
+    /// </summary>
+    private void UpdateNozzleInGlobalState(TNozzle nozzle)
+    {
+        try
+        {
+            if (Service.GlobalTool.g_mNozzleState.ContainsKey(nozzle.NozzleId))
+            {
+                // 只更新状态,不改变其他运行时数据
+                var existingState = Service.GlobalTool.g_mNozzleState[nozzle.NozzleId];
+                existingState.oil = nozzle.product.ToString() + "#";
+
+                // 如果需要,可以在这里更新特定字段
+                Console.WriteLine($"已更新油枪 {nozzle.NozzleId} 的全局状态");
+            }
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine($"更新油枪全局状态失败:{ex.Message}");
+        }
+    }
+    
+    /// <summary>
+    /// 从全局状态中移除油枪
+    /// </summary>
+    private void RemoveNozzleFromGlobalState(int nozzleId)
+    {
+        try
+        {
+            if (Service.GlobalTool.g_mNozzleState.ContainsKey(nozzleId))
+            {
+                Service.GlobalTool.g_mNozzleState.Remove(nozzleId);
+                Console.WriteLine($"已从全局状态移除油枪 {nozzleId}");
+            }
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine($"从全局状态移除油枪失败:{ex.Message}");
+        }
+    }
 }
 
 <!-- ID选择器组件 -->