package com.casic.tube.controller; import cn.hutool.core.thread.ThreadUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.casic.common.CasicFrame; import com.casic.common.general.ConfigFrame; import com.casic.dao.service.IBusDeviceService; import com.casic.tube.service.ITubeFrameService; import com.casic.util.ReturnDTO; 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 ITubeFrameService frameService; @Resource private IBusDeviceService deviceService; @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 = frameService.dataParse(frameStr); // 异步推送 ThreadUtil.execAsync(() -> { log.info("异步推送数据到接口:{}", frame.toJSON().toJSONString()); frameService.pushToApi(frame); }); // 存库 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("/tube/config/send") public Object configSend(@RequestBody Map map) throws Exception { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); if (!map.containsKey("devCode")) { return new ReturnDTO<>(500, "设备编号不能为空"); } String devCode = (String) map.get("devCode"); 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 = frameService.doBuildCommand(cmdFrame); log.info("下发指令: {}", JSON.toJSONString(frameStr)); // TODO-LIST // 不直接下发到AEP平台 暂时存入数据库 待设备上数之后通过回复的方式下发给设备 // 以下代码取消 /* // 从数据库中获取master-api-key 回复响应消息 BusDevice device = deviceService.getDeviceByCode(devCode); // 判空及抛出异常 if (ObjectUtil.isNull(device)) { return new ReturnDTO<>(500, "没有找到编号为" + devCode + "设备"); } if (ObjectUtil.isNull(device.getNbDeviceId())) { return new ReturnDTO<>(500, "NB_Device_ID为空"); } if (ObjectUtil.isNull(device.getNbProductId())) { return new ReturnDTO<>(500, "NB_Product_ID为空"); } if (ObjectUtil.isNull(device.getMasterApiKey())) { return new ReturnDTO<>(500, "masterApiKey为空"); } String deviceId = device.getNbDeviceId(); String productId = device.getNbProductId(); String masterApiKey = device.getMasterApiKey(); AepCommandSend aepCommandSend = new AepCommandSend(deviceId, productId, masterApiKey); int code = aepCommandSend.handleAndReply(frameStr); */ retObj.put("code", 200); retObj.put("success", true); return retObj; } }