2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-21 680ff9e2cca45bcb23e373aaa221a1e1dfab2472
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.sandu.ximon.admin.controller;
 
import cn.hutool.core.util.HexUtil;
import com.github.pagehelper.util.StringUtil;
import com.sandu.common.domain.ResponseVO;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.security.annotation.AnonymousAccess;
import com.sandu.common.util.ResponseUtil;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.manager.iot.frame.A1Frame;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder;
import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A1TernaryCodeReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.MainBoardDataTransparentReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonRequest;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.InvokeParam;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A1OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.param.MQTTTestPrarm;
import com.sandu.ximon.admin.security.PermissionConfig;
import com.sandu.ximon.admin.service.PoleService;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.enums.MenuEnum;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.LinkedHashMap;
import java.util.Map;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * MQTT测试
 *
 * @author ZZQ
 * @date 2022/10/19 15:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/v1/admin/mqtt")
public class MQTTTestController {
    private PermissionConfig permissionConfig;
 
    /**
     * 模糊查询
     *
     * @return
     */
    @PostMapping("/sendRRPC")
    public ResponseVO<Object> sendRRPC(@RequestBody @Validated MQTTTestPrarm mqttTestPrarm) {
        /**
         * 测试数据
         * poleMac:32313243305005ff3867ffff
         * frameStr:/qUBAAv+AQAD//8ARZcUd7bYwso=
         */
        if (!permissionConfig.check(MenuEnum.MQTT_TEST.getCode())) {
            return ResponseUtil.fail("缺少对应用户权限");
        }
 
 
        Pole pole = SpringContextHolder.getBean(PoleService.class).getById(mqttTestPrarm.getPoleId());
        if (pole == null) {
            throw new BusinessException("灯杆不存在!");
        }
 
        if (StringUtil.isEmpty(pole.getDeviceCode())) {
            throw new BusinessException("灯杆MAC不能为空!");
        }
 
        InvokeParam param = new InvokeParam();
        param.setOperate("1001");
        param.setFrame(mqttTestPrarm.getFrameStr());
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(pole.getDeviceCode(), param);
        if (commonFrame == null || !commonFrame.isValidate()) {
            throw new BusinessException("响应数据校验失败,请重新请求数据!");
        }
        String value = commonFrame.getConnectType() + commonFrame.getFunctionCode()
                + commonFrame.getOrderType() + commonFrame.getPayloadLength()
                + commonFrame.getPayload() + commonFrame.getCrc32();
        Map map = new LinkedHashMap();
        map.put("frame", value);
        return ResponseUtil.success(map);
    }
 
    @PostMapping("/transparentFrame")
    public ResponseVO<Object> transparentFrame(@RequestBody @Validated CommonRequest commonRequest) {
        boolean isAscii = commonRequest.getAscii().equals(0) ? false : true;
        Pole pole = SpringContextHolder.getBean(PoleService.class).getById(commonRequest.getPoleID());
        if (pole == null) {
            throw new BusinessException("灯杆不存在!");
        }
        if (StringUtil.isEmpty(pole.getDeviceCode())) {
            throw new BusinessException("灯杆MAC不能为空!");
        }
 
        MainBoardDataTransparentReqInnerFrame mainBoardDataTransparentReqInnerFrame = new MainBoardDataTransparentReqInnerFrame(commonRequest.getFrame(), isAscii);
        A1Frame a1Frame = new A1Frame(A1OrderEnum.TRANSPARENT.getCode(), mainBoardDataTransparentReqInnerFrame);
        System.out.println("a1Frame:" + a1Frame.getEncodeFrame());
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(pole.getDeviceCode(), a1Frame);
 
        if (null == commonFrame) {
            throw new BusinessException("主板无响应!");
        }
        if (commonFrame == null || !commonFrame.isValidate()) {
            throw new BusinessException("响应数据校验失败,请重新请求数据!");
        }
        String value = commonFrame.getConnectType() + commonFrame.getFunctionCode()
                + commonFrame.getOrderType() + commonFrame.getPayloadLength()
                + commonFrame.getPayload() + commonFrame.getCrc32();
        Map map = new LinkedHashMap();
        map.put("frame", value);
        map.put("data", HexUtil.decodeHexStr(commonFrame.getPayload()));
        System.out.println(HexUtil.decodeHexStr(commonFrame.getPayload()));
 
        return ResponseUtil.success(map);
    }
}