jiazx0107@163.com
2023-11-18 328eba66ddc6fdf2f324b9cd04cd6acec9f642de
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
package com.fzzy.protocol.weightyh;
 
import com.alibaba.fastjson2.JSONObject;
import com.fzzy.api.data.GatewayDeviceProtocol;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.api.GatewaySyncWeightService;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.data.BaseResp;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.gateway.util.GatewayHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
 
/**
 * HTTP方式获取地磅信息
 * <p>
 * 注:当前协议包含JSON和JSONP两种支持,自动识别
 */
@Slf4j
@Component
public class GatewaySyncWeightImplHttp implements GatewaySyncWeightService {
 
 
    @Resource
    private GatewayRemoteManager gatewayRemoteManager;
 
    @Override
    public String getWeightProtocol() {
        return GatewayDeviceProtocol.DEVICE_WEIGHT_HTTP.getCode();
    }
 
    @Override
    public BaseResp syncWeightInfo(BaseReqData reqData) {
        GatewayDevice device = reqData.getDevice();
 
        if (null == device) return new BaseResp(500, "没有获取到设备信息");
 
        if (StringUtils.isEmpty(device.getHttpUrl())) new BaseResp(500, "没有配置HTTP请求地址");
        try {
 
            Map<String, String> paramsMap = new HashMap<>();
            paramsMap.put("time_", System.currentTimeMillis() + "");
 
            String responseText = GatewayHttpUtil.doGet(device.getHttpUrl(), paramsMap);
 
            log.debug("--HTTP-WEIGHT-请求返回--{}", responseText);
 
            String respJson;
            JSONObject resp;
            //说明是JSONP格式-jsonpCallback({"content":""})
            if (responseText.indexOf("jsonpCallback") >= 0) {
                respJson = responseText.substring(responseText.indexOf("(") + 1, responseText.length() - 1);
                resp = JSONObject.parseObject(respJson);
                resp.put("code", 200);
            } else {
                respJson = responseText;
                resp = JSONObject.parseObject(respJson);
            }
 
            if (200 == (Integer) resp.get("code")) {
                if (null == resp.get("content") || "".equals(resp.get("content"))) {
                    reqData.setWeight(0.0);
                } else {
                    reqData.setWeight((Double) resp.get("content"));
                }
            }
 
            //执行推送
            if (reqData.getWeight() > 0) {
                gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData);
            }
        } catch (Exception e) {
            log.error("--------------地磅-HTTP协议执行异常----{}", e.getMessage());
        }
        return new BaseResp(500, "后台执行失败");
    }
}