更新眠花糖兼容wifi配置

This commit is contained in:
wyf
2025-10-28 14:51:28 +08:00
parent 7cfd3bf8f5
commit 3c5db3bdf2
14 changed files with 1259 additions and 882 deletions

View File

@@ -100,7 +100,7 @@ Future<bool> sendWifiSetting(wifiItem, String password, THapp tHapp) async {
return true;
}
return false;
}, 1);
}, 10, 3000);
if (!success) {
edm.EasyDartModule.logger.error("WiFi配置超时或失败");
@@ -119,69 +119,85 @@ getDeviceWifiStatus(
int times, {
bool link = false,
}) async {
edm.EasyDartModule.logger.info("发送请求设备已配置网络状态指令");
DailyLogUtils.writeLog("发送请求设备已配置网络状态指令");
print("at+system info");
bool success = false;
final now = DateTime.now();
final currenttIme =
"${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} "
"${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}";
edm.EasyDartModule.logger.info("[aaa:配置开始]发送请求设备已配置网络状态指令:${currenttIme}");
ef.log("[aaa:配置开始]${currenttIme}");
var stream = tHapp.logingStream.listen((data) {
ef.log("[设备日志]:${data}");
});
try {
var result = await tHapp.send(
"at+system info",
true,
(ss) {
var log = ss.log;
// 匹配设备状态
final statusMatch = RegExp(r'Status=([^\s]+)').firstMatch(log);
final status = statusMatch?.group(1);
if (status != null) {
print('提取到的 status: $status');
var result = await tHapp.send("at+system info", true, (ss) {
var log = ss.log;
// ef.log("[设备WiFi配置信息]:${log}");
edm.EasyDartModule.logger.info("设备WiFi配置信息->log:$log");
// 匹配设备状态
final statusMatch = RegExp(r'Status=([^\s]+)').firstMatch(log);
final status = statusMatch?.group(1);
print('提取到的 status: 为空');
if (status != null) {
// print('提取到的 status: $status');
final now = DateTime.now();
final formattedTime =
"${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} "
"${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}";
// 如果设备连接状态是 "connect",继续检测
if (status.contains('connect')) {
// 匹配 Wi-Fi 连接信息
final wifiInfoMatch = RegExp(
r'WIFI CONNECTED INFO:SSID=([^\s]+),RSSI=([-0-9]+),AUTH=([0-9]+),CH=([0-9]+),BSSID=([A-F0-9]+)')
.firstMatch(log);
if (wifiInfoMatch != null) {
final ssid = wifiInfoMatch.group(1);
final rssi = wifiInfoMatch.group(2);
final auth = wifiInfoMatch.group(3);
final ch = wifiInfoMatch.group(4);
final bssid = wifiInfoMatch.group(5);
// 如果设备连接状态是 "connect",继续检测
if (status.contains('connect')) {
// 匹配 Wi-Fi 连接信息
final wifiInfoMatch = RegExp(
r'WIFI CONNECTED INFO:SSID=([^\s]+),RSSI=([-0-9]+),AUTH=([0-9]+),CH=([0-9]+),BSSID=([A-F0-9]+)')
.firstMatch(log);
if (wifiInfoMatch != null) {
final ssid = wifiInfoMatch.group(1);
final rssi = wifiInfoMatch.group(2);
final auth = wifiInfoMatch.group(3);
final ch = wifiInfoMatch.group(4);
final bssid = wifiInfoMatch.group(5);
// 打印并返回 Wi-Fi 信息
print(
'Wi-Fi 信息: SSID=$ssid, RSSI=$rssi, AUTH=$auth, CH=$ch, BSSID=$bssid');
// 停止监听并返回信息
ss.result = {
'ssid': ssid,
'rssi': rssi,
'auth': auth,
'ch': ch,
'bssid': bssid,
};
ss.over = true;
success = true;
return true;
}
} else {
if (!link) {
ss.over = true;
return true;
}
// 打印并返回 Wi-Fi 信息
print(
'Wi-Fi 信息: SSID=$ssid, RSSI=$rssi, AUTH=$auth, CH=$ch, BSSID=$bssid');
final currenttIme =
"${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} "
"${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}";
ef.log("[aaa:配置结束]${currenttIme}");
// 停止监听并返回信息
ss.result = {
'ssid': ssid,
'rssi': rssi,
'auth': auth,
'ch': ch,
'bssid': bssid,
};
ss.over = true;
success = true;
return true;
}
} else {
if (!link) {
ss.over = true;
return true;
}
}
// 继续监听
return false;
},
times,
);
if(!success){
}
// 继续监听
return false;
}, times, 3000);
if (!success) {
return success;
}
return result;
} catch (e) {
print(e);
} finally {
if (stream != null) {
stream.cancel();
}
}
}
@@ -221,3 +237,207 @@ Future<String> getDeviceNetVersion(THapp tHapp, int times) async {
return netType;
}
}
/// 发送关闭blog日志与开启业务日志的指令
Future<bool> sendBlogAndDlogCommands(THapp tHapp) async {
edm.EasyDartModule.logger.info("发送 blog disable 与 dlog on business 指令");
DailyLogUtils.writeLog("发送 blog disable 与 dlog on business 指令");
try {
// 第一步:发送 blog disable
await Future.delayed(const Duration(seconds: 20));
ef.log("[发送指令] blog disable");
var blogResult = await tHapp.send("blog disable", true, (ss) {
var log = ss.log;
if (log.contains("disable") ||
log.contains("ok") ||
log.contains("success")) {
ef.log("[blog disable 响应] ${log}");
ss.result = true;
ss.over = true;
return true;
}
return true;
}, 0, 1000);
// 第二步:发送 dlog on business
ef.log("[发送指令] dlog on business");
var dlogResult = await tHapp.send("dlog on hal", true, (ss) {
var log = ss.log;
if (log.contains("on business") ||
log.contains("ok") ||
log.contains("success")) {
ef.log("[dlog on business 响应] ${log}");
ss.result = true;
ss.over = true;
return true;
}
return true;
}, 0, 1000);
if (!dlogResult) {
edm.EasyDartModule.logger.error("dlog on business 指令执行失败或超时");
DailyLogUtils.writeLog("dlog on business 指令执行失败或超时");
return false;
}
edm.EasyDartModule.logger.info("blog disable 与 dlog on business 指令发送完成");
DailyLogUtils.writeLog("blog disable 与 dlog on business 指令发送完成");
return true;
} catch (e) {
edm.EasyDartModule.logger.error("发送 blog/dlog 指令异常: ${e.toString()}");
DailyLogUtils.writeLog("发送 blog/dlog 指令异常 -> ${e.toString()}");
return false;
}
}
/// 执行 wscan close -> 延迟 1s -> wscan open
Future<bool> sendCloseAndOpenWscanCommand(THapp tHapp) async {
edm.EasyDartModule.logger.info("执行 wscan close -> wscan open 指令");
DailyLogUtils.writeLog("执行 wscan close -> wscan open 指令开始");
try {
// 第一步:关闭 wscan
ef.log("[发送指令] wscan close");
edm.EasyDartModule.logger.info("发送 wscan close 指令");
DailyLogUtils.writeLog("发送 wscan close 指令");
await tHapp.send("wscan close", true, (ss) {
// 不判断是否成功,直接结束
ss.over = true;
return true;
}, 0, 1000);
// 等待 1 秒再执行下一个指令
await Future.delayed(const Duration(seconds: 1));
// 第二步:开启 wscan
ef.log("[发送指令] wscan open");
edm.EasyDartModule.logger.info("发送 wscan open 指令");
DailyLogUtils.writeLog("发送 wscan open 指令");
await tHapp.send("wscan open", true, (ss) {
// 不判断是否成功,直接结束
ss.over = true;
return true;
}, 0, 1000);
edm.EasyDartModule.logger.info("wscan close -> wscan open 指令执行完成");
DailyLogUtils.writeLog("wscan close -> wscan open 指令执行完成");
ef.log("[执行完成] wscan close -> wscan open");
await Future.delayed(const Duration(seconds: 3));
return true;
} catch (e) {
edm.EasyDartModule.logger
.error("执行 wscan close/open 指令异常: ${e.toString()}");
DailyLogUtils.writeLog("执行 wscan close/open 指令异常 -> ${e.toString()}");
ef.log("[异常] 执行 wscan close/open 失败: ${e.toString()}");
return false;
}
}
/// 查询设备内存状态(指令: free all
///
/// 示例返回:
/// 2025-10-27 11:47:22 当前系统内存剩余:895643 字节
/// 2025-10-27 11:47:22 关键内存剩余:27111 字节 , jiffies = 164029
///
/// 若匹配到“关键内存剩余:xxxx 字节”,其中 xxxx < 20000 返回 false
/// 否则返回 true。
Future<bool> queryMemory(THapp tHapp, {int times = 1}) async {
edm.EasyDartModule.logger.info("发送查询内存状态指令 (free all)");
DailyLogUtils.writeLog("发送查询内存状态指令 (free all)");
try {
final result = await tHapp.send("free all", true, (ss) {
final log = ss.log;
ef.log("[内存查询日志]$log");
// 匹配关键内存剩余:xxxxx 字节
final match = RegExp(r'关键内存剩余:(\d+)\s*字节').firstMatch(log);
if (match != null) {
final freeValue = int.tryParse(match.group(1) ?? "0") ?? 0;
edm.EasyDartModule.logger
.info("提取到关键内存剩余: $freeValue 字节");
DailyLogUtils.writeLog("提取到关键内存剩余: $freeValue 字节");
// 判断内存是否充足
final bool isEnough = freeValue > 20000;
ss.result = isEnough;
ss.over = true;
return true;
}
return false; // 未匹配到继续监听
}, times, 3000);
// result 就是 send 内部设置的 ss.result
if (result is bool) {
edm.EasyDartModule.logger
.info("内存检测结果 -> ${result ? "充足" : "不足"}");
DailyLogUtils.writeLog("内存检测结果 -> ${result ? "充足" : "不足"}");
return result;
}
edm.EasyDartModule.logger.error("未检测到有效内存信息");
DailyLogUtils.writeLog("未检测到有效内存信息");
return false;
} catch (e) {
edm.EasyDartModule.logger.error("查询内存状态异常: ${e.toString()}");
DailyLogUtils.writeLog("查询内存状态异常 -> ${e.toString()}");
return false;
}
}
/// 重启设备指令
///
/// 发送 "reboot" 命令,若响应中包含 reboot / restart / ok / success 等关键字
/// 视为执行成功,返回 true否则返回 false。
Future<bool> rebootDevice(THapp tHapp) async {
edm.EasyDartModule.logger.info("发送设备重启指令 (reboot)");
DailyLogUtils.writeLog("发送设备重启指令 (reboot)");
try {
ef.log("[发送指令] reboot");
final result = await tHapp.send("reboot", true, (ss) {
final log = ss.log;
ef.log("[设备重启响应]$log");
// 匹配常见的重启反馈
if (log.contains("reboot") ||
log.contains("restart") ||
log.contains("ok") ||
log.contains("success")) {
edm.EasyDartModule.logger.info("设备重启命令执行成功");
DailyLogUtils.writeLog("设备重启命令执行成功");
ss.result = true;
ss.over = true;
return true;
}
// 继续监听
return false;
}, 1, 3000);
// 如果 tHapp.send 没返回 true记录失败
if (result != true) {
edm.EasyDartModule.logger.error("设备重启指令执行失败或超时");
DailyLogUtils.writeLog("设备重启指令执行失败或超时");
return false;
}
edm.EasyDartModule.logger.info("设备重启指令执行完成");
DailyLogUtils.writeLog("设备重启指令执行完成");
return true;
} catch (e) {
edm.EasyDartModule.logger.error("发送设备重启指令异常: ${e.toString()}");
DailyLogUtils.writeLog("发送设备重启指令异常 -> ${e.toString()}");
ef.log("[异常] 执行 reboot 失败: ${e.toString()}");
return false;
}
}