Sfoglia il codice sorgente

增加恢复任务的原始状态

djs 5 mesi fa
parent
commit
0f34b8b1c1

+ 78 - 2
MeterVision/Patch/UCPatchGrid.xaml.cs

@@ -489,7 +489,7 @@ namespace MeterVision.Patch
                 {
                     string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
                     //查询任务
-                    List<TPatchDetail> patchDetails = DBPatch.GetPatchDetailsWithRunFlagZero(CurPatchItem.PatchId,stationId);
+                    List<TPatchDetail> patchDetails = DBPatch.GetPatchDetailsWithRunFlag(CurPatchItem.PatchId,stationId,0);
                     for(int i = 0; i < patchDetails.Count; i++)
                     {
                         if (!IsProcessingTask)
@@ -525,7 +525,7 @@ namespace MeterVision.Patch
                             {
                                 patchDetails[i+1].LatestValue = patchDetails[i].LatestValue;
                                 patchDetails[i+1].LatestComplete = patchDetails[i].LatestComplete;
-                                patchDetails[i + 1].LatestTime = patchDetails[i].LatestTime;
+                                patchDetails[i+1].LatestTime = patchDetails[i].LatestTime;
                             }
                             //更新改变了Latest的数据
                             if (DBPatch.UpdatePatchDetails_Latest(patchDetails[i + 1]))
@@ -583,6 +583,82 @@ namespace MeterVision.Patch
             }
         }
 
+
+        //如果是存在已执行的任务,恢复未执行任务的数据状态
+        public async Task<bool> ResetPatchTask()
+        {
+            if (CurPatchItem == null) return false;
+
+            string titleInfo = "正在恢复为任务未执行的状态,请稍后...";
+            WaitWindow waitWindow = new WaitWindow(titleInfo)
+            {
+                Owner = Application.Current.MainWindow,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner
+            };
+            waitWindow.Show();
+
+            try
+            {
+                List<TPatchDetail> patchDetails = null;
+                bool blUpdate = false;
+
+                await Task.Run(() =>
+                {
+                    string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
+                    //查询数据
+                    patchDetails = DBPatch.GetPatchDetailsWithRunFlag(CurPatchItem.PatchId, stationId,1);
+                    foreach(var patchDetail in patchDetails)
+                    {
+                        //删除目标图像及识别日志
+                        try
+                        {
+                            if (File.Exists(patchDetail.DstImage))
+                            {
+                                File.Delete(patchDetail.DstImage);
+                            }
+                            if (File.Exists(patchDetail.LogPath))
+                            {
+                                File.Delete(patchDetail.LogPath);
+                            }
+                        }
+                        catch
+                        {
+
+                        }
+                        patchDetail.ResetRunValue();        //恢复初始值
+                    }
+                    
+                    if (patchDetails.Count > 0)
+                    {
+                        blUpdate = DBPatch.UpdatePatchDetails(patchDetails);
+                    }
+                });
+
+                if (blUpdate && patchDetails != null)
+                {
+                    foreach (PatchDetailItem detailItem in PatchDetailItemList)
+                    {
+                        var findItem = patchDetails.Find((a) => a.PatchDetailId == detailItem.PatchDetailId);
+                        if(findItem!= null)
+                        {
+                            ObjectHelper.CopyMatchingFields(findItem, detailItem);
+                        }
+                    }//foreach
+                }
+                return true;
+            }
+            catch(Exception ex)
+            {
+                MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
+                    MessageBoxButton.OK, MessageBoxImage.Error);
+                return false;
+            }
+            finally
+            {
+                waitWindow.Close();
+            }
+        }
+
         private void PatchTaskDlg_OnStopTask()
         {
             IsProcessingTask = false;

+ 6 - 1
MeterVision/Patch/UCPatchMain.xaml

@@ -11,7 +11,7 @@
              mc:Ignorable="d"
              Padding="5 2 5 5"
              Background="WhiteSmoke"
-             d:DesignHeight="800" d:DesignWidth="1366">
+             d:DesignHeight="800" d:DesignWidth="1600">
     <UserControl.Resources>
         <!--<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
         <local1:InverseBoolToVisibilityConverter x:Key="InverseBoolToVisibilityConverter" />-->
@@ -556,6 +556,11 @@
                                               Background="#2196F3" Content="开始批量任务" Foreground="White"
                                               Width="100" Height="28" FontSize="13" Margin="0 0 5 0" />
 
+                        <zdfflatui:FlatButton Grid.Column="1" x:Name="btnResetTask" HorizontalAlignment="Center"
+                                              Background="#FF4C4C" Content="恢复原始"
+                                                Click="BtnResetTask_Click" Foreground="White"
+                                              Width="80" Height="28" FontSize="13" Margin="0 0 5 0" />
+                        
                         <zdfflatui:FlatButton Grid.Column="2" x:Name="btnRefreshDetail" HorizontalAlignment="Center"
                                               Background="#2196F3" Foreground="White" Content="🔄"
                                           ToolTip="刷新数据列表"

+ 41 - 1
MeterVision/Patch/UCPatchMain.xaml.cs

@@ -132,7 +132,6 @@ namespace MeterVision.Patch
         }
 
 
