添加获取设备实时数据定时任务等
This commit is contained in:
@ -6,6 +6,7 @@ import com.fastbee.common.annotation.Excel;
|
||||
import com.fastbee.iot.model.ThingsModelItem.*;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@ -17,8 +18,7 @@ import java.util.List;
|
||||
* @author kerwincui
|
||||
* @date 2021-12-16
|
||||
*/
|
||||
public class DeviceShortOutput
|
||||
{
|
||||
public class DeviceShortOutput implements Serializable {
|
||||
public DeviceShortOutput(){
|
||||
this.stringList=new ArrayList<>();
|
||||
this.integerList=new ArrayList<>();
|
||||
|
@ -0,0 +1,87 @@
|
||||
package com.fastbee.iot.timer;
|
||||
|
||||
import cn.hutool.http.HttpConfig;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fastbee.common.core.redis.RedisCache;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author mijiupro
|
||||
*/
|
||||
@Component("renkeDeviceDateTask")
|
||||
public class DeviceDateTask {
|
||||
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
/**
|
||||
* 在执行定时任务中出现了异常会终止调度,所以需要捕获异常以便于下一轮
|
||||
* 执行,不会影响下一次执行
|
||||
*/
|
||||
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)
|
||||
.execute();
|
||||
String respBodyStr = response.body();
|
||||
if(StringUtils.isBlank(respBodyStr)) {
|
||||
throw new RuntimeException("获取设备实时数据失败!");
|
||||
}
|
||||
JSONObject respBody = JSONUtil.parseObj(respBodyStr);
|
||||
JSONArray realtimeList = JSONUtil.parseArray(respBody.get("data"));
|
||||
System.err.println("设备实时数据:"+realtimeList);
|
||||
//TODO 解析实时数据保存到数据库
|
||||
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user