//蓝牙指令 import 'dart:convert'; import 'package:EasyDartModule/EasyDartModule.dart' as edm; import 'package:easydevice/src/app/thapp.dart'; import 'package:ef/ef.dart'; import 'package:vbvs_app/common/util/DailyLogUtils.dart'; // wifi列表指令 getWifiList(THapp tHapp) async { try { print("wscan scan"); edm.EasyDartModule.logger.info("发送请求网络列表指令"); DailyLogUtils.writeLog("发送请求网络列表指令"); List data = []; var wifilist = await tHapp.send("wscan scan", true, (log) { print("[aaaaaa]${log.log}"); if (log.log.contains("SCAN RESULT OVER!")) { final wifiList = >[]; final items = log.log.split('[wifi]: SCAN RESULT ITEM:'); final reg = RegExp(r'SSID=([^\t\r\n]+)\s+RSSI=(-?\d+)\s*,\s*auth\s*=\s*(\d+)'); for (var item in items) { final match = reg.firstMatch(item); if (match != null) { wifiList.add({ 'ssid': match.group(1), 'rssi': int.parse(match.group(2)!), 'auth': int.parse(match.group(3)!), }); } } print('解析得到 Wi-Fi 列表: $wifiList'); if (wifiList.length != 0) { log.result = wifiList; data = wifiList; log.over = true; return true; } } return false; }, 1); return wifilist; } catch (e) { print(e); } return []; } getWifiStatus(THapp tHapp) async { edm.EasyDartModule.logger.info("发送请求设备网络状态指令"); DailyLogUtils.writeLog("发送请求设备网络状态指令"); var result = await tHapp.send( "wl show", true, (ss) { var log = ss.log; final match = RegExp(r'status=([^\s]+)').firstMatch(log); final status = match?.group(1); if (status != null) { print('提取到的 status: $status'); if (status == 'connect') { ss.result = true; ss.over = true; return true; } else { ss.result = false; ss.over = true; return false; } } else { return false; } }, ); return result; } Future sendWifiSetting(wifiItem, String password, THapp tHapp) async { try { edm.EasyDartModule.logger.info("发送wifi配置指令"); DailyLogUtils.writeLog("发送wifi配置指令->"); // String cmd = "vtouch save update -a -i .wifi.sta.auth=${wifiItem['auth']} " // ".wifi.sta.ssid=${wifiItem['ssid']} .wifi.sta.pwd=$password"; String cmd = "vtouch save update -a -i .wifi.sta.auth=${wifiItem['auth']} " ".wifi.sta.ssid=\"${wifiItem['ssid']}\" .wifi.sta.pwd=\"$password\""; ef.log("[wifi配置指令]:${cmd}"); final success = await tHapp.send(utf8.encode(cmd), true, (log) { if (log.log.contains("update parm is successful")) { print("[wifi456]:" + log.log); edm.EasyDartModule.logger.info("WiFi配置参数成功-》log:$log"); DailyLogUtils.writeLog("WiFi配置参数成功->log:$log"); log.result = true; log.over = true; return true; } return false; }, 10, 3000); if (!success) { edm.EasyDartModule.logger.error("WiFi配置超时或失败"); DailyLogUtils.writeLog("WiFi配置超时或失败"); } return success; } catch (e) { edm.EasyDartModule.logger.error("发送wifi配置指令异常: ${e.toString()}"); DailyLogUtils.writeLog("发送wifi配置指令异常-> ${e.toString()}"); return false; } } getDeviceWifiStatus( THapp tHapp, int times, { bool link = false, }) async { DailyLogUtils.writeLog("发送请求设备已配置网络状态指令"); 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; // 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); // 打印并返回 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, 3000); if (!success) { return success; } return result; } catch (e) { print(e); } finally { if (stream != null) { stream.cancel(); } } } //只有4g并且没有wifi Future getDeviceNetVersion(THapp tHapp, int times) async { edm.EasyDartModule.logger.info("发送请求设备的网络信息"); DailyLogUtils.writeLog("发送请求设备的网络信息"); print("ls /root/mnt"); String netType = "unknown"; try { var result = await tHapp.send("ls /root/mnt", true, (ss) { var log = ss.log; print("[获取]$log"); if (log.contains("wifi.json") || log.contains("a76xx.json")) { if (log.contains("wifi.json")) { netType = "wifi"; } else if (log.contains("a76xx.json")) { netType = "4g"; } else { netType = "unknown"; } ss.over = true; ss.result = netType; // 继续监听 return true; } ss.over = false; return false; }, times, 15000); return netType; } catch (e) { print(e); return netType; } } /// 发送关闭blog日志与开启业务日志的指令 Future 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 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 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 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; } }