FaBefore.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using MeterVision.db;
  2. using MeterVision.model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using static MeterVision.FreeAi.FaImport;
  10. namespace MeterVision.FreeAi
  11. {
  12. public class FaBefore
  13. {
  14. //给beforeAI设置默认值
  15. //private static void setDefaultValue_BeforeAI(ref BeforeAI beforeAI)
  16. //{
  17. // beforeAI.AI_result = FaConstant.IMPOSSIBLE_MAX_VALUE;
  18. // beforeAI.result_of_1st_part = FaConstant.IMPOSSIBLE_MAX_VALUE;
  19. // beforeAI.result_of_2st_part = FaConstant.IMPOSSIBLE_MAX_VALUE;
  20. // beforeAI.big_jpg_max_size = 12 * 1024;
  21. // beforeAI.bright_factor = 1.2f;
  22. // beforeAI.center_x = FaConstant.IMPOSSIBLE_VALUE;
  23. // beforeAI.center_y = FaConstant.IMPOSSIBLE_VALUE;
  24. // beforeAI.lean_angle = FaConstant.IMPOSSIBLE_VALUE;
  25. // beforeAI.is_nums_in_upper = 1;
  26. // beforeAI.is_OK_preValue = 0;
  27. // beforeAI.is_upload_red_ind_part = 1;
  28. // beforeAI.manual_parameter_enable = 0;
  29. // beforeAI.MAX_FLOW_PER_HOUR = FaConstant.IMPOSSIBLE_MAX_VALUE;
  30. // beforeAI.meter_type = 0;
  31. // beforeAI.NUM_BLACK_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  32. // beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  33. // beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  34. // beforeAI.UNIT_OF_THE_LAST_NUMBER = FaConstant.ONE_STERE;
  35. // beforeAI.compress_jpg_index = 0;
  36. // beforeAI.number_image_zoomrate = 1.0f;
  37. // beforeAI.indicator_image_zommrate = 1.0f;
  38. // beforeAI.use_test_jpg_flag = 1;
  39. // beforeAI.is_get_whole_image = 1;
  40. // beforeAI.num_region = new ushort[4];
  41. // beforeAI.config_num_region = new ushort[4];
  42. // beforeAI.indicators_regions = new ushort[10 * 4];
  43. //}
  44. public static BeforeAI getBeforeAI()
  45. {
  46. BeforeAI beforeAI = new BeforeAI();
  47. //setDefaultValue_BeforeAI(ref beforeAI);
  48. return beforeAI;
  49. }
  50. private static void SetDefaultValue(ref BeforeAI beforeAI)
  51. {
  52. beforeAI.METER_TYPE = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  53. beforeAI.NUM_NUMBERS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  54. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)FaConstant.IMPOSSIBLE_VALUE;
  55. beforeAI.NUM_INDICATORS = FaConstant.IMPOSSIBLE_NUM_NUMBER;
  56. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)FaConstant.IMPOSSIBLE_VALUE;
  57. beforeAI.HOURS_FORM_LAST_1278 = 0; //20250402
  58. beforeAI.MAX_FLOW_THRESHOLD = FaConstant.IMPOSSIBLE_MAX_VALUE;
  59. beforeAI.AI_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
  60. beforeAI.AI_COMPLETE_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
  61. beforeAI.CONFIG_NUM_REGION = new ushort[8];
  62. for(int i = 0; i < 8; i++)
  63. {
  64. beforeAI.CONFIG_NUM_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  65. }
  66. beforeAI.CONFIG_TWO_IND = new ushort[4];
  67. for(int i = 0; i < 4; i++)
  68. {
  69. beforeAI.CONFIG_TWO_IND[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  70. }
  71. beforeAI.CONFIG_METER_REGION = new ushort[4];
  72. for(int i = 0; i < 4; i++)
  73. {
  74. beforeAI.CONFIG_METER_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  75. }
  76. beforeAI.BRIGHT_FACTOR = 1.20f;
  77. beforeAI.BIG_JPG_SIZE = 12 * 1024;
  78. beforeAI.USE_TEST_JPG_FLAG = 1;
  79. beforeAI.COMPRESS_JPG_INDEX = 100;
  80. beforeAI.IS_UPLOAD_RED_IND_PART = 1;
  81. beforeAI.IS_NUM_IN_UPPER = 1;
  82. }
  83. public static BeforeAI getBeforeAI(SingleDetailItem detailItem)
  84. {
  85. BeforeAI beforeAI = new BeforeAI();
  86. SetDefaultValue(ref beforeAI);
  87. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  88. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  89. {
  90. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  91. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  92. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  93. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  94. }
  95. else if(beforeAI.METER_TYPE == 2)
  96. {
  97. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  98. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  99. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  100. }
  101. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  102. {
  103. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  104. //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
  105. beforeAI.HOURS_FORM_LAST_1278 = 1;
  106. beforeAI.MAX_FLOW_THRESHOLD = (ulong)detailItem.FlowRate * FaConstant.CUBE_VALUE;
  107. beforeAI.AI_RESULT = (ulong)detailItem.LastValue * FaConstant.CUBE_VALUE;
  108. }
  109. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  110. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  111. beforeAI.COMPRESS_JPG_INDEX = 100;
  112. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  113. beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper;
  114. return beforeAI;
  115. }
  116. public static BeforeAI getBeforeAI(PatchDetailItem detailItem)
  117. {
  118. BeforeAI beforeAI = new BeforeAI();
  119. SetDefaultValue(ref beforeAI);
  120. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  121. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  122. {
  123. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  124. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  125. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  126. if (!string.IsNullOrEmpty(detailItem.FeatureRegion)){
  127. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  128. }
  129. }
  130. else if (beforeAI.METER_TYPE == 2)
  131. {
  132. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  133. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  134. if (!string.IsNullOrEmpty(detailItem.FeatureRegion))
  135. {
  136. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  137. }
  138. }
  139. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  140. {
  141. if (!string.IsNullOrEmpty(detailItem.MeterRegion))
  142. {
  143. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  144. }
  145. //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
  146. double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
  147. beforeAI.HOURS_FORM_LAST_1278 = (uint)hourDiffs;
  148. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
  149. //上次的值
  150. beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
  151. if(detailItem.LatestComplete >= 0)
  152. {
  153. beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete;
  154. }
  155. }
  156. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  157. beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper;
  158. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  159. if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
  160. {
  161. beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress;
  162. }
  163. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  164. if(beforeAI.MAX_FLOW_THRESHOLD == 0)
  165. {
  166. beforeAI.HOURS_FORM_LAST_1278 = 1;
  167. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * 1 * FaConstant.CUBE_VALUE);
  168. }
  169. return beforeAI;
  170. }
  171. public static BeforeAI getBeforeAI(TPatchDetail detailItem)
  172. {
  173. BeforeAI beforeAI = new BeforeAI();
  174. SetDefaultValue(ref beforeAI);
  175. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  176. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  177. {
  178. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  179. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  180. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  181. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  182. }
  183. else if (beforeAI.METER_TYPE == 2)
  184. {
  185. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  186. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  187. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  188. }
  189. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  190. {
  191. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  192. double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
  193. beforeAI.HOURS_FORM_LAST_1278 = (uint)hourDiffs;
  194. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
  195. //上次的值
  196. beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
  197. if (detailItem.LatestComplete>=0)
  198. {
  199. beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete;
  200. }
  201. }
  202. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  203. beforeAI.IS_NUM_IN_UPPER = (byte)detailItem.NumInUpper;
  204. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  205. if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
  206. {
  207. beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress;
  208. }
  209. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  210. if (beforeAI.MAX_FLOW_THRESHOLD == 0)
  211. {
  212. beforeAI.HOURS_FORM_LAST_1278 = 1;
  213. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * 1 * FaConstant.CUBE_VALUE);
  214. }
  215. return beforeAI;
  216. }
  217. static double CalculateHourDifference(string time1, string time2)
  218. {
  219. // 解析字符串为 DateTime
  220. DateTime dateTime1 = DateTime.ParseExact(time1, "yyyy-MM-dd HH:mm:ss", null);
  221. DateTime dateTime2 = DateTime.ParseExact(time2, "yyyy-MM-dd HH:mm:ss", null);
  222. // 计算小时差(带小数)
  223. return (dateTime2 - dateTime1).TotalHours;
  224. }
  225. static bool TryParseCoordinates(string input, int numPairs, ref ushort[] numbers)
  226. {
  227. // 确保传入的数组足够大
  228. if (numbers.Length < numPairs * 2)
  229. {
  230. Console.WriteLine("错误: 数组长度不足");
  231. return false;
  232. }
  233. // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
  234. string pattern = "^" + string.Join(" ", Enumerable.Repeat(@"(\d+),(\d+)", numPairs)) + "$";
  235. Match match = Regex.Match(input, pattern);
  236. if (match.Success)
  237. {
  238. for (int i = 0; i < numPairs * 2; i++)
  239. {
  240. numbers[i] = ushort.Parse(match.Groups[i + 1].Value);
  241. }
  242. return true;
  243. }
  244. return false;
  245. }
  246. //static bool TryParseCoordinates(string input, int numPairs, out int[] numbers)
  247. //{
  248. // numbers = new int[numPairs * 2];
  249. // // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
  250. // string pattern = "^" + string.Join(" ", new string[numPairs].Select(_ => @"(\d+),(\d+)")) + "$";
  251. // Match match = Regex.Match(input, pattern);
  252. // if (match.Success)
  253. // {
  254. // for (int i = 0; i < numPairs * 2; i++)
  255. // {
  256. // numbers[i] = int.Parse(match.Groups[i + 1].Value);
  257. // }
  258. // return true;
  259. // }
  260. // return false;
  261. //}
  262. //static bool TryParseCoordinates4(string input, out int[] numbers)
  263. //{
  264. // numbers = new int[8];
  265. // // 正则表达式匹配 4 组 "数字,数字",中间用空格分隔
  266. // string pattern = @"^(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)$";
  267. // Match match = Regex.Match(input, pattern);
  268. // if (match.Success)
  269. // {
  270. // // 提取匹配到的 8 个数值
  271. // for (int i = 0; i < 8; i++)
  272. // {
  273. // numbers[i] = int.Parse(match.Groups[i + 1].Value);
  274. // }
  275. // return true;
  276. // }
  277. // return false;
  278. //}
  279. //--------------------------------------------------------
  280. }
  281. }