Commit f3a70dcc by 杨伟程

实时状态更新

parent 68eb3096
package com.makeit.module.iot.enums;
import com.makeit.enums.BaseEnum;
import com.makeit.utils.sys.SysDictUtil;
public class DeviceInfoContentFallEnum {
public enum PersonStateEnum {
......
package com.makeit.module.iot.enums;
public class DeviceInfoContentSpaceEnum {
public enum PersonStateEnum {
NOBODY(0, "无人"),//0
ANYBODY(1, "有人");//1
private Integer value;
private String name;
PersonStateEnum(Integer value, String name) {
this.value = value;
this.name = name;
}
public Integer getValue() {
return value;
}
public String getName() {
return name;
}
}
}
package com.makeit.utils;
import java.util.List;
public class StandardDeviationUtil {
/**
* 传入一个数列x计算平均值
*
* @param x
* @return 平均值
*/
public static double average(double[] x) {
int n = x.length; //数列元素个数
double sum = 0;
for (double i : x) { //求和
sum += i;
}
return sum / n;
}
public static double average(List<Double> x) {
int n = x.size(); //数列元素个数
double sum = 0;
for (double i : x) { //求和
sum += i;
}
return sum / n;
}
/**
* 传入一个数列x计算方差
* 方差s^2=[(x1-x)^2+(x2-x)^2+......(xn-x)^2]/(n)(x为平均数)
*
* @param x 要计算的数列
* @return 方差
*/
public static double variance(double[] x) {
int n = x.length; //数列元素个数
double avg = average(x); //求平均值
double var = 0;
for (double i : x) {
var += (i - avg) * (i - avg); //(x1-x)^2+(x2-x)^2+......(xn-x)^2
}
return var / n;
}
public static double variance(List<Double> x) {
int n = x.size(); //数列元素个数
double avg = average(x); //求平均值
double var = 0;
for (double i : x) {
var += (i - avg) * (i - avg); //(x1-x)^2+(x2-x)^2+......(xn-x)^2
}
return var / n;
}
/**
* 传入一个数列x计算标准差
* 标准差σ=sqrt(s^2),即标准差=方差的平方根
*
* @param x 要计算的数列
* @return 标准差
*/
public static double standardDeviation(double[] x) {
return Math.sqrt(variance(x));
}
}
......@@ -3,7 +3,7 @@ package com.makeit.enums.platform.elder;
import com.makeit.enums.BaseEnum;
import com.makeit.utils.sys.SysDictUtil;
public class PlatElderMonitorReportEnum {
public class PlatElderRealtimeReportEnum {
public enum NowStatus implements BaseEnum {
OUT("elder.realtime.now.status.out"),
RUN("elder.realtime.now.status.run"),
......
package com.makeit.service.platform.elder.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.makeit.common.entity.BaseEntity;
import com.makeit.dto.platform.elder.PlatElderIdDTO;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.saas.analysis.SaasSleepAnalysisModel;
import com.makeit.entity.saas.analysis.SaasSleepEvaluateReport;
import com.makeit.enums.platform.elder.PlatElderRealtimeReportEnum;
import com.makeit.module.iot.enums.DeviceInfoContentBreatheEnum;
import com.makeit.module.iot.enums.DeviceInfoContentSpaceEnum;
import com.makeit.module.iot.service.IotProductDeviceService;
import com.makeit.module.iot.vo.breathe.DeviceInfoContentBreathe;
import com.makeit.module.iot.vo.space.DeviceInfoContentSpace;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.platform.elder.PlatElderDayReportDayService;
import com.makeit.service.platform.elder.PlatElderRealTimeService;
import com.makeit.service.platform.elder.PlatElderService;
import com.makeit.service.saas.SaasSleepAnalysisModelService;
import com.makeit.utils.StandardDeviationUtil;
import com.makeit.utils.data.convert.StreamUtil;
import com.makeit.utils.data.validate.CollectionUtils;
import com.makeit.utils.old.StringUtils;
......@@ -15,6 +25,7 @@ import com.makeit.vo.platform.elder.realtime.PlatElderCoordinateVO;
import com.makeit.vo.platform.elder.realtime.PlatElderRealTimeBodyVO;
import com.makeit.vo.platform.elder.realtime.PlatElderRealTimeHeartRespiratoryVO;
import com.makeit.vo.platform.elder.realtime.PlatElderRealTimeNowVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -48,6 +59,12 @@ public class PlatElderRealTimeServiceImpl implements PlatElderRealTimeService {
@Autowired
private IotProductDeviceService iotProductDeviceService;
@Autowired
private PlatElderDayReportDayService platElderDayReportDayService;
@Autowired
private SaasSleepAnalysisModelService saasSleepAnalysisModelService;
@Override
public PlatDevice getBreathDevice(String elderId, String deviceId) {
PlatDevice platDevice = null;
......@@ -121,17 +138,126 @@ public class PlatElderRealTimeServiceImpl implements PlatElderRealTimeService {
}
private void nowStatusOut(PlatElderRealTimeNowVO platElderRealTimeNowVO, PlatElderIdDTO platElderIdDTO) {
List<DeviceInfoContentSpace> deviceInfoContentSpaceList = getNowDataSpace(platElderIdDTO.getElderId(), platElderIdDTO.getDeviceId());
if (CollectionUtils.isEmpty(deviceInfoContentSpaceList)) {
return;
}
if (StreamUtil.allMatch(deviceInfoContentSpaceList, e -> DeviceInfoContentSpaceEnum.PersonStateEnum.NOBODY.getValue().equals(e.getProperties().getPersonState()))) {
platElderRealTimeNowVO.setStatus(PlatElderRealtimeReportEnum.NowStatus.OUT.getValue());
}
}
private void nowStatusRun(PlatElderRealTimeNowVO platElderRealTimeNowVO, PlatElderIdDTO platElderIdDTO, DeviceInfoContentBreathe nowDataBreathe, List<PlatElderCoordinateVO> coordinateList) {
if (nowDataBreathe == null) {
return;
}
boolean flag = DeviceInfoContentBreatheEnum.PersonStateEnum.NOBODY.getValue().equals(nowDataBreathe.getProperties().getPersonState());
if (!flag) {
return;
}
if (CollectionUtils.isNotEmpty(coordinateList)) {
double v = StandardDeviationUtil.average(StreamUtil.map(coordinateList, e -> e.getDistance().doubleValue()));
if (new BigDecimal(v + "").compareTo(new BigDecimal(2)) > 0) {
platElderRealTimeNowVO.setStatus((PlatElderRealtimeReportEnum.NowStatus.RUN.getValue()));
}
}
}
private void nowStatusRest(PlatElderRealTimeNowVO platElderRealTimeNowVO, PlatElderIdDTO platElderIdDTO, DeviceInfoContentBreathe nowDataBreathe, List<PlatElderCoordinateVO> coordinateList) {
if (nowDataBreathe == null) {
return;
}
boolean flag = DeviceInfoContentBreatheEnum.PersonStateEnum.NOBODY.getValue().equals(nowDataBreathe.getProperties().getPersonState());
if (!flag) {
return;
}
if (CollectionUtils.isNotEmpty(coordinateList)) {
double v = StandardDeviationUtil.average(StreamUtil.map(coordinateList, e -> e.getDistance().doubleValue()));
if (new BigDecimal(v + "").compareTo(new BigDecimal(2)) <= 0) {
platElderRealTimeNowVO.setStatus((PlatElderRealtimeReportEnum.NowStatus.RUN.getValue()));
}
}
}
private void nowStatusSleepAndBed(PlatElderRealTimeNowVO platElderRealTimeNowVO, PlatElderIdDTO platElderIdDTO, DeviceInfoContentBreathe nowDataBreathe) {
if (nowDataBreathe == null) {
return;
}
boolean flag = !DeviceInfoContentBreatheEnum.PersonStateEnum.NOBODY.getValue().equals(nowDataBreathe.getProperties().getPersonState());
if (!flag) {
return;
}
SaasSleepAnalysisModel analysisModel = saasSleepAnalysisModelService.getOne(new QueryWrapper<SaasSleepAnalysisModel>().lambda()
.orderByDesc(BaseEntity::getCreateBy)
.last("limit 1"));
if (analysisModel == null) {
return;
}
Integer sleepTimeActionDuration = Integer.valueOf(analysisModel.getSleepTimeActionDuration() + "");
Integer sleepTimeActionThreshold = Integer.valueOf(analysisModel.getSleepTimeActionThreshold() + "");
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = now.minusHours(sleepTimeActionDuration);
PlatDevice platDevice = getBreathDevice(platElderIdDTO.getElderId(), platElderIdDTO.getDeviceId());
List<DeviceInfoContentBreathe> breatheList = iotProductDeviceService.getDeviceLogByTimeRangeBreathe(platDevice.getOriDeviceId(), 2 * 24 * 3600, start, now);
if (CollectionUtils.isNotEmpty(breatheList)) {
if (StreamUtil.allMatch(breatheList, e -> e.getProperties().getBodymove() < sleepTimeActionThreshold)) {
platElderRealTimeNowVO.setStatus(PlatElderRealtimeReportEnum.NowStatus.SLEEP.getValue());
}
}
platElderRealTimeNowVO.setStatus(PlatElderRealtimeReportEnum.NowStatus.BED.getValue());
}
@Override
public PlatElderRealTimeNowVO nowStatus(PlatElderIdDTO platElderIdDTO) {
// DeviceInfoContentBreathe deviceInfoContentBreathe = getNowDataBreathe(platElderIdDTO.getElderId(), platElderIdDTO.getDeviceId());
//
PlatElderRealTimeNowVO platElderRealTimeNowVO = new PlatElderRealTimeNowVO();
//
// if (deviceInfoContentBreathe == null) {
// return platElderRealTimeNowVO;
// }
//
//platElderRealTimeNowVO.setStatus(deviceInfoContentBreathe.getProperties().getPersonState() + "");
DeviceInfoContentBreathe deviceInfoContentBreathe = getNowDataBreathe(platElderIdDTO.getElderId(), platElderIdDTO.getDeviceId());
PlatElderRealTimeNowVO platElderRealTimeNowVO = new PlatElderRealTimeNowVO();
nowStatusOut(platElderRealTimeNowVO, platElderIdDTO);
if (deviceInfoContentBreathe == null) {
return platElderRealTimeNowVO;
}
LocalDateTime now = LocalDateTime.now();
LocalDateTime start = now.minusSeconds(10);
List<PlatElderCoordinateVO> coordinateList = platElderDayReportDayService.coordinateList(platElderIdDTO.getElderId(), platElderIdDTO.getDeviceId(), start, now);
nowStatusRun(platElderRealTimeNowVO, platElderIdDTO, deviceInfoContentBreathe, coordinateList);
nowStatusRest(platElderRealTimeNowVO, platElderIdDTO, deviceInfoContentBreathe, coordinateList);
nowStatusSleepAndBed(platElderRealTimeNowVO, platElderIdDTO, deviceInfoContentBreathe);
platElderRealTimeNowVO.setStatus(deviceInfoContentBreathe.getProperties().getPersonState() + "");
platElderRealTimeNowVO.setHeartRate(deviceInfoContentBreathe.getProperties().getHr());
platElderRealTimeNowVO.setRespiratoryRate(deviceInfoContentBreathe.getProperties().getBr());
platElderRealTimeNowVO.setBodyMove(deviceInfoContentBreathe.getProperties().getBodymove());
......
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