diff --git a/index/index.js b/index/index.js index 8f3f720..098ff6a 100644 --- a/index/index.js +++ b/index/index.js @@ -82,8 +82,8 @@ Page({ // 连接指定设备 // if(device.deviceId=="03:02:02:07:65:98"){ // if(device.deviceId=="03:02:02:07:5C:B7"){ - if(device.deviceId=="03:02:02:07:5E:C6"){ - // if(device.deviceId=="03:02:02:07:5E:BE"){ + // if(device.deviceId=="03:02:02:07:5E:C6"){ + if(device.deviceId=="03:02:02:07:62:3E"){ this.connectToDevice(device.deviceId); } }); @@ -169,8 +169,8 @@ Page({ }); wx.onBLECharacteristicValueChange((res) => { - const hexStr = this.uint8ArrayToHex(new Uint8Array(res.value)) - console.log("收到手表端发送的数据>>>>>>:",hexStr); + // const hexStr = this.uint8ArrayToHex(new Uint8Array(res.value)) + // console.log("收到手表端发送的数据>>>>>>:",hexStr); const dataView = new DataView(res.value); // 获取第一个字节 let cmdType = dataView.getUint8(0); @@ -300,10 +300,10 @@ Page({ let list = this.data.sleepList; if(list.length > 0){ - console.log("list:",list) + // console.log("list:",list) // 补全数据 let completeList = CommonUtil.completeMissingData(list); - console.log("completeList:",completeList); + // console.log("completeList:",completeList); // 组装数据 let arr = CommonUtil.assembleSleepData(completeList); // 组装后数据进行排序 @@ -336,8 +336,10 @@ Page({ sleepList.sort((a, b) => { return Number(a.minute) - Number(b.minute); // b.minute > a.minute 排前面 }); - // console.log("日期打印:",sleepList[0].yyyyMMdd) - console.log("排序后数据:",sleepList) + if(sleepList.length > 0){ + console.log("日期打印:",sleepList[0].yyyyMMdd) + } + // console.log("排序后数据:",sleepList) // 获取当前已存在的 sleepList(如果有) let currentSleepList = this.data.sleepList || []; @@ -356,8 +358,8 @@ Page({ if(this.data.sleepCount % 50 == 0){ // 如果大于等于50次都没有获取到睡眠结束符,则再次从上一次的位置获取睡眠数据; const sleepPacket = DataPacket.generateReadSleepDataPacket(); - console.log("继续获取睡眠数据:",sleepPacket) - console.log("sleepCount:",this.data.sleepCount) + // console.log("继续获取睡眠数据:",sleepPacket) + // console.log("sleepCount:",this.data.sleepCount) SendCommandUtil.sendPackage(this.data.deviceId,sleepPacket); } @@ -678,8 +680,19 @@ Page({ return parseInt(Y); }, testFoun(){ - let state = CommonUtil.isNextDay("20250131","20250201"); - console.log("state",state) + // let state = CommonUtil.isNextDay("20250131","20250201"); + // console.log("state",state) + + // if(time >= dateRange.startTimestamp && time <= dateRange.endTimestamp){ + // // 在范围内,是同一天睡眠 + // return true; + // } + if(1738068240000 >= 1737979200000 && 1738068240000 <= 1738033200000){ + // 在范围内,是同一天睡眠 + console.log("1") + }else{ + console.log("2") + } }, }); \ No newline at end of file diff --git a/utils/CommonUtil.js b/utils/CommonUtil.js index 17e8f5b..e8c6da7 100644 --- a/utils/CommonUtil.js +++ b/utils/CommonUtil.js @@ -194,24 +194,48 @@ const CommonUtil = { sleepArray.forEach((item,index) => { // 判断是否是同一天的数据 let isSameDay = this.timeComparison(compareDate,compareMinute, item.yyyyMMdd, item.minute); - + // if(compareDate== "20250128"){ + // console.log("是否是同一天:",isSameDay) + // } // 如果状态发生变化,且日期不同,就表示开始新的睡眠数据 - if (item.status !== firstSleepData.status) { - if (isSameDay) { - resItem.sleepList.push(item); - } else { - // 取出数据 - let lastItem = sleepArray[index - 1]; - lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点 - resItem.sleepList.push(lastItem); - res.push(resItem); // 否则,将当天的数据推入结果数组 - resItem = { // 新的一天开始 - date: item.yyyyMMdd, - sleepList: [item] // 将当前项作为新的睡眠数据开始 - }; - compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据 - compareMinute = item.minute; - } + // if (item.status !== firstSleepData.status) { + // if (isSameDay) { + // resItem.sleepList.push(item); + // } else { + // // 取出数据 + // let lastItem = sleepArray[index - 1]; + // lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点 + // resItem.sleepList.push(lastItem); + // res.push(resItem); // 否则,将当天的数据推入结果数组 + // resItem = { // 新的一天开始 + // date: item.yyyyMMdd, + // sleepList: [item] // 将当前项作为新的睡眠数据开始 + // }; + // compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据 + // compareMinute = item.minute; + // } + // } + // firstSleepData = item; // 更新当前比对的睡眠数据 + + + if(isSameDay && item.status !== firstSleepData.status){ + // 如果是一天的睡眠 + resItem.sleepList.push(item); + }else{ + // 如果不是同一天的睡眠 + if(!isSameDay){ + let lastItem = sleepArray[index - 1]; + // console.log("lastItem:",lastItem) + lastItem.minute = lastItem.minute + 1;//加1分钟作为起床点 + resItem.sleepList.push(lastItem); + res.push(resItem); // 否则,将当天的数据推入结果数组 + resItem = { // 新的一天开始 + date: item.yyyyMMdd, + sleepList: [item] // 将当前项作为新的睡眠数据开始 + }; + compareDate = item.yyyyMMdd; // 更新对比日期,用于比对后续数据 + compareMinute = item.minute; + } } firstSleepData = item; // 更新当前比对的睡眠数据 }); @@ -229,23 +253,12 @@ const CommonUtil = { /** - * 计算出一天的时间范围 + * 计算出一天的时间范围,手表计算睡眠的时间为20:00~11:00(即晚上20点至第二天的11点) * @param {对比日期} compareDate */ getDayTimeRange(compareDate,compareMinute) { // 将 "YYYYMMDD" 格式的日期转换为 "YYYY-MM-DD" 格式,方便解析 const formattedDateStr = compareDate.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'); - // 创建目标日期对象 - // const date = new Date(formattedDateStr); // 例如:2024-12-12 - - // 获取对比日期的Date,前一天的20:00:00 - // const prevDayStart = new Date(date); - // prevDayStart.setDate(date.getDate() - 1); //设置为前一天 - // prevDayStart.setHours(20, 0, 0, 0); // 设置为20:00:00 - // // 获取对比日期的Date,后一天的10:00:00 - // const nextDayEnd = new Date(date); - // nextDayEnd.setDate(date.getDate()); // 设置为后一天 - // nextDayEnd.setHours(10, 0, 0, 0); // 设置为10:00:00 // 获取对比日期的Date,前一天的20:00:00 const date1 = new Date(formattedDateStr) @@ -256,15 +269,15 @@ const CommonUtil = { } else { prevDayStart.setDate(date1.getDate() - 1); } - prevDayStart.setHours(20, 0, 0, 0); // 设置为20:00:00 - // 获取对比日期的Date,后一天的10:00:00 + prevDayStart.setHours(20, 0, 0, 0); + // 获取对比日期的Date,后一天的11:00:00 const nextDayEnd = new Date(date2); if(this.isEveningTime(compareMinute)){ nextDayEnd.setDate(date2.getDate() + 1); } else { nextDayEnd.setDate(date2.getDate()); } - nextDayEnd.setHours(10, 0, 0, 0); // 设置为10:00:00 + nextDayEnd.setHours(11, 0, 0, 0); // 返回时间戳 return { @@ -311,6 +324,10 @@ const CommonUtil = { let dateRange = this.getDayTimeRange(compareDate,compareMinute); let time = this.getTimestampFromDateAndMinutes(comparisonValue,minute); + // if(compareDate == "20250128"){ + // console.log(`start:${dateRange.startTimestamp}-end:${dateRange.endTimestamp}-time${time}`) + // console.log(`${compareDate}-${compareMinute}-${comparisonValue}-${minute}`) + // } if(time >= dateRange.startTimestamp && time <= dateRange.endTimestamp){ // 在范围内,是同一天睡眠 return true; @@ -320,15 +337,6 @@ const CommonUtil = { sortSleep(data){ data.forEach((item) =>{ - // 排序算法 - // item.sleepList.sort((a, b) => { - // // 先比较 yyyyMMdd - // if (a.yyyyMMdd !== b.yyyyMMdd) { - // return a.yyyyMMdd.localeCompare(b.yyyyMMdd); - // } - // // 再比较 minute - // return a.minute - b.minute; - // }); // 因为后端的接口处理方式为如果第一个数据为清醒,不会作为睡眠时间,故将第一个数据作为浅睡 item.sleepList[0].status = 1 item.date = item.sleepList[item.sleepList.length - 1].yyyyMMdd @@ -337,7 +345,7 @@ const CommonUtil = { }, /** - * + * 睡眠计算时间是晚上的20:00~到第二天上午11:00,即上午11点~晚上20点间隔540分钟 * @param {补全数据} data */ completeMissingData(data){ @@ -360,7 +368,7 @@ const CommonUtil = { if (i < data.length - 1) { let currentMinute = data[i].minute; let nextMinute = data[i + 1].minute; - if(data[i].yyyyMMdd == data[i + 1].yyyyMMdd && nextMinute - currentMinute < 600){ + if(data[i].yyyyMMdd == data[i + 1].yyyyMMdd && nextMinute - currentMinute < 540){ // 如果不连续,补全缺失的数据 if (nextMinute > currentMinute + 1) { for (let j = currentMinute + 1; j < nextMinute; j++) { @@ -378,10 +386,10 @@ const CommonUtil = { if(CommonUtil.isNextDay(currentDate,nextDate) && currentMinute > 1200 && nextDate > 480 ){ - console.log(currentDate,currentMinute); - console.log(nextDate,nextMinute); - console.log("相减的差值:",nextMinute - currentMinute); - console.log("是否跨天:",CommonUtil.isNextDay(currentDate,nextDate)); + // console.log(currentDate,currentMinute); + // console.log(nextDate,nextMinute); + // console.log("相减的差值:",nextMinute - currentMinute); + // console.log("是否跨天:",CommonUtil.isNextDay(currentDate,nextDate)); if(currentMinute < 1439){ for (let j = currentMinute + 1; j < 1440; j++) { result.push({ @@ -401,30 +409,7 @@ const CommonUtil = { } } } - - // if(nextMinute - currentMinute < 1){ - // let currentDate = parseInt(data[i].yyyyMMdd); - // let nextDate = parseInt(data[i + 1].yyyyMMdd); - // if((nextDate - currentDate) == 1){ - // for (let j = currentMinute + 1; j <= 1440; j++) { - // result.push({ - // minute: j, - // status: 3, - // yyyyMMdd: data[i].yyyyMMdd - // }); - // } - - // for (let j = 0; j < nextMinute; j++) { - // result.push({ - // minute: j, - // status: 3, - // yyyyMMdd: data[i].yyyyMMdd - // }); - // } - // } - // } } - } }