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);
|
|
JSONObject resp;
|
//说明是JSONP格式-jsonpCallback({"content":""})
|
if (responseText.indexOf("jsonpCallback") >= 0) {
|
responseText = responseText.substring(responseText.indexOf("(") + 1, responseText.length() - 1);
|
resp = JSONObject.parseObject(responseText);
|
resp.put("code", "200");
|
} else {
|
resp = JSONObject.parseObject(responseText);
|
}
|
|
if ("200".equals(resp.get("code"))) {
|
if (null == resp.get("content") || "".equals(resp.get("content"))) {
|
reqData.setWeight(0.0);
|
} else {
|
reqData.setWeight(Double.valueOf(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, "后台执行失败");
|
}
|
}
|