package com.casic.senitnel.controller; import com.alibaba.fastjson.JSONObject; import com.casic.common.CasicFrame; import com.casic.common.general.ConfigFrame; import com.casic.dao.model.BusDevice; import com.casic.dao.service.IBusDeviceService; import com.casic.senitnel.service.ISentinelFrameService; import com.casic.util.aep.AepCommandSend; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Base64; import java.util.Map; @Slf4j @RestController public class SentinelDataController { @Resource private ISentinelFrameService frameService; @Resource private IBusDeviceService deviceService; @RequestMapping("/sentinel/data/recv") public Object dataRecv(@RequestBody Map map) { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); String deviceId = (String) map.get("deviceId"); String productId = (String) map.get("productId"); JSONObject recvObj = (JSONObject) JSONObject.toJSON(map); if (recvObj.containsKey("payload")) { JSONObject payload = recvObj.getJSONObject("payload"); String value = payload.getString("APPdata"); if (value.isEmpty()) { retObj.put("resp", "payload not matched"); retObj.put("code", 200); retObj.put("success", false); } else { byte[] baseBytes = Base64.getDecoder().decode(value); String frameStr = new String(baseBytes); // 根据协议进行解析 CasicFrame frame = frameService.dataParse(frameStr); if (frame != null) { // 回复响应 String replyFrameStr = frameService.replyMessage(frame.getDeviceCode(), frame.getSequence()); // 从数据库中获取master-api-key 回复响应消息 BusDevice device = deviceService.getDeviceByCode(frame.getDeviceCode()); if (device != null && StringUtils.isNotBlank(device.getMasterApiKey())) { String masterApiKey = device.getMasterApiKey(); AepCommandSend aepCommandSend = new AepCommandSend(deviceId, productId, masterApiKey); try { int code = aepCommandSend.handleAndReply(replyFrameStr); } catch (Exception ex) { log.error("向设备回复响应异常:{}", ex.getMessage()); } } // 存库 frameService.afterAction(frame); } retObj.put("code", 200); retObj.put("success", true); } } else { retObj.put("resp", "payload not matched"); retObj.put("code", 200); retObj.put("success", false); } return retObj; } @RequestMapping("/sentinel/config/send") public Object configSend(@RequestBody Map map) throws Exception { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); CasicFrame cmdFrame = new ConfigFrame(); cmdFrame.setDeviceType("21"); // 设备类型为 燃气监测桩(防第三方破坏) cmdFrame.setDeviceCode((String) map.get("devCode")); cmdFrame.setSequence("01"); cmdFrame.setUptime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); // 当前时刻 JSONObject messageBody = new JSONObject(); messageBody.put("datas", map.get("cmdList")); cmdFrame.setMessageBody(messageBody); String frameStr = frameService.doBuildCommand(cmdFrame); // TODO-LIST // 这三个参数根据设备编号从数据库中取 String deviceId = (String) map.get("deviceId"); String productId = (String) map.get("productId"); String masterApiKey = (String) map.get("masterApiKey"); AepCommandSend aepCommandSend = new AepCommandSend(deviceId, productId, masterApiKey); int code = aepCommandSend.handleAndReply(frameStr); retObj.put("code", 200); retObj.put("success", true); retObj.put("AEPRetCode", code); return retObj; } }