流量计设备实时数据接口
This commit is contained in:
@ -0,0 +1,86 @@
|
|||||||
|
package com.fastbee.data.controller.userRecharge.zhanLian;
|
||||||
|
|
||||||
|
import com.fastbee.deviceData.api.devlink.service.ZhanLianConfig;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.crypto.Mac;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author mijiupro
|
||||||
|
*/
|
||||||
|
@Component("zhanlianAuthorizationService1")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class ZhanLianAuthorizationService {
|
||||||
|
|
||||||
|
private String ak = "nanyang";
|
||||||
|
|
||||||
|
private String sk = "VMSMSV4D";
|
||||||
|
|
||||||
|
private String authId = "25";
|
||||||
|
|
||||||
|
private String endpoint = "https://plat.developlink.cloud/prod-api/iot/data/forward";
|
||||||
|
|
||||||
|
public String getAuthEndpoint(){
|
||||||
|
return endpoint + getDefaultAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 鉴权参数生成
|
||||||
|
*/
|
||||||
|
public String getDefaultAuth(){
|
||||||
|
String sign;
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
String message = "ak=" + ak + "&authId=" + authId + "&time=" + time;
|
||||||
|
try {
|
||||||
|
SecretKeySpec keySpec = new SecretKeySpec(
|
||||||
|
sk.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
||||||
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
mac.init(keySpec);
|
||||||
|
byte[] rawHmac = mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
|
||||||
|
sign = DatatypeConverter.printHexBinary(rawHmac).toLowerCase();
|
||||||
|
// System.err.println("result:"+sign);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("签名生成失败! : " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return "?ak="+ak+"&time="+time+"&sign="+sign+"&authId="+authId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuth(ZhanLianConfig zhanLianConfig){
|
||||||
|
String ak = zhanLianConfig.getAk();
|
||||||
|
String sk = zhanLianConfig.getSk();
|
||||||
|
String authId = zhanLianConfig.getAuthId();
|
||||||
|
String sign;
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
String message = "ak=" + ak + "&authId=" + authId + "&time=" + time;
|
||||||
|
try {
|
||||||
|
SecretKeySpec keySpec = new SecretKeySpec(
|
||||||
|
sk.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
||||||
|
Mac mac = Mac.getInstance("HmacSHA256");
|
||||||
|
mac.init(keySpec);
|
||||||
|
byte[] rawHmac = mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
|
||||||
|
sign = DatatypeConverter.printHexBinary(rawHmac).toLowerCase();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("签名生成失败! : " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return "?ak="+ak+"&time="+time+"&sign="+sign+"&authId="+authId;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ZhanLianAuthorizationService de = new ZhanLianAuthorizationService();
|
||||||
|
System.out.println("完整请求路径:"+de.getAuthEndpoint());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
package com.fastbee.data.controller.userRecharge.zhanLian;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.hutool.json.JSONArray;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.fastbee.common.exception.ServiceException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component("zhanLianBaseService1")
|
||||||
|
public class ZhanLianBaseService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ZhanLianAuthorizationService authorizationService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基本请求
|
||||||
|
* @param body 请求负载
|
||||||
|
* @return 请求结果 data
|
||||||
|
*/
|
||||||
|
public JSONObject baseRequest(Map<String,Object> body){
|
||||||
|
String jsonStr = JSONUtil.toJsonStr(body);
|
||||||
|
String respStr = HttpUtil.post(authorizationService.getAuthEndpoint(), jsonStr);
|
||||||
|
JSONObject resp = JSONUtil.parseObj(respStr);
|
||||||
|
System.err.println(resp);
|
||||||
|
if(!resp.get("code").toString().equals("200")){
|
||||||
|
throw new ServiceException(resp.get("message").toString());
|
||||||
|
}
|
||||||
|
return resp.getJSONObject("data");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求设备实时数据
|
||||||
|
* @param deviceId 设备id
|
||||||
|
* @return 实时数据
|
||||||
|
*/
|
||||||
|
private JSONObject requestDeviceData(String deviceId) {
|
||||||
|
//构建请求体
|
||||||
|
HashMap<String, Object> body = new HashMap<>();
|
||||||
|
body.put("method","get");
|
||||||
|
body.put("path","api/v1/product/device/run_status");
|
||||||
|
Map<String,Object> params=new HashMap<>();
|
||||||
|
params.put("id", deviceId);
|
||||||
|
body.put("params",params);
|
||||||
|
return baseRequest(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据设备id请求数据并解析设备实时数据为易于存储的map键值对
|
||||||
|
* @param deviceId 设备id
|
||||||
|
* @return 实时数据
|
||||||
|
*/
|
||||||
|
public Map<String,String> getDeviceRealTimeData(String deviceId){
|
||||||
|
//解析返回数据
|
||||||
|
JSONObject data = requestDeviceData(deviceId);
|
||||||
|
//获取设备状态
|
||||||
|
JSONObject jsonObjectData = JSONUtil.parseObj(data);
|
||||||
|
Object status = jsonObjectData.get("status");
|
||||||
|
//获取设备属性以及实时数据值
|
||||||
|
Object properties = jsonObjectData.get("properties");
|
||||||
|
//获取数据上报时间
|
||||||
|
String time = jsonObjectData.get("lastOnlineTime").toString();
|
||||||
|
JSONArray propertiesArray = JSONUtil.parseArray(properties);
|
||||||
|
//获取设备各项属性实时数据
|
||||||
|
//设备属性与值建立map
|
||||||
|
Map<String, String> propertiesMap = new HashMap<>();
|
||||||
|
propertiesArray.forEach(p -> {
|
||||||
|
Object property = JSONUtil.parseObj(p).get("property");
|
||||||
|
JSONObject propertyMap = JSONUtil.parseObj(property);
|
||||||
|
//属性名称
|
||||||
|
String name = propertyMap.get("name").toString();
|
||||||
|
//属性key
|
||||||
|
String key = propertyMap.get("key").toString();
|
||||||
|
//属性值
|
||||||
|
Object value1 = JSONUtil.parseObj(p).get("value");
|
||||||
|
String value="" ;
|
||||||
|
if(value1!=null){
|
||||||
|
value = value1.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO 属性值历史数据,是否需要
|
||||||
|
Object list = JSONUtil.parseObj(p).get("list");
|
||||||
|
//建立属性-值映射关系
|
||||||
|
propertiesMap.put(key,value);
|
||||||
|
|
||||||
|
});
|
||||||
|
propertiesMap.put("status",status.toString());
|
||||||
|
propertiesMap.put("deviceId",deviceId);
|
||||||
|
propertiesMap.put("realTime",time);
|
||||||
|
return propertiesMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public BigDecimal toBigDecimalValue(String str) {
|
||||||
|
if (str == null|| str.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new BigDecimal(str);
|
||||||
|
}
|
||||||
|
public Long toLongValue(String str) {
|
||||||
|
if (str == null|| str.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Long.parseLong(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.fastbee.data.controller.userRecharge.zhanLian;
|
||||||
|
|
||||||
|
import com.fastbee.common.core.domain.AjaxResultPro;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author mijiupro
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/flow/device")
|
||||||
|
@Api(tags = "村官员信息")
|
||||||
|
public class ZhanLianFlowDeviceDataController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ZhanLianBaseService zhanLianBaseService;
|
||||||
|
@GetMapping("/realtimeData")
|
||||||
|
public AjaxResultPro realtimeData(@Param("deviceNumber") String deviceNumber) {
|
||||||
|
System.err.println(deviceNumber);
|
||||||
|
return AjaxResultPro.success(zhanLianBaseService.getDeviceRealTimeData(deviceNumber));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user