From 88ac8a6dadf36d3c4ed41b68adf677896fd7eadd Mon Sep 17 00:00:00 2001 From: snickersun Date: Mon, 3 Aug 2020 23:58:07 +0800 Subject: [PATCH] =?utf8?q?=E6=B7=BB=E5=8A=A0=E7=9D=A1=E7=9C=A0=E6=A3=80?= =?utf8?q?=E6=B5=8B=E6=9C=8D=E5=8A=A1=EF=BC=8C21=E7=82=B9=E8=87=B3?= =?utf8?q?=E6=AC=A1=E6=97=A59=E7=82=B9=20=E6=A3=80=E6=B5=8B=E7=9D=A1?= =?utf8?q?=E7=9C=A0=E6=83=85=E5=86=B5=EF=BC=8C=E6=89=93=E5=88=86=E5=B9=B6?= =?utf8?q?=E5=BB=B6=E6=97=B6=E4=B8=8A=E6=8A=A5=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?utf8?q?=E3=80=82=20=E6=B7=BB=E5=8A=A0=20protocol=5Fic=20=E4=B8=8A?= =?utf8?q?=E4=BC=A0=E7=9D=A1=E7=9C=A0=E6=83=85=E5=86=B5=E5=91=BD=E4=BB=A4?= =?utf8?q?=20APSL=EF=BC=8CBPSL?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/com/enqualcomm/support/Config.java | 22 ++- .../com/enqualcomm/support/Constants.java | 30 ++++ .../com/enqualcomm/support/CoreService.java | 52 ++++++- .../enqualcomm/support/MySqlitOpenHelper.java | 6 +- .../com/enqualcomm/support/bean/Runtime.java | 2 + .../enqualcomm/support/bean/SleepInfo.java | 144 ++++++++++++++---- .../enqualcomm/support/bean/SleepRecord.java | 34 +++++ .../enqualcomm/support/utils/DBHelper.java | 84 +++++++++- .../enqualcomm/support/utils/TimeUtil.java | 78 +++++++++- .../com/ic/protocols/protocolAPIHelper.java | 22 ++- .../java/com/ic/protocols/protocol_ic.java | 38 ++++- 11 files changed, 469 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/com/enqualcomm/support/bean/SleepRecord.java diff --git a/app/src/main/java/com/enqualcomm/support/Config.java b/app/src/main/java/com/enqualcomm/support/Config.java index e0ca68b2..1d120935 100644 --- a/app/src/main/java/com/enqualcomm/support/Config.java +++ b/app/src/main/java/com/enqualcomm/support/Config.java @@ -28,7 +28,7 @@ public class Config { public static int antioff = 0; public static int heart_rate = 0; private static String json; - + public static int defaultHeartbeatCycle=5; public static int sw_release_no = -1; public static String work_mode = "2@1"; @@ -530,5 +530,25 @@ public class Config { ICLogger.i("Load Health adjust value ppg "+ppgadj+", bph "+bphadj+", bpl "+bpladj+", spo2 "+spo2adj); } + + + //久坐时间获取 + public static int getKeepSitRangeInMinutes(Context context ){ + return SharPreferenceUtil.getPrefInt(context,Constants.SHAREKEY_SETTING_SIT_ALARM_RANGE_IN_MINUTES,0); + } + + //久坐时间设置 + public static void setKeepSitRangeInMinutes(Context context, int RangeInMinutes){ + SharPreferenceUtil.setPrefInt(context,Constants.SHAREKEY_SETTING_SIT_ALARM_RANGE_IN_MINUTES,RangeInMinutes); + } + + public static void setSleepDate(Context context,String dateyyyyMMddHHmmss){ + SharPreferenceUtil.setPrefStringPublic(context,Constants.SHAREKEY_SETTING_SLEEP_SCORE_DATE,dateyyyyMMddHHmmss); + } + + + public static void setSleepScore(Context context,int score){ + SharPreferenceUtil.setPrefInt(context,Constants.SHAREKEY_SETTING_SLEEP_SCORE,score); + } } diff --git a/app/src/main/java/com/enqualcomm/support/Constants.java b/app/src/main/java/com/enqualcomm/support/Constants.java index fd924adc..769221bb 100644 --- a/app/src/main/java/com/enqualcomm/support/Constants.java +++ b/app/src/main/java/com/enqualcomm/support/Constants.java @@ -319,5 +319,35 @@ public interface Constants { public static String PersistFotaServer2Url="persist.sys.ota.host2"; +/** + * PreferenceManager.getDefaultSharedPreferences(context) , package name "com.enqualcomm.support" + */ + /** + * 睡眠分数 , int + */ + public static String SHAREKEY_SETTING_SLEEP_SCORE = "SleepScore"; + /** + *当前睡眠分数的日期 String yyMMddHHmm + *可能时昨天的,算完就是当天的 + * */ + public static String SHAREKEY_SETTING_SLEEP_SCORE_DATE = "SleepScoreDate"; + + /** + * 久坐检测时长,int 分钟为单位,此处为读取方法,可在显示前读取当前设置。 + * 设置请采用broadcast方式发给协议层存储 + */ + public static String SHAREKEY_SETTING_SIT_ALARM_RANGE_IN_MINUTES = "SIT_RANGE"; + +/*** + * ACTion 用于 设置久坐时长分钟为单位 + */ + + public static String ACTION_SET_SIT_RANGE = "com.ic.action.set_sit_range"; + /** + * ACTION_SET_SIT_RANGE 参数 int,分钟数。 + */ + public static String KEY_SIT_RANGE = "sit_range"; + + } diff --git a/app/src/main/java/com/enqualcomm/support/CoreService.java b/app/src/main/java/com/enqualcomm/support/CoreService.java index 442e064b..d8ec1ac2 100644 --- a/app/src/main/java/com/enqualcomm/support/CoreService.java +++ b/app/src/main/java/com/enqualcomm/support/CoreService.java @@ -16,11 +16,14 @@ import android.os.SystemClock; import android.widget.Toast; import com.enqualcomm.support.bean.ICLocation; +import com.enqualcomm.support.bean.SleepInfo; +import com.enqualcomm.support.bean.SleepRecord; import com.enqualcomm.support.service.AntiMonitorService; import com.enqualcomm.support.utils.DeviceInfoUtil; import com.enqualcomm.support.utils.LedUtil; import com.enqualcomm.support.utils.StorageUtils; import com.enqualcomm.support.utils.TemperatureUtil; +import com.enqualcomm.support.utils.TimeUtil; import com.enqualcomm.support.utils.VibrateUtil; import com.ic.jni.ICJniUtils; import com.enqualcomm.support.adapter.LBSHelper; @@ -59,6 +62,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; +import java.util.Random; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -673,6 +677,44 @@ public class CoreService extends Service { } + public void updateSleepDate(){ + String dateyyyyMMDDhhmmss=TimeUtil.getSystemTime4(); + String sleepdate=TimeUtil.getCurrentSleepRecordDate(dateyyyyMMDDhhmmss); + if(TimeUtil.isInSleepRange == true) { + runtime.getDbHelper().insertSleepRecord(new SleepRecord(dateyyyyMMDDhhmmss,sleepdate,runtime.getTotalStepCount())); + runtime.sleepDataReported=false; //只要记录过数据 就证明有数据没有report + }else{ + if (runtime.sleepDataReported == false){ + //calculate sleep data and report it randomly within 1 hour + SleepInfo si=SleepInfo.getInstance(); + si.setDate(sleepdate); + si.calculate_sleepInfo(runtime); + runtime.getDbHelper().updateSleepInfo(si); + runtime.sleepDataReported=true; + Config.setSleepDate(this,sleepdate); + Config.setSleepScore(this,si.getScore()); + Random r = new Random(); + int randomint = r.nextInt(30 - 1) + 1; + ICLogger.i("Delayed " +randomint +" minutes , send sleep info " + si.getCurrentDate() + +" deep "+ si.getDeepsleep_minutes() + +" light "+ si.getLightsleep_minutes() + +" score "+ si.getScore() + ); + Observable.timer(randomint * 60 * 1000L, TimeUnit.MILLISECONDS).subscribe(new Action1() { + public void call(Long param1Long) { +// connection.send(protocolAPIHelper); + ICLogger.i("Send sleep info " + si.getCurrentDate() + +" deep "+ si.getDeepsleep_minutes() + +" light "+ si.getLightsleep_minutes() + +" score "+ si.getScore() + ); + Deliver.sendLocationMSG(protocolAPIHelper.getSleepMSG(SleepInfo.getInstance())); + } + }); + + } + } + } public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) { if (paramIntent != null) { StringBuilder stringBuilder = new StringBuilder(); @@ -796,8 +838,10 @@ public class CoreService extends Service { return Service.START_STICKY; } } else if (HEART_ALARM.equals(paramIntent.getAction())) { - if (this.mKeepAlive != null) { + locationClient.updateStepCount(); + updateSleepDate(); + if (this.mKeepAlive != null) { if (connection.isServerConnected() && runtime.isServerProtocolConnected()) { this.mKeepAlive.onHeartAlarm(this.connection, this.runtime); } else { @@ -817,10 +861,14 @@ public class CoreService extends Service { } else if (STEP_COUNT_ALARM_ACTION.equals(paramIntent.getAction())) { ICLogger.i("Reset Count At 00:00 "); //update stepCount and broadcast before reset - this.locationClient.updateStepCount(); + this.locationClient.updateStepCount(); //先记录下最后一次步数 runtime.getContextProxy().brodcastResetStepCount(runtime.getTotalStepCount()); + updateSleepDate();//更新睡眠信息 resetStepCount(); + this.locationClient.updateStepCount(); //重置后马上跟新当前步数为0, 以便及时更新睡眠信息 一分钟内更新的步数可以忽略不记,从而平滑切换 0点reset 计步数据的计算问题 + updateSleepDate();//更新睡眠信息 setCleanStepCountAlarm(); + Config.logBootDate(this);//重置步数的时候,要把开机时间也更新一下,否则第二天 任意时间重启都会造成计步数据清零 return Service.START_STICKY; } else if (ACTION_HEATR_RATE_START.equals(paramIntent.getAction())) { diff --git a/app/src/main/java/com/enqualcomm/support/MySqlitOpenHelper.java b/app/src/main/java/com/enqualcomm/support/MySqlitOpenHelper.java index 6d530035..c26d8e55 100644 --- a/app/src/main/java/com/enqualcomm/support/MySqlitOpenHelper.java +++ b/app/src/main/java/com/enqualcomm/support/MySqlitOpenHelper.java @@ -51,10 +51,10 @@ Pushtxt、message 根据时间段进行检测,时间段内,根据翻滚次数确认其睡眠类型。如深度睡眠或者浅睡。 2个数据库翻滚次数记录,当每日睡眠情况统计计算,检测时间段记在settings里,就不放在数据库了。 */ - /*每次打点的记录表*/ - sqlite.execSQL("CREATE TABLE SLEEP_RECORDS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,DATE TEXT, TIME TEXT ,ROLLTIMES INTEGER ,STATUS TEXT ,REM TEXT)"); + /*每次打点的记录表 SLEEP_DATE 代表记录睡眠的日期,例如 22日上午7:00的计入到21日的睡眠记录中。 DATE代表记录的时间,此出应该等于上文的 22日 7:00 */ + sqlite.execSQL("CREATE TABLE SLEEP_RECORDS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,SLEEP_DATE TEXT,DATE TEXT, STEPCOUNT INTEGER ,ROLLTIMES INTEGER ,STATUS TEXT )"); /*计算后的分值,每天会在睡眠结束后计算Records获得*/ - sqlite.execSQL("CREATE TABLE SLEEP_STATUS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,TIME TEXT , DEEP_SLEEP INTEGER , LIGHT_SLEEP INTEGER ,SCORE INTEGER )"); + sqlite.execSQL("CREATE TABLE SLEEP_STATUS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,DATE TEXT , DEEP_SLEEP INTEGER , LIGHT_SLEEP INTEGER ,SCORE INTEGER )"); /*久坐 根据时间段进行检测,时间段内,根据计步变化确认其是否在坐着。 diff --git a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java index eef9cafd..43b2fd01 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java +++ b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java @@ -624,6 +624,8 @@ public class Runtime { } + public boolean sleepDataReported=false; + } 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 515e136d..2c854632 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java +++ b/app/src/main/java/com/enqualcomm/support/bean/SleepInfo.java @@ -1,51 +1,75 @@ 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 Date currentDate; - private Date sleep_start; - private Date sleep_end; + 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 ArrayList turnovertimesArray ; - private int DEEP_TURN_LEVEL=0; - private int LIGHT_TURN_LEVEL=25; + private int Score; + /** + * 睡眠算法 + * 使用记步同样的算法,计算翻身次数 + * 每30秒记录一次 + * 深睡眠,通过加速度传感器,在30秒内翻身次数小于3 + * 浅睡眠,通过加速度传感器,在30秒内翻身次数大于3 + * + * 本方案换算为每分钟翻身几次来计算。 + * 并设置了浅睡的翻身次数,如果太多认为没睡。 + */ + private int DEEP_TURN_LEVEL=6; + private int LIGHT_TURN_LEVEL=12; - private int step_for_each_record_in_minutes; + private static SleepInfo sleepinfo=null; + public static SleepInfo getInstance(){ + if (sleepinfo==null) + sleepinfo=new SleepInfo(); - public Date getCurrentDate() + return sleepinfo; + } + public void setDate(String _date){ + currentDate=_date; + } + public String getCurrentDate() { return currentDate; } - public Date getSleep_start() + + public int getScore(){ + return Score; + } + + public Time getSleep_start() { return sleep_start; } - public Date getSleep_end() + public Time getSleep_end() { return sleep_end; } /*** *根据参数创建睡眠检测记录, - * @param datevalue + * @ datevalue *当前测试的 日期 - * @param sleepstart + * @ sleepstart * 系统设置的 睡眠开始监测时间 例如晚上9点开始 - * @param sleepend - * 系统设置的 睡眠停止监测时间 例如第二天早7点 - * @param step_in_minutes - * 系统当前的 心跳间隔时间,单位分钟 + * @ sleepend + * 系统设置的 睡眠停止监测时间 例如第二天早9点 */ - public SleepInfo(Date datevalue , Date sleepstart,Date sleepend,int step_in_minutes){ - currentDate=datevalue; - sleep_start=sleepstart; - sleep_end=sleepend; - turnovertimesArray=new ArrayList<>(); - step_for_each_record_in_minutes=step_in_minutes; + + public SleepInfo(){ + currentDate= TimeUtil.getSystemDateYYMMDD(); } /*** @@ -53,19 +77,79 @@ public class SleepInfo { * 翻滚次数小于最低阈值DEEP_TURN_LEVEL 认为是深度睡眠 * 翻滚次数在最低阈值DEEP_TURN_LEVEL 和最高阈值 LIGHT_TURN_LEVEL 之间认为是浅度睡眠, * 翻滚次数大于最高阈值认为没有睡眠。 + * @param runtime * - * @param turnovertime */ - public void append_sleep_check_item(int turnovertime){ - System.currentTimeMillis(); - if (turnovertime <= DEEP_TURN_LEVEL){ - deepsleep_minutes+=step_for_each_record_in_minutes; - }else if (turnovertime > DEEP_TURN_LEVEL && turnovertime 0) { + for( i = 0;i < turnovertimesArray.size();i++ ){ + if (i>0){ + turnovertime = (turnovertimesArray.get(i).stepCount - turnovertimesArray.get(i-1).stepCount); + long minutes=TimeUtil.diffBetween2DateInMinutes(turnovertimesArray.get(i-1).date,turnovertimesArray.get(i).date); + 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); + } - turnovertimesArray.add(turnovertime); + } + /*** + * * 返回我们服务器要有深度睡眠与浅度睡眠的时长; + * * 手表展示睡眠评分规则: + * * 睡眠时间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); //深度睡眠/睡眠时间 百分比 + 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; diff --git a/app/src/main/java/com/enqualcomm/support/bean/SleepRecord.java b/app/src/main/java/com/enqualcomm/support/bean/SleepRecord.java new file mode 100644 index 00000000..21f31625 --- /dev/null +++ b/app/src/main/java/com/enqualcomm/support/bean/SleepRecord.java @@ -0,0 +1,34 @@ +package com.enqualcomm.support.bean; + +import com.enqualcomm.support.utils.TimeUtil; + +public class SleepRecord { + /*每次打点的记录表 SLEEP_DATE 代表记录睡眠的日期,例如 22日上午7:00的计入到21日的睡眠记录中。 DATE代表记录的时间,此出应该等于上文的 22日 7:00 */ +// sqlite.execSQL("CREATE TABLE SLEEP_RECORDS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,SLEEP_DATE TEXT,DATE TEXT, STEPCOUNT INTEGER ,STATUS TEXT )"); + /*计算后的分值,每天会在睡眠结束后计算Records获得*/ + //sqlite.execSQL("CREATE TABLE SLEEP_STATUS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,TIME TEXT , DEEP_SLEEP INTEGER , LIGHT_SLEEP INTEGER ,SCORE INTEGER )"); + public String date; + public String sleep_date; + public int stepCount; + public String status; + + public SleepRecord(Runtime runtime){ + date=TimeUtil.getSystemTime4(); + sleep_date=TimeUtil.getCurrentSleepRecordDate( date); + stepCount=runtime.getTotalStepCount(); + status=""; + } + + public SleepRecord(String _date,int _stepcount){ + date=_date; + sleep_date=TimeUtil.getCurrentSleepRecordDate( _date); + stepCount=_stepcount; + status=""; + } + public SleepRecord(String _date_yyyyMMDDhhmmss,String _sleep_date,int _stepcount){ + date=_date_yyyyMMDDhhmmss; + sleep_date=_sleep_date; + stepCount=_stepcount; + status=""; + } +} diff --git a/app/src/main/java/com/enqualcomm/support/utils/DBHelper.java b/app/src/main/java/com/enqualcomm/support/utils/DBHelper.java index 20f5ebc0..e1a67bae 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/DBHelper.java +++ b/app/src/main/java/com/enqualcomm/support/utils/DBHelper.java @@ -26,6 +26,8 @@ import com.enqualcomm.support.bean.DeviceStatus; import com.enqualcomm.support.bean.NoticeContent; import com.enqualcomm.support.bean.NotificationParam; import com.enqualcomm.support.bean.Silent; +import com.enqualcomm.support.bean.SleepInfo; +import com.enqualcomm.support.bean.SleepRecord; import com.enqualcomm.support.bean.StheartRate; import com.enqualcomm.support.bean.Weather; @@ -891,8 +893,6 @@ public class DBHelper { } - public void updateIcon2SystemContact() { - } public void updateMessage(Content var1, Uri var2) { ContentValues var3 = new ContentValues(); @@ -907,4 +907,84 @@ public class DBHelper { var3.put("LOCAL_TIME", var1.localTime); this.cr.update(var2, var3, (String)null, (String[])null); } + + + public void insertSleepRecord(SleepRecord sr){ + /*每次打点的记录表 SLEEP_DATE 代表记录睡眠的日期,例如 22日上午7:00的计入到21日的睡眠记录中。 DATE代表记录的时间,此出应该等于上文的 22日 7:00 */ +// sqlite.execSQL("CREATE TABLE SLEEP_RECORDS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,SLEEP_DATE TEXT,DATE TEXT, TIME TEXT,STEPCOUNT INTEGER ,ROLLTIMES INTEGER ,STATUS TEXT )"); + ContentValues values=new ContentValues(); + values.put("DATE",sr.date); + values.put("SLEEP_DATE",sr.sleep_date); + values.put("STEPCOUNT",sr.stepCount); + values.put("STATUS",sr.status); + this.cr.insert( ICContentProvider.SLEEP_RECORDS_URI, values); + } + + + public ArrayList querySleepRecord(String sleep_date) { + ArrayList sleeprecordList = new ArrayList(); + ContentResolver pcr = this.cr; + Uri table = ICContentProvider.SLEEP_RECORDS_URI; + StringBuilder where = new StringBuilder(); + where.append("SLEEP_DATE like '"); + where.append(sleep_date); + where.append("'"); + Cursor cr_sleeprecord = pcr.query(table, (String[])null, where.toString(), (String[])null, (String)null); + if (cr_sleeprecord != null) { + while (true) { + if (!cr_sleeprecord.moveToNext()) { + cr_sleeprecord.close(); + break; + } + sleeprecordList.add(new SleepRecord( + cr_sleeprecord.getString(1), cr_sleeprecord.getString(2), cr_sleeprecord.getInt(3))); + } + } + return sleeprecordList; + } + public void updateSleepInfo(SleepInfo si){ + /*计算后的分值,每天会在睡眠结束后计算Records获得*/ +// sqlite.execSQL("CREATE TABLE SLEEP_STATUS(_id INTEGER PRIMARY KEY AUTOINCREMENT ,DATE TEXT , DEEP_SLEEP INTEGER , LIGHT_SLEEP INTEGER ,SCORE INTEGER )"); + + ContentValues values = new ContentValues(); + values.put("DATE",si.getCurrentDate()); + values.put("DEEP_SLEEP",si.getDeepsleep_minutes()); + values.put("LIGHT_SLEEP",si.getLightsleep_minutes()); + values.put("SCORE",si.getScore()); + this.cr.insert( ICContentProvider.SLEEP_STATUS_URI, values); + } + + + public int querySleepScore(String dateyyyyMMddHHmmss){ + + ContentResolver pcr = this.cr; + Uri table = ICContentProvider.SLEEP_STATUS_URI; + StringBuilder where = new StringBuilder(); + where.append("DATE like '"); + where.append(dateyyyyMMddHHmmss); + where.append("'"); + Cursor cr_sleeprecord = pcr.query(table, (String[])null, where.toString(), (String[])null, (String)null); + if (cr_sleeprecord != null) { + int scroe=cr_sleeprecord.getInt(cr_sleeprecord.getColumnIndex("SCORE")); + cr_sleeprecord.close(); + return scroe; + } + return 0; + } + + public void insertEventMsg(String dateyyyyMMddHHmmss,int event_type,String event_name){ + //日志表格,存储开机关机,低电报警等消息。。。 +// sqlite.execSQL("CREATE TABLE EVENT_MESSAGE(_id INTEGER PRIMARY KEY AUTOINCREMENT , EVENT_TYPE INTEGER, EVENT_NAME TEXT, TIME TEXT )"); + + ContentValues values = new ContentValues(); + values.put("EVENT_TYPE",event_type); + values.put("EVENT_NAME",event_name); + values.put("TIME",dateyyyyMMddHHmmss); + this.cr.insert( ICContentProvider.EVENT_MESSAGE_URI, values); + + } + + + + } diff --git a/app/src/main/java/com/enqualcomm/support/utils/TimeUtil.java b/app/src/main/java/com/enqualcomm/support/utils/TimeUtil.java index ce5d3f4a..2b44ffe3 100644 --- a/app/src/main/java/com/enqualcomm/support/utils/TimeUtil.java +++ b/app/src/main/java/com/enqualcomm/support/utils/TimeUtil.java @@ -6,6 +6,7 @@ import android.os.SystemClock; import android.provider.Settings; import android.text.format.DateFormat; +import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -46,9 +47,9 @@ public class TimeUtil { } public static Date parseDatetiime(String datetimeStr){ - SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式 - SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式 - SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); +// SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式 +// SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式 +// SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss"); Date date1 = null; try { @@ -225,5 +226,76 @@ public class TimeUtil { public static String getDefaultTimeZone(){ return TimeZone.getDefault().getDisplayName(); } + + private static long HOURS12 = 12*60*60*1000 ; + private static long HOURS24 = 24*60*60*1000 ; + + public static String getCurrentSleepRecordDate(){ + return getCurrentSleepRecordDate(System.currentTimeMillis()); + } + public static String getCurrentSleepRecordDate(String date_yyyyMMddHHmmss){ + SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss"); + Date date1 = null; + try { + //获取当天的开始睡眠检测时间 + date1 = df3.parse(date_yyyyMMddHHmmss); + } catch (ParseException e) { + e.printStackTrace(); + return null; + } + return getCurrentSleepRecordDate(date1.getTime()); + } + public static boolean isInSleepRange=false; + public static String getCurrentSleepRecordDate(long currentMS){ + //睡眠检测时间跨天,所以计算要注意日期问题。 + //默认时间段是当天的晚上21点到次日的上午9点。跨度12小时 + + /** currentDate yyyyMMdd */ + String currentDate=getSystemTime4(currentMS); + SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMddHHmmss"); + + Date date1 = null; + try { + //获取当天的开始睡眠检测时间 + date1 = df3.parse(currentDate+"210000"); + } catch (ParseException e) { + e.printStackTrace(); + } + + if (date1!=null) { + //当天开始睡眠检测时间 + long sleep_check_starttime= date1.getTime(); + + long sleep_check_endtime=sleep_check_starttime+HOURS12; + + + long sleep_check_starttime_yestoday = sleep_check_starttime - HOURS24; + long sleep_check_endtime_yestoday=sleep_check_starttime_yestoday+HOURS12; + + if (currentMS > sleep_check_starttime && currentMS sleep_check_starttime_yestoday && currentMS sleep_check_endtime_yestoday && currentMS