utils.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import dayjs, { Dayjs } from 'dayjs';
  2. import { uid } from 'uid';
  3. import NP from 'number-precision';
  4. import { isEqual } from 'lodash-es';
  5. import { TimeRangeItem, TimePeriodItem } from '@/modules/algo/algo-params-edit/types';
  6. import { ALGO_ENABLED_STATUS, CameraAlgoItem, FENCE_ENBALED_STATUS } from '@/api/camera/camera-preview';
  7. import safeParse from '@/utils/safeParse';
  8. import { QueryAlgoInfoRes } from '@/api/camera/camera-config';
  9. // export const createDefaultTime = (): TimeRangeItem => {
  10. // return { id: uid(), value: [dayjs(), dayjs().add(1, 'hour')] as [Dayjs, Dayjs] };
  11. // };
  12. export const createDefaultTime = (): TimePeriodItem => {
  13. return {
  14. id: uid(),
  15. startDay: null,
  16. endDay: null,
  17. timeRangeList: [{ id: uid(), startTime: '', endTime: '' }],
  18. };
  19. };
  20. export enum FrequencyEnum {
  21. second = 1,
  22. miniute = 60,
  23. hour = 3600,
  24. }
  25. export const frequencyOptions = [
  26. { label: '秒', value: FrequencyEnum.second },
  27. { label: '分钟', value: FrequencyEnum.miniute },
  28. { label: '小时', value: FrequencyEnum.hour },
  29. ];
  30. export interface DetectionJSON {
  31. detectionNum: number;
  32. detectionUnit: FrequencyEnum;
  33. }
  34. /** 根据后端返回的时间(单位是秒),拆分成单位和数值 */
  35. export const getDetectionJSON = (time: number | undefined | null): DetectionJSON => {
  36. if (time && time > 0) {
  37. for (let i = frequencyOptions.length - 1; i >= 0; i--) {
  38. const unit = frequencyOptions[i].value;
  39. if (time >= unit) {
  40. return { detectionNum: NP.divide(time, unit), detectionUnit: unit };
  41. }
  42. }
  43. }
  44. return { detectionNum: 5, detectionUnit: FrequencyEnum.miniute };
  45. };
  46. export const getDetectionTimeJSON = (time?: string): TimeRangeItem[] | null => {
  47. if (!time) return null;
  48. const timeArr = time.split(';');
  49. const nowStr = dayjs().format('YYYY-MM-DD');
  50. const timeStrArr = timeArr
  51. .map((x) => {
  52. const [startDate, endDate] = x.split('-');
  53. return [dayjs(`${nowStr} ${startDate}`), dayjs(`${nowStr} ${endDate}`)] as [Dayjs, Dayjs];
  54. })
  55. .map((x) => {
  56. return { id: uid(), value: x };
  57. });
  58. return timeStrArr;
  59. };
  60. export const getInferParam = (extra: string | undefined | null) => {
  61. if (!extra) return {};
  62. const extraObj = safeParse(extra);
  63. const params = extraObj?.inferParams;
  64. if (!params || (params && params.length == 0)) return {};
  65. return params[0];
  66. };
  67. export const getMetaValues = (extra: string | undefined | null) => {
  68. if (!extra) return [];
  69. const extraObj = safeParse(extra);
  70. const params = extraObj?.inferParams;
  71. if (!params || (params && params.length == 0)) return [];
  72. const metaObjs = params[0]?.metaObjs;
  73. if (!metaObjs || (metaObjs && metaObjs.length == 0)) return [];
  74. const metaArr = metaObjs.map((item: any) => {
  75. const val = {
  76. id: uid(),
  77. label: item.label,
  78. confidence: Number((item.confidence * 100).toFixed(0)),
  79. min_width: item['min_width'],
  80. min_height: item['min_height'],
  81. } as any;
  82. item.nextObjs.forEach((next) => {
  83. val[`${next.label}.confidence`] = Number((next.confidence * 100).toFixed(0));
  84. val[next.label + '.' + 'min_width'] = next['min_width'];
  85. val[next.label + '.' + 'min_height'] = next['min_height'];
  86. });
  87. return val;
  88. });
  89. return metaArr;
  90. };
  91. export const getDetectionTime = (time: string | undefined | null) => {
  92. if (!time) return [];
  93. const timeList = safeParse(time);
  94. if (!timeList || (timeList && timeList.length === 0)) {
  95. return [];
  96. }
  97. return timeList;
  98. };
  99. export const getInferCode = (extra: string | undefined | null) => {
  100. if (!extra) return '';
  101. const extraObj = safeParse(extra);
  102. return extraObj?.inferCode || '';
  103. };
  104. export const getAlgoType = (extra: string | undefined | null) => {
  105. if (!extra) return 0;
  106. const extraObj = safeParse(extra);
  107. const infers = extraObj?.inferParams;
  108. if (!infers || infers.length === 0) return 0;
  109. return infers[0]?.algoType || 0;
  110. };
  111. export const getCriticalCounts = (extra: string | undefined | null) => {
  112. if (!extra) return [];
  113. const extraObj = safeParse(extra);
  114. const infers = extraObj?.inferParams;
  115. if (!infers || infers.length === 0) return [];
  116. return infers[0]?.criticalCounts || [];
  117. };
  118. export const getExtraCommonInfo = (detail: CameraAlgoItem | undefined | null) => {
  119. if (!detail) return {};
  120. let extraValue = getCommonInfoByExtra(detail.extra);
  121. if (isEqual(extraValue, {})) {
  122. extraValue = getCommonInfoByExtra(detail.algoInfo?.extra);
  123. }
  124. return extraValue;
  125. };
  126. interface CommonInfo {
  127. regionJudge?: number;
  128. judge?: number;
  129. eventDurationMinMs?: number;
  130. eventDurationMinFrames?: number;
  131. eventAlarmIntervalMs?: number;
  132. eventAlarmIntervalFrames?: number;
  133. timeWindow?: number;
  134. }
  135. // 根据算法的extra字符串转化为JSON信息
  136. const getCommonInfoByExtra = (extra: string | undefined | null): CommonInfo => {
  137. if (!extra) return {};
  138. const extraObj = safeParse(extra);
  139. const params = extraObj?.inferParams;
  140. if (!params || (params && params.length == 0)) return {};
  141. const regionJudge = params[0]?.regionJudge;
  142. const judge = params[0]?.judge;
  143. const eventDurationMinMs = params[0]?.eventDurationMinMs;
  144. const eventDurationMinFrames = params[0]?.eventDurationMinFrames;
  145. const eventAlarmIntervalMs = params[0]?.eventAlarmIntervalMs;
  146. const eventAlarmIntervalFrames = params[0]?.eventAlarmIntervalFrames;
  147. const timeWindow = params[0]?.timeWindow;
  148. const ret = {} as CommonInfo;
  149. if (regionJudge || regionJudge == 0) {
  150. ret.regionJudge = regionJudge;
  151. }
  152. if (judge || judge == 0) {
  153. ret.judge = judge;
  154. }
  155. if (eventDurationMinMs || eventDurationMinMs == 0) {
  156. ret.eventDurationMinMs = eventDurationMinMs;
  157. }
  158. if (eventDurationMinFrames || eventDurationMinFrames == 0) {
  159. ret.eventDurationMinFrames = eventDurationMinFrames;
  160. }
  161. if (eventAlarmIntervalMs || eventAlarmIntervalMs == 0) {
  162. ret.eventAlarmIntervalMs = eventAlarmIntervalMs;
  163. }
  164. if (eventAlarmIntervalFrames || eventAlarmIntervalFrames == 0) {
  165. ret.eventAlarmIntervalFrames = eventAlarmIntervalFrames;
  166. }
  167. if (timeWindow) {
  168. ret.timeWindow = timeWindow;
  169. }
  170. return ret;
  171. };
  172. export const getTimeCompletion = (time: TimePeriodItem) => {
  173. if (!time.startDay || !time.endDay) {
  174. return false;
  175. }
  176. time.timeRangeList.forEach((item) => {
  177. if (!item.startTime || !item.endTime) {
  178. return false;
  179. }
  180. });
  181. return true;
  182. };
  183. /** 生成算法相关的提交参数 */
  184. export const createAlgoSubmitParams = (param, initialAlgoDetail) => {
  185. const inferParams = getInferParam(initialAlgoDetail.extra);
  186. inferParams.metaObjs = param.metaObjs;
  187. inferParams.regionJudge = param.regionJudge;
  188. inferParams.criticalCounts = param.criticalCounts;
  189. inferParams.judge = param.judge;
  190. inferParams.eventDurationMinMs = param.eventDurationMinMs;
  191. inferParams.eventDurationMinFrames = param.eventDurationMinFrames;
  192. inferParams.eventAlarmIntervalMs = param.eventAlarmIntervalMs;
  193. inferParams.eventAlarmIntervalFrames = param.eventAlarmIntervalFrames;
  194. inferParams.algoCode = initialAlgoDetail.algoInfo.code;
  195. inferParams.algoType = getAlgoType(initialAlgoDetail.algoInfo.extra);
  196. if (param.timeWindow) {
  197. inferParams.timeWindow = param.timeWindow;
  198. }
  199. const extraValue = {
  200. inferCode: param.inferCode,
  201. inferParams: [inferParams],
  202. } as any;
  203. const newParam = {
  204. algoId: initialAlgoDetail.algoId,
  205. detectionFrequency: param.detectionFrequency,
  206. detectionTime: param.detectionTime,
  207. extra: JSON.stringify(extraValue),
  208. };
  209. return newParam;
  210. };
  211. /** 相机关联的算法转化数据结构 */
  212. export const cameraAlgoToJSON = (detail: CameraAlgoItem) => {
  213. const enableCard = detail?.status === ALGO_ENABLED_STATUS.enabled ? true : false;
  214. const electronicFenceBool = detail?.electronicFence === FENCE_ENBALED_STATUS.enabled ? true : false;
  215. // const timeRangeArr = getDetectionTimeJSON(detail?.detectionTime) || [];
  216. const timeRangeArr = getDetectionTime(detail?.detectionTime) || [];
  217. const metaValues = getMetaValues(detail?.extra) || [];
  218. const commonInfo = getExtraCommonInfo(detail);
  219. return {
  220. ...detail,
  221. algoId: detail.algoId, // 兼容算法默认参数
  222. algoInfo: detail, // 兼容算法默认参数
  223. inferCode: getInferCode(detail?.extra),
  224. enableCardBool: enableCard,
  225. electronicFenceBool,
  226. timeRangeArr,
  227. metaValues,
  228. ...commonInfoToJSON(commonInfo),
  229. };
  230. };
  231. /** 算法自身数据结构转化json */
  232. export const algoMetaToJSON = (detail: QueryAlgoInfoRes) => {
  233. const enableCard = detail?.status === ALGO_ENABLED_STATUS.enabled ? true : false;
  234. const metaValues = getMetaValues(detail?.extra) || [];
  235. const commonInfo = getCommonInfoByExtra(detail.extra);
  236. return {
  237. ...detail,
  238. algoId: detail.id, // 兼容算法默认参数
  239. algoInfo: detail, // 兼容算法默认参数
  240. inferCode: getInferCode(detail?.extra),
  241. enableCardBool: enableCard,
  242. electronicFenceBool: false,
  243. timeRangeArr: [],
  244. metaValues,
  245. ...commonInfoToJSON(commonInfo),
  246. };
  247. };
  248. const commonInfoToJSON = (commonInfo) => {
  249. return {
  250. regionJudge: commonInfo.regionJudge || 0,
  251. judge: commonInfo.judge || commonInfo.judge == 0 ? commonInfo.judge : 1,
  252. eventDurationMinMs:
  253. commonInfo.eventDurationMinMs || commonInfo.eventDurationMinMs == 0 ? commonInfo.eventDurationMinMs : 1,
  254. eventDurationMinFrames:
  255. commonInfo.eventDurationMinFrames || commonInfo.eventDurationMinFrames == 0
  256. ? commonInfo.eventDurationMinFrames
  257. : 1,
  258. eventAlarmIntervalMs:
  259. commonInfo.eventAlarmIntervalMs || commonInfo.eventAlarmIntervalMs == 0 ? commonInfo.eventAlarmIntervalMs : 1,
  260. eventAlarmIntervalFrames:
  261. commonInfo.eventAlarmIntervalFrames || commonInfo.eventAlarmIntervalFrames == 0
  262. ? commonInfo.eventAlarmIntervalFrames
  263. : 1,
  264. timeWindow: commonInfo.timeWindow,
  265. };
  266. };