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.dao.model.BusConfig;
import com.casic.dao.model.BusDevice;
import com.casic.dao.service.IBusConfigService;
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.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.util.Base64;
import java.util.Map;

@Slf4j
@RestController
public class SentinelDataController {

    @Resource
    private ISentinelFrameService frameService;

    @Resource
    private IBusDeviceService deviceService;

    @Resource
    IBusConfigService busConfigService;

    @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) {
                    // 存库
                    frameService.afterAction(frame);

                    String devCode = frame.getDeviceCode();

                    // 回复和参数配置
                    BusDevice device = deviceService.getDeviceByCode(devCode);
                    if (ObjectUtil.isNotEmpty(device)) {
                        AepCommandSend aepCommandSend = new AepCommandSend(deviceId, productId);
                        aepCommandSend.setMasterApiKey(device.getMasterApiKey());
                        aepCommandSend.setAppKey(device.getNbAppKey());
                        aepCommandSend.setAppSecret(device.getNbAppSecret());

                        // 回复响应
                        String replyFrameStr = frameService.replyMessage(devCode, frame.getSequence());
                        try {
                            int code = aepCommandSend.handleAndReply(replyFrameStr);
                            log.info("回复响应[retCode={}]: {}", code, JSON.toJSONString(replyFrameStr));
                        } catch (Exception ex) {
                            log.error("向设备回复响应异常:[message={}], {}", ex.getMessage(), replyFrameStr);
                        }

                        // 查询数据库中是否有配置参数
                        BusConfig busConfig = busConfigService.getConfigTobeSend(device.getId());
                        if (ObjectUtil.isNotEmpty(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("/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");

        // 存入数据库
        busConfigService.saveOrUpdateConfig(devCode, new JSONObject(map).getJSONArray("cmdList"));

        retObj.put("code", 200);
        retObj.put("success", true);
        return retObj;
    }
}