Commit 5be0be8c authored by 003's avatar 003

20201013 影子服务跟设备之间做状态同步

parent be4f679a
......@@ -9,6 +9,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
/**
* 设备的全状态上报
*
......@@ -27,6 +30,7 @@ public class ServiceDevicePower implements CmdShadowService {
if ("shadowCmd".equals(id)) return;
//获取数据
JSONObject json = JSON.parseObject(bean.getData());
json.put("option_time", LocalDateTime.now().toString());
String deviceId = bean.getDeviceId();
String key = "desiredData" + deviceId;
//缓存数据
......
......@@ -7,12 +7,17 @@ import com.dubbo.client.device.DeviceCMDBean;
import com.dubbo.client.device.DeviceCmdJsonService;
import com.shadow.service.CmdShadowService;
import com.shadow.service.ParseService;
import com.shadow.util.ExpireTimeUtil;
import com.shadow.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
/**
* 设备的全状态上报
*
......@@ -32,6 +37,7 @@ public class ServiceStatusAll implements ParseService {
String deviceId = bean.getDeviceId();
String key = "desiredData" + deviceId;
String jsonString = RedisUtil.get(key);
if (null == jsonString)return;
JSONObject json = JSONObject.parseObject(jsonString);
Integer desiredPower = null == json.getInteger("power") ? 0 : json.getInteger("power");
......@@ -39,6 +45,10 @@ public class ServiceStatusAll implements ParseService {
JSONObject realJson = JSON.parseObject(bean.getData());
Integer realPower = null == realJson.getInteger("power") ? 0 : realJson.getInteger("power");
//间隔10秒以内的数据不需要处理
String optionTimeStr = json.getString("option_time");
if(ExpireTimeUtil.getIsInnerIntervalTime(optionTimeStr,10)) return;
//比较影子的状态跟设备上报状态,不一致则下发控制命令
if (!desiredPower.equals(realPower)) {
DeviceCMDBean cmd = new DeviceCMDBean();
......@@ -50,4 +60,5 @@ public class ServiceStatusAll implements ParseService {
logger.info("update shadow status " + jsonString);
}
}
}
package com.shadow.util;
import com.alibaba.fastjson.JSONObject;
import java.time.Duration;
import java.time.LocalDateTime;
public class ExpireTimeUtil {
public static void main(String[] args) {
JSONObject order = new JSONObject();
String expireTime = "2020-10-14T09:45:45.140";
order.put("expireTime", expireTime);
getLeftTime(order);
LocalDateTime a = LocalDateTime.now();
long realIntervalTime = 2l;
int intervalTime = 1;
System.out.println(Math.abs(realIntervalTime) <= Math.abs(intervalTime));
}
//获取剩余时间
public static long getLeftTime(JSONObject order){
String expireTime_s = order.getString("expireTime");
LocalDateTime expireTime = LocalDateTime.parse(expireTime_s);
Duration duration = Duration.between(LocalDateTime.now(), expireTime);
long left = duration.toMillis()/1000;
return left < 0 ? 0 : left;
}
//获取剩余时间
public static long getUsedTime(JSONObject order){
String payTime_s = order.getString("payTime");
LocalDateTime payTime = LocalDateTime.parse(payTime_s);
Duration duration = Duration.between(payTime,LocalDateTime.now());
long left = duration.toMillis()/1000;
return left < 0 ? 0 : left;
}
public static boolean getIsInnerIntervalTime(String optionTimeStr,int intervalTime){
LocalDateTime optionTime = LocalDateTime.parse(optionTimeStr);
Duration duration = Duration.between(LocalDateTime.now(), optionTime);
long realIntervalTime = duration.toMillis()/1000;
return Math.abs(realIntervalTime) <= Math.abs(intervalTime);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment