Newer
Older
br-data-forwarding / src / main / java / com / casic / senitnel / controller / SentinelDataController.java
package com.casic.senitnel.controller;

import cn.hutool.core.util.ObjectUtil;
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.BusDevice;
import com.casic.dao.service.IBusDeviceService;
import com.casic.senitnel.service.ISentinelFrameService;
import com.casic.util.ReturnDTO;
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());
                    log.info("回复响应: {}", JSON.toJSONString(replyFrameStr));

                    // 从数据库中获取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<String, Object> 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("21"); // 设备类型为 燃气监测桩(防第三方破坏)
        cmdFrame.setDeviceCode(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));

        // 从数据库中获取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);
        retObj.put("AEPRetCode", code);
        return retObj;
    }
}