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
2ad734d0
authored
Dec 12, 2023
by
汪志阳
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix:离床时间范围临界值处理
parent
bee9903b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
107 additions
and
38 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 @
2ad734d0
...
@@ -25,8 +25,7 @@ import org.springframework.stereotype.Component;
...
@@ -25,8 +25,7 @@ import org.springframework.stereotype.Component;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.text.DateFormat
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDate
;
import
java.time.*
;
import
java.time.LocalDateTime
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
...
@@ -88,60 +87,130 @@ public class OffBedAlarm implements IAlarm {
...
@@ -88,60 +87,130 @@ public class OffBedAlarm implements IAlarm {
String
deviceId
=
platDevice
.
getId
();
String
deviceId
=
platDevice
.
getId
();
List
<
LocalDateTime
>
timeRange
=
getOffTimeRange
(
ruleConfig
);
List
<
LocalDateTime
>
timeRange
=
getOffTimeRange
(
ruleConfig
);
if
(
CollUtil
.
isEmpty
(
timeRange
))
{
if
(
CollUtil
.
isEmpty
(
timeRange
))
{
log
.
warn
(
"离床告警配置时间段解析失败,config:{}"
,
JSONUtil
.
toJsonStr
(
ruleConfig
));
log
.
error
(
"离床告警配置时间段解析失败,config:{}"
,
JSONUtil
.
toJsonStr
(
ruleConfig
));
return
;
return
;
}
}
LocalDateTime
startTime
=
timeRange
.
get
(
0
);
LocalDateTime
startTime
=
timeRange
.
get
(
0
);
LocalDateTime
endTime
=
timeRange
.
get
(
1
);
LocalDateTime
endTime
=
timeRange
.
get
(
1
);
Integer
duration
=
ruleConfig
.
getDuration
();
Integer
duration
=
ruleConfig
.
getDuration
();
String
personState
=
Convert
.
toStr
(
properties
.
get
(
"person"
));
String
personState
=
Convert
.
toStr
(
properties
.
get
(
"person"
));
boolean
isOffBed
=
"0"
.
equals
(
personState
);
// 1-有人 0-无人
// 1-有人 0-无人
log
.
warn
(
"离床config:{}"
,
JSONUtil
.
toJsonStr
(
properties
));
log
.
info
(
"离床config:{}"
,
JSONUtil
.
toJsonStr
(
properties
));
if
(
"1"
.
equals
(
personState
))
{
AlarmRedisDTO
alarmRedisDTO
=
RedisUtil
.
get
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
if
(!
isOffBed
&&
alarmRedisDTO
!=
null
)
{
log
.
warn
(
"离床有人状态下,删除redis!"
);
log
.
warn
(
"离床有人状态下,删除redis!"
);
RedisUtil
.
delete
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
RedisUtil
.
delete
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
return
;
}
}
// 设备无人,在范围内上报存储redis,超过离床持续时间,则预警
// 设备无人,在范围内上报存储redis,超过离床持续时间,则预警
boolean
isInRange
=
LocalDateTime
.
now
().
isAfter
(
startTime
)
&&
LocalDateTime
.
now
().
isBefore
(
endTime
);
String
messageType
=
platAlarmCheckDTO
.
getMessageType
();
if
(
"0"
.
equals
(
personState
)
&&
isInRange
)
{
if
(
StringUtils
.
equalsAnyIgnoreCase
(
messageType
,
"OFFLINE"
,
"DISCONNECT"
))
{
log
.
warn
(
"设备下线,删除redis off_bed 预警"
);
AlarmRedisDTO
alarmRedisDTO
=
RedisUtil
.
get
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
RedisUtil
.
delete
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
if
(
alarmRedisDTO
==
null
)
{
}
Date
now
=
new
Date
();
if
(
isOffBed
)
{
long
endLong
=
now
.
getTime
();
// 离床推送redis
alarmRedisDTO
=
new
AlarmRedisDTO
();
sendToRedis
(
alarmRedisDTO
,
deviceId
);
alarmRedisDTO
.
setAlarm
(
CommonEnum
.
NO
.
getValue
());
alarmRedisDTO
.
setStart
(
now
);
alarmRedisDTO
=
RedisUtil
.
get
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
alarmRedisDTO
.
setStartLong
(
endLong
);
long
localDateLong
=
System
.
currentTimeMillis
();
RedisUtil
.
set
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
,
alarmRedisDTO
);
Long
firstOffBedLong
=
alarmRedisDTO
.
getStartLong
();
log
.
warn
(
"离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}"
,
boolean
isOverTime
=
(
localDateLong
-
firstOffBedLong
)
/
1000
>=
duration
*
60
;
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
LocalDateTime
firstOffBedTime
=
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
firstOffBedLong
),
ZoneOffset
.
of
(
"+8"
));
log
.
info
(
"第一次离床时间为:"
+
firstOffBedTime
);
// 第一次离床时间在范围前,则已范围开始时间起始算
if
(
firstOffBedTime
.
isBefore
(
startTime
))
{
boolean
over
=
(
Duration
.
between
(
LocalDateTime
.
now
(),
startTime
).
toMillis
())
/
1000
>=
duration
*
60
;
if
(
over
)
{
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
localDateLong
-
firstOffBedLong
));
log
.
info
(
"离床时间在范围前,发送预警"
);
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
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
));
log
.
info
(
"离床时间在范围时间内(小于范围结束时间-持续分钟)"
);
noticeAlarm
(
alarmRedisDTO
,
platAlarmCheckDTO
,
deviceId
);
return
;
return
;
}
}
Date
now
=
new
Date
();
long
endLong
=
now
.
getTime
();
if
(
firstOffBedTime
.
isAfter
(
endTime
))
{
Long
startLong
=
alarmRedisDTO
.
getStartLong
();
log
.
warn
(
"离床时间超出时间范围!"
);
long
count
=
endLong
-
startLong
;
// 持续时间满足、且未预警过
if
(
count
/
1000
>=
duration
*
60
&&
StringUtils
.
equals
(
alarmRedisDTO
.
getAlarm
(),
CommonEnum
.
NO
.
getValue
()))
{
log
.
warn
(
"离床持续时间:{} 秒,当前时间:{},duration:{}"
,
count
/
1000
,
now
,
duration
);
// alarmRedisDTO.setStartLong(endLong);
alarmRedisDTO
.
setStart
(
now
);
alarmRedisDTO
.
setAlarm
(
CommonEnum
.
YES
.
getValue
());
platAlarmCheckDTO
.
setAbnormalValue
(
String
.
valueOf
(
count
));
RedisUtil
.
set
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
,
alarmRedisDTO
);
log
.
warn
(
"推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}"
,
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
notice
(
platAlarmCheckDTO
);
}
}
}
}
String
messageType
=
platAlarmCheckDTO
.
getMessageType
();
if
(
StringUtils
.
equalsAnyIgnoreCase
(
messageType
,
"OFFLINE"
,
"DISCONNECT"
))
{
// if (isOffBed && isInRange) {
log
.
warn
(
"删除redisoffbed预警"
);
//
RedisUtil
.
delete
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
);
// // AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
// if (alarmRedisDTO == null) {
// Date now = new Date();
// long endLong = now.getTime();
// alarmRedisDTO = new AlarmRedisDTO();
// alarmRedisDTO.setAlarm(CommonEnum.NO.getValue());
// alarmRedisDTO.setStart(now);
// alarmRedisDTO.setStartLong(endLong);
// RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
// log.warn("离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}",
// getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
// return;
// }
// Date now = new Date();
// long endLong = now.getTime();
// Long startLong = alarmRedisDTO.getStartLong();
// long count = endLong - startLong;
// // 持续时间满足、且未预警过
// if (count / 1000 >= duration * 60 && StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.NO.getValue())) {
// log.warn("离床持续时间:{} 秒,当前时间:{},duration:{}", count / 1000, now, duration);
// // alarmRedisDTO.setStartLong(endLong);
// alarmRedisDTO.setStart(now);
// alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
// platAlarmCheckDTO.setAbnormalValue(String.valueOf(count));
// RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
// log.warn("推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}",
// getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
// notice(platAlarmCheckDTO);
// }
// }
}
private
void
sendToRedis
(
AlarmRedisDTO
alarmRedisDTO
,
String
deviceId
)
{
Date
now
=
new
Date
();
long
endLong
=
now
.
getTime
();
// 第一次上报
if
(
alarmRedisDTO
==
null
)
{
alarmRedisDTO
=
new
AlarmRedisDTO
();
alarmRedisDTO
.
setAlarm
(
CommonEnum
.
NO
.
getValue
());
alarmRedisDTO
.
setStart
(
now
);
alarmRedisDTO
.
setStartLong
(
endLong
);
RedisUtil
.
set
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
,
alarmRedisDTO
);
log
.
warn
(
"离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}"
,
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
}
else
{
alarmRedisDTO
.
setStart
(
now
);
log
.
info
(
"离床更新redis,第一次上报时间:{},更新时间:{},是否上报:{}"
,
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
RedisUtil
.
set
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
,
alarmRedisDTO
);
}
}
}
}
private
void
noticeAlarm
(
AlarmRedisDTO
alarmRedisDTO
,
PlatAlarmCheckDTO
platAlarmCheckDTO
,
String
deviceId
)
{
notice
(
platAlarmCheckDTO
);
alarmRedisDTO
.
setStart
(
new
Date
());
alarmRedisDTO
.
setAlarm
(
CommonEnum
.
YES
.
getValue
());
RedisUtil
.
set
(
RedisConst
.
ALARM_DEVICE_OFF_BED_ID
+
deviceId
,
alarmRedisDTO
);
log
.
warn
(
"推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}"
,
getDateTime
(
alarmRedisDTO
.
getStartLong
()),
alarmRedisDTO
.
getStart
(),
alarmRedisDTO
.
getAlarm
());
}
private
List
<
LocalDateTime
>
getOffTimeRange
(
PlatAlarmConfigOffBedDTOVO
ruleConfig
)
{
private
List
<
LocalDateTime
>
getOffTimeRange
(
PlatAlarmConfigOffBedDTOVO
ruleConfig
)
{
List
<
LocalDateTime
>
result
=
new
ArrayList
<>();
List
<
LocalDateTime
>
result
=
new
ArrayList
<>();
LocalDate
localDate
=
LocalDate
.
now
();
LocalDate
localDate
=
LocalDate
.
now
();
...
...
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