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

import cn.hutool.core.thread.ThreadUtil;
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.methane.service.IMethaneFrameService;
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 MethaneDataController {

    @Resource
    private IMethaneFrameService frameService;

    @Resource
    private IBusDeviceService deviceService;

    @Resource
    IBusConfigService busConfigService;

    @RequestMapping("/methane/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);

                // 异步推送
                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("/methane/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"));

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