Commit 97525de9 by 汪志阳

fix:睡眠时间计算

parent cc7b9fa8
...@@ -51,6 +51,8 @@ public class AnalysisVO { ...@@ -51,6 +51,8 @@ public class AnalysisVO {
private String createdTime; private String createdTime;
private String deviceId;
// private Integer sleepDeepMinuteCount; // 深睡每分钟体动和翻身次数 // private Integer sleepDeepMinuteCount; // 深睡每分钟体动和翻身次数
......
...@@ -56,5 +56,7 @@ public class WechatUserInfo implements Serializable { ...@@ -56,5 +56,7 @@ public class WechatUserInfo implements Serializable {
private String isTenant; private String isTenant;
private String weChatOpenid;
} }
...@@ -16,7 +16,7 @@ public interface TestLogService { ...@@ -16,7 +16,7 @@ public interface TestLogService {
void save(Map<String, List<DeviceInfoContentBreathe>> minuteMap); void save(Map<String, List<DeviceInfoContentBreathe>> minuteMap);
void saveEntity(TreeMap<String, AnalysisVO> statisticsMap); void saveEntity(TreeMap<String, AnalysisVO> statisticsMap, String id);
TreeMap<String, AnalysisVO> getData(); TreeMap<String, AnalysisVO> getData();
......
...@@ -23,7 +23,6 @@ import com.makeit.enums.report.SleepTypeEnum; ...@@ -23,7 +23,6 @@ import com.makeit.enums.report.SleepTypeEnum;
import com.makeit.global.aspect.tenant.TenantIdIgnore; import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.mapper.platform.elder.PlatElderSleepMapper; import com.makeit.mapper.platform.elder.PlatElderSleepMapper;
import com.makeit.module.iot.service.IotProductDeviceService; import com.makeit.module.iot.service.IotProductDeviceService;
import com.makeit.module.iot.vo.DeviceOperationLogEntity;
import com.makeit.module.iot.vo.analysis.*; import com.makeit.module.iot.vo.analysis.*;
import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe; import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe;
import com.makeit.service.platform.device.PlatDeviceService; import com.makeit.service.platform.device.PlatDeviceService;
...@@ -35,7 +34,6 @@ import com.makeit.service.platform.space.PlatRoomBedDeviceService; ...@@ -35,7 +34,6 @@ import com.makeit.service.platform.space.PlatRoomBedDeviceService;
import com.makeit.service.saas.SaasSleepAnalysisModelService; import com.makeit.service.saas.SaasSleepAnalysisModelService;
import com.makeit.service.saas.SaasSleepEvaluateReportService; import com.makeit.service.saas.SaasSleepEvaluateReportService;
import com.makeit.service.saas.SaasSleepEvaluateStandardReportService; import com.makeit.service.saas.SaasSleepEvaluateStandardReportService;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.data.convert.StreamUtil; import com.makeit.utils.data.convert.StreamUtil;
import com.makeit.vo.platform.elder.report.day.PlatSleepRangeVO; import com.makeit.vo.platform.elder.report.day.PlatSleepRangeVO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -47,7 +45,6 @@ import javax.annotation.Resource; ...@@ -47,7 +45,6 @@ import javax.annotation.Resource;
import java.time.*; import java.time.*;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -226,7 +223,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -226,7 +223,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
String startSleepTime = null; String startSleepTime = null;
String getUpTime1 = null; String getUpTime1 = null;
String getUpTime2 = null; String getUpTime2 = null;
String getUpTime = null; String getUpTime;
boolean isSleep = false; boolean isSleep = false;
int moveMinute = 0; int moveMinute = 0;
int minuteActionCount = 0; int minuteActionCount = 0;
...@@ -260,7 +257,16 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -260,7 +257,16 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StrUtil.isBlank(getUpTime1)) { if (StrUtil.isBlank(getUpTime1)) {
getUpTime1 = entry.getKey(); getUpTime1 = entry.getKey();
} }
LocalDateTime nowTime = LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER);
long between = Duration.between(LocalDateTime.parse(getUpTime1, DEFAULT_FORMATTER),
nowTime).toMinutes();
// 数据需要连续
if (between == moveMinute) {
moveMinute++; moveMinute++;
} else {
moveMinute = 0;
getUpTime1 = null;
}
} }
if (moveMinute > riseLeaveThreshold * 60 && StrUtil.isBlank(getUpTime)) { if (moveMinute > riseLeaveThreshold * 60 && StrUtil.isBlank(getUpTime)) {
getUpTime = entry.getKey(); getUpTime = entry.getKey();
...@@ -273,7 +279,14 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -273,7 +279,14 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StrUtil.isBlank(getUpTime2)) { if (StrUtil.isBlank(getUpTime2)) {
getUpTime2 = entry.getKey(); getUpTime2 = entry.getKey();
} }
long between = Duration.between(LocalDateTime.parse(getUpTime2, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
if (between == minuteActionCount) {
minuteActionCount++; minuteActionCount++;
} else {
minuteActionCount = 0;
getUpTime2 = null;
}
} }
if (minuteActionCount > riseActionDuration && StrUtil.isBlank(getUpTime)) { if (minuteActionCount > riseActionDuration && StrUtil.isBlank(getUpTime)) {
getUpTime = entry.getKey(); getUpTime = entry.getKey();
...@@ -291,6 +304,11 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -291,6 +304,11 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
startSleepTime = null; startSleepTime = null;
sleepMinute = 0; sleepMinute = 0;
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
getUpTime2 = null;
getUpTime1 = null;
moveMinute = 0;
minuteActionCount = 0;
getUpTime = null;
isSleep = false; isSleep = false;
} }
} }
...@@ -325,166 +343,21 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -325,166 +343,21 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
return sleepTypeMap; return sleepTypeMap;
} }
private void setGetUpTime(TreeMap<String, TreeMap<String, AnalysisVO>> sleepMap,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList,
SaasSleepAnalysisModel analysisModel) {
int riseActionDuration = Integer.parseInt(analysisModel.getRiseActionDuration());
double riseLeaveThreshold = Double.parseDouble(analysisModel.getRiseLeaveThreshold());
List<SleepTimeAnalysisVO> sleepList = sleepTimeAnalysisVOList.stream().filter(f -> f.getType().equals(SleepTypeEnum.DAY_SLEEP.getCode())).collect(Collectors.toList());
if (CollUtil.isEmpty(sleepList)) {
return;
}
sleepList.forEach(s -> {
TreeMap<String, AnalysisVO> treeMap = sleepMap.get(s.getStartTime());
if (CollUtil.isEmpty(treeMap)) {
return;
}
final String[] startGetupTime = {null};
AtomicInteger getupMinute = new AtomicInteger();
treeMap.forEach((k, analysisVO) -> {
if (analysisVO.getIsMoveBed()) {
if (StrUtil.isBlank(startGetupTime[0])) {
startGetupTime[0] = k;
}
getupMinute.getAndIncrement();
return;
}
if (StringUtils.isEmpty(startGetupTime[0]) && getupMinute.get() == 0) {
return;
}
if (getupMinute.get() > riseLeaveThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime[0]);
sleepTimeAnalysisVO.setEndTime(k);
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVO.setRemark("人离床时间时效满足起床");
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
s.setGetUpTime(startGetupTime[0]);
}
startGetupTime[0] = null;
getupMinute.set(0);
});
});
List<SleepTimeAnalysisVO> getUpList = sleepTimeAnalysisVOList.stream()
.filter(f -> SleepTypeEnum.GETUP.getCode().equals(f.getType())).collect(Collectors.toList());
sleepList.forEach(s -> {
TreeMap<String, AnalysisVO> treeMap = sleepMap.get(s.getStartTime());
if (CollUtil.isEmpty(treeMap)) {
return;
}
final String[] startGetupTime = {null};
AtomicInteger getupMinute = new AtomicInteger();
treeMap.forEach((k, analysisVO) -> {
if (analysisVO.getIsMinuteActionFlag()) {
if (StrUtil.isBlank(startGetupTime[0])) {
startGetupTime[0] = k;
}
getupMinute.getAndIncrement();
return;
}
if (StringUtils.isEmpty(startGetupTime[0]) && getupMinute.get() == 0) {
return;
}
if (getupMinute.get() > riseActionDuration && startGetupTime[0] != null) {
String finalStartGetupTime = startGetupTime[0];
boolean exist = getUpList.stream().anyMatch(f -> f.getStartTime().equals(finalStartGetupTime));
if (!exist) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime[0]);
sleepTimeAnalysisVO.setEndTime(k);
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVO.setRemark("每分钟大于30秒体动值满足起床");
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
}
if (StrUtil.isBlank(s.getGetUpTime())) {
s.setGetUpTime(startGetupTime[0]);
}
}
startGetupTime[0] = null;
getupMinute.set(0);
});
});
}
/** /**
* 获取起床数据(离床大于0.5小时,或者每分钟大于30秒体动值大于50持续10min,且在两小时内无再次入睡
* *
* @param totalMap * @param totalMap
* @param riseLeaveThreshold
* @param riseActionDuration
* @param sleepTimeAnalysisVOList
*/ */
private void getUp(TreeMap<String, AnalysisVO> totalMap, double riseLeaveThreshold, int riseActionDuration, private void countTotal(TreeMap<String, AnalysisVO> totalMap,
double repeatSleepThreshold, List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList,
SleepCountDTO sleepCountDTO) { SleepCountDTO sleepCountDTO) {
Integer actionCount = 0; Integer actionCount = 0;
Integer turnedCount = 0; Integer turnedCount = 0;
String startGetupTime = null;
int getupMinute = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) { for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
AnalysisVO analysisVO = entry.getValue(); AnalysisVO analysisVO = entry.getValue();
actionCount += analysisVO.getActionCount(); actionCount += analysisVO.getActionCount();
turnedCount += analysisVO.getTurnedCount(); turnedCount += analysisVO.getTurnedCount();
if (analysisVO.getIsMoveBed()) {
if (StrUtil.isBlank(startGetupTime)) {
startGetupTime = entry.getKey();
}
getupMinute++;
continue;
}
if (StringUtils.isEmpty(startGetupTime) && getupMinute == 0) {
continue;
}
if (getupMinute > riseLeaveThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime);
sleepTimeAnalysisVO.setEndTime(entry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
}
startGetupTime = null;
getupMinute = 0;
} }
sleepCountDTO.setTotalActionCount(actionCount); sleepCountDTO.setTotalActionCount(actionCount);
sleepCountDTO.setTotalTurnedCount(turnedCount); sleepCountDTO.setTotalTurnedCount(turnedCount);
List<SleepTimeAnalysisVO> getUpList = sleepTimeAnalysisVOList.stream()
.filter(f -> SleepTypeEnum.GETUP.getCode().equals(f.getType())).collect(Collectors.toList());
List<SleepTimeAnalysisVO> sleepList = sleepTimeAnalysisVOList.stream()
.filter(f -> SleepTypeEnum.SLEEP.getCode().equals(f.getType())).collect(Collectors.toList());
if (CollUtil.isEmpty(sleepList)) {
return;
}
String startGetupTime2 = null;
int getupMinute2 = 0;
for (Map.Entry<String, AnalysisVO> entry : totalMap.entrySet()) {
AnalysisVO analysisVO = entry.getValue();
if (analysisVO.getIsMinuteActionFlag()) {
if (StrUtil.isBlank(startGetupTime)) {
startGetupTime2 = entry.getKey();
}
getupMinute2++;
continue;
}
if (StringUtils.isEmpty(startGetupTime2) && getupMinute2 == 0) {
continue;
}
if (getupMinute2 > riseActionDuration && startGetupTime2 != null) {
String finalStartGetupTime = startGetupTime2;
boolean exist = getUpList.stream().anyMatch(f -> f.getStartTime().equals(finalStartGetupTime));
if (!exist) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
sleepTimeAnalysisVO.setStartTime(startGetupTime2);
sleepTimeAnalysisVO.setEndTime(entry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.GETUP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
}
}
startGetupTime2 = null;
getupMinute2 = 0;
}
setWakeUpTime(sleepTimeAnalysisVOList);
} }
/** /**
...@@ -505,6 +378,8 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -505,6 +378,8 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (CollUtil.isEmpty(sleepTypeMap)) { if (CollUtil.isEmpty(sleepTypeMap)) {
return; return;
} }
boolean isDeepSleep = false;
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) { for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue(); TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) { for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
...@@ -516,19 +391,33 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -516,19 +391,33 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StringUtils.isEmpty(startSleepDeepTime)) { if (StringUtils.isEmpty(startSleepDeepTime)) {
startSleepDeepTime = voEntry.getKey(); startSleepDeepTime = voEntry.getKey();
} }
long between = Duration.between(LocalDateTime.parse(startSleepDeepTime, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
if (between == sleepDeepMinute) {
sleepDeepMinute++; sleepDeepMinute++;
} else {
sleepDeepMinute = 0;
startSleepDeepTime = null;
}
continue; continue;
} }
if (StringUtils.isEmpty(startSleepDeepTime) && sleepDeepMinute == 0) { if (StringUtils.isEmpty(startSleepDeepTime) && sleepDeepMinute == 0) {
continue; continue;
} }
if (sleepDeepMinute > sleepDeepActionTimeBegin) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
if (sleepDeepMinute > sleepDeepActionTimeBegin) {
sleepTimeAnalysisVO.setStartTime(startSleepDeepTime); sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
sleepTimeAnalysisVO.setEndTime(voEntry.getKey()); // sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode()); sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
isDeepSleep = true;
} }
String endTime = sleepTimeAnalysisVO.getEndTime();
if(isDeepSleep) {
}
startSleepDeepTime = null; startSleepDeepTime = null;
sleepDeepMinute = 0; sleepDeepMinute = 0;
} }
...@@ -540,11 +429,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -540,11 +429,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) { List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
String startSleepDeepTime; String startSleepDeepTime;
int napThreshold = Integer.parseInt(analysisModel.getNapThreshold()); int napThreshold = Integer.parseInt(analysisModel.getNapThreshold());
int napDuration = Integer.parseInt(analysisModel.getNapDuration());
List<String> startTimeList = new ArrayList<>(sleepTypeMap.keySet());
List<LocalDateTime> timeList = new ArrayList<>();
startTimeList.forEach(t -> timeList.add(LocalDateTime.parse(t, DEFAULT_FORMATTER)));
TreeMap<String, TreeMap<String, AnalysisVO>> daySleepMap = new TreeMap<>(); TreeMap<String, TreeMap<String, AnalysisVO>> daySleepMap = new TreeMap<>();
TreeMap<String, TreeMap<String, AnalysisVO>> restSleepMap = new TreeMap<>(); TreeMap<String, TreeMap<String, AnalysisVO>> restSleepMap = new TreeMap<>();
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) { for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
...@@ -562,9 +447,6 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -562,9 +447,6 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
daySleepMap.put(startSleepDeepTime, sleepMap); daySleepMap.put(startSleepDeepTime, sleepMap);
} }
LocalDateTime startTime = LocalDateTime.parse(startSleepDeepTime, DEFAULT_FORMATTER);
// LocalDateTime repeatSleepTime = timeList.stream().filter(f -> Duration.between(startTime, f).toMinutes() <= napDuration * 60L).findFirst().orElse(null);
// 小憩:深度睡眠和浅度睡眠<=3小时,且2小时内无再次入睡 // 小憩:深度睡眠和浅度睡眠<=3小时,且2小时内无再次入睡
if (sleepMinute <= napThreshold * 60) { if (sleepMinute <= napThreshold * 60) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO(); SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
...@@ -572,7 +454,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -572,7 +454,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
sleepTimeAnalysisVO.setEndTime(lastEntry.getKey()); sleepTimeAnalysisVO.setEndTime(lastEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.RESTING.getCode()); sleepTimeAnalysisVO.setType(SleepTypeEnum.RESTING.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO); sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
daySleepMap.put(startSleepDeepTime, sleepMap); restSleepMap.put(startSleepDeepTime, sleepMap);
} }
} }
...@@ -603,7 +485,15 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -603,7 +485,15 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StringUtils.isEmpty(startSleepMidTime)) { if (StringUtils.isEmpty(startSleepMidTime)) {
startSleepMidTime = voEntry.getKey(); startSleepMidTime = voEntry.getKey();
} }
long between = Duration.between(LocalDateTime.parse(startSleepMidTime, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
if(between == sleepMidMinute){
sleepMidMinute++; sleepMidMinute++;
}else {
sleepMidMinute = 0;
startSleepMidTime = null;
}
continue; continue;
} }
if (StringUtils.isEmpty(startSleepMidTime) && sleepMidMinute == 0) { if (StringUtils.isEmpty(startSleepMidTime) && sleepMidMinute == 0) {
...@@ -640,7 +530,14 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -640,7 +530,14 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StringUtils.isEmpty(startAwakeTime)) { if (StringUtils.isEmpty(startAwakeTime)) {
startAwakeTime = voEntry.getKey(); startAwakeTime = voEntry.getKey();
} }
long between = Duration.between(LocalDateTime.parse(startAwakeTime, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
if (between == awakeMinute) {
awakeMinute++; awakeMinute++;
} else {
awakeMinute = 0;
startAwakeTime = null;
}
continue; continue;
} }
if (StringUtils.isEmpty(startAwakeTime) && awakeMinute == 0) { if (StringUtils.isEmpty(startAwakeTime) && awakeMinute == 0) {
...@@ -693,38 +590,6 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -693,38 +590,6 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
return sleepTimeDTO; return sleepTimeDTO;
} }
private void setWakeUpTime(List<SleepTimeAnalysisVO> list) {
if (CollUtil.isEmpty(list)) {
return;
}
List<SleepTimeAnalysisVO> getUpList = list.stream()
.filter(f -> SleepTypeEnum.GETUP.getCode().equals(f.getType())).collect(Collectors.toList());
List<SleepTimeAnalysisVO> sleepList = list.stream()
.filter(f -> SleepTypeEnum.SLEEP.getCode().equals(f.getType())).collect(Collectors.toList());
if (CollUtil.isEmpty(sleepList) || CollUtil.isEmpty(getUpList)) {
return;
}
sleepList.forEach(s -> {
List<SleepTimeAnalysisVO> wakeList = getUpList.stream().filter(f -> LocalDateTime.parse(f.getStartTime(), DEFAULT_FORMATTER)
.isAfter(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER))).collect(Collectors.toList());
// 查询最近的清醒时间,设置
if (CollUtil.isNotEmpty(wakeList)) {
final long[] minute = {24 * 60};
wakeList.forEach(w -> {
long between = Duration.between(LocalDateTime.parse(s.getStartTime(), DEFAULT_FORMATTER),
LocalDateTime.parse(w.getStartTime(), DEFAULT_FORMATTER)).toMinutes();
if (between < minute[0]) {
minute[0] = between;
s.setEndTime(w.getStartTime());
}
});
}
});
}
/** /**
* 小憩结束时间为清醒时间 * 小憩结束时间为清醒时间
* @param list * @param list
...@@ -929,7 +794,9 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -929,7 +794,9 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
for (PlatRoomBedDevice platRoomBedDevice : platRoomBedDeviceList) { for (PlatRoomBedDevice platRoomBedDevice : platRoomBedDeviceList) {
TreeMap<String, AnalysisVO> totalMap = new TreeMap<>(); TreeMap<String, AnalysisVO> totalMap = new TreeMap<>();
if (!"1732647368962203650".equals(platRoomBedDevice.getId())) {
continue;
}
String bedId = platRoomBedDevice.getBedId(); String bedId = platRoomBedDevice.getBedId();
PlatElder elder = platElderService.getOne(new QueryWrapper<PlatElder>().lambda() PlatElder elder = platElderService.getOne(new QueryWrapper<PlatElder>().lambda()
.eq(PlatElder::getBedId, bedId)); .eq(PlatElder::getBedId, bedId));
...@@ -937,42 +804,41 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -937,42 +804,41 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
continue; continue;
} }
PlatDevice platDevice = platDeviceService.getById(platRoomBedDevice.getDeviceId()); PlatDevice platDevice = platDeviceService.getById(platRoomBedDevice.getDeviceId());
if (platDevice == null || !"218A00XE2669104".equals(platDevice.getOriDeviceId())) { if (platDevice == null ) {
continue; continue;
} }
String tenantId = elder.getTenantId(); String tenantId = elder.getTenantId();
for (String hourRange : dayHourRangeList) { // for (String hourRange : dayHourRangeList) {
String[] hourRangeArray = hourRange.split("~"); // String[] hourRangeArray = hourRange.split("~");
List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]); // List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]);
if (CollUtil.isEmpty(deviceOperationLogEntities)) { // if (CollUtil.isEmpty(deviceOperationLogEntities)) {
continue; // continue;
} // }
List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream() // List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream()
.filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty")) // .filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty"))
.map(deviceOperationLogEntity -> { // .map(deviceOperationLogEntity -> {
DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class); // DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class);
assert deviceInfoContentBreathe != null; // assert deviceInfoContentBreathe != null;
deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp())); // deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp()));
return deviceInfoContentBreathe; // return deviceInfoContentBreathe;
}) // })
.collect(Collectors.toList()); // .collect(Collectors.toList());
deviceInfoContentBreatheList = deviceInfoContentBreatheList.stream().filter(f -> f.getTimestamp() >= 1704786060000L && f.getTimestamp() <= 1704814860000L).collect(Collectors.toList()); // Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime);
Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime); // // testLogService.save(minuteMap);
// testLogService.save(minuteMap); // deviceOperationLogEntities.clear();
deviceOperationLogEntities.clear(); // TreeMap<String, AnalysisVO> statisticsMap = getPerMinuteData(minuteMap, analysisModel);
TreeMap<String, AnalysisVO> statisticsMap = getPerMinuteData(minuteMap, analysisModel); // if (CollUtil.isNotEmpty(statisticsMap)) {
if (CollUtil.isNotEmpty(statisticsMap)) { // // device_minute_info
// device_minute_info // // testLogService.saveEntity(statisticsMap, platRoomBedDevice.getId());
// testLogService.saveEntity(statisticsMap); // totalMap.putAll(statisticsMap);
totalMap.putAll(statisticsMap); // }
} // }
} totalMap = testLogService.getData();
// totalMap = testLogService.getData();
if (CollUtil.isEmpty(totalMap)) { if (CollUtil.isEmpty(totalMap)) {
continue; continue;
} }
// 没有上报数据的时间,填默认值 // 没有上报数据的时间,填默认值
testLogService.fillDefaultData(totalMap); // testLogService.fillDefaultData(totalMap);
// 记录长者不同类型的睡眠时间 // 记录长者不同类型的睡眠时间
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList = Lists.newArrayList(); List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList = Lists.newArrayList();
// 入睡时间 // 入睡时间
...@@ -988,8 +854,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -988,8 +854,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
middleSleep(daySleepMap, analysisModel, sleepTimeAnalysisVOList); middleSleep(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
// 清醒 // 清醒
sober(sleepTypeMap, sleepTimeAnalysisVOList); sober(sleepTypeMap, sleepTimeAnalysisVOList);
// 起床 countTotal(totalMap, sleepCountDTO);
getUp(totalMap, riseLeaveThreshold, riseActionDuration, repeatSleepThreshold, sleepTimeAnalysisVOList, sleepCountDTO);
//浅睡 //浅睡
SleepTimeDTO sleepTimeDTO = groupSleepData(sleepTimeAnalysisVOList); SleepTimeDTO sleepTimeDTO = groupSleepData(sleepTimeAnalysisVOList);
List<PlatElderSleep> platElderSleeps = saveElderSleep(sleepTimeDTO, elder.getId(), elder.getTenantId(), currentDate); List<PlatElderSleep> platElderSleeps = saveElderSleep(sleepTimeDTO, elder.getId(), elder.getTenantId(), currentDate);
...@@ -1064,7 +929,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper, ...@@ -1064,7 +929,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
int count = 24; int count = 24;
LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
if (month != null && day != null) { if (month != null && day != null) {
localDateTime = LocalDateTime.of(LocalDate.of(2024, month, day), LocalTime.now()); localDateTime = LocalDateTime.of(LocalDate.of(2024, month, day), LocalTime.of(8,0));
} }
List<String> list = Lists.newArrayList(); List<String> list = Lists.newArrayList();
String startTime; String startTime;
......
...@@ -5,6 +5,7 @@ import cn.hutool.core.date.DatePattern; ...@@ -5,6 +5,7 @@ import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.makeit.dto.SaveLogDTO; import com.makeit.dto.SaveLogDTO;
import com.makeit.exception.BusinessException;
import com.makeit.mapper.platform.auth.PlatOrgMapper; import com.makeit.mapper.platform.auth.PlatOrgMapper;
import com.makeit.module.iot.vo.analysis.AnalysisVO; import com.makeit.module.iot.vo.analysis.AnalysisVO;
import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe; import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe;
...@@ -59,13 +60,13 @@ public class TestLogServiceImpl implements TestLogService { ...@@ -59,13 +60,13 @@ public class TestLogServiceImpl implements TestLogService {
} }
@Override @Override
public void saveEntity(TreeMap<String, AnalysisVO> statisticsMap) { public void saveEntity(TreeMap<String, AnalysisVO> statisticsMap, String id) {
List<AnalysisVO> list = new ArrayList<>(); List<AnalysisVO> list = new ArrayList<>();
statisticsMap.forEach((k,v) -> { statisticsMap.forEach((k,v) -> {
v.setCreatedTime(k); v.setCreatedTime(k);
v.setDeviceId(id);
list.add(v); list.add(v);
}); });
platOrgMapper.saveBatch(list); platOrgMapper.saveBatch(list);
} }
...@@ -74,7 +75,7 @@ public class TestLogServiceImpl implements TestLogService { ...@@ -74,7 +75,7 @@ public class TestLogServiceImpl implements TestLogService {
List<AnalysisVO> data = platOrgMapper.getData(); List<AnalysisVO> data = platOrgMapper.getData();
LinkedHashMap<String, AnalysisVO> collect1 = data.stream().collect(Collectors.toMap(AnalysisVO::getCreatedTime, a -> a, (u, v) -> { LinkedHashMap<String, AnalysisVO> collect1 = data.stream().collect(Collectors.toMap(AnalysisVO::getCreatedTime, a -> a, (u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u)); throw new BusinessException(String.format("Duplicate key %s", u));
}, LinkedHashMap::new)); }, LinkedHashMap::new));
TreeMap<String, AnalysisVO> result = new TreeMap<>(); TreeMap<String, AnalysisVO> result = new TreeMap<>();
collect1.forEach(result::put); collect1.forEach(result::put);
...@@ -119,14 +120,22 @@ public class TestLogServiceImpl implements TestLogService { ...@@ -119,14 +120,22 @@ public class TestLogServiceImpl implements TestLogService {
private AnalysisVO buildDefaultData(String minute) { private AnalysisVO buildDefaultData(String minute) {
AnalysisVO analysisVO = new AnalysisVO(); AnalysisVO analysisVO = new AnalysisVO();
analysisVO.setCreatedTime(minute); analysisVO.setTotalHr(0);
analysisVO.setActionCount(0);
analysisVO.setAvgBr(0);
analysisVO.setAvgHr(0); analysisVO.setAvgHr(0);
analysisVO.setMaxHr(0);
analysisVO.setMinHr(0);
analysisVO.setTotalBr(0);
analysisVO.setAvgBr(0);
analysisVO.setMaxBr(0);
analysisVO.setMinBr(0);
analysisVO.setHrBrCount(0);
analysisVO.setActionCount(0);
analysisVO.setTurnedCount(0);
analysisVO.setIsAction(false); analysisVO.setIsAction(false);
analysisVO.setIsMinuteActionFlag(false);
analysisVO.setIsMoveBed(true); analysisVO.setIsMoveBed(true);
analysisVO.setIsMinuteActionFlag(false);
analysisVO.setAwakeMinuteActionFlag(false); analysisVO.setAwakeMinuteActionFlag(false);
analysisVO.setCreatedTime(minute);
return analysisVO; return analysisVO;
} }
} }
...@@ -45,19 +45,20 @@ ...@@ -45,19 +45,20 @@
action_count, turned_count, is_action, is_move_bed, is_minute_action_flag, action_count, turned_count, is_action, is_move_bed, is_minute_action_flag,
awake_minute_action_flag, br_stop_threshold, br_stop, br_stop_time, br_fast_threshold, awake_minute_action_flag, br_stop_threshold, br_stop, br_stop_time, br_fast_threshold,
br_fast, br_fast_time, br_slow_threshold, br_slow, br_slow_time, hr_fast_threshold, br_fast, br_fast_time, br_slow_threshold, br_slow, br_slow_time, hr_fast_threshold,
hr_fast, hr_fast_time, hr_slow_threshold, hr_slow, hr_slow_time, created_time) hr_fast, hr_fast_time, hr_slow_threshold, hr_slow, hr_slow_time, created_time,device_id)
values values
<foreach collection="list" item="item" index="index" separator=","> <foreach collection="list" item="item" index="index" separator=",">
(#{item.totalHr},#{item.avgHr},#{item.maxHr},#{item.minHr},#{item.totalBr},#{item.avgBr},#{item.maxBr},#{item.minBr},#{item.hrBrCount}, (#{item.totalHr},#{item.avgHr},#{item.maxHr},#{item.minHr},#{item.totalBr},#{item.avgBr},#{item.maxBr},#{item.minBr},#{item.hrBrCount},
#{item.actionCount},#{item.turnedCount},#{item.isAction},#{item.isMoveBed},#{item.isMinuteActionFlag}, #{item.actionCount},#{item.turnedCount},#{item.isAction},#{item.isMoveBed},#{item.isMinuteActionFlag},
#{item.awakeMinuteActionFlag},#{item.brStopThreshold},#{item.brStop},#{item.brStopTime},#{item.brFastThreshold}, #{item.awakeMinuteActionFlag},#{item.brStopThreshold},#{item.brStop},#{item.brStopTime},#{item.brFastThreshold},
#{item.brFast},#{item.brFastTime},#{item.brSlowThreshold},#{item.brSlow},#{item.brSlowTime},#{item.hrFastThreshold}, #{item.brFast},#{item.brFastTime},#{item.brSlowThreshold},#{item.brSlow},#{item.brSlowTime},#{item.hrFastThreshold},
#{item.hrFast},#{item.hrFastTime},#{item.hrSlowThreshold},#{item.hrSlow},#{item.hrSlowTime},#{item.createdTime}) #{item.hrFast},#{item.hrFastTime},#{item.hrSlowThreshold},#{item.hrSlow},#{item.hrSlowTime},#{item.createdTime},#{item.deviceId})
</foreach> </foreach>
</insert> </insert>
<select id="getData" resultType="com.makeit.module.iot.vo.analysis.AnalysisVO"> <select id="getData" resultType="com.makeit.module.iot.vo.analysis.AnalysisVO">
select * from device_minute_info group by created_time order by created_time select * from device_minute_info where created_time >= '2024-01-09 23:40'
and created_time &lt; '2024-01-10 08:00' and device_id = '1732647368962203650' order by created_time
</select> </select>
......
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 sign in to comment