HikvisionController.java 14 KB

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