package com.casic.tube.controller; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; 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.model.BusConfig; import com.casic.dao.model.BusDevice; import com.casic.dao.service.IBusConfigService; import com.casic.dao.service.IBusDeviceService; import com.casic.tube.service.ITubeFrameService; import com.casic.util.ReturnDTO; 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 ITubeFrameService frameService; @Resource private IBusDeviceService deviceService; @Resource IBusConfigService busConfigService; @RequestMapping("/tube/data/recv") public Object dataRecv(@RequestBody Map map) { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); String deviceId = StrUtil.toString(map.get("deviceId")); String productId = StrUtil.toString(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 (ObjectUtil.isNotNull(frame)) { // 异步推送 ThreadUtil.execAsync(() -> { log.info("异步推送数据到接口:{}", frame.toJSON().toJSONString()); frameService.pushToApi(frame); }); // 存库 frameService.afterAction(frame); String devCode = frame.getDeviceCode(); // 回复和参数配置 BusDevice device = deviceService.getDeviceByCode(devCode); if (ObjectUtil.isNotNull(device)) { AepCommandSend aepCommandSend = new AepCommandSend(deviceId, productId); aepCommandSend.setMasterApiKey(device.getMasterApiKey()); aepCommandSend.setAppKey(device.getNbAppKey()); aepCommandSend.setAppSecret(device.getNbAppSecret()); // 查询数据库中是否有配置参数 BusConfig busConfig = busConfigService.getConfigTobeSend(device.getId()); if (ObjectUtil.isNotNull(busConfig)) { String configCmdStr = frameService.doBuildCommand(devCode, busConfig.getFrameContent()); try { int code = aepCommandSend.handleAndReply(configCmdStr); log.info("下发参数配置[retCode={}]: {}", code, JSON.toJSONString(configCmdStr)); if (code == 0) { busConfigService.updateConfigState(device.getId(), "1"); } else { busConfigService.updateConfigState(device.getId(), "2"); } } catch (Exception ex) { log.error("向设备下发参数配置异常:[message={}], {}", ex.getMessage(), configCmdStr); busConfigService.updateConfigState(device.getId(), "2"); } } } } 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"); // 存入数据库 busConfigService.saveOrUpdateConfig(devCode, new JSONObject(map).getJSONArray("cmdList")); // 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; } }