using MeterVision.db; using MeterVision.model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static MeterVision.FreeAi.FaImport; namespace MeterVision.FreeAi { public class FaBefore { //给beforeAI设置默认值 //private static void setDefaultValue_BeforeAI(ref BeforeAI beforeAI) //{ // beforeAI.AI_result = FaConstant.IMPOSSIBLE_MAX_VALUE; // beforeAI.result_of_1st_part = FaConstant.IMPOSSIBLE_MAX_VALUE; // beforeAI.result_of_2st_part = FaConstant.IMPOSSIBLE_MAX_VALUE; // beforeAI.big_jpg_max_size = 12 * 1024; // beforeAI.bright_factor = 1.2f; // beforeAI.center_x = FaConstant.IMPOSSIBLE_VALUE; // beforeAI.center_y = FaConstant.IMPOSSIBLE_VALUE; // beforeAI.lean_angle = FaConstant.IMPOSSIBLE_VALUE; // beforeAI.is_nums_in_upper = 1; // beforeAI.is_OK_preValue = 0; // beforeAI.is_upload_red_ind_part = 1; // beforeAI.manual_parameter_enable = 0; // beforeAI.MAX_FLOW_PER_HOUR = FaConstant.IMPOSSIBLE_MAX_VALUE; // beforeAI.meter_type = 0; // beforeAI.NUM_BLACK_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER; // beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER; // beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER; // beforeAI.UNIT_OF_THE_LAST_NUMBER = FaConstant.ONE_STERE; // beforeAI.compress_jpg_index = 0; // beforeAI.number_image_zoomrate = 1.0f; // beforeAI.indicator_image_zommrate = 1.0f; // beforeAI.use_test_jpg_flag = 1; // beforeAI.is_get_whole_image = 1; // beforeAI.num_region = new ushort[4]; // beforeAI.config_num_region = new ushort[4]; // beforeAI.indicators_regions = new ushort[10 * 4]; //} public static BeforeAI getBeforeAI() { BeforeAI beforeAI = new BeforeAI(); //setDefaultValue_BeforeAI(ref beforeAI); return beforeAI; } private static void SetDefaultValue(ref BeforeAI beforeAI) { beforeAI.METER_TYPE = FaConstant.IMPOSSIBLE_NUM_NUMBER; beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER; beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)FaConstant.IMPOSSIBLE_VALUE; beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER; beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)FaConstant.IMPOSSIBLE_VALUE; beforeAI.HOURS_FORM_LAST_1278 = 0; //20250402 beforeAI.MAX_FLOW_THRESHOLD = FaConstant.IMPOSSIBLE_MAX_VALUE; beforeAI.AI_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE; beforeAI.AI_COMPLETE_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE; beforeAI.CONFIG_NUM_REGION = new ushort[8]; for(int i = 0; i < 8; i++) { beforeAI.CONFIG_NUM_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE; } beforeAI.CONFIG_TWO_IND = new ushort[4]; for(int i = 0; i < 4; i++) { beforeAI.CONFIG_TWO_IND[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE; } beforeAI.CONFIG_METER_REGION = new ushort[4]; for(int i = 0; i < 4; i++) { beforeAI.CONFIG_METER_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE; } beforeAI.BRIGHT_FACTOR = 1.20f; beforeAI.BIG_JPG_SIZE = 12 * 1024; beforeAI.USE_TEST_JPG_FLAG = 1; beforeAI.COMPRESS_JPG_INDEX = 100; beforeAI.IS_UPLOAD_RED_IND_PART = 1; beforeAI.IS_NUM_IN_UPPER = 1; } public static BeforeAI getBeforeAI(SingleDetailItem detailItem) { BeforeAI beforeAI = new BeforeAI(); SetDefaultValue(ref beforeAI); beforeAI.METER_TYPE = (byte)detailItem.MeterType; if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3) { beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount; beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION); } else if(beforeAI.METER_TYPE == 2) { beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND); } if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3) { TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION); //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧 beforeAI.HOURS_FORM_LAST_1278 = 1; beforeAI.MAX_FLOW_THRESHOLD = (ulong)detailItem.FlowRate * FaConstant.CUBE_VALUE; beforeAI.AI_RESULT = (ulong)detailItem.LastValue * FaConstant.CUBE_VALUE; } beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal; beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use beforeAI.COMPRESS_JPG_INDEX = 100; beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项 beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper; return beforeAI; } public static BeforeAI getBeforeAI(PatchDetailItem detailItem) { BeforeAI beforeAI = new BeforeAI(); SetDefaultValue(ref beforeAI); beforeAI.METER_TYPE = (byte)detailItem.MeterType; if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3) { beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount; beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); if (!string.IsNullOrEmpty(detailItem.FeatureRegion)){ TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION); } } else if (beforeAI.METER_TYPE == 2) { beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); if (!string.IsNullOrEmpty(detailItem.FeatureRegion)) { TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND); } } if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3) { if (!string.IsNullOrEmpty(detailItem.MeterRegion)) { TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION); } //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧 double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime); beforeAI.HOURS_FORM_LAST_1278 = (uint)hourDiffs; beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE); //上次的值 beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE; if(detailItem.LatestComplete >= 0) { beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete; } } beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal; beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper; beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100) { beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress; } beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项 if(beforeAI.MAX_FLOW_THRESHOLD == 0) { beforeAI.HOURS_FORM_LAST_1278 = 1; beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * 1 * FaConstant.CUBE_VALUE); } return beforeAI; } public static BeforeAI getBeforeAI(TPatchDetail detailItem) { BeforeAI beforeAI = new BeforeAI(); SetDefaultValue(ref beforeAI); beforeAI.METER_TYPE = (byte)detailItem.MeterType; if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3) { beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount; beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION); } else if (beforeAI.METER_TYPE == 2) { beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount; beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE); TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND); } if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3) { TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION); double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime); beforeAI.HOURS_FORM_LAST_1278 = (uint)hourDiffs; beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE); //上次的值 beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE; if (detailItem.LatestComplete>=0) { beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete; } } beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal; beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper; beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100) { beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress; } beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项 if (beforeAI.MAX_FLOW_THRESHOLD == 0) { beforeAI.HOURS_FORM_LAST_1278 = 1; beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * 1 * FaConstant.CUBE_VALUE); } return beforeAI; } static double CalculateHourDifference(string time1, string time2) { // 解析字符串为 DateTime DateTime dateTime1 = DateTime.ParseExact(time1, "yyyy-MM-dd HH:mm:ss", null); DateTime dateTime2 = DateTime.ParseExact(time2, "yyyy-MM-dd HH:mm:ss", null); // 计算小时差(带小数) return (dateTime2 - dateTime1).TotalHours; } static bool TryParseCoordinates(string input, int numPairs, ref ushort[] numbers) { // 确保传入的数组足够大 if (numbers.Length < numPairs * 2) { Console.WriteLine("错误: 数组长度不足"); return false; } // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)" string pattern = "^" + string.Join(" ", Enumerable.Repeat(@"(\d+),(\d+)", numPairs)) + "$"; Match match = Regex.Match(input, pattern); if (match.Success) { for (int i = 0; i < numPairs * 2; i++) { numbers[i] = ushort.Parse(match.Groups[i + 1].Value); } return true; } return false; } //static bool TryParseCoordinates(string input, int numPairs, out int[] numbers) //{ // numbers = new int[numPairs * 2]; // // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)" // string pattern = "^" + string.Join(" ", new string[numPairs].Select(_ => @"(\d+),(\d+)")) + "$"; // Match match = Regex.Match(input, pattern); // if (match.Success) // { // for (int i = 0; i < numPairs * 2; i++) // { // numbers[i] = int.Parse(match.Groups[i + 1].Value); // } // return true; // } // return false; //} //static bool TryParseCoordinates4(string input, out int[] numbers) //{ // numbers = new int[8]; // // 正则表达式匹配 4 组 "数字,数字",中间用空格分隔 // string pattern = @"^(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)$"; // Match match = Regex.Match(input, pattern); // if (match.Success) // { // // 提取匹配到的 8 个数值 // for (int i = 0; i < 8; i++) // { // numbers[i] = int.Parse(match.Groups[i + 1].Value); // } // return true; // } // return false; //} //-------------------------------------------------------- } }