From: snickersun Date: Fri, 8 Jan 2021 06:35:40 +0000 (+0800) Subject: 修正睡眠算法 X-Git-Url: http://git.snicker.com.cn/sitemap.xml?a=commitdiff_plain;h=39f364a6601fa0c5b057406a889b2645d1d66fb4;p=protocol_srv.git 修正睡眠算法 --- diff --git a/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java b/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java index 114e927f..e2aa01b2 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java +++ b/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java @@ -1,168 +1,170 @@ -package com.enqualcomm.support.bean; - -import com.enqualcomm.support.Config; -import com.enqualcomm.support.utils.ICLogger; -import com.enqualcomm.support.utils.TimeUtil; - -import java.sql.Time; -import java.util.ArrayList; -import java.util.Date; - -public class SleepInfo { - private String currentDate; - private Time sleep_start=new Time(21,0,0); - private Time sleep_end=new Time(9,0,0); - private int deepsleep_minutes; - private int lightsleep_minutes; - private ArrayList turnovertimesArray ; - - private int Score; - /** - * 睡眠算法 - * 使用记步同样的算法,计算翻身次数 - * 每30秒记录一次 - * 深睡眠,通过加速度传感器,在30秒内翻身次数小于3 - * 浅睡眠,通过加速度传感器,在30秒内翻身次数大于3 - * - * 本方案换算为每分钟翻身几次来计算。 - * 并设置了浅睡的翻身次数,如果太多认为没睡。 - */ - private int DEEP_TURN_LEVEL=6; - private int LIGHT_TURN_LEVEL=12; - - private static SleepInfo sleepinfo=null; - public static SleepInfo getInstance(){ - if (sleepinfo==null) - sleepinfo=new SleepInfo(); - - return sleepinfo; - } - public void setDate(String _date){ - currentDate=_date; - } - public String getCurrentDate() - { - return currentDate; - } - - public int getScore(){ - return Score; - } - - public Time getSleep_start() - { - return sleep_start; - } - public Time getSleep_end() - { - return sleep_end; - } - - /*** - *根据参数创建睡眠检测记录, - * @ datevalue - *当前测试的 日期 - * @ sleepstart - * 系统设置的 睡眠开始监测时间 例如晚上9点开始 - * @ sleepend - * 系统设置的 睡眠停止监测时间 例如第二天早9点 - */ - - public SleepInfo(){ - currentDate= TimeUtil.getSystemDateYYMMDD(); - } - - /*** - * 每次加入的turnovertime 是一个计量周期(心跳周期)内翻滚的次数(此处暂时用计步数据进行代替) - * 翻滚次数小于最低阈值DEEP_TURN_LEVEL 认为是深度睡眠 - * 翻滚次数在最低阈值DEEP_TURN_LEVEL 和最高阈值 LIGHT_TURN_LEVEL 之间认为是浅度睡眠, - * 翻滚次数大于最高阈值认为没有睡眠。 - * @param runtime - * - */ - public void calculate_sleepInfo(Runtime runtime){ - - int i=0; - int turnovertime=0; - turnovertimesArray = runtime.getDbHelper().querySleepRecord(currentDate); - if (turnovertimesArray.size()>0) { - for( i = 0;i < turnovertimesArray.size();i++ ){ - if (i>0){ - turnovertime = (turnovertimesArray.get(i).stepCount - turnovertimesArray.get(i-1).stepCount); - String before=turnovertimesArray.get(i-1).date; - String after =turnovertimesArray.get(i).date; - ICLogger.i("======diffBetween2DateInMinutes before "+before+", after "+after); - long minutes=TimeUtil.diffBetween2DateInMinutes(before,after); - if (minutes>0){ - long avg_turnovertime= turnovertime/minutes; //平均每分钟翻滚次数 - if ( avg_turnovertime<= DEEP_TURN_LEVEL) { //小于深度睡眠标准都属于深度睡眠 - deepsleep_minutes += minutes; - ICLogger.i("======深度睡眠+="+ minutes); - } else if (avg_turnovertime > DEEP_TURN_LEVEL && avg_turnovertime < LIGHT_TURN_LEVEL) { //深度睡眠和浅睡标准之间属于浅睡。 - lightsleep_minutes += minutes; - ICLogger.i("======浅睡+="+minutes); - }else{ - ICLogger.i("======未睡+="+minutes); - } - }else{ - // - ICLogger.i("=====两条睡眠记录间隔小于1分钟,skip计算"); //此处可以有效解决0点 reset步数造成的计算问题。 - } - - } - } - ICLogger.i("======深睡 = "+deepsleep_minutes+" , 浅睡 = "+lightsleep_minutes); - Score=(int)calculateScore(deepsleep_minutes,lightsleep_minutes,runtime); - - } - - } - - /*** - * * 返回我们服务器要有深度睡眠与浅度睡眠的时长; - * * 手表展示睡眠评分规则: - * * 睡眠时间3小时内3-60分。计算公式:睡眠时间3分钟等于1分 - * * - * * 睡眠时间3小时以上,深度睡眠占睡眠时间1-20%为61-80分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,默认分数为60分,每1%则增加1分。 - * * - * * 睡眠时间3小时以上,深度睡眠占睡眠时间21-25%为81-98分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,21%为83分,22%为86分,23%为89分,24%为92分。25为98% - * * - * * 睡眠时间3小时以上,深度睡眠占睡眠时间25%以上固定为98分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,超过等于25%,均为98分 - * * - * * 以上是昨天21:00~今天9:00的规则; - * * 超过测量时间2个小时,若还能检测到在睡觉则判断未佩戴手表测量;这以上监测作废 - * @param deepsleep - * @param lightsleep - * @param runtime - * @return - */ - private long calculateScore(long deepsleep,long lightsleep , Runtime runtime){ - long sleeptime=deepsleep+lightsleep; - - if (sleeptime>0 && sleeptime <= 3*60) { - return (sleeptime/3); - }else if (sleeptime >3*60 ){ - long tmp1= 100*deepsleep/sleeptime; //深度睡眠/睡眠时间 百分比 - ICLogger.i("======深度睡眠/睡眠时间 百分比"+tmp1); - if (tmp1 >=0 && tmp1 < 20){ - return 60+tmp1; - }else if (tmp1 >=21 && tmp1 < 25 ){ - return (80+ (tmp1-20)*3); - }else if (tmp1 >=26){ - return 98; - } - } - return 0; - } - - public int getDeepsleep_minutes(){ - return deepsleep_minutes; - } - - public int getLightsleep_minutes(){ - return lightsleep_minutes; - } - - - -} +package com.enqualcomm.support.bean; + +import com.enqualcomm.support.Config; +import com.enqualcomm.support.utils.ICLogger; +import com.enqualcomm.support.utils.TimeUtil; + +import java.sql.Time; +import java.util.ArrayList; +import java.util.Date; + +public class SleepInfo { + private String currentDate; + private Time sleep_start=new Time(21,0,0); + private Time sleep_end=new Time(9,0,0); + private int deepsleep_minutes; + private int lightsleep_minutes; + private ArrayList turnovertimesArray ; + + private int Score; + /** + * 睡眠算法 + * 使用记步同样的算法,计算翻身次数 + * 每30秒记录一次 + * 深睡眠,通过加速度传感器,在30秒内翻身次数小于3 + * 浅睡眠,通过加速度传感器,在30秒内翻身次数大于3 + * + * 本方案换算为每分钟翻身几次来计算。 + * 并设置了浅睡的翻身次数,如果太多认为没睡。 + */ + private int DEEP_TURN_LEVEL=6; + private int LIGHT_TURN_LEVEL=12; + + private static SleepInfo sleepinfo=null; + public static SleepInfo getInstance(){ + if (sleepinfo==null) + sleepinfo=new SleepInfo(); + + return sleepinfo; + } + public void setDate(String _date){ + currentDate=_date; + } + public String getCurrentDate() + { + return currentDate; + } + + public int getScore(){ + return Score; + } + + public Time getSleep_start() + { + return sleep_start; + } + public Time getSleep_end() + { + return sleep_end; + } + + /*** + *根据参数创建睡眠检测记录, + * @ datevalue + *当前测试的 日期 + * @ sleepstart + * 系统设置的 睡眠开始监测时间 例如晚上9点开始 + * @ sleepend + * 系统设置的 睡眠停止监测时间 例如第二天早9点 + */ + + public SleepInfo(){ + currentDate= TimeUtil.getSystemDateYYMMDD(); + } + + /*** + * 每次加入的turnovertime 是一个计量周期(心跳周期)内翻滚的次数(此处暂时用计步数据进行代替) + * 翻滚次数小于最低阈值DEEP_TURN_LEVEL 认为是深度睡眠 + * 翻滚次数在最低阈值DEEP_TURN_LEVEL 和最高阈值 LIGHT_TURN_LEVEL 之间认为是浅度睡眠, + * 翻滚次数大于最高阈值认为没有睡眠。 + * @param runtime + * + */ + public void calculate_sleepInfo(Runtime runtime){ + + int i=0; + int turnovertime=0; + deepsleep_minutes=0; + lightsleep_minutes=0; + turnovertimesArray = runtime.getDbHelper().querySleepRecord(currentDate); + if (turnovertimesArray.size()>0) { + for( i = 0;i < turnovertimesArray.size();i++ ){ + if (i>0){ + turnovertime = (turnovertimesArray.get(i).stepCount - turnovertimesArray.get(i-1).stepCount); + String before=turnovertimesArray.get(i-1).date; + String after =turnovertimesArray.get(i).date; + ICLogger.i("======diffBetween2DateInMinutes before "+before+", after "+after); + long minutes=TimeUtil.diffBetween2DateInMinutes(before,after); + if (minutes>0){ + long avg_turnovertime= turnovertime/minutes; //平均每分钟翻滚次数 + if ( avg_turnovertime<= DEEP_TURN_LEVEL) { //小于深度睡眠标准都属于深度睡眠 + deepsleep_minutes += minutes; + ICLogger.i("======深度睡眠+="+ minutes); + } else if (avg_turnovertime > DEEP_TURN_LEVEL && avg_turnovertime < LIGHT_TURN_LEVEL) { //深度睡眠和浅睡标准之间属于浅睡。 + lightsleep_minutes += minutes; + ICLogger.i("======浅睡+="+minutes); + }else{ + ICLogger.i("======未睡+="+minutes); + } + }else{ + // + ICLogger.i("=====两条睡眠记录间隔小于1分钟,skip计算"); //此处可以有效解决0点 reset步数造成的计算问题。 + } + + } + } + ICLogger.i("======深睡 = "+deepsleep_minutes+" , 浅睡 = "+lightsleep_minutes); + Score=(int)calculateScore(deepsleep_minutes,lightsleep_minutes,runtime); + + } + + } + + /*** + * * 返回我们服务器要有深度睡眠与浅度睡眠的时长; + * * 手表展示睡眠评分规则: + * * 睡眠时间3小时内3-60分。计算公式:睡眠时间3分钟等于1分 + * * + * * 睡眠时间3小时以上,深度睡眠占睡眠时间1-20%为61-80分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,默认分数为60分,每1%则增加1分。 + * * + * * 睡眠时间3小时以上,深度睡眠占睡眠时间21-25%为81-98分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,21%为83分,22%为86分,23%为89分,24%为92分。25为98% + * * + * * 睡眠时间3小时以上,深度睡眠占睡眠时间25%以上固定为98分。计算公式:(深度睡眠/睡眠时间)*100%,四舍五入,超过等于25%,均为98分 + * * + * * 以上是昨天21:00~今天9:00的规则; + * * 超过测量时间2个小时,若还能检测到在睡觉则判断未佩戴手表测量;这以上监测作废 + * @param deepsleep + * @param lightsleep + * @param runtime + * @return + */ + private long calculateScore(long deepsleep,long lightsleep , Runtime runtime){ + long sleeptime=deepsleep+lightsleep; + + if (sleeptime>0 && sleeptime <= 3*60) { + return (sleeptime/3); + }else if (sleeptime >3*60 ){ + long tmp1= 100*deepsleep/sleeptime; //深度睡眠/睡眠时间 百分比 + ICLogger.i("======深度睡眠/睡眠时间 百分比"+tmp1); + if (tmp1 >=0 && tmp1 < 20){ + return 60+tmp1; + }else if (tmp1 >=21 && tmp1 < 25 ){ + return (80+ (tmp1-20)*3); + }else if (tmp1 >=26){ + return 98; + } + } + return 0; + } + + public int getDeepsleep_minutes(){ + return deepsleep_minutes; + } + + public int getLightsleep_minutes(){ + return lightsleep_minutes; + } + + + +}