更新睡眠习惯

This commit is contained in:
wyf
2025-07-30 18:01:46 +08:00
parent 300e3b31f6
commit 699ed03672
4 changed files with 138 additions and 108 deletions

View File

@@ -306,6 +306,100 @@ class MHTBlueToothController extends GetControllerEx<MHTBlueToothModel> {
return result; // 在 requestWithLog 完成之后返回 result
}
saveJiYiData(sleepData) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.user_setting;
String type = "sleep_jiyi_${sleepData['mac']}";
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
var data = {
"type": type,
"mac": sleepData['mac'],
"time": DateTime.now().millisecondsSinceEpoch,
"data": sleepData,
};
await requestWithLog(
logTitle: "更新睡眠记忆",
method: MyHttpMethod.put,
queryUrl: queryUrl,
data: data,
);
}
loadJiYiData(String mac, {int time = 3}) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.user_setting;
String type = "sleep_jiyi_${mac}";
String queryUrl =
"${serviceAddress}${serviceName}${serviceApi}?type=${type}";
// 使用 Future 来等待异步操作完成
Map<String, dynamic> result = {};
await requestWithLog(
logTitle: "更新记忆",
method: MyHttpMethod.get,
queryUrl: queryUrl,
onSuccess: (res) {
ef.log("加载记忆成功: ${res.data}");
result = res.data; // 将返回的数据存入 result
},
onFailure: (res) {
ef.log("加载记忆失败: ${res.msg}");
result = {}; // 如果失败,可以返回空的 Map
},
);
return result; // 在 requestWithLog 完成之后返回 result
}
saveMattressTimeData(sleepData) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.user_setting;
String type = "sleep_new_time_${sleepData['mac']}";
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
var data = {
"type": type,
"mac": sleepData['mac'],
"time": DateTime.now().millisecondsSinceEpoch,
"data": sleepData,
};
await requestWithLog(
logTitle: "更新新版倒计时",
method: MyHttpMethod.put,
queryUrl: queryUrl,
data: data,
);
}
loadMattressTimeData(String mac,{int time = 3}) async {
String serviceAddress = ServiceConstant.service_address;
String serviceName = ServiceConstant.server_service;
String serviceApi = ServiceConstant.user_setting;
String type = "sleep_new_time_${mac}";
String queryUrl =
"${serviceAddress}${serviceName}${serviceApi}?type=${type}";
// 使用 Future 来等待异步操作完成
Map<String, dynamic> result = {};
await requestWithLog(
logTitle: "更新新版倒计时",
method: MyHttpMethod.get,
queryUrl: queryUrl,
onSuccess: (res) {
ef.log("更新新版倒计时成功: ${res.data}");
result = res.data; // 将返回的数据存入 result
},
onFailure: (res) {
ef.log("更新新版倒计时失败: ${res.msg}");
result = {}; // 如果失败,可以返回空的 Map
},
);
return result; // 在 requestWithLog 完成之后返回 result
}
//todo 解绑的时候删除自己所拥有的所有设备的睡眠习惯
}

View File

@@ -163,6 +163,50 @@ class WebviewTestController extends GetControllerEx<WebviewTestModel> {
}
return true;
});
//--
bridge.sdk.saveMattressTimeData((args) async {
ef.log('更新睡眠习惯: $args[0]');
try {
MHTBlueToothController blueToothController = Get.find();
await blueToothController.saveMattressTimeData(args[0]);
} catch (e) {
ef.log("[更新睡眠习惯失败]:$e");
}
return true;
});
bridge.sdk.loadMattressTimeData((args) async {
ef.log('查询睡眠习惯: $args[0]');
try {
MHTBlueToothController blueToothController = Get.find();
var sleepData = await blueToothController.loadMattressTimeData(args[0]);
return sleepData['data'];
} catch (e) {
ef.log("[查询睡眠习惯失败]:$e");
}
return true;
});
bridge.sdk.saveMemory((args) async {
ef.log('更新记忆: $args[0]');
try {
MHTBlueToothController blueToothController = Get.find();
// await blueToothController.saveHabitData(args[0]);
await blueToothController.saveJiYiData(args[0]);
} catch (e) {
ef.log("[更新记忆失败]:$e");
}
return true;
});
bridge.sdk.loadMemory((args) async {
try {
ef.log('查询记忆: $args[0]');
MHTBlueToothController blueToothController = Get.find();
var sleepData = await blueToothController.loadJiYiData(args[0]);
return sleepData['data'];
} catch (e) {
ef.log("[查询记忆失败]:$e");
}
return true;
});
bridge.sdk.webPageBuild((args) async {
ef.log('网页加载完成: $args[0]');
try {

View File

@@ -164,114 +164,6 @@ class SnoreWaveform extends StatelessWidget {
}
}
// class SnoreWaveformPainter extends CustomPainter {
// final List<dynamic> snoreValues;
// final int startTime;
// final int endTime;
// SnoreWaveformPainter({
// required this.snoreValues,
// required this.startTime,
// required this.endTime,
// });
// @override
// void paint(Canvas canvas, Size size) {
// final double width = size.width;
// final double height = size.height;
// final double centerY = height / 2;
// final double totalDuration = (endTime - startTime).toDouble();
// final double pixelPerMs = width / totalDuration;
// final Paint wavePaint = Paint()
// ..color = stringToColor("#8E7DEF")
// ..strokeWidth = 1.5
// ..style = PaintingStyle.stroke;
// final Path upperPath = Path();
// final Path lowerPath = Path();
// const double scaleY = 0.5; //波形图比例
// for (int i = 0; i < snoreValues.length; i++) {
// final timestamp = snoreValues[i]["st"];
// final value = snoreValues[i]["value"]?.toDouble() ?? 0;
// final x = (timestamp - startTime) * pixelPerMs;
// final y = centerY - value * scaleY;
// final yMirror = centerY + value * scaleY;
// if (i == 0) {
// upperPath.moveTo(x, y);
// lowerPath.moveTo(x, yMirror);
// } else {
// upperPath.lineTo(x, y);
// lowerPath.lineTo(x, yMirror);
// }
// }
// canvas.drawPath(upperPath, wavePaint);
// canvas.drawPath(lowerPath, wavePaint);
// final Paint axisPaint = Paint()
// ..color = Colors.grey
// ..strokeWidth = 0.5;
// // 画中心线
// canvas.drawLine(Offset(0, centerY), Offset(width, centerY), axisPaint);
// // 时间刻度绘制
// final textPainter = TextPainter(
// textAlign: TextAlign.center,
// textDirection: ui.TextDirection.ltr,
// );
// final int hourMs = 60 * 60 * 1000;
// // 循环绘制整点小时标签(不包含终点)
// for (int t = startTime; t < endTime; t += hourMs) {
// double x = (t - startTime) * pixelPerMs;
// DateTime dt = DateTime.fromMillisecondsSinceEpoch(t);
// String label;
// if (t == startTime) {
// label = DateFormat('HH:mm').format(dt); // 起点显示 HH:mm
// } else {
// label = DateFormat('h').format(dt); // 中间显示小时不带前导0
// }
// textPainter.text = TextSpan(
// text: label,
// style: TextStyle(fontSize: 10, color: Colors.grey),
// );
// textPainter.layout();
// textPainter.paint(
// canvas,
// Offset(x - textPainter.width / 2, height + 20.rpx),
// );
// }
// // 单独绘制终点时间标签,确保显示具体时分
// {
// double x = (endTime - startTime) * pixelPerMs;
// DateTime dt = DateTime.fromMillisecondsSinceEpoch(endTime);
// String label = DateFormat('HH:mm').format(dt);
// textPainter.text = TextSpan(
// text: label,
// style: TextStyle(fontSize: 10, color: Colors.grey),
// );
// textPainter.layout();
// textPainter.paint(
// canvas,
// Offset(x - textPainter.width / 2, height + 20.rpx),
// );
// }
// }
// @override
// bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
// }
class SnoreWaveformPainter extends CustomPainter {
final List<dynamic> snoreValues;
final int startTime;