-
         private void Apply_Station_Title(VPatchStation stationItem)
         {
             pnlDetailFunc.Visibility = stationItem == null ? Visibility.Hidden : Visibility.Visible;
@@ -576,6 +575,46 @@ namespace MeterVision.Patch
         }
 
 
+        //清空已识别任务的各种数据及状态
+        private async void BtnResetTask_Click(object sender, RoutedEventArgs e)
+        {
+            MessageBoxResult result = MessageBox.Show("确定要删除该项吗?", "确认删除",
+                MessageBoxButton.YesNo, MessageBoxImage.Warning);
+
+            // 判断用户的选择
+            if (result != MessageBoxResult.Yes)
+            {
+                return;
+            }
+
+            bool blTask = await ucPatchGird.ResetPatchTask();            
+            //更新目录内容
+            if (blTask && SelectedPatchItem != null)
+            {
+                VPatch vPatch = null;
+                await Task.Run(() =>
+                {
+                    vPatch = DBPatch.GetVPatchById(SelectedPatchItem.PatchId);
+                });
+
+                if (vPatch != null)
+                {
+                    //var NewPatchItem = new PatchItem(vPatch);
+                    foreach (var patchItem in PatchItemList)
+                    {
+                        if (patchItem.PatchId == vPatch.PatchId)
+                        {
+                            ObjectHelper.CopyMatchingFields(vPatch, patchItem);
+                            break;
+                        }
+                    }//foreach
+                }//if vpatch!=null
+                RefresUIPatchItem(SelectedPatchItem.PatchId);
+                //刷新站点的统计信息
+                ucStationGrid.RefreshItems();
+            }//blTask
+        }
+
         private void LogScrollTop_Click(object sender, RoutedEventArgs e)
         {
             LogRichTextBox.ScrollToHome();
@@ -745,6 +784,7 @@ namespace MeterVision.Patch
             //Apply_UCPatchDetail_Title(SelectedPatchItem);
             Apply_Station_Title(SelectedStationItem);
         }
+
         //----------------------------------------------------------------
     }
 

+ 89 - 4
MeterVision/db/DBPatch.cs

@@ -210,6 +210,82 @@ namespace MeterVision.db
             }
         }
 
+
+        public static bool UpdatePatchDetails(List<TPatchDetail> patchDetails)
+        {
+            // 更新 SQL 语句
+            string sql = @"
+                UPDATE t_patch_detail
+                SET 
+                    run_flag = @RunFlag,
+                    run_time = @RunTime,
+                    dst_image = @DstImage,
+                    result_meter = @ResultMeter,
+                    result_type = @ResultType,
+                    raw_value = @RawValue,
+                    final_value = @FinalValue,
+                    complete_value = @CompleteValue,
+                    value_changed = @ValueChanged,
+                    equal_flag = @EqualFlag,
+                    ai_ver = @AiVer,
+                    debug_info = @DebugInfo,
+                    log_path = @LogPath,
+                    memo = @Memo,
+                    latest_complete = @LatestComplete,
+                    last_compress = @LastCompress,
+                    latest_value = @LatestValue,
+                    latest_time = @LatestTime
+                WHERE patch_detail_id = @PatchDetailId;";
+
+            try
+            {
+                using (SQLiteConnection connection = SQLiteHelper.GetConnection())
+                {
+                    connection.Open();
+                    using (SQLiteTransaction transaction = connection.BeginTransaction())
+                    {
+                        foreach (var patchDetail in patchDetails)
+                        {
+                            SQLiteParameter[] parameters = new SQLiteParameter[]
+                            {
+                                new SQLiteParameter("@RunFlag", patchDetail.RunFlag),
+                                new SQLiteParameter("@RunTime", patchDetail.RunTime),
+                                new SQLiteParameter("@DstImage", patchDetail.DstImage),
+                                new SQLiteParameter("@ResultMeter", patchDetail.ResultMeter),
+                                new SQLiteParameter("@ResultType", patchDetail.ResultType),
+                                new SQLiteParameter("@RawValue", patchDetail.RawValue),
+                                new SQLiteParameter("@FinalValue", patchDetail.FinalValue),
+                                new SQLiteParameter("@CompleteValue", patchDetail.CompleteValue),
+                                new SQLiteParameter("@ValueChanged", patchDetail.ValueChanged),
+                                new SQLiteParameter("@EqualFlag", patchDetail.EqualFlag),
+                                new SQLiteParameter("@AiVer", patchDetail.AiVer),
+                                new SQLiteParameter("@DebugInfo", patchDetail.DebugInfo),
+                                new SQLiteParameter("@LogPath", patchDetail.LogPath),
+                                new SQLiteParameter("@Memo", patchDetail.Memo),
+                                new SQLiteParameter("@LatestComplete", patchDetail.LatestComplete),
+                                new SQLiteParameter("@LastCompress", patchDetail.LastCompress),
+                                new SQLiteParameter("@LatestValue", patchDetail.LatestValue),
+                                new SQLiteParameter("@LatestTime", patchDetail.LatestTime),
+                                new SQLiteParameter("@PatchDetailId", patchDetail.PatchDetailId) // 条件字段
+                            };
+
+                            SQLiteHelper.ExecuteNonQuery(sql, parameters, transaction);
+                        }
+
+                        // 提交事务
+                        transaction.Commit();
+                    }
+                }
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine("更新数据时出错: " + ex.Message);
+                return false;
+            }
+        }
+
+
         //根据识别结果修改表内容
         public static bool UpdatePatchDetailWithResult(TPatchDetail detail)
         {
@@ -223,6 +299,7 @@ namespace MeterVision.db
                     raw_value = @RawValue,
                     final_value = @FinalValue,
                     complete_value = @CompleteValue,
+                    value_changed = @ValueChanged,
                     equal_flag = @EqualFlag,
                     ai_ver = @AiVer,
                     debug_info = @DebugInfo,
@@ -241,6 +318,7 @@ namespace MeterVision.db
                 new SQLiteParameter("@RawValue", detail.RawValue),
                 new SQLiteParameter("@FinalValue", detail.FinalValue),
                 new SQLiteParameter("@CompleteValue",detail.CompleteValue),
+                new SQLiteParameter("@ValueChanged",detail.ValueChanged),
                 new SQLiteParameter("@EqualFlag", detail.EqualFlag),
                 new SQLiteParameter("@AiVer", detail.AiVer),
                 new SQLiteParameter("@DebugInfo", detail.DebugInfo),
@@ -850,7 +928,7 @@ namespace MeterVision.db
 
 
 
-        public static List<TPatchDetail> GetPatchDetailsWithRunFlagZero(string patchId,string stationId)
+        public static List<TPatchDetail> GetPatchDetailsWithRunFlag(string patchId,string stationId,int runFlag)
         {
             // 查询 SQL
             //string sql = @"
@@ -869,19 +947,26 @@ namespace MeterVision.db
             string whereStation = "";
             if (!string.IsNullOrEmpty(stationId))
             {
-                whereStation = " AND station_id = @StationId";
+                whereStation = " AND run_flag = @RunFlag AND station_id = @StationId";
             }
 
+            //string sql = $@"
+            //    SELECT * FROM t_patch_detail
+            //    WHERE patch_id = @PatchId AND run_flag = 0{whereStation}
+            //    ORDER BY station_id,sample_time ASC";
+
             string sql = $@"
                 SELECT * FROM t_patch_detail
-                WHERE patch_id = @PatchId AND run_flag = 0{whereStation}
+                WHERE patch_id = @PatchId{whereStation}
                 ORDER BY station_id,sample_time ASC";
 
+
             // 定义参数
             SQLiteParameter[] parameters = new SQLiteParameter[]
             {
                 new SQLiteParameter("@PatchId", patchId),
-                new SQLiteParameter("@StationId",stationId)
+                new SQLiteParameter("@StationId",stationId),
+                new SQLiteParameter("@RunFlag",runFlag)
             };
 
             // 执行查询并读取数据

+ 45 - 1
MeterVision/db/TPatchDetail.cs

@@ -254,7 +254,17 @@ namespace MeterVision.db
                 else if (MeterType == 2)
                 {
                     //全指针
-                    isEqual = (standValue * FaConstant.CUBE_VALUE) == finalValue;
+                    //数字+指针(只比较数字部分)
+                    //获取真正数据部分的值
+                    int standValue1 = (int)Math.Ceiling(standValue / LastUnit);
+                    int finalValue1 = (int)(finalValue / (FaConstant.CUBE_VALUE * LastUnit));
+                    isEqual = (standValue1 == finalValue1);
+                    //isEqual = (standValue * FaConstant.CUBE_VALUE) == finalValue;
+
+                    /*if(finalValue == 22066060)
+                    {
+                        Console.WriteLine("测试");
+                    }*/
                     return isEqual ? 1 : 0;
                 }
                 else if (MeterType == 3)
@@ -275,6 +285,40 @@ namespace MeterVision.db
             }
         }
 
+
+        //恢复运行时的默认值
+        public void ResetRunValue()
+        {
+            RunFlag = 0;
+            RunTime = string.Empty;
+            DstImage = string.Empty;
+            ResultMeter = -1;
+            ResultType = 0;
+            RawValue = -1;
+            FinalValue = -1;
+            CompleteValue = -1;
+            ValueChanged = -1;
+            EqualFlag = -1;
+            AiVer = string.Empty;
+            DebugInfo = string.Empty;
+            LogPath = string.Empty;
+            Memo = string.Empty;
+
+            //说明这条数据是在模板中被配置的数据
+            //latest_complete=-1,last_compress = 100
+            LatestComplete = -1;
+            if (LatestTime.Equals(SampleTime))
+            {
+                LastCompress = 100;
+            }
+            else
+            {
+                LastCompress = -1;
+                LatestValue = -1;
+                LatestTime = string.Empty;                
+            }
+        }
+
         //----------------------------------------------
     }
     ////////////////////////////////////////////////////////////