Newer
Older
br-data-forwarding / src / main / java / com / casic / tube / controller / TubeDataRecvController.java
tanyue on 3 Jul 2 KB 20240703 初始提交
package com.casic.tube.controller;

import com.alibaba.fastjson.JSONObject;
import com.casic.common.CasicFrame;
import com.casic.dao.service.IDataTubeOtherService;
import com.casic.tube.service.ITubeDataService;
import com.casic.tube.service.TubeDataServiceImpl;
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 TubeDataRecvController {

    @Resource
    private ITubeDataService tubeDataService;

    @RequestMapping("/tube/data/recv")
    public String 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 = "";
            if (payload.containsKey("APPdata")) {
                value = payload.getString("APPdata");
            } else if (payload.containsKey("serviceData")) {
                JSONObject serviceData = payload.getJSONObject("serviceData");
                value = serviceData.getString("Value");
            }

            log.info(value);

            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 = tubeDataService.dataParse(frameStr);

                // 存库
                tubeDataService.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.toJSONString();
    }
}