FaBefore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.MAX_FLOW_THRESHOLD = FaConstant.IMPOSSIBLE_MAX_VALUE;
  58. beforeAI.AI_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
  59. beforeAI.AI_COMPLETE_RESULT = FaConstant.IMPOSSIBLE_MAX_VALUE;
  60. beforeAI.CONFIG_NUM_REGION = new ushort[8];
  61. for(int i = 0; i < 8; i++)
  62. {
  63. beforeAI.CONFIG_NUM_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  64. }
  65. beforeAI.CONFIG_TWO_IND = new ushort[4];
  66. for(int i = 0; i < 4; i++)
  67. {
  68. beforeAI.CONFIG_TWO_IND[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  69. }
  70. beforeAI.CONFIG_METER_REGION = new ushort[4];
  71. for(int i = 0; i < 4; i++)
  72. {
  73. beforeAI.CONFIG_METER_REGION[i] = (ushort)FaConstant.IMPOSSIBLE_VALUE;
  74. }
  75. beforeAI.BRIGHT_FACTOR = 1.20f;
  76. beforeAI.BIG_JPG_SIZE = 12 * 1024;
  77. beforeAI.USE_TEST_JPG_FLAG = 1;
  78. beforeAI.COMPRESS_JPG_INDEX = 100;
  79. beforeAI.IS_UPLOAD_RED_IND_PART = 1;
  80. }
  81. public static BeforeAI getBeforeAI(SingleDetailItem detailItem)
  82. {
  83. BeforeAI beforeAI = new BeforeAI();
  84. SetDefaultValue(ref beforeAI);
  85. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  86. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  87. {
  88. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  89. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  90. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  91. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  92. }
  93. else if(beforeAI.METER_TYPE == 2)
  94. {
  95. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  96. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  97. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  98. }
  99. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  100. {
  101. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  102. //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
  103. beforeAI.MAX_FLOW_THRESHOLD = (ulong)detailItem.FlowRate * FaConstant.CUBE_VALUE;
  104. beforeAI.AI_RESULT = (ulong)detailItem.LastValue * FaConstant.CUBE_VALUE;
  105. }
  106. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  107. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  108. beforeAI.COMPRESS_JPG_INDEX = 100;
  109. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  110. return beforeAI;
  111. }
  112. public static BeforeAI getBeforeAI(PatchDetailItem detailItem)
  113. {
  114. BeforeAI beforeAI = new BeforeAI();
  115. SetDefaultValue(ref beforeAI);
  116. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  117. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  118. {
  119. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  120. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  121. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  122. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  123. }
  124. else if (beforeAI.METER_TYPE == 2)
  125. {
  126. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  127. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  128. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  129. }
  130. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  131. {
  132. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  133. //单张图计算最大流量,意义不大,也不太好确定时间,就按1小时计算吧
  134. double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
  135. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
  136. //上次的值
  137. beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
  138. if(detailItem.LatestComplete >= 0)
  139. {
  140. beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.LatestComplete;
  141. }
  142. }
  143. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  144. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  145. if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
  146. {
  147. beforeAI.COMPRESS_JPG_INDEX = (byte)detailItem.LastCompress;
  148. }
  149. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  150. return beforeAI;
  151. }
  152. public static BeforeAI getBeforeAI(TPatchDetail detailItem)
  153. {
  154. BeforeAI beforeAI = new BeforeAI();
  155. SetDefaultValue(ref beforeAI);
  156. beforeAI.METER_TYPE = (byte)detailItem.MeterType;
  157. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 3)
  158. {
  159. beforeAI.NUM_NUMBERS = (byte)detailItem.DigitCount;
  160. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  161. beforeAI.UNIT_OF_THE_LAST_NUMBER = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  162. TryParseCoordinates(detailItem.FeatureRegion, 4, ref beforeAI.CONFIG_NUM_REGION);
  163. }
  164. else if (beforeAI.METER_TYPE == 2)
  165. {
  166. beforeAI.NUM_INDICATORS = (byte)detailItem.PointerCount;
  167. beforeAI.UNIT_OF_THE_LAST_INDICATOR = (uint)(detailItem.LastUnit * FaConstant.CUBE_VALUE);
  168. TryParseCoordinates(detailItem.FeatureRegion, 2, ref beforeAI.CONFIG_TWO_IND);
  169. }
  170. if (beforeAI.METER_TYPE == 1 || beforeAI.METER_TYPE == 2 || beforeAI.METER_TYPE == 3)
  171. {
  172. TryParseCoordinates(detailItem.MeterRegion, 2, ref beforeAI.CONFIG_METER_REGION);
  173. double hourDiffs = CalculateHourDifference(detailItem.LatestTime, detailItem.SampleTime);
  174. beforeAI.MAX_FLOW_THRESHOLD = (ulong)(detailItem.FlowRate * hourDiffs * FaConstant.CUBE_VALUE);
  175. //上次的值
  176. beforeAI.AI_RESULT = (ulong)detailItem.LatestValue; // * FaConstant.CUBE_VALUE;
  177. if (detailItem.LatestComplete>=0)
  178. {
  179. beforeAI.AI_COMPLETE_RESULT = (ulong)detailItem.CompleteValue;
  180. }
  181. }
  182. beforeAI.BRIGHT_FACTOR = (float)detailItem.BrightVal;
  183. beforeAI.USE_TEST_JPG_FLAG = 1; //(float)detailItem.use
  184. if (detailItem.LastCompress > 0 && detailItem.LastCompress <= 100)
  185. {
  186. beforeAI.COMPRESS_JPG_INDEX = 100;
  187. }
  188. beforeAI.IS_UPLOAD_RED_IND_PART = 1; //这个应该价格配置项
  189. return beforeAI;
  190. }
  191. static double CalculateHourDifference(string time1, string time2)
  192. {
  193. // 解析字符串为 DateTime
  194. DateTime dateTime1 = DateTime.ParseExact(time1, "yyyy-MM-dd HH:mm:ss", null);
  195. DateTime dateTime2 = DateTime.ParseExact(time2, "yyyy-MM-dd HH:mm:ss", null);
  196. // 计算小时差(带小数)
  197. return (dateTime2 - dateTime1).TotalHours;
  198. }
  199. static bool TryParseCoordinates(string input, int numPairs, ref ushort[] numbers)
  200. {
  201. // 确保传入的数组足够大
  202. if (numbers.Length < numPairs * 2)
  203. {
  204. Console.WriteLine("错误: 数组长度不足");
  205. return false;
  206. }
  207. // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
  208. string pattern = "^" + string.Join(" ", Enumerable.Repeat(@"(\d+),(\d+)", numPairs)) + "$";
  209. Match match = Regex.Match(input, pattern);
  210. if (match.Success)
  211. {
  212. for (int i = 0; i < numPairs * 2; i++)
  213. {
  214. numbers[i] = ushort.Parse(match.Groups[i + 1].Value);
  215. }
  216. return true;
  217. }
  218. return false;
  219. }
  220. //static bool TryParseCoordinates(string input, int numPairs, out int[] numbers)
  221. //{
  222. // numbers = new int[numPairs * 2];
  223. // // 生成正则表达式,例如匹配 4 组时:"(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)"
  224. // string pattern = "^" + string.Join(" ", new string[numPairs].Select(_ => @"(\d+),(\d+)")) + "$";
  225. // Match match = Regex.Match(input, pattern);
  226. // if (match.Success)
  227. // {
  228. // for (int i = 0; i < numPairs * 2; i++)
  229. // {
  230. // numbers[i] = int.Parse(match.Groups[i + 1].Value);
  231. // }
  232. // return true;
  233. // }
  234. // return false;
  235. //}
  236. //static bool TryParseCoordinates4(string input, out int[] numbers)
  237. //{
  238. // numbers = new int[8];
  239. // // 正则表达式匹配 4 组 "数字,数字",中间用空格分隔
  240. // string pattern = @"^(\d+),(\d+) (\d+),(\d+) (\d+),(\d+) (\d+),(\d+)$";
  241. // Match match = Regex.Match(input, pattern);
  242. // if (match.Success)
  243. // {
  244. // // 提取匹配到的 8 个数值
  245. // for (int i = 0; i < 8; i++)
  246. // {
  247. // numbers[i] = int.Parse(match.Groups[i + 1].Value);
  248. // }
  249. // return true;
  250. // }
  251. // return false;
  252. //}
  253. //--------------------------------------------------------
  254. }
  255. }