HikvisionController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package com.template.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.template.annotation.DESRespondSecret;
  6. import com.template.api.HikvisionAPI;
  7. import com.template.api.SmartNotificationControllerAPI;
  8. import com.template.common.utils.GetCameraPreviewURL;
  9. import com.template.common.utils.Message2;
  10. import com.template.model.pojo.SmartFaceDiscern;
  11. import com.template.model.pojo.SmartNotification;
  12. import com.template.model.pojo.SmartUser;
  13. import com.template.model.pojo.SmartWarning;
  14. import com.template.services.SmartFaceDiscernService;
  15. import com.template.services.SmartNotificationService;
  16. import com.template.services.SmartUserService;
  17. import com.template.services.SmartWarningService;
  18. import org.aspectj.weaver.ast.Var;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.io.BufferedReader;
  23. import java.io.InputStreamReader;
  24. import java.time.LocalDateTime;
  25. import java.time.format.DateTimeFormatter;
  26. import java.util.List;
  27. @RestController
  28. //返回参数加密注解
  29. @DESRespondSecret
  30. public class HikvisionController implements HikvisionAPI {
  31. @Autowired
  32. SmartWarningService smartWarningService;
  33. @Autowired
  34. SmartFaceDiscernService smartFaceDiscernService;
  35. @Autowired
  36. SmartUserService smartUserService;
  37. @Autowired
  38. SmartNotificationService smartNotificationService;
  39. /**
  40. * 事件订阅
  41. *
  42. * @param request
  43. */
  44. @Override
  45. public void subscription(HttpServletRequest request) {
  46. try {
  47. BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
  48. StringBuilder responseStrBuilder = new StringBuilder();
  49. String inputStr;
  50. while ((inputStr = streamReader.readLine()) != null) {
  51. responseStrBuilder.append(inputStr);
  52. }
  53. JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
  54. // System.out.println("jsonObject = " + jsonObject);
  55. JSONObject params = jsonObject.getJSONObject("params");
  56. JSONArray events = params.getJSONArray("events");
  57. JSONObject jsonObject1 = events.getJSONObject(0);
  58. // 类型
  59. String eventType = jsonObject1.getString("eventType");
  60. // 时间
  61. String happenTime = jsonObject1.getString("happenTime");
  62. SmartWarning smartWarning = new SmartWarning();
  63. smartWarning.setDateTime(happenTime);
  64. smartWarning.setStatu(0);
  65. JSONObject data = jsonObject1.getJSONObject("data");
  66. // 地点
  67. String srcName = jsonObject1.getString("srcName");
  68. smartWarning.setLocation(srcName);
  69. // 推送的事件
  70. String pushType = "";
  71. if ("131588".equals(eventType)) {
  72. pushType = "区域入侵";
  73. smartWarning.setType("区域入侵");
  74. JSONArray fielddetection = data.getJSONArray("fielddetection");
  75. JSONObject jsonObject2 = fielddetection.getJSONObject(0);
  76. // 图片
  77. String imageUrl = jsonObject2.getString("imageUrl");
  78. smartWarning.setImage(imageUrl);
  79. } else if ("131586".equals(eventType)) {
  80. pushType = "进入危险区域";
  81. smartWarning.setType("进入区域");
  82. JSONArray regionEntrance = data.getJSONArray("regionEntrance");
  83. JSONObject jsonObject2 = regionEntrance.getJSONObject(0);
  84. // 图片
  85. String imageUrl = jsonObject2.getString("imageUrl");
  86. smartWarning.setImage(imageUrl);
  87. } else if ("131587".equals(eventType)) {
  88. pushType = "离开安全区域";
  89. smartWarning.setType("离开区域");
  90. JSONArray regionExiting = data.getJSONArray("regionExiting");
  91. JSONObject jsonObject2 = regionExiting.getJSONObject(0);
  92. // 图片
  93. String imageUrl = jsonObject2.getString("imageUrl");
  94. smartWarning.setImage(imageUrl);
  95. } else if ("131605".equals(eventType)) {
  96. pushType = "人员倒地";
  97. smartWarning.setType("倒地");
  98. JSONArray fallDown = data.getJSONArray("fallDown");
  99. JSONObject jsonObject2 = fallDown.getJSONObject(0);
  100. // 图片
  101. String imageUrl = jsonObject2.getString("imageUrl");
  102. smartWarning.setImage(imageUrl);
  103. }
  104. smartWarningService.save(smartWarning);
  105. // 获取推送人
  106. List<SmartUser> smartUserList = smartUserService.getListPush();
  107. if (ObjectUtils.isNotEmpty(smartUserList) && smartUserList.size() > 0) {
  108. for (SmartUser smartUser : smartUserList) {
  109. SmartNotification smartNotification = new SmartNotification();
  110. smartNotification.setUserId(smartUser.getId());
  111. smartNotification.setTypeName(pushType);
  112. smartNotification.setLocation(smartWarning.getLocation());
  113. smartNotification.setImage(smartWarning.getImage());
  114. smartNotification.setDateTime(smartWarning.getDateTime());
  115. smartNotification.setType(1);
  116. String gzhOpenId = smartUser.getGzhOpenId();
  117. if (ObjectUtils.isNotEmpty(gzhOpenId)) {
  118. // 时间格式是 yyyy-MM-dd HH:mm:ss,当前时间格式有问题
  119. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  120. LocalDateTime now = LocalDateTime.now();
  121. String format = now.format(dateTimeFormatter1);
  122. // 公众号信息推送
  123. Message2.send(gzhOpenId, pushType, smartWarning.getLocation(), format);
  124. smartNotificationService.save(smartNotification);
  125. }
  126. }
  127. }
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. }
  132. /**
  133. * 人脸比对
  134. *
  135. * @param request
  136. */
  137. @Override
  138. public void faceComparison(HttpServletRequest request) {
  139. try {
  140. BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
  141. StringBuilder responseStrBuilder = new StringBuilder();
  142. String inputStr;
  143. while ((inputStr = streamReader.readLine()) != null) {
  144. responseStrBuilder.append(inputStr);
  145. }
  146. JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
  147. // System.out.println("jsonObject = " + jsonObject);
  148. JSONObject params = jsonObject.getJSONObject("params");
  149. JSONArray events = params.getJSONArray("events");
  150. JSONObject jsonObject1 = events.getJSONObject(0);
  151. // 类型
  152. String eventType = jsonObject1.getString("eventType");
  153. // 时间
  154. String happenTime = jsonObject1.getString("happenTime");
  155. SmartFaceDiscern smartFaceDiscern = new SmartFaceDiscern();
  156. smartFaceDiscern.setDateTime(happenTime);
  157. JSONObject data = jsonObject1.getJSONObject("data");
  158. // 地点
  159. String channelName = data.getString("channelName");
  160. smartFaceDiscern.setLocation(channelName);
  161. if ("131659".equals(eventType)) {
  162. smartFaceDiscern.setType("人脸比对");
  163. JSONArray alarmResult = data.getJSONArray("alarmResult");
  164. JSONObject jsonObject2 = alarmResult.getJSONObject(0);
  165. JSONObject targetAttrs = jsonObject2.getJSONObject("targetAttrs");
  166. String bkgUrl = targetAttrs.getString("bkgUrl");
  167. smartFaceDiscern.setImage(bkgUrl);
  168. // 获取人脸信息
  169. JSONArray faces = jsonObject2.getJSONArray("faces");
  170. JSONObject jsonObject3 = faces.getJSONObject(0);
  171. // 对比成功的图片
  172. JSONArray identify = jsonObject3.getJSONArray("identify");
  173. if (ObjectUtils.isNotEmpty(identify) && identify.size() > 0) {
  174. // 取对比成功的第一张
  175. JSONObject jsonObject4 = identify.getJSONObject(0);
  176. JSONArray candidate = jsonObject4.getJSONArray("candidate");
  177. JSONObject jsonObject5 = candidate.getJSONObject(0);
  178. JSONObject reserve_field = jsonObject5.getJSONObject("reserve_field");
  179. // 证件号
  180. String certificateNumber = reserve_field.getString("certificateNumber");
  181. // 名字
  182. String name = reserve_field.getString("name");
  183. smartFaceDiscern.setName(name);
  184. // todo 通过身份证查询是谁
  185. smartFaceDiscernService.save(smartFaceDiscern);
  186. }
  187. }
  188. } catch (Exception e) {
  189. e.printStackTrace();
  190. }
  191. }
  192. /**
  193. * 重点人员
  194. *
  195. * @param request
  196. */
  197. @Override
  198. public void emphasisFace(HttpServletRequest request) {
  199. try {
  200. BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
  201. StringBuilder responseStrBuilder = new StringBuilder();
  202. String inputStr;
  203. while ((inputStr = streamReader.readLine()) != null) {
  204. responseStrBuilder.append(inputStr);
  205. }
  206. JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
  207. // System.out.println("jsonObject = " + jsonObject);
  208. JSONObject params = jsonObject.getJSONObject("params");
  209. JSONArray events = params.getJSONArray("events");
  210. JSONObject jsonObject1 = events.getJSONObject(0);
  211. // 类型
  212. String eventType = jsonObject1.getString("eventType");
  213. // 时间
  214. String happenTime = jsonObject1.getString("happenTime");
  215. SmartWarning smartWarning = new SmartWarning();
  216. smartWarning.setDateTime(happenTime);
  217. smartWarning.setStatu(0);
  218. JSONObject data = jsonObject1.getJSONObject("data");
  219. // 获取地点
  220. JSONArray resInfo = data.getJSONArray("resInfo");
  221. JSONObject jsonObject3 = resInfo.getJSONObject(0);
  222. String cn = jsonObject3.getString("cn");
  223. smartWarning.setLocation(cn);
  224. if ("1644175361".equals(eventType)) {
  225. smartWarning.setType("重点人员识别");
  226. JSONObject faceRecognitionResult = data.getJSONObject("faceRecognitionResult");
  227. // 获取图片
  228. JSONObject snap = faceRecognitionResult.getJSONObject("snap");
  229. String bkgUrl = snap.getString("bkgUrl");
  230. smartWarning.setImage(bkgUrl);
  231. // 获取重点人员身份
  232. JSONArray faceMatch = faceRecognitionResult.getJSONArray("faceMatch");
  233. JSONObject jsonObject2 = faceMatch.getJSONObject(0);
  234. // 名字
  235. String faceInfoName = jsonObject2.getString("faceInfoName");
  236. smartWarning.setWarningName(faceInfoName);
  237. // 证件号码
  238. String certificate = jsonObject2.getString("certificate");
  239. smartWarningService.save(smartWarning);
  240. // 获取推送人
  241. List<SmartUser> smartUserList = smartUserService.getListPush();
  242. String pushType = "危险人员";
  243. for (SmartUser smartUser : smartUserList) {
  244. String gzhOpenId = smartUser.getGzhOpenId();
  245. if (ObjectUtils.isNotEmpty(gzhOpenId)) {
  246. SmartNotification smartNotification = new SmartNotification();
  247. smartNotification.setUserId(smartUser.getId());
  248. smartNotification.setUserName(faceInfoName);
  249. smartNotification.setTypeName(pushType);
  250. smartNotification.setLocation(smartWarning.getLocation());
  251. smartNotification.setImage(smartWarning.getImage());
  252. smartNotification.setDateTime(smartWarning.getDateTime());
  253. smartNotification.setType(1);
  254. // 时间格式是 yyyy-MM-dd HH:mm:ss,当前时间格式有问题
  255. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  256. LocalDateTime now = LocalDateTime.now();
  257. String format = now.format(dateTimeFormatter1);
  258. // 公众号信息推送
  259. Message2.send(gzhOpenId, pushType, smartWarning.getLocation(), format);
  260. smartNotificationService.save(smartNotification);
  261. }
  262. }
  263. }
  264. } catch (Exception e) {
  265. e.printStackTrace();
  266. }
  267. }
  268. @Override
  269. public JSONObject playback() {
  270. String url = "/api/video/v2/cameras/playbackURLs";
  271. JSONObject jsonBoxdy = new JSONObject();
  272. jsonBoxdy.put("cameraIndexCode", "ff2a2978a71c4ba0ac43a1a6be97a1a1");
  273. jsonBoxdy.put("streamType", 0);
  274. jsonBoxdy.put("protocol", "rtsp");
  275. jsonBoxdy.put("transmode", 1);
  276. String s = GetCameraPreviewURL.CameraPreviewURL(url, jsonBoxdy);
  277. JSONObject jsonObject1 = JSONObject.parseObject(s);
  278. return jsonObject1;
  279. }
  280. @Override
  281. public JSONObject general(JSONObject jsonObject) {
  282. String url = jsonObject.getString("url");
  283. JSONObject jsonBoxdy = jsonObject.getJSONObject("jsonBoxdy");
  284. String s = GetCameraPreviewURL.CameraPreviewURL(url, jsonBoxdy);
  285. JSONObject jsonObject1 = JSONObject.parseObject(s);
  286. return jsonObject1;
  287. }
  288. }