|
@@ -1,4 +1,459 @@
|
|
|
package xn.update;
|
|
|
|
|
|
-public class StartActivity {
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
+import android.os.AsyncTask;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.CountDownTimer;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+import android.widget.EditText;
|
|
|
+
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.core.util.Pair;
|
|
|
+
|
|
|
+import com.blankj.utilcode.util.ActivityUtils;
|
|
|
+import com.blankj.utilcode.util.AppUtils;
|
|
|
+import com.blankj.utilcode.util.FileUtils;
|
|
|
+import com.blankj.utilcode.util.GsonUtils;
|
|
|
+import com.blankj.utilcode.util.LogUtils;
|
|
|
+import com.blankj.utilcode.util.SPUtils;
|
|
|
+import com.blankj.utilcode.util.ShellUtils;
|
|
|
+import com.blankj.utilcode.util.ThreadUtils;
|
|
|
+import com.blankj.utilcode.util.TimeUtils;
|
|
|
+import com.blankj.utilcode.util.ToastUtils;
|
|
|
+import com.hikvision.dmb.EthernetConfig;
|
|
|
+import com.hikvision.dmb.LauncherInfo;
|
|
|
+import com.hikvision.dmb.display.InfoDisplayApi;
|
|
|
+import com.hikvision.dmb.network.InfoNetworkApi;
|
|
|
+import com.hikvision.dmb.system.InfoSystemApi;
|
|
|
+import com.hikvision.dmb.time.InfoTimeApi;
|
|
|
+import com.hikvision.dmb.util.InfoUtilApi;
|
|
|
+import com.hjq.permissions.Permission;
|
|
|
+import com.hjq.permissions.XXPermissions;
|
|
|
+import com.lxj.xpopup.XPopup;
|
|
|
+import com.lxj.xpopup.impl.InputConfirmPopupView;
|
|
|
+
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import okhttp3.Response;
|
|
|
+import xn.update.constant.AppConstant;
|
|
|
+import xn.update.databinding.ActivityMainBinding;
|
|
|
+import xn.update.http.HttpTool;
|
|
|
+import xn.update.http.bean.response.UpdateTask;
|
|
|
+import xn.update.receiver.TimeTickReceiver;
|
|
|
+
|
|
|
+public class StartActivity extends AppCompatActivity {
|
|
|
+ private ActivityMainBinding binding;
|
|
|
+ private TimeTickReceiver timeTickReceiver;
|
|
|
+ private CountDownTimer requestPermissionCdTimer;
|
|
|
+ private CountDownTimer updateCdTimer;
|
|
|
+ private CountDownTimer initCdTimer;
|
|
|
+ private CountDownTimer timeCdTimer;
|
|
|
+ private final AtomicBoolean isUploading = new AtomicBoolean(false);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ LogUtils.d("onCreate");
|
|
|
+ binding = ActivityMainBinding.inflate(getLayoutInflater());
|
|
|
+ setContentView(binding.getRoot());
|
|
|
+ timeCdTimer = new CountDownTimer(1000, 1000) {
|
|
|
+ @Override
|
|
|
+ public void onTick(long millisUntilFinished) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+ if (!isFinishing() && !isDestroyed()) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
|
|
|
+ String currentTime = sdf.format(new Date());
|
|
|
+ if (currentTime.equals("06:01")) {
|
|
|
+ LogUtils.d("定时重启");
|
|
|
+ ShellUtils.execCmd("reboot", true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ binding.time.setText(TimeUtils.getNowString());
|
|
|
+ timeCdTimer.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ timeCdTimer.start();
|
|
|
+ requestPermissionCdTimer = new CountDownTimer(1000, 1000) {
|
|
|
+ @Override
|
|
|
+ public void onTick(long millisUntilFinished) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+ requestPermission();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ binding.main.postDelayed(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ if (!isFinishing() && !isDestroyed()) {
|
|
|
+ if (!isUploading.get()) {
|
|
|
+ LogUtils.d("初始化失败");
|
|
|
+ ShellUtils.execCmd("reboot", true);
|
|
|
+ } else {
|
|
|
+ LogUtils.d("初始化成功");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 60 * 1000);
|
|
|
+
|
|
|
+ initCdTimer = new CountDownTimer(30 * 1000, 100) {
|
|
|
+ @Override
|
|
|
+ public void onTick(long millisUntilFinished) {
|
|
|
+ binding.tipsTV.setText((millisUntilFinished / 1000) + "秒后开始初始化!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+ binding.tipsTV.setText("开始初始化...");
|
|
|
+ AsyncTask.execute(() -> {
|
|
|
+ try {
|
|
|
+ int root = InfoUtilApi.getRoot();
|
|
|
+ LogUtils.d("ROOT", "获取" + (root == 0 ? "成功" : "失败"));
|
|
|
+ LogUtils.d("打开adb");
|
|
|
+ InfoSystemApi.openAdb();
|
|
|
+ LogUtils.d("关闭状态栏");
|
|
|
+ InfoDisplayApi.setStatusBarEnable(false);
|
|
|
+ LogUtils.d("关闭导航栏");
|
|
|
+ InfoDisplayApi.setNavigationBarEnable(false);
|
|
|
+ LogUtils.d("不保护本App");
|
|
|
+ InfoUtilApi.enableProtection(AppUtils.getAppPackageName(), false);
|
|
|
+ LogUtils.d("默认桌面");
|
|
|
+ InfoSystemApi.setLauncherForced(AppUtils.getAppPackageName());
|
|
|
+ boolean clearPlan = InfoTimeApi.clearPlan();
|
|
|
+ LogUtils.d("清除开关机计划", clearPlan);
|
|
|
+ requestPermissionCdTimer.start();
|
|
|
+ isUploading.set(true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e("重启", e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ initCdTimer.cancel();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ initCdTimer.start();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onNewIntent(Intent intent) {
|
|
|
+ super.onNewIntent(intent);
|
|
|
+ LogUtils.d("onNewIntent");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void requestPermission() {
|
|
|
+ if (!XXPermissions.isGranted(this, Permission.READ_EXTERNAL_STORAGE)
|
|
|
+ || !XXPermissions.isGranted(this, Permission.WRITE_EXTERNAL_STORAGE)
|
|
|
+ || !XXPermissions.isGranted(this, Permission.NOTIFICATION_SERVICE)
|
|
|
+ ) {
|
|
|
+ Tool.INSTANCE.cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.READ_EXTERNAL_STORAGE");
|
|
|
+ Tool.INSTANCE.cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.WRITE_EXTERNAL_STORAGE");
|
|
|
+ Tool.INSTANCE.cmd("pm grant " + AppUtils.getAppPackageName() + " android.permission.NOTIFICATION_SERVICE");
|
|
|
+ requestPermissionCdTimer.start();
|
|
|
+ } else {
|
|
|
+ requestPermissionCdTimer.cancel();
|
|
|
+ terminalAuth();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ThreadUtils.SimpleTask<Boolean> simpleTask = new ThreadUtils.SimpleTask<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public Boolean doInBackground() throws Throwable {
|
|
|
+ ShellUtils.CommandResult result = ShellUtils.execCmd(String.format("ping -c 5 %s", Tool.INSTANCE.getBaseUrl().host()), false);
|
|
|
+ if (result.result == 0) {
|
|
|
+ PingResult pingResult = parsePingOutput(result.successMsg);
|
|
|
+ return pingResult.getAvgRtt() < 100;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Boolean result) {
|
|
|
+ if (result) {
|
|
|
+ binding.tipsTV.setText("鉴权中...");
|
|
|
+ ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Pair<Boolean, String>>() {
|
|
|
+ @Override
|
|
|
+ public Pair<Boolean, String> doInBackground() throws Throwable {
|
|
|
+ try {
|
|
|
+ SPUtils.getInstance().put("TerminalAuth", "");
|
|
|
+ Response response = HttpTool.INSTANCE.terminalAuth();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ String json = response.body().string();
|
|
|
+ JSONObject jsonObject = new JSONObject(json);
|
|
|
+ int code = jsonObject.getInt("code");
|
|
|
+ if (200 == code) {
|
|
|
+ String data = jsonObject.getString("data");
|
|
|
+ SPUtils.getInstance().put("TerminalAuth", TextUtils.isEmpty(data) ? "" : data);
|
|
|
+ return Pair.create(true, "");
|
|
|
+ } else {
|
|
|
+ return Pair.create(false, jsonObject.getString("message"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ }
|
|
|
+ return Pair.create(false, "鉴权异常,请联系管理员!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Pair<Boolean, String> result) {
|
|
|
+ if (result.first) {
|
|
|
+// // 状态栏是否开启
|
|
|
+// boolean isStatusBarEnable = InfoDisplayApi.getStatusBarEnable();
|
|
|
+// // 导航栏是否开启
|
|
|
+// boolean isNavBarEnable = InfoDisplayApi.getNavigationBarEnable();
|
|
|
+//
|
|
|
+// EthernetConfig ethernetConfig = InfoNetworkApi.getEthernetConfig();
|
|
|
+// // Ip
|
|
|
+// String ip = ethernetConfig.getIpAddress();
|
|
|
+// // dhcp
|
|
|
+// int dhcp = ethernetConfig.getDhcp();
|
|
|
+// // dns1
|
|
|
+// String dns1 = ethernetConfig.getDns1Address();
|
|
|
+// // dns2
|
|
|
+// String dns2 = ethernetConfig.getDns2Address();
|
|
|
+// // mac
|
|
|
+// String mac = ethernetConfig.getMacAddress();
|
|
|
+// // route
|
|
|
+// String route = ethernetConfig.getRouteAddress();
|
|
|
+// // subnetMask
|
|
|
+// String subnetMask = ethernetConfig.getSubnetMask();
|
|
|
+// // 最优Ip地址 以太网>无线网>3、4G
|
|
|
+// String optimalIp = InfoNetworkApi.getOptimalIp();
|
|
|
+// // 设备型号
|
|
|
+// String deviceType = InfoSystemApi.getDeviceType();
|
|
|
+// // 设备序列号
|
|
|
+// String sn = InfoSystemApi.getSerialNumber();
|
|
|
+// // adb状态
|
|
|
+// int adbStatus = InfoSystemApi.getAdbStatus();
|
|
|
+// // 设备箱体温度
|
|
|
+// String temperature = InfoSystemApi.getTemperature();
|
|
|
+// // 系统编译版本号 V2.0.0 build 171226
|
|
|
+// String buildDesc = InfoSystemApi.getBuildDesc();
|
|
|
+// // 系统MCU版本号 V1.0.0 build 171226
|
|
|
+// String mcuVersion = InfoSystemApi.getMcuVersion();
|
|
|
+// // CPU使用率 当前 Cpu 的使用率,百分比,获取失败时返回”-1.00”字符串
|
|
|
+// String cpuUsageRate = InfoSystemApi.getCpuUsageRate();
|
|
|
+// // GPU使用率 当前 Gpu 的使用率,百分比,获取失败时返回”-1.00”字符串
|
|
|
+// String gpuUsageRate = InfoSystemApi.getGpuUsageRate();
|
|
|
+// // 获取内存使用情况
|
|
|
+// MemoryInfo memoryInfo = InfoSystemApi.getMemoryUsage();
|
|
|
+// // MemoryInfo .used 已使用内存大小(单位 MB)
|
|
|
+// int used = memoryInfo.used;
|
|
|
+// // MemoryInfo.total 总内存大小(MB)
|
|
|
+// int total = memoryInfo.total;
|
|
|
+// // MemoryInfo.describe 583.00MB/1.96GB
|
|
|
+// String describe = memoryInfo.describe;
|
|
|
+// int describeContents = memoryInfo.describeContents();
|
|
|
+// // 获取SDK版本 返回 SDK 版本号,获取失败返回”none”字符串
|
|
|
+// String sdkVersion = InfoSystemApi.getSdkVersion();
|
|
|
+// // 如果系统不支持该接口,则 currentLauncherName 和 preferredLauncherName 的值均为空字符串(“”)。
|
|
|
+// // 关于“默认“一词,默认 Launcher 即多个 Launcher 应用中优先级最高的,默认启动的。
|
|
|
+// LauncherInfo launcherInfo = InfoSystemApi.getLauncherName();
|
|
|
+// // 为当前生效的默认 Launcher 应用包名,默认(始终)Launcher 应用修改后该值即时更新。如果当前系统未设置默认(始终)Launcher,则该值为空字符串(“”)。
|
|
|
+// String currentLauncherName = launcherInfo.getCurrentLauncherName();
|
|
|
+// // 为持久化文件中保存的默认 Launcher 应用包名,默认 Launcher 应用修改后约 10 秒后该值才会更新,该值仅当系统内存在 2 个及以上Launcher 应用时才会存在,否则为空字符串(“”)。
|
|
|
+// String preferredLauncherName = launcherInfo.getPreferredLauncherName();
|
|
|
+// // 获取定时开关机计划
|
|
|
+// TimeSwitchConfig timeSwitchConfig = InfoTimeApi.getTimeSwitch();
|
|
|
+// // 开机时间 默认-1
|
|
|
+// long onTime = timeSwitchConfig.setOnTime;
|
|
|
+// // 关机时间 默认-1
|
|
|
+// long offTime = timeSwitchConfig.setOffTime;
|
|
|
+// // 门禁点锁状态1 true-当前锁处于上电锁门状态
|
|
|
+// // false-当前锁处于断电开门状态或参数错误
|
|
|
+// boolean electricLock1 = InfoUtilApi.getElectricLock(1);
|
|
|
+// // 门禁点锁状态2
|
|
|
+// boolean electricLock2 = InfoUtilApi.getElectricLock(2);
|
|
|
+ ThreadUtils.cancel(simpleTask);
|
|
|
+ // 监听分钟广播
|
|
|
+ timeTickReceiver = new TimeTickReceiver();
|
|
|
+ registerReceiver(timeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
|
|
|
+ // 检查软件更新
|
|
|
+ updateCdTimer = new CountDownTimer(20000, 1000) {
|
|
|
+ @Override
|
|
|
+ public void onTick(long millisUntilFinished) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+ UpdateTool.INSTANCE.checkUpdate(StartActivity.this);
|
|
|
+ AsyncTask.execute(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ HttpTool.INSTANCE.heartbeat();
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ updateCdTimer.start();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ updateCdTimer.start();
|
|
|
+ Tool.INSTANCE.stopApp("xn.xxp");
|
|
|
+ Tool.INSTANCE.openApp("xn.xxp");
|
|
|
+
|
|
|
+ AsyncTask.execute(() -> {
|
|
|
+ boolean isSelfUpdate = SPUtils.getInstance().getBoolean("isSelfUpdate", false);
|
|
|
+ if (isSelfUpdate) {
|
|
|
+ try {
|
|
|
+ String json = SPUtils.getInstance().getString("SelfUpdate", "");
|
|
|
+ UpdateTask.Task task = GsonUtils.fromJson(json, UpdateTask.Task.class);
|
|
|
+ if (null != task) {
|
|
|
+ HttpTool.INSTANCE.updateCallBack(task.getTaskId(), task.getDeviceCode(), true, true);
|
|
|
+ SPUtils.getInstance().put("isSelfUpdate", false, true);
|
|
|
+ }
|
|
|
+ } catch (IOException | JSONException e) {
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ binding.tipsTV.setText("运行中");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ ThreadUtils.cancel(simpleTask);
|
|
|
+ } else {
|
|
|
+ binding.tipsTV.setText("无法连接到服务器,正在重试...");
|
|
|
+ }
|
|
|
+ EthernetConfig ethernetConfig = InfoNetworkApi.getEthernetConfig();
|
|
|
+ String ip = ethernetConfig.getIpAddress();
|
|
|
+ binding.ipTV.setText((TextUtils.isEmpty(ip) ? "NULL" : ip));
|
|
|
+ String sn = InfoSystemApi.getSerialNumber();
|
|
|
+ binding.snTV.setText((TextUtils.isEmpty(sn) ? "NULL" : sn));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ private void terminalAuth() {
|
|
|
+ ThreadUtils.executeByCachedAtFixRate(simpleTask, ThreadLocalRandom.current().nextInt(10, 20), TimeUnit.SECONDS);
|
|
|
+ // 清理一个月前的log
|
|
|
+ AsyncTask.execute(() -> {
|
|
|
+ List<File> fileList = FileUtils.listFilesInDir("/sdcard/logs/");
|
|
|
+ List<File> crashList = FileUtils.listFilesInDir("/sdcard/logs/crash");
|
|
|
+ for (int i = 0; i < fileList.size(); i++) {
|
|
|
+ File file = fileList.get(i);
|
|
|
+ long oneMonth = 30 * 86400 * 1000L;
|
|
|
+ if ((System.currentTimeMillis() - file.lastModified()) > oneMonth) {
|
|
|
+ LogUtils.d("删除日志文件", file);
|
|
|
+ FileUtils.delete(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < crashList.size(); i++) {
|
|
|
+ File file = crashList.get(i);
|
|
|
+ long oneMonth = 30 * 86400 * 1000L;
|
|
|
+ if ((System.currentTimeMillis() - file.lastModified()) > oneMonth) {
|
|
|
+ LogUtils.d("删除crash文件", file);
|
|
|
+ FileUtils.delete(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private InputConfirmPopupView inputConfirmPopupView;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ super.onPostCreate(savedInstanceState);
|
|
|
+ binding.versionNameTV.setText(AppUtils.getAppVersionName());
|
|
|
+
|
|
|
+ inputConfirmPopupView = new XPopup.Builder(this).autoDismiss(false).asInputConfirm("Tips", "请输入管理员密码",
|
|
|
+ text -> {
|
|
|
+ if (text.equals(SPUtils.getInstance().getString(AppConstant.ADMIN_PASSWORD, "admin@098&"))) {
|
|
|
+ ActivityUtils.startActivity(SettingActivity.class);
|
|
|
+ inputConfirmPopupView.dismiss();
|
|
|
+ } else {
|
|
|
+ ToastUtils.showLong("密码不正确,请重新输入!");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ binding.logoIV.setOnLongClickListener(v -> {
|
|
|
+ if (AppUtils.isAppDebug()) {
|
|
|
+ ActivityUtils.startActivity(SettingActivity.class);
|
|
|
+ } else {
|
|
|
+ EditText editText = inputConfirmPopupView.getEditText();
|
|
|
+ if (null != editText) {
|
|
|
+ editText.setText("");
|
|
|
+ }
|
|
|
+ inputConfirmPopupView.show();
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostResume() {
|
|
|
+ super.onPostResume();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (null != timeTickReceiver) {
|
|
|
+ unregisterReceiver(timeTickReceiver);
|
|
|
+ timeTickReceiver = null;
|
|
|
+ }
|
|
|
+ if (null != inputConfirmPopupView) {
|
|
|
+ inputConfirmPopupView.destroy();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public PingResult parsePingOutput(String pingOutput) {
|
|
|
+ PingResult result = new PingResult();
|
|
|
+ // 解析目标IP
|
|
|
+ Pattern ipPattern = Pattern.compile("PING (\\d+\\.\\d+\\.\\d+\\.\\d+)");
|
|
|
+ Matcher ipMatcher = ipPattern.matcher(pingOutput);
|
|
|
+ if (ipMatcher.find()) {
|
|
|
+ result.setTargetIP(ipMatcher.group(1));
|
|
|
+ }
|
|
|
+ // 解析统计信息
|
|
|
+ Pattern statsPattern = Pattern.compile(
|
|
|
+ "(\\d+) packets transmitted, (\\d+) received, (\\d+)% packet loss, time (\\d+)ms"
|
|
|
+ );
|
|
|
+ Matcher statsMatcher = statsPattern.matcher(pingOutput);
|
|
|
+ if (statsMatcher.find()) {
|
|
|
+ result.setPacketsTransmitted(Integer.parseInt(statsMatcher.group(1)));
|
|
|
+ result.setPacketsReceived(Integer.parseInt(statsMatcher.group(2)));
|
|
|
+ result.setPacketLossPercentage(Double.parseDouble(statsMatcher.group(3)));
|
|
|
+ }
|
|
|
+ // 解析RTT信息
|
|
|
+ Pattern rttPattern = Pattern.compile(
|
|
|
+ "rtt min/avg/max/mdev = (\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+)/(\\d+\\.\\d+) ms"
|
|
|
+ );
|
|
|
+ Matcher rttMatcher = rttPattern.matcher(pingOutput);
|
|
|
+ if (rttMatcher.find()) {
|
|
|
+ result.setMinRtt(Double.parseDouble(rttMatcher.group(1)));
|
|
|
+ result.setAvgRtt(Double.parseDouble(rttMatcher.group(2)));
|
|
|
+ result.setMaxRtt(Double.parseDouble(rttMatcher.group(3)));
|
|
|
+ result.setMdevRtt(Double.parseDouble(rttMatcher.group(4)));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|