|
|
|
@ -8,6 +8,7 @@ import cn.hutool.json.JSONArray;
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
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.DeviceRealtimedataMeteorology;
|
|
|
|
|
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
|
|
|
|
|
import com.fastbee.deviceData.service.impl.DeviceRealtimeDataMeteorologyServiceImpl;
|
|
|
|
@ -15,6 +16,7 @@ import com.fastbee.deviceData.service.impl.DeviceRealtimeDataWormsServiceImpl;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -36,17 +38,22 @@ public class DeviceDateTask {
|
|
|
|
|
@Autowired
|
|
|
|
|
private DeviceRealtimeDataMeteorologyServiceImpl deviceRealtimedataMeteorologyServiceImpl;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RenKeAuthorizationService renKeAuthorizationService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在执行定时任务中出现了异常会终止调度,所以需要捕获异常以便于下一轮
|
|
|
|
|
* 执行,不会影响下一次执行
|
|
|
|
|
*/
|
|
|
|
|
public void getRenkeDeviceRealtimeData() throws Exception{
|
|
|
|
|
//处理鉴权
|
|
|
|
|
String token = getAuth();
|
|
|
|
|
|
|
|
|
|
//获取设备实时数据
|
|
|
|
|
//请求参数
|
|
|
|
|
HttpResponse response = HttpRequest.get("http://api.farm.0531yun.cn/api/v2.0/entrance/device/getRealTimeData")
|
|
|
|
|
.header("token", token)
|
|
|
|
|
.header("token", renKeAuthorizationService.getToken())
|
|
|
|
|
.execute();
|
|
|
|
|
String respBodyStr = response.body();
|
|
|
|
|
if(StringUtils.isBlank(respBodyStr)) {
|
|
|
|
@ -54,60 +61,32 @@ public class DeviceDateTask {
|
|
|
|
|
}
|
|
|
|
|
JSONObject respBody = JSONUtil.parseObj(respBodyStr);
|
|
|
|
|
JSONArray realtimeList = JSONUtil.parseArray(respBody.get("data"));
|
|
|
|
|
System.err.println("设备实时数据:"+realtimeList);
|
|
|
|
|
threadPoolTaskExecutor.execute( () -> {System.err.println("设备实时数据:"+realtimeList);});
|
|
|
|
|
|
|
|
|
|
//TODO 解析实时数据保存到数据库
|
|
|
|
|
for (Object jsonObject : realtimeList){
|
|
|
|
|
JSONObject josnConversion = (JSONObject) jsonObject;
|
|
|
|
|
if (RenKeDeviceTypeConstant.WORM.equals(josnConversion.get("deviceType"))){
|
|
|
|
|
JSONObject josnArray = JSONUtil.parseObj(josnConversion.get("data"));
|
|
|
|
|
DeviceRealtimedataWorms deviceRealtimedataWorms = JSONUtil.toBean(josnArray, DeviceRealtimedataWorms.class);
|
|
|
|
|
deviceRealtimedataWorms.setCreateTime(DateTime.now());
|
|
|
|
|
if(deviceRealtimedataWormsServiceImpl.insertDeviceRealtimedataWorms(deviceRealtimedataWorms)==1){
|
|
|
|
|
System.out.println("插入成功");
|
|
|
|
|
};
|
|
|
|
|
}else if (RenKeDeviceTypeConstant.MET.equals(josnConversion.get("deviceType"))){
|
|
|
|
|
JSONArray josnArray = JSONUtil.parseArray(josnConversion.get("data"));
|
|
|
|
|
for (Object json : josnArray){
|
|
|
|
|
DeviceRealtimedataMeteorology deviceRealtimedataMeteorology = JSONUtil.toBean((JSONObject) json, DeviceRealtimedataMeteorology.class);
|
|
|
|
|
deviceRealtimedataMeteorology.setCreateTime(DateTime.now());
|
|
|
|
|
if(deviceRealtimedataMeteorologyServiceImpl.insertDeviceRealtimedataMeteorology(deviceRealtimedataMeteorology)==1){
|
|
|
|
|
System.out.println("插入成功");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// for (Object jsonObject : realtimeList){
|
|
|
|
|
// JSONObject josnConversion = (JSONObject) jsonObject;
|
|
|
|
|
// if (RenKeDeviceTypeConstant.WORM.equals(josnConversion.get("deviceType"))){
|
|
|
|
|
// JSONObject josnArray = JSONUtil.parseObj(josnConversion.get("data"));
|
|
|
|
|
// DeviceRealtimedataWorms deviceRealtimedataWorms = JSONUtil.toBean(josnArray, DeviceRealtimedataWorms.class);
|
|
|
|
|
// deviceRealtimedataWorms.setCreateTime(DateTime.now());
|
|
|
|
|
// if(deviceRealtimedataWormsServiceImpl.insertDeviceRealtimedataWorms(deviceRealtimedataWorms)==1){
|
|
|
|
|
// System.out.println("插入成功");
|
|
|
|
|
// };
|
|
|
|
|
// }else if (RenKeDeviceTypeConstant.MET.equals(josnConversion.get("deviceType"))){
|
|
|
|
|
// JSONArray josnArray = JSONUtil.parseArray(josnConversion.get("data"));
|
|
|
|
|
// for (Object json : josnArray){
|
|
|
|
|
// DeviceRealtimedataMeteorology deviceRealtimedataMeteorology = JSONUtil.toBean((JSONObject) json, DeviceRealtimedataMeteorology.class);
|
|
|
|
|
// deviceRealtimedataMeteorology.setCreateTime(DateTime.now());
|
|
|
|
|
// if(deviceRealtimedataMeteorologyServiceImpl.insertDeviceRealtimedataMeteorology(deviceRealtimedataMeteorology)==1){
|
|
|
|
|
// System.out.println("插入成功");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getAuth(){
|
|
|
|
|
//判断token是否过期
|
|
|
|
|
String cacheToken = stringRedisTemplate.opsForValue().get("rkckth:user:token");
|
|
|
|
|
if (!Objects.isNull(cacheToken)){
|
|
|
|
|
System.err.println("缓存中获取token:"+cacheToken);
|
|
|
|
|
return cacheToken;
|
|
|
|
|
}
|
|
|
|
|
//获取token
|
|
|
|
|
//构建请求体
|
|
|
|
|
Map<String,Object> reqBody =new HashMap<>();
|
|
|
|
|
reqBody.put("loginName","heilongjiang");
|
|
|
|
|
reqBody.put("loginPwd","123456");
|
|
|
|
|
|
|
|
|
|
HttpResponse response = HttpRequest.post("http://api.farm.0531yun.cn/api/v2.0/entrance/user/userLogin")
|
|
|
|
|
.body(JSONUtil.toJsonStr(reqBody)).execute();
|
|
|
|
|
System.err.println("响应:"+response.body());
|
|
|
|
|
String resultObjectStr = response.body();
|
|
|
|
|
if(StringUtils.isBlank(response.toString())){
|
|
|
|
|
throw new RuntimeException("获取token失败!");
|
|
|
|
|
}
|
|
|
|
|
JSONObject resultObject = JSONUtil.parseObj(resultObjectStr);
|
|
|
|
|
JSONObject tokenObject = JSONUtil.parseObj(resultObject.get("data"));
|
|
|
|
|
int currDate = Integer.parseInt(tokenObject.get("currDate").toString());
|
|
|
|
|
int expDate = Integer.parseInt(tokenObject.get("expDate").toString());
|
|
|
|
|
Integer periodValidity= expDate - currDate;
|
|
|
|
|
//将token存入redis
|
|
|
|
|
stringRedisTemplate.opsForValue().set("rkckth:user:token", tokenObject.get("token").toString(),2,TimeUnit.HOURS);
|
|
|
|
|
System.err.println("请求获取到token:"+tokenObject.get("token").toString());
|
|
|
|
|
return stringRedisTemplate.opsForValue().get("rkckth:user:token").toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|