package com.tievd.jyz.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.tievd.cube.commons.annotations.DictApi; import com.tievd.cube.commons.base.Result; import com.tievd.jyz.constants.SystemConstant; import com.tievd.jyz.mqtt.MqttMsgReceiver; import com.tievd.jyz.mqtt.dto.SystemStatusDTO; import com.tievd.jyz.util.MultiMinioUtil; import com.tievd.jyz.util.StringCampareUtil; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest; import java.util.*; @Slf4j @DictApi @RestController @RequestMapping("/third") public class ThirdController { @Autowired MqttMsgReceiver mqttMsgReceiver; @Value("${third.aibox.url:http://25.30.13.30}") String thirdBoxUrl; @Value("${third.aibox.url:/api/v1/notify/config}") String aiboxConfig; @Autowired MultiMinioUtil s3Util; @Autowired ApplicationContext context; /** * 第三方卸油 算法code map */ static Map algCodeMap = new HashMap<>(); static { algCodeMap.put(23, "X10001"); algCodeMap.put(24, "X10002"); algCodeMap.put(25, "X10004"); algCodeMap.put(26, "X10003"); algCodeMap.put(27, "X10006"); algCodeMap.put(28, "X10005"); algCodeMap.put(29, "X10007"); algCodeMap.put(30, "X10008"); } //http://25.30.14.164:8080/cube/third/deviceHeart @PostMapping("/deviceHeart") public void deviceHeart(@RequestBody JSONObject param, HttpServletRequest request) { log.info("=====>接收到卸油aibox心跳", param.toJSONString()); param.put("type", MqttMsgReceiver.REGISTER); param.put("time", System.currentTimeMillis()); JSONObject data = new JSONObject(); data.put("deviceType", "AIbox"); data.put("version", param.get("soft_version")); data.put("name", "AIbox卸油设备"); data.put("ip", request.getRemoteHost()); data.put("port", 80); param.put("data", data); mqttMsgReceiver.dealCommandMsg(param.toJSONString()); } @SneakyThrows @PostMapping("/oilOutEvent") public void oilOutEvent(@RequestBody JSONObject thirdData) { log.info("=====>接收到卸油aibox事件上报"); JSONArray events = thirdData.getJSONArray("oil_results"); if (CollectionUtils.isEmpty(events)) { log.info("=====>接收到卸油aibox事件异常--未包含完整信息"); return; } JSONObject msgJson = new JSONObject(); msgJson.put("time", System.currentTimeMillis()); List eventList = new ArrayList(); List cameraList = new ArrayList(); for (int i = 0; i < events.size(); i++) { JSONObject event = events.getJSONObject(i); msgJson.putIfAbsent("sn", event.getString("serial_number")); try { //数据 JSONObject convertEvent = converEvent(event); log.info("=====>转换卸油事件信息: {}", convertEvent.toJSONString()); eventList.add(convertEvent); JSONObject camera = convertCamera(event); cameraList.add(camera); } catch (Exception e) { log.error("第三方数据转换异常", e); } } //上报摄像头数据-------- msgJson.put("type", MqttMsgReceiver.BASE_UPDATE); msgJson.put("data", generateCameraMsg(cameraList)); mqttMsgReceiver.dealCommandMsg(msgJson.toJSONString()); Thread.sleep(5000); //上报摄像头状态-------- msgJson.put("type", MqttMsgReceiver.HEARTBEAT); msgJson.put("data", new JSONObject(){{ put("camera", cameraList); put("system", new SystemStatusDTO()); }}); mqttMsgReceiver.dealCommandMsg(msgJson.toJSONString()); Thread.sleep(1000); //上报事件数据---------- msgJson.put("type", MqttMsgReceiver.EVENT_INFO); msgJson.put("data", eventList); mqttMsgReceiver.dealEventMsg(msgJson.toJSONString()); } /** * 摄像头数据 * @param cameraList * @return */ JSONObject generateCameraMsg(List cameraList) { JSONObject cameraMsg = new JSONObject(); cameraMsg.put("mode", "inc"); cameraMsg.put("exist", new JSONObject(){{ put("cameras", cameraList); }}); return cameraMsg; } public JSONObject convertCamera(JSONObject event) { JSONObject camera = new JSONObject(); //主要数据 camera.put("code", event.getString("IPC_serial_num")); camera.put("name", event.getString("channel_name")); camera.put("ip", event.getString("IPC_addr")); camera.put("rtsp", event.getString("IPC_addr")); camera.put("installAddress", SystemConstant.INSTALL_ADDRESS_OIL_OUT); camera.put("status", SystemConstant.DEVICE_ONLINE); camera.put("updateTime", new Date().getTime()); return camera; } /** * 事件信息 * @param event * @return */ JSONObject converEvent(JSONObject event){ JSONObject convertEvent = new JSONObject(); //主要数据 convertEvent.put("eventCode", event.getString("process_id") + event.getString("step_id")); //上传信息中无车牌号,暂时使用第三方的processId作为record id传入记录中 convertEvent.put("id", event.getString("process_id").substring(2)); convertEvent.put("eventType", event.getString("step_status")); convertEvent.put("algCode", algCodeMap.get(event.getIntValue("step_type"))); convertEvent.put("cameraCode", event.getString("IPC_serial_num")); convertEvent.put("code", event.getString("IPC_serial_num")); convertEvent.put("cameraName", event.getString("channel_name")); convertEvent.put("cameraIp", event.getString("IPC_addr")); //采集时间,UTC 格式,单位s long time = event.getLong("timestamp") + 8 * 60 * 60; convertEvent.put("alarmTime", time); //转s3地址 String imgType = event.getJSONObject("big_image").getString("ImageType"); String imgBase = event.getJSONObject("big_image").getString("Image"); byte[] imgByte = Base64.getDecoder().decode(imgBase); String parentPath = "oilout/"; String key = s3Util.upload(imgByte, parentPath + convertEvent.getString("eventCode") + "." + imgType); String url = s3Util.getBucketName() + SystemConstant.s3_split_char + key; convertEvent.put("sourceType", imgType); convertEvent.put("imgPath", url); JSONObject extend = new JSONObject(); extend.putAll(convertEvent); convertEvent.put("extend", extend); return convertEvent; } @SneakyThrows @PostMapping("/reportVideo") public void reportVideo(HttpServletRequest request) { log.info("=====>接收到卸油aibox视频上报"); // String key = s3Util.upload(file.getBytes(), file.getName()); // String url = s3Util.getBucketName() + SystemConstant.s3_split_char + key; JSONObject data = new JSONObject(); data.put("type", MqttMsgReceiver.CUT_VIDEO); //todo 缺少事件基础数据 mqttMsgReceiver.dealCommandMsg(""); } @GetMapping("/aiboxConfig") Result getAiboxConfig() { JSONObject config = new RestTemplate().getForObject(thirdBoxUrl + aiboxConfig, JSONObject.class); return Result.ok(config); } @PutMapping("/aiboxConfig") Result putAiboxConfig(JSONObject putConfig) { RestTemplate restTemplate = new RestTemplate(); restTemplate.put(thirdBoxUrl + aiboxConfig, putConfig); JSONObject config = restTemplate.getForObject(thirdBoxUrl + aiboxConfig, JSONObject.class); return Result.ok(config); } @GetMapping("/grade/{grade}") Result gradeSet(@PathVariable int grade) { StringCampareUtil.grade = grade; return Result.ok("成功"); } //调试接口 @GetMapping("/similar") Result similarGet() { return Result.ok(StringCampareUtil.similarMap); } @GetMapping("/similar/put/{sameStr}") Result similarPut(@PathVariable String sameStr) { char key = sameStr.charAt(0); StringCampareUtil.similarMap.put(key, sameStr); return Result.ok("成功"); } @PutMapping("/similar") Result similarPut(@RequestBody Map sameMap) { StringCampareUtil.similarMap.putAll(sameMap); return Result.ok(StringCampareUtil.similarMap); } }