Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄嘉阳
/
iot-platform-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6d3dacde
authored
Dec 15, 2023
by
汪志阳
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix:bug修复
parent
4a4bd7c4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
72 deletions
server-service/src/main/java/com/makeit/service/platform/alarm/alarmStrategy/OffBedAlarm.java
server-service/src/main/java/com/makeit/service/platform/alarm/alarmStrategy/OffBedAlarm.java
View file @
6d3dacde
package
com
.
makeit
.
service
.
platform
.
alarm
.
alarmStrategy
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.json.JSONUtil
;
import
com.alibaba.fastjson.JSONObject
;
...
...
@@ -90,15 +89,14 @@ public class OffBedAlarm implements IAlarm {
properties
=
new
JSONObject
();
}
String
deviceId
=
platDevice
.
getId
();
List
<
LocalDateTime
>
timeRange
=
getOffTimeRange
(
ruleConfig
);
if
(
CollUtil
.
isEmpty
(
timeRange
)
||
timeRange
.
get
(
0
)
==
null
||
timeRange
.
get
(
1
)
==
null
)
{
LocalTime
startTime
=
ruleConfig
.
getOffBedStart
();
LocalTime
endTime
=
ruleConfig
.
getOffBedEnd
();
if
(
startTime
==
null
||
endTime
==
null
)
{
log
.
error
(
"离床告警配置时间段解析失败,config:{}"
,
JSONUtil
.
toJsonStr
(
ruleConfig
));
return
;
}
LocalDateTime
startTime
=
timeRange
.
get
(
0
);
LocalDateTime
endTime
=
timeRange
.
get
(
1
);
log
.
info
(
"离床预警时间范围,begin:{},end:{}"
,
startTime
,
endTime
);
Integer
duration
=
ruleConfig
.
getDuration
();
String
personState
=
Convert
.
toStr
(
properties
.
get
(
"person"
));
boolean
isOffBed
=
"0"
.
equals
(
personState
);
// 1-有人 0-无人
...
...
@@ -120,45 +118,129 @@ public class OffBedAlarm implements IAlarm {
sendToRedis
(
alarmRedisDTO
,
deviceId
);
alarmRedisDTO
=
RedisUtil
.
get
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
long
localDateLong
=
System
.
currentTimeMillis
();
Long
firstOffBedLong
=
alarmRedisDTO
.
getStartLong
();
boolean
isOverTime
=
(
localDateLong
-
firstOffBedLong
)
/
1000
>=
duration
*
60
;
LocalDateTime
firstOffBedTime
=
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
firstOffBedLong
),
ZoneOffset
.
of
(
"+8"
));
log
.
info
(
"离床告警第一次离床时间为:"
+
firstOffBedTime
);
if
(
"1"
.
equals
(
alarmRedisDTO
.
getAlarm
()))
{
log
.
info
(
"离床告警已发送预警过!"
);
return
;
}
// 第一次离床时间在范围前,则已范围开始时间起始算
if
(
firstOffBedTime
.
isBefore
(
startTime
))
{
boolean
over
=
(
Math
.
abs
(
Duration
.
between
(
LocalDateTime
.
now
(),
startTime
).
toMillis
()))
/
1000
>=
duration
*
60
;
LocalDateTime
warmTime
=
startTime
.
plusMinutes
(
duration
);
if
(
over
&&
LocalDateTime
.
now
().
isAfter
(
warmTime
))
{
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
localDateLong
-
firstOffBedLong
));
platAlarmCheckDTO
.
setPlatAlarmConfig
(
config
);
log
.
info
(
"离床告警离床时间在范围前,发送预警,配置:{}"
,
config
);
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
return
;
}
// 是否跨天
boolean
isCrossDay
=
startTime
.
isAfter
(
endTime
);
if
(
isCrossDay
)
{
log
.
info
(
"离床预警跨天处理:config:{}"
,
config
.
getRuleConfig
());
handleCrossDay
(
alarmRedisDTO
,
ruleConfig
,
platAlarmCheckDTO
,
config
,
deviceId
);
}
else
{
log
.
info
(
"离床预警未跨天处理:config:{}"
,
config
.
getRuleConfig
());
handleUnCrossDay
(
alarmRedisDTO
,
ruleConfig
,
platAlarmCheckDTO
,
config
,
deviceId
);
}
}
}
private
void
handleCrossDay
(
AlarmRedisDTO
alarmRedisDTO
,
PlatAlarmConfigOffBedDTOVO
ruleConfig
,
PlatAlarmCheckDTO
platAlarmCheckDTO
,
PlatAlarmConfig
config
,
String
deviceId
)
{
Integer
duration
=
ruleConfig
.
getDuration
();
LocalTime
startTime
=
ruleConfig
.
getOffBedStart
();
LocalTime
endTime
=
ruleConfig
.
getOffBedEnd
();
Long
firstOffBedLong
=
alarmRedisDTO
.
getStartLong
();
boolean
isOverTime
=
(
System
.
currentTimeMillis
()
-
firstOffBedLong
)
/
1000
>=
duration
*
60
;
LocalDateTime
firstOffBedTime
=
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
firstOffBedLong
),
ZoneOffset
.
of
(
"+8"
));
LocalTime
firstTime
=
firstOffBedTime
.
toLocalTime
();
boolean
isInTime
=
firstTime
.
isAfter
(
startTime
)
||
firstTime
.
isBefore
(
endTime
);
LocalTime
endTimeLimit
=
endTime
.
plusMinutes
(-
duration
);
// 离床时间在范围内
if
(
isInTime
&&
isOverTime
)
{
if
(
firstTime
.
isAfter
(
endTimeLimit
))
{
log
.
info
(
"handleCrossDay 第一次离床时间,{}+持续时间:{}将超过范围,{}"
,
firstTime
,
duration
,
endTime
);
return
;
}
LocalDateTime
validEndTime
=
endTime
.
plusMinutes
(
duration
);
// 离床时间在范围时间内(小于范围结束时间-持续分钟)
boolean
isOffBedValid
=
firstOffBedTime
.
isAfter
(
startTime
)
&&
firstOffBedTime
.
isBefore
(
validEndTime
);
boolean
isInRange
=
LocalDateTime
.
now
().
isAfter
(
startTime
)
&&
LocalDateTime
.
now
().
isBefore
(
endTime
);
if
(
isOffBedValid
&&
isInRange
&&
isOverTime
)
{
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
localDateLong
-
firstOffBedLong
));
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
System
.
currentTimeMillis
()
-
firstOffBedLong
));
platAlarmCheckDTO
.
setPlatAlarmConfig
(
config
);
log
.
info
(
"cross离床告警离床时间在范围时间内,配置:{}"
,
config
.
getRuleConfig
());
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
return
;
}
LocalDateTime
now
=
LocalDateTime
.
now
();
LocalDateTime
startLocalDteTime
=
LocalDateTime
.
of
(
LocalDate
.
now
(),
startTime
);
if
(!
isInTime
)
{
long
mills
=
Duration
.
between
(
startLocalDteTime
,
now
).
toMillis
()
/
1000
;
boolean
noInOverTime
=
mills
>=
duration
*
60
;
if
(
noInOverTime
)
{
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
System
.
currentTimeMillis
()
-
firstOffBedLong
));
platAlarmCheckDTO
.
setPlatAlarmConfig
(
config
);
log
.
info
(
"
离床告警离床时间在范围时间内(小于范围结束时间-持续分钟),配置:{}"
,
config
);
log
.
info
(
"
cross离床告警第一次离床时间在范围前,配置:{}"
,
config
.
getRuleConfig
()
);
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
return
;
}
else
{
log
.
info
(
"cross离床告警,第一次离床时间在范围前,未预警,配置:{},时间持续:{},start:{}"
,
config
.
getRuleConfig
(),
mills
,
startLocalDteTime
);
}
if
(
firstOffBedTime
.
isAfter
(
endTime
))
{
log
.
info
(
"离床告警离床时间超出时间范围!"
);
}
}
else
{
log
.
info
(
"handleCrossDay 未满足预警条件!fisrttime:{},config:{}"
,
firstTime
,
config
.
getRuleConfig
());
}
}
private
void
handleUnCrossDay
(
AlarmRedisDTO
alarmRedisDTO
,
PlatAlarmConfigOffBedDTOVO
ruleConfig
,
PlatAlarmCheckDTO
platAlarmCheckDTO
,
PlatAlarmConfig
config
,
String
deviceId
)
{
Integer
duration
=
ruleConfig
.
getDuration
();
LocalTime
startTime
=
ruleConfig
.
getOffBedStart
();
LocalDateTime
startLocalDteTime
=
LocalDateTime
.
of
(
LocalDate
.
now
(),
startTime
);
LocalTime
endTime
=
ruleConfig
.
getOffBedEnd
();
LocalDateTime
endLocalDteTime
=
LocalDateTime
.
of
(
LocalDate
.
now
(),
endTime
);
Long
firstOffBedLong
=
alarmRedisDTO
.
getStartLong
();
boolean
isOverTime
=
(
System
.
currentTimeMillis
()
-
firstOffBedLong
)
/
1000
>=
duration
*
60
;
LocalDateTime
firstOffBedTime
=
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
firstOffBedLong
),
ZoneOffset
.
of
(
"+8"
));
LocalTime
firstTime
=
firstOffBedTime
.
toLocalTime
();
if
(
firstTime
.
isAfter
(
endTime
))
{
log
.
info
(
"handleUnCrossDay第一次离床时间:{},在时间范围外 config:{}"
,
firstTime
,
config
.
getRuleConfig
());
return
;
}
boolean
isInTime
=
firstTime
.
isAfter
(
startTime
)
&&
firstTime
.
isBefore
(
endTime
);
LocalTime
endTimeLimit
=
endTime
.
plusMinutes
(-
duration
);
// 离床时间在范围内
if
(
isInTime
&&
isOverTime
)
{
if
(
firstTime
.
isAfter
(
endTimeLimit
))
{
log
.
info
(
"handleUnCrossDay第一次离床时间,{}+持续时间:{}将超过范围,{}"
,
firstTime
,
duration
,
endTime
);
return
;
}
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
System
.
currentTimeMillis
()
-
firstOffBedLong
));
platAlarmCheckDTO
.
setPlatAlarmConfig
(
config
);
log
.
info
(
"uncross离床告警离床时间在范围时间内,配置:{}"
,
config
.
getRuleConfig
());
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
return
;
}
LocalDateTime
now
=
LocalDateTime
.
now
();
if
(!
isInTime
&&
now
.
isAfter
(
startLocalDteTime
)
&&
now
.
isBefore
(
endLocalDteTime
))
{
long
mills
=
Duration
.
between
(
startLocalDteTime
,
now
).
toMillis
()
/
1000
;
boolean
noInOverTime
=
mills
>=
duration
*
60
;
if
(
noInOverTime
)
{
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
System
.
currentTimeMillis
()
-
firstOffBedLong
));
platAlarmCheckDTO
.
setPlatAlarmConfig
(
config
);
log
.
info
(
"uncross离床告警第一次离床时间在范围前,配置:{}"
,
config
.
getRuleConfig
());
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
}
else
{
log
.
info
(
"uncross离床告警第一次离床时间在范围前,未预警,配置:{},时间持续:{},start:{}"
,
config
.
getRuleConfig
(),
mills
,
startLocalDteTime
);
}
}
else
{
log
.
info
(
"handleUnCrossDay 未满足预警条件!fisrttime:{},config:{}"
,
firstTime
,
config
.
getRuleConfig
());
}
}
private
void
sendToRedis
(
AlarmRedisDTO
alarmRedisDTO
,
String
deviceId
)
{
...
...
@@ -181,18 +263,6 @@ public class OffBedAlarm implements IAlarm {
}
}
public
static
void
main
(
String
[]
args
)
{
LocalDateTime
startTime
=
LocalDateTime
.
of
(
2023
,
12
,
14
,
18
,
00
);
LocalDateTime
localDateTime
=
startTime
.
plusMinutes
(
1
);
System
.
out
.
println
(
localDateTime
);
LocalDateTime
startTime2
=
LocalDateTime
.
of
(
2023
,
12
,
14
,
18
,
01
,
34
);
System
.
out
.
println
(
startTime2
.
isAfter
(
localDateTime
));
System
.
out
.
println
(
localDateTime
.
equals
(
LocalDateTime
.
now
()));
}
private
void
noticeAlarm
(
AlarmRedisDTO
alarmRedisDTO
,
PlatAlarmCheckDTO
platAlarmCheckDTO
,
String
deviceId
)
{
notice
(
platAlarmCheckDTO
);
alarmRedisDTO
.
setStart
(
new
Date
());
...
...
@@ -202,35 +272,6 @@ public class OffBedAlarm implements IAlarm {
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
}
/**
* 22-4 :昨天22-今天4 22-20:昨天22-今天20 22-23:今天22-23
* @param ruleConfig
* @return
*/
private
List
<
LocalDateTime
>
getOffTimeRange
(
PlatAlarmConfigOffBedDTOVO
ruleConfig
)
{
List
<
LocalDateTime
>
result
=
new
ArrayList
<>();
LocalTime
offBedStart
=
ruleConfig
.
getOffBedStart
();
LocalTime
offBedEnd
=
ruleConfig
.
getOffBedEnd
();
LocalDate
localDate
=
LocalDate
.
now
();
LocalDateTime
startTime
=
null
;
LocalDateTime
endTime
=
null
;
// 22-23:今天22-23
if
(
offBedStart
.
isBefore
(
offBedEnd
))
{
startTime
=
LocalDateTime
.
of
(
localDate
,
offBedStart
);
endTime
=
LocalDateTime
.
of
(
localDate
,
offBedEnd
);
}
// 22-4 :昨天22-今天4
if
(
offBedStart
.
isAfter
(
offBedEnd
))
{
startTime
=
LocalDateTime
.
of
(
localDate
.
plusDays
(-
1
),
offBedStart
);
endTime
=
LocalDateTime
.
of
(
localDate
,
offBedEnd
);
}
result
.
add
(
startTime
);
result
.
add
(
endTime
);
return
result
;
}
@Override
public
void
notice
(
PlatAlarmCheckDTO
platAlarmCheckDTO
)
{
PlatDevice
platDevice
=
platAlarmCheckDTO
.
getPlatDevice
();
...
...
@@ -251,7 +292,7 @@ public class OffBedAlarm implements IAlarm {
}
// PlatRoom platRoom = platAlarmCheckDTO.getPlatRoom();
for
(
PlatElder
platElder
:
platElderList
)
{
if
(!
platElder
.
getOrgId
().
equals
(
config
.
getOrgId
()))
{
if
(!
platElder
.
getOrgId
().
equals
(
config
.
getOrgId
()))
{
log
.
info
(
"长者不再配置组织内:长者:{},config:{}"
,
platElder
.
getName
(),
config
.
getRuleConfig
());
continue
;
}
...
...
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