Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shadow-server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ubocare
shadow-server
Commits
5be0be8c
Commit
5be0be8c
authored
Oct 14, 2020
by
003
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
20201013 影子服务跟设备之间做状态同步
parent
be4f679a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
0 deletions
+59
-0
ServiceDevicePower.java
...main/java/com/shadow/service/impl/ServiceDevicePower.java
+4
-0
ServiceStatusAll.java
src/main/java/com/shadow/service/impl/ServiceStatusAll.java
+11
-0
ExpireTimeUtil.java
src/main/java/com/shadow/util/ExpireTimeUtil.java
+44
-0
No files found.
src/main/java/com/shadow/service/impl/ServiceDevicePower.java
View file @
5be0be8c
...
@@ -9,6 +9,9 @@ import org.slf4j.Logger;
...
@@ -9,6 +9,9 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.time.LocalDateTime
;
/**
/**
* 设备的全状态上报
* 设备的全状态上报
*
*
...
@@ -27,6 +30,7 @@ public class ServiceDevicePower implements CmdShadowService {
...
@@ -27,6 +30,7 @@ public class ServiceDevicePower implements CmdShadowService {
if
(
"shadowCmd"
.
equals
(
id
))
return
;
if
(
"shadowCmd"
.
equals
(
id
))
return
;
//获取数据
//获取数据
JSONObject
json
=
JSON
.
parseObject
(
bean
.
getData
());
JSONObject
json
=
JSON
.
parseObject
(
bean
.
getData
());
json
.
put
(
"option_time"
,
LocalDateTime
.
now
().
toString
());
String
deviceId
=
bean
.
getDeviceId
();
String
deviceId
=
bean
.
getDeviceId
();
String
key
=
"desiredData"
+
deviceId
;
String
key
=
"desiredData"
+
deviceId
;
//缓存数据
//缓存数据
...
...
src/main/java/com/shadow/service/impl/ServiceStatusAll.java
View file @
5be0be8c
...
@@ -7,12 +7,17 @@ import com.dubbo.client.device.DeviceCMDBean;
...
@@ -7,12 +7,17 @@ import com.dubbo.client.device.DeviceCMDBean;
import
com.dubbo.client.device.DeviceCmdJsonService
;
import
com.dubbo.client.device.DeviceCmdJsonService
;
import
com.shadow.service.CmdShadowService
;
import
com.shadow.service.CmdShadowService
;
import
com.shadow.service.ParseService
;
import
com.shadow.service.ParseService
;
import
com.shadow.util.ExpireTimeUtil
;
import
com.shadow.util.RedisUtil
;
import
com.shadow.util.RedisUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cglib.core.Local
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.time.Duration
;
import
java.time.LocalDateTime
;
/**
/**
* 设备的全状态上报
* 设备的全状态上报
*
*
...
@@ -32,6 +37,7 @@ public class ServiceStatusAll implements ParseService {
...
@@ -32,6 +37,7 @@ public class ServiceStatusAll implements ParseService {
String
deviceId
=
bean
.
getDeviceId
();
String
deviceId
=
bean
.
getDeviceId
();
String
key
=
"desiredData"
+
deviceId
;
String
key
=
"desiredData"
+
deviceId
;
String
jsonString
=
RedisUtil
.
get
(
key
);
String
jsonString
=
RedisUtil
.
get
(
key
);
if
(
null
==
jsonString
)
return
;
JSONObject
json
=
JSONObject
.
parseObject
(
jsonString
);
JSONObject
json
=
JSONObject
.
parseObject
(
jsonString
);
Integer
desiredPower
=
null
==
json
.
getInteger
(
"power"
)
?
0
:
json
.
getInteger
(
"power"
);
Integer
desiredPower
=
null
==
json
.
getInteger
(
"power"
)
?
0
:
json
.
getInteger
(
"power"
);
...
@@ -39,6 +45,10 @@ public class ServiceStatusAll implements ParseService {
...
@@ -39,6 +45,10 @@ public class ServiceStatusAll implements ParseService {
JSONObject
realJson
=
JSON
.
parseObject
(
bean
.
getData
());
JSONObject
realJson
=
JSON
.
parseObject
(
bean
.
getData
());
Integer
realPower
=
null
==
realJson
.
getInteger
(
"power"
)
?
0
:
realJson
.
getInteger
(
"power"
);
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
))
{
if
(!
desiredPower
.
equals
(
realPower
))
{
DeviceCMDBean
cmd
=
new
DeviceCMDBean
();
DeviceCMDBean
cmd
=
new
DeviceCMDBean
();
...
@@ -50,4 +60,5 @@ public class ServiceStatusAll implements ParseService {
...
@@ -50,4 +60,5 @@ public class ServiceStatusAll implements ParseService {
logger
.
info
(
"update shadow status "
+
jsonString
);
logger
.
info
(
"update shadow status "
+
jsonString
);
}
}
}
}
}
}
src/main/java/com/shadow/util/ExpireTimeUtil.java
0 → 100644
View file @
5be0be8c
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
=
2
l
;
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment