气象设备实时数据封装
This commit is contained in:
parent
47edc68082
commit
fb5d6c8b58
@ -12,14 +12,14 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* devLink气象设备数据服务
|
||||
*/
|
||||
@Service
|
||||
public class DevLinkMetDataService {
|
||||
public class DevLinkMetDataService extends DevLinkRealTimeDataService {
|
||||
|
||||
@Autowired
|
||||
private DevLinkAuthorizationService authorizationService;
|
||||
@ -27,122 +27,33 @@ public class DevLinkMetDataService {
|
||||
@Autowired
|
||||
private DeviceRealtimedataMeteorologyMapper deviceRealtimedataMeteorologyMapper;
|
||||
|
||||
/**
|
||||
* 获取气象设备实时数据
|
||||
* @param deviceId 设备id
|
||||
* @return 实时数据
|
||||
*/
|
||||
public boolean getMetDeviceRealData(String deviceId){
|
||||
DevLinkAuthorizationService authorization = new DevLinkAuthorizationService();
|
||||
//构建请求体
|
||||
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);
|
||||
String jsonStr = JSONUtil.toJsonStr(body);
|
||||
System.err.println(authorization.getBaseUrl() + authorization.getAuth());
|
||||
String result = HttpUtil.post(authorization.getBaseUrl() + authorization.getAuth(), jsonStr);
|
||||
// System.err.println(result);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(result);
|
||||
Object data = jsonObject.get("data");
|
||||
//获取设备状态
|
||||
JSONObject jsonObjectData = JSONUtil.parseObj(data);
|
||||
Object status = jsonObjectData.get("status");
|
||||
// System.err.println("status:"+status);
|
||||
//获取设备属性以及实时数据值
|
||||
Object properties = jsonObjectData.get("properties");
|
||||
JSONArray propertiesArray = JSONUtil.parseArray(properties);
|
||||
DeviceRealtimedataMeteorology realData = new DeviceRealtimedataMeteorology();
|
||||
//获取设备各项属性实时数据
|
||||
propertiesArray.forEach(p -> {
|
||||
|
||||
Object property = JSONUtil.parseObj(p).get("property");
|
||||
JSONObject propertyMap = JSONUtil.parseObj(property);
|
||||
// System.err.println("propertyMap:"+propertyMap);
|
||||
//属性名称
|
||||
String name = propertyMap.get("name").toString();
|
||||
//属性key
|
||||
String key = propertyMap.get("key").toString();
|
||||
//属性值
|
||||
String value = JSONUtil.parseObj(p).get("value").toString();
|
||||
//TODO 属性值历史数据,是否需要
|
||||
Object list = JSONUtil.parseObj(p).get("list");
|
||||
|
||||
// System.err.print("name:"+name);
|
||||
System.err.print("key:"+key);
|
||||
System.err.println("------------value:"+value);
|
||||
|
||||
saveRealData(key,value,realData);
|
||||
|
||||
});
|
||||
System.err.println(realData);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//数据持久化
|
||||
public void saveRealData(String key, String value, DeviceRealtimedataMeteorology realData){
|
||||
|
||||
switch (key) {
|
||||
case "airTemp":
|
||||
realData.setAirTemp(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "airHumi":
|
||||
realData.setAirHumi(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "windSpeed":
|
||||
realData.setWindSpeed(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "windDir":
|
||||
realData.setWindDir(Long.parseLong(value));
|
||||
break;
|
||||
case "airPre":
|
||||
realData.setAirPre(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "guangzhao":
|
||||
realData.setGuangzhao(Long.parseLong(value));
|
||||
break;
|
||||
case "fushe":
|
||||
realData.setFushe(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "sumRain":
|
||||
realData.setSumRain(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "insRain":
|
||||
realData.setInsRain(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "dayRain":
|
||||
realData.setDayRain(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "zhengfa":
|
||||
realData.setZhengfa(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "soilTemp":
|
||||
realData.setSoilTemp(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
case "soilHumi":
|
||||
realData.setSoilHumi(BigDecimal.valueOf(Double.parseDouble(value)));
|
||||
break;
|
||||
}
|
||||
realData.setCreateTime(DateUtils.getNowDate());
|
||||
deviceRealtimedataMeteorologyMapper.insertDeviceRealtimedataMeteorology(realData);
|
||||
}
|
||||
|
||||
//获取属性值
|
||||
public String getPropertyValue(JSONObject map, String key){
|
||||
// System.err.println(map);
|
||||
String mapKey = map.get("key").toString();
|
||||
if(mapKey.equals(key)){
|
||||
return map.get("value").toString();
|
||||
}
|
||||
return null;
|
||||
//封装数据
|
||||
public DeviceRealtimedataMeteorology setData(Map<String, Object> propertiesMap){
|
||||
DeviceRealtimedataMeteorology build = DeviceRealtimedataMeteorology.builder()
|
||||
.deviceId(Long.parseLong(propertiesMap.get("deviceId").toString()))
|
||||
.airTemp(new BigDecimal(propertiesMap.get("airTemp").toString()))
|
||||
.airHumi(new BigDecimal(propertiesMap.get("airHumi").toString()))
|
||||
.windSpeed(new BigDecimal(propertiesMap.get("windSpeed").toString()))
|
||||
.windDir(new BigDecimal(propertiesMap.get("windDir").toString()))
|
||||
.airPre(new BigDecimal(propertiesMap.get("airPre").toString()))
|
||||
.guangzhao(Long.parseLong(propertiesMap.get("guangzhao").toString()))
|
||||
.fushe(new BigDecimal(propertiesMap.get("fushe").toString()))
|
||||
.sumRain(new BigDecimal(propertiesMap.get("sumRain").toString()))
|
||||
.sumRain(new BigDecimal(propertiesMap.get("sumRain").toString()))
|
||||
.insRain(new BigDecimal(propertiesMap.get("insRain").toString()))
|
||||
.dayRain(new BigDecimal(propertiesMap.get("dayRain").toString()))
|
||||
.zhengfa(new BigDecimal(propertiesMap.get("zhengfa").toString()))
|
||||
.soilTemp(new BigDecimal(propertiesMap.get("soilTemp").toString()))
|
||||
.soilHumi(new BigDecimal(propertiesMap.get("soilHumi").toString()))
|
||||
.build();
|
||||
System.err.println(build);
|
||||
return build;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
DevLinkMetDataService service = new DevLinkMetDataService();
|
||||
service.getMetDeviceRealData("3269");
|
||||
// service.;
|
||||
DevLinkMetDataService devLinkMetDataService = new DevLinkMetDataService();
|
||||
Map<String, Object> metDeviceRealData = devLinkMetDataService.getMetDeviceRealData("3269");
|
||||
devLinkMetDataService.setData(metDeviceRealData);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
package com.fastbee.deviceData.api.devlink.service;
|
||||
|
||||
public class DevLinkMoistureDataService extends DevLinkRealTimeDataService{
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package com.fastbee.deviceData.api.devlink.service;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fastbee.common.utils.DateUtils;
|
||||
import com.fastbee.deviceData.domain.DeviceRealtimedataMeteorology;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class DevLinkRealTimeDataService {
|
||||
@Autowired
|
||||
private DevLinkAuthorizationService authorizationService;
|
||||
|
||||
|
||||
/**
|
||||
* 获取气象设备实时数据
|
||||
* @param deviceId 设备id
|
||||
* @return 实时数据
|
||||
*/
|
||||
public Map<String,Object> getMetDeviceRealData(String deviceId){
|
||||
String result = requestDeviceData(deviceId);
|
||||
//解析返回数据
|
||||
JSONObject jsonObject = JSONUtil.parseObj(result);
|
||||
Object data = jsonObject.get("data");
|
||||
//获取设备状态
|
||||
JSONObject jsonObjectData = JSONUtil.parseObj(data);
|
||||
Object status = jsonObjectData.get("status");
|
||||
// System.err.println("status:"+status);
|
||||
//获取设备属性以及实时数据值
|
||||
Object properties = jsonObjectData.get("properties");
|
||||
JSONArray propertiesArray = JSONUtil.parseArray(properties);
|
||||
DeviceRealtimedataMeteorology realData = new DeviceRealtimedataMeteorology();
|
||||
//获取设备各项属性实时数据
|
||||
//设备属性与值建立map
|
||||
Map<String, Object> propertiesMap = new HashMap<>();
|
||||
propertiesArray.forEach(p -> {
|
||||
Object property = JSONUtil.parseObj(p).get("property");
|
||||
JSONObject propertyMap = JSONUtil.parseObj(property);
|
||||
// System.err.println("propertyMap:"+propertyMap);
|
||||
//属性名称
|
||||
String name = propertyMap.get("name").toString();
|
||||
//属性key
|
||||
String key = propertyMap.get("key").toString();
|
||||
//属性值
|
||||
String value = JSONUtil.parseObj(p).get("value").toString();
|
||||
//TODO 属性值历史数据,是否需要
|
||||
Object list = JSONUtil.parseObj(p).get("list");
|
||||
// System.err.print("name:"+name);
|
||||
// System.err.print("key:"+key);
|
||||
// System.err.println("------------value:"+value);
|
||||
//建立属性-值映射关系
|
||||
propertiesMap.put(key,value);
|
||||
// saveRealData(key,value,realData);
|
||||
});
|
||||
propertiesMap.put("status",status);
|
||||
propertiesMap.put("deviceId",deviceId);
|
||||
|
||||
return propertiesMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求气象设备实时数据
|
||||
* @param deviceId 设备id
|
||||
* @return 实时数据
|
||||
*/
|
||||
private String requestDeviceData(String deviceId) {
|
||||
DevLinkAuthorizationService authorization = new DevLinkAuthorizationService();
|
||||
//构建请求体
|
||||
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);
|
||||
String jsonStr = JSONUtil.toJsonStr(body);
|
||||
System.err.println(authorization.getBaseUrl() + authorization.getAuth());
|
||||
return HttpUtil.post(authorization.getBaseUrl() + authorization.getAuth(), jsonStr);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
DevLinkRealTimeDataService devLinkRealTimeDataService = new DevLinkRealTimeDataService();
|
||||
Map<String, Object> metDeviceRealData = devLinkRealTimeDataService.getMetDeviceRealData("3269");
|
||||
System.err.println(metDeviceRealData);
|
||||
}
|
||||
}
|
@ -3,12 +3,12 @@ package com.fastbee.deviceData.domain;
|
||||
import java.math.BigDecimal;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.*;
|
||||
import org.apache.commons.lang3.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.fastbee.common.annotation.Excel;
|
||||
import com.fastbee.common.core.domain.BaseEntity;
|
||||
import org.apache.poi.hpsf.Decimal;
|
||||
|
||||
/**
|
||||
* 气象设备实时数据对象 iot_device_realtimedata_meteorology
|
||||
@ -18,6 +18,9 @@ import com.fastbee.common.core.domain.BaseEntity;
|
||||
*/
|
||||
@ApiModel(value = "DeviceRealtimedataMeteorology",description = "气象设备实时数据 iot_device_realtimedata_meteorology")
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class DeviceRealtimedataMeteorology extends BaseEntity
|
||||
{
|
||||
@ -49,7 +52,7 @@ private static final long serialVersionUID = 1L;
|
||||
/** 风向 */
|
||||
@Excel(name = "风向")
|
||||
@ApiModelProperty("风向")
|
||||
private Long windDir;
|
||||
private BigDecimal windDir;
|
||||
|
||||
/** 大气压力 */
|
||||
@Excel(name = "大气压力")
|
||||
|
@ -1,20 +1,22 @@
|
||||
package com.fastbee.deviceData.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fastbee.deviceData.service.IDeviceRealtimedataWormsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.fastbee.deviceData.mapper.DeviceRealtimedataWormsMapper;
|
||||
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
|
||||
import com.fastbee.deviceData.service.IDeviceRealtimedataWormsService;
|
||||
|
||||
|
||||
/**
|
||||
* 虫情设备实时数据Service业务层处理
|
||||
*
|
||||
* @author kerwincui
|
||||
* @date 2024-11-14
|
||||
* @date 2024-11-07
|
||||
*/
|
||||
@Service
|
||||
public class DeviceRealtimedataWormsServiceImpl implements IDeviceRealtimedataWormsService
|
||||
public class DeviceRealtimedataWormsServiceImpl implements IDeviceRealtimedataWormsService
|
||||
{
|
||||
@Autowired
|
||||
private DeviceRealtimedataWormsMapper deviceRealtimedataWormsMapper;
|
||||
@ -52,7 +54,8 @@ public class DeviceRealtimedataWormsServiceImpl implements IDeviceRealtimedataWo
|
||||
@Override
|
||||
public int insertDeviceRealtimedataWorms(DeviceRealtimedataWorms deviceRealtimedataWorms)
|
||||
{
|
||||
return deviceRealtimedataWormsMapper.insertDeviceRealtimedataWorms(deviceRealtimedataWorms);
|
||||
// return deviceRealtimedataWormsMapper.insert(deviceRealtimedataWorms);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,7 @@ import cn.hutool.json.JSONUtil;
|
||||
import com.fastbee.deviceData.api.renke.constant.RenKeDeviceTypeConstant;
|
||||
import com.fastbee.deviceData.api.renke.service.RenKeAuthorizationService;
|
||||
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
|
||||
|
||||
import com.fastbee.deviceData.service.impl.DeviceRealtimedataWormsServiceImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -22,7 +23,7 @@ import org.springframework.stereotype.Component;
|
||||
*/
|
||||
@Component("renkeDeviceDateTask")
|
||||
public class DeviceDateTask {
|
||||
|
||||
//
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
@ -66,23 +67,7 @@ public class DeviceDateTask {
|
||||
System.out.println("插入失败!");
|
||||
};
|
||||
}
|
||||
// else if (RenKeDeviceTypeConstant.MET.equals(jsonConversion.get("deviceType"))){
|
||||
// //获取设备地址
|
||||
// Object deviceAddr = jsonConversion.get("deviceAddr");
|
||||
// JSONArray jsonArray = JSONUtil.parseArray(jsonConversion.get("data"));
|
||||
//// System.err.println("气象设备数据列表:"+jsonArray);
|
||||
// for (Object json : jsonArray){
|
||||
// JSONObject jsonConversion1 = JSONUtil.parseObj(json);
|
||||
// DeviceRealtimedataMeteorology deviceRealtimedataMeteorology = JSONUtil.toBean(jsonConversion1, DeviceRealtimedataMeteorology.class);
|
||||
// deviceRealtimedataMeteorology.setCreateTime(DateTime.now());
|
||||
// deviceRealtimedataMeteorology.setDeviceaddr(deviceAddr.toString());
|
||||
//// System.err.println("序列化为气象设备数据类:"+deviceRealtimedataMeteorology);
|
||||
// if(deviceRealtimedataMeteorologyServiceImpl.insertDeviceRealtimedataMeteorology(deviceRealtimedataMeteorology)!=1){
|
||||
// System.out.println("插入失败!");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user