package com.casic.tube.controller; import com.alibaba.fastjson.JSONObject; import com.casic.common.CasicFrame; import com.casic.tube.frame.ConfigFrame; import com.casic.tube.service.ITubeDataService; import com.casic.util.aep.AepCommandSend; import lombok.extern.slf4j.Slf4j; 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 TubeDataController { @Resource private ITubeDataService tubeDataService; @RequestMapping("/tube/data/recv") public Object dataRecv(@RequestBody Map map) { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); 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 = tubeDataService.dataParse(frameStr); // 存库 tubeDataService.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("/tube/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("34"); // 设备类型为 管线浓度监测设备-管盯(管线哨兵) 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 = tubeDataService.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; } }