Commit 4183b965 by 汪志阳

fix:睡眠时间计算

parent 97525de9
......@@ -12,7 +12,7 @@ import java.util.TreeMap;
* @description
* @createDate 2024-01-24-16:38
*/
public interface TestLogService {
public interface DeviceLogService {
void save(Map<String, List<DeviceInfoContentBreathe>> minuteMap);
......@@ -20,5 +20,5 @@ public interface TestLogService {
TreeMap<String, AnalysisVO> getData();
void fillDefaultData(TreeMap<String, AnalysisVO> statisticsMap);
void fillDefaultData(TreeMap<String, AnalysisVO> statisticsMap,Integer month, Integer day);
}
......@@ -9,7 +9,7 @@ import com.makeit.exception.BusinessException;
import com.makeit.mapper.platform.auth.PlatOrgMapper;
import com.makeit.module.iot.vo.analysis.AnalysisVO;
import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe;
import com.makeit.service.platform.elder.TestLogService;
import com.makeit.service.platform.elder.DeviceLogService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -26,7 +26,7 @@ import java.util.stream.Collectors;
* @createDate 2024-01-24-16:38
*/
@Service
public class TestLogServiceImpl implements TestLogService {
public class DeviceLogServiceImpl implements DeviceLogService {
@Resource
private PlatOrgMapper platOrgMapper;
......@@ -80,7 +80,7 @@ public class TestLogServiceImpl implements TestLogService {
TreeMap<String, AnalysisVO> result = new TreeMap<>();
collect1.forEach(result::put);
getDayMinute().forEach(minute -> {
getDayMinute(1,10).forEach(minute -> {
boolean key = result.containsKey(minute);
if (!key) {
AnalysisVO analysisVO = buildDefaultData(minute);
......@@ -91,9 +91,9 @@ public class TestLogServiceImpl implements TestLogService {
}
public static List<String> getDayMinute() {
public static List<String> getDayMinute(Integer month, Integer day) {
List<String> list = Lists.newArrayList();
LocalDateTime now = LocalDateTime.of(2024, 1, 9, 8, 0);
LocalDateTime now = LocalDateTime.of(2024, month, day - 1, 8, 0);
for (int i = 0; i < 24; i++) {
int hour = now.getHour();
for (int j = 0; j < 60; j++) {
......@@ -107,8 +107,8 @@ public class TestLogServiceImpl implements TestLogService {
@Override
public void fillDefaultData(TreeMap<String, AnalysisVO> statisticsMap) {
List<String> dayMinute = getDayMinute();
public void fillDefaultData(TreeMap<String, AnalysisVO> statisticsMap, Integer month, Integer day) {
List<String> dayMinute = getDayMinute(month, day);
dayMinute.forEach(minute -> {
boolean key = statisticsMap.containsKey(minute);
if (!key) {
......
......@@ -23,17 +23,19 @@ import com.makeit.enums.report.SleepTypeEnum;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.mapper.platform.elder.PlatElderSleepMapper;
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.breathe.DeviceInfoContentBreathe;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.platform.elder.DeviceLogService;
import com.makeit.service.platform.elder.PlatElderService;
import com.makeit.service.platform.elder.PlatElderSleepAnalysisService;
import com.makeit.service.platform.elder.PlatElderSleepService;
import com.makeit.service.platform.elder.TestLogService;
import com.makeit.service.platform.space.PlatRoomBedDeviceService;
import com.makeit.service.saas.SaasSleepAnalysisModelService;
import com.makeit.service.saas.SaasSleepEvaluateReportService;
import com.makeit.service.saas.SaasSleepEvaluateStandardReportService;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.data.convert.StreamUtil;
import com.makeit.vo.platform.elder.report.day.PlatSleepRangeVO;
import org.apache.commons.lang3.StringUtils;
......@@ -80,7 +82,7 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
@Autowired
private PlatElderSleepService platElderSleepService;
@Resource
private TestLogService testLogService;
private DeviceLogService deviceLogService;
private TreeMap<String, AnalysisVO> getPerMinuteData(Map<String, List<DeviceInfoContentBreathe>> minuteMap, SaasSleepAnalysisModel analysisModel) {
......@@ -386,16 +388,22 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
AnalysisVO analysisVO = voEntry.getValue();
boolean actionFlag = analysisVO.getActionCount() == sleepDeepActionThreshold;
boolean turnedFlag = analysisVO.getTurnedCount() == sleepDeepActionThreshold;
if (actionFlag && turnedFlag
&& sleepDeepBreatheMin <= analysisVO.getAvgBr() && sleepDeepBreatheMax >= analysisVO.getAvgBr()) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
boolean condition = actionFlag && turnedFlag
&& sleepDeepBreatheMin <= analysisVO.getAvgBr() && sleepDeepBreatheMax >= analysisVO.getAvgBr();
if (condition) {
if (StringUtils.isEmpty(startSleepDeepTime)) {
startSleepDeepTime = voEntry.getKey();
}
long between = Duration.between(LocalDateTime.parse(startSleepDeepTime, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
LocalDateTime.parse(voEntry.getKey(), DEFAULT_FORMATTER)).toMinutes();
// 数据连续
if (between == sleepDeepMinute) {
sleepDeepMinute++;
} else {
if (isDeepSleep) {
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
}
sleepDeepMinute = 0;
startSleepDeepTime = null;
}
......@@ -404,27 +412,48 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
if (StringUtils.isEmpty(startSleepDeepTime) && sleepDeepMinute == 0) {
continue;
}
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
if (sleepDeepMinute > sleepDeepActionTimeBegin) {
if (sleepDeepMinute > sleepDeepActionTimeBegin && !isDeepSleep) {
sleepTimeAnalysisVO.setStartTime(startSleepDeepTime);
// sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setEndTime(getLastMinute(voEntry.getKey()));
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
// sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
isDeepSleep = true;
}
String endTime = sleepTimeAnalysisVO.getEndTime();
if(isDeepSleep) {
if (isDeepSleep && StrUtil.isNotBlank(endTime)) {
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
startSleepDeepTime = null;
sleepDeepMinute = 0;
isDeepSleep = false;
}
startSleepDeepTime = null;
sleepDeepMinute = 0;
}
}
}
private String getLastMinute (String minute) {
LocalDateTime parse = LocalDateTime.parse(minute, DEFAULT_FORMATTER);
return DateUtil.format(parse.plusMinutes(-1), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
}
private TreeMap<String, TreeMap<String, AnalysisVO>> filterTime(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap) {
TreeMap<String, TreeMap<String, AnalysisVO>> result = new TreeMap<>();
sleepTypeMap.forEach((k, v) -> {
TreeMap<String, AnalysisVO> treeMap = new TreeMap<>();
v.forEach((k2, v2) -> {
boolean after = LocalDateTime.parse(k2, DEFAULT_FORMATTER).isAfter(LocalDateTime.of(2024, 1, 10, 1, 0));
boolean before = LocalDateTime.parse(k2, DEFAULT_FORMATTER).isBefore(LocalDateTime.of(2024, 1, 10, 2, 0));
if (after && before) {
treeMap.put(k2, v2);
}
});
result.put(k,treeMap);
});
return result;
}
private SleepCountDTO deepSleepAndRestData(TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap, SaasSleepAnalysisModel analysisModel,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
String startSleepDeepTime;
......@@ -477,37 +506,48 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
int sleepModerateActionTimeBegin = Integer.parseInt(analysisModel.getSleepModerateActionTimeBegin());
int sleepMidMinute = 0;
String startSleepMidTime = null;
boolean isMiddleSleep = false;
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
AnalysisVO analysisVO = voEntry.getValue();
if (analysisVO.getActionCount() < sleepModerateActionThreshold && analysisVO.getTurnedCount() < sleepModerateActionThreshold) {
boolean condition = analysisVO.getActionCount() < sleepModerateActionThreshold && analysisVO.getTurnedCount() < sleepModerateActionThreshold;
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
if (condition) {
if (StringUtils.isEmpty(startSleepMidTime)) {
startSleepMidTime = voEntry.getKey();
}
long between = Duration.between(LocalDateTime.parse(startSleepMidTime, DEFAULT_FORMATTER),
LocalDateTime.parse(entry.getKey(), DEFAULT_FORMATTER)).toMinutes();
LocalDateTime.parse(voEntry.getKey(), DEFAULT_FORMATTER)).toMinutes();
if(between == sleepMidMinute){
sleepMidMinute++;
}else {
if(isMiddleSleep){
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
}
sleepMidMinute = 0;
startSleepMidTime = null;
}
continue;
}
String endTime = sleepTimeAnalysisVO.getEndTime();
if (StringUtils.isEmpty(startSleepMidTime) && sleepMidMinute == 0) {
continue;
}
if (sleepMidMinute > sleepModerateActionTimeBegin) {
SleepTimeAnalysisVO sleepTimeAnalysisVO = new SleepTimeAnalysisVO();
if (sleepMidMinute > sleepModerateActionTimeBegin && !isMiddleSleep) {
sleepTimeAnalysisVO.setStartTime(startSleepMidTime);
sleepTimeAnalysisVO.setEndTime(voEntry.getKey());
sleepTimeAnalysisVO.setType(SleepTypeEnum.SLEEP_MODERATE.getCode());
sleepTimeAnalysisVOList.add(sleepTimeAnalysisVO);
isMiddleSleep = true;
}
if (isMiddleSleep && StrUtil.isNotBlank(endTime)) {
startSleepMidTime = null;
sleepMidMinute = 0;
isMiddleSleep = false;
}
startSleepMidTime = null;
sleepMidMinute = 0;
}
}
}
......@@ -590,6 +630,139 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
return sleepTimeDTO;
}
private long between(String start, String end) {
return Duration.between(LocalDateTime.parse(start, DEFAULT_FORMATTER),
LocalDateTime.parse(end, DEFAULT_FORMATTER)).toMinutes();
}
private void groupSleep (TreeMap<String, TreeMap<String, AnalysisVO>> sleepTypeMap, SaasSleepAnalysisModel analysisModel,
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList) {
if (CollUtil.isEmpty(sleepTypeMap)) {
return;
}
// 深度睡眠
int sleepDeepMinute = 0;
String startSleepDeepTime = null;
int sleepDeepActionThreshold = Integer.parseInt(analysisModel.getSleepDeepActionThreshold());
int sleepDeepActionTimeBegin = Integer.parseInt(analysisModel.getSleepDeepActionTimeBegin());
int sleepDeepBreatheMin = Integer.parseInt(analysisModel.getSleepDeepBreatheMin());
int sleepDeepBreatheMax = Integer.parseInt(analysisModel.getSleepDeepBreatheMax());
// 中度
int sleepModerateActionThreshold = Integer.parseInt(analysisModel.getSleepModerateActionThreshold());
int sleepModerateActionTimeBegin = Integer.parseInt(analysisModel.getSleepModerateActionTimeBegin());
int sleepMidMinute = 0;
String startSleepMidTime = null;
boolean isMiddleSleep = false;
boolean isDeepSleep = false;
// 浅度
String sleepLightTime = null;
boolean isLightSleep = false;
int sleepLightMinute = 0;
for (Map.Entry<String, TreeMap<String, AnalysisVO>> entry : sleepTypeMap.entrySet()) {
TreeMap<String, AnalysisVO> sleepMap = entry.getValue();
for (Map.Entry<String, AnalysisVO> voEntry : sleepMap.entrySet()) {
AnalysisVO analysisVO = voEntry.getValue();
boolean actionFlag = analysisVO.getActionCount() == sleepDeepActionThreshold;
boolean turnedFlag = analysisVO.getTurnedCount() == sleepDeepActionThreshold;
SleepTimeAnalysisVO deepAnalysisVO = new SleepTimeAnalysisVO();
SleepTimeAnalysisVO midAnalysisVO = new SleepTimeAnalysisVO();
boolean deepCondition = actionFlag && turnedFlag
&& sleepDeepBreatheMin <= analysisVO.getAvgBr() && sleepDeepBreatheMax >= analysisVO.getAvgBr();
boolean midCondition = analysisVO.getActionCount() < sleepModerateActionThreshold
&& analysisVO.getTurnedCount() < sleepModerateActionThreshold;
String minute = voEntry.getKey();
if (deepCondition) {
if (StringUtils.isEmpty(startSleepDeepTime)) {
startSleepDeepTime = minute;
}
long between = between(startSleepDeepTime, minute);
// 数据连续
if (between == sleepDeepMinute) {
sleepDeepMinute++;
} else {
sleepDeepMinute = 0;
startSleepDeepTime = null;
}
sleepLightTime = null;
sleepLightMinute = 0;
} else if (midCondition) {
if (StringUtils.isEmpty(startSleepMidTime)) {
startSleepMidTime = minute;
}
long between = Duration.between(LocalDateTime.parse(startSleepMidTime, DEFAULT_FORMATTER),
LocalDateTime.parse(minute, DEFAULT_FORMATTER)).toMinutes();
if (between == sleepMidMinute) {
sleepMidMinute++;
} else {
sleepMidMinute = 0;
startSleepMidTime = null;
}
sleepLightTime = null;
sleepLightMinute = 0;
} else {
if (StringUtils.isEmpty(sleepLightTime)) {
sleepLightTime = minute;
}
long between = Duration.between(LocalDateTime.parse(sleepLightTime, DEFAULT_FORMATTER),
LocalDateTime.parse(minute, DEFAULT_FORMATTER)).toMinutes();
if (between == sleepMidMinute) {
sleepLightMinute++;
} else {
sleepLightMinute = 0;
sleepLightTime = null;
}
}
// 深度
if (StrUtil.isNotBlank(startSleepDeepTime) && sleepDeepMinute != 0) {
String endTime = deepAnalysisVO.getEndTime();
if (sleepDeepMinute > sleepDeepActionTimeBegin && !isDeepSleep) {
deepAnalysisVO.setStartTime(startSleepDeepTime);
endTime = getLastMinute(minute);
deepAnalysisVO.setEndTime(endTime);
deepAnalysisVO.setType(SleepTypeEnum.SLEEP_DEEP.getCode());
isDeepSleep = true;
}
if (isDeepSleep && StrUtil.isNotBlank(endTime)) {
sleepTimeAnalysisVOList.add(deepAnalysisVO);
startSleepDeepTime = null;
sleepDeepMinute = 0;
isDeepSleep = false;
}
}
// 中度
if (StrUtil.isNotBlank(startSleepMidTime) && sleepMidMinute != 0) {
String endTime = midAnalysisVO.getEndTime();
if (sleepMidMinute > sleepModerateActionTimeBegin && !isMiddleSleep) {
midAnalysisVO.setStartTime(startSleepMidTime);
endTime = getLastMinute(minute);
midAnalysisVO.setEndTime(endTime);
midAnalysisVO.setType(SleepTypeEnum.SLEEP_MODERATE.getCode());
isMiddleSleep = true;
}
if (isMiddleSleep && StrUtil.isNotBlank(endTime)) {
sleepTimeAnalysisVOList.add(midAnalysisVO);
startSleepMidTime = null;
sleepMidMinute = 0;
isMiddleSleep = false;
}
}
// 浅度
if (StrUtil.isNotBlank(sleepLightTime) && sleepLightMinute != 0) {
midAnalysisVO.setStartTime(sleepLightTime);
midAnalysisVO.setEndTime(minute);
midAnalysisVO.setType(SleepTypeEnum.SLEEP_LIGHTNESS.getCode());
sleepTimeAnalysisVOList.add(midAnalysisVO);
sleepLightTime = null;
sleepLightMinute = 0;
}
}
}
}
/**
* 小憩结束时间为清醒时间
* @param list
......@@ -794,9 +967,9 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
for (PlatRoomBedDevice platRoomBedDevice : platRoomBedDeviceList) {
TreeMap<String, AnalysisVO> totalMap = new TreeMap<>();
if (!"1732647368962203650".equals(platRoomBedDevice.getId())) {
continue;
}
// if (!"1732647368962203650".equals(platRoomBedDevice.getId())) {
// continue;
// }
String bedId = platRoomBedDevice.getBedId();
PlatElder elder = platElderService.getOne(new QueryWrapper<PlatElder>().lambda()
.eq(PlatElder::getBedId, bedId));
......@@ -808,37 +981,37 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
continue;
}
String tenantId = elder.getTenantId();
// for (String hourRange : dayHourRangeList) {
// String[] hourRangeArray = hourRange.split("~");
// List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]);
// if (CollUtil.isEmpty(deviceOperationLogEntities)) {
// continue;
// }
// List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream()
// .filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty"))
// .map(deviceOperationLogEntity -> {
// DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class);
// assert deviceInfoContentBreathe != null;
// deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp()));
// return deviceInfoContentBreathe;
// })
// .collect(Collectors.toList());
// Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime);
// // testLogService.save(minuteMap);
// deviceOperationLogEntities.clear();
// TreeMap<String, AnalysisVO> statisticsMap = getPerMinuteData(minuteMap, analysisModel);
// if (CollUtil.isNotEmpty(statisticsMap)) {
// // device_minute_info
// // testLogService.saveEntity(statisticsMap, platRoomBedDevice.getId());
// totalMap.putAll(statisticsMap);
// }
// }
totalMap = testLogService.getData();
for (String hourRange : dayHourRangeList) {
String[] hourRangeArray = hourRange.split("~");
List<DeviceOperationLogEntity> deviceOperationLogEntities = productDeviceService.getDeviceLogByTimeRange(platDevice.getOriDeviceId(), "reportProperty", 5000, hourRangeArray[0], hourRangeArray[1]);
if (CollUtil.isEmpty(deviceOperationLogEntities)) {
continue;
}
List<DeviceInfoContentBreathe> deviceInfoContentBreatheList = deviceOperationLogEntities.stream()
.filter(deviceOperationLogEntity -> deviceOperationLogEntity.getType().contains("reportProperty"))
.map(deviceOperationLogEntity -> {
DeviceInfoContentBreathe deviceInfoContentBreathe = JsonUtil.toObj((String) deviceOperationLogEntity.getContent(), DeviceInfoContentBreathe.class);
assert deviceInfoContentBreathe != null;
deviceInfoContentBreathe.setReportTime(formatLongTime(deviceInfoContentBreathe.getTimestamp()));
return deviceInfoContentBreathe;
})
.collect(Collectors.toList());
Map<String, List<DeviceInfoContentBreathe>> minuteMap = StreamUtil.groupBy(deviceInfoContentBreatheList, DeviceInfoContentBreathe::getReportTime);
deviceOperationLogEntities.clear();
TreeMap<String, AnalysisVO> statisticsMap = getPerMinuteData(minuteMap, analysisModel);
if (CollUtil.isNotEmpty(statisticsMap)) {
//测试环境数据 device_minute_info
// testLogService.saveEntity(statisticsMap, platRoomBedDevice.getId());
totalMap.putAll(statisticsMap);
}
}
// 本地测试用
// totalMap = deviceLogService.getData();
if (CollUtil.isEmpty(totalMap)) {
continue;
}
// 没有上报数据的时间,填默认值
// testLogService.fillDefaultData(totalMap);
deviceLogService.fillDefaultData(totalMap, month, day);
// 记录长者不同类型的睡眠时间
List<SleepTimeAnalysisVO> sleepTimeAnalysisVOList = Lists.newArrayList();
// 入睡时间
......@@ -848,12 +1021,13 @@ public class PlatElderSleepServiceImpl extends ServiceImpl<PlatElderSleepMapper,
SleepCountDTO sleepCountDTO = deepSleepAndRestData(sleepTypeMap, analysisModel, sleepTimeAnalysisVOList);
// 满足睡眠条件的map
TreeMap<String, TreeMap<String, AnalysisVO>> daySleepMap = sleepCountDTO.getDaySleepMap();
// groupSleep(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
// 深度睡眠
deepSleepData(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
// 中度睡眠
middleSleep(daySleepMap, analysisModel, sleepTimeAnalysisVOList);
// 清醒
sober(sleepTypeMap, sleepTimeAnalysisVOList);
// sober(sleepTypeMap, sleepTimeAnalysisVOList);
countTotal(totalMap, sleepCountDTO);
//浅睡
SleepTimeDTO sleepTimeDTO = groupSleepData(sleepTimeAnalysisVOList);
......
......@@ -57,8 +57,7 @@
</insert>
<select id="getData" resultType="com.makeit.module.iot.vo.analysis.AnalysisVO">
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 * from device_minute_info where device_id = '1732647368962203650' order by created_time
</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