Commit 3d0ce647 by pye52

Merge branch 'fea-reboot_monitor' into develop

parents 1c3df0fa ac01383f
package com.bgycc.smartcanteen.monitor;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.bgycc.smartcanteen.utils.DangerousUtils;
import com.blankj.utilcode.util.LogUtils;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class ReBootMonitor extends Worker {
public ReBootMonitor(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
LogUtils.d(TAG, "尝试重启设备");
DangerousUtils.reboot();
return Result.success();
}
}
...@@ -2,20 +2,69 @@ package com.bgycc.smartcanteen.utils; ...@@ -2,20 +2,69 @@ package com.bgycc.smartcanteen.utils;
import android.content.Context; import android.content.Context;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest; import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import com.bgycc.smartcanteen.monitor.DatabaseMonitor; import com.bgycc.smartcanteen.monitor.DatabaseMonitor;
import com.bgycc.smartcanteen.monitor.LogFileMonitor; import com.bgycc.smartcanteen.monitor.LogFileMonitor;
import com.bgycc.smartcanteen.monitor.ReBootMonitor;
import com.blankj.utilcode.util.LogUtils;
import java.util.Calendar;
import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class MonitorUtils { public class MonitorUtils {
private static final String LOG_MONITOR_WORKER = "worker_log_monitor"; private static final String LOG_MONITOR_WORKER = "worker_log_monitor";
private static final String DATABASE_MONITOR_WORKER = "database_log_monitor"; private static final String DATABASE_MONITOR_WORKER = "database_log_monitor";
private static final String REBOOT_MONITOR_WORKER = "worker_reboot_monitor";
private static final int LOG_REPEAT_INTERVAL = 4; private static final int LOG_REPEAT_INTERVAL = 4;
private static final int DATABASE_REPEAT_INTERVAL = 12; private static final int DATABASE_REPEAT_INTERVAL = 12;
// 每天早上指定时间重启设备
private static final int REBOOT_HOUR_OF_DAY = 3;
private static final int REBOOT_MIN = 0;
public static void startMonitors(Context context) {
startLogFilesMonitor(context);
startDatabaseMonitor(context);
startRebootMonitor(context);
}
public static void stopMonitors(Context context) {
stopLogFilesMonitor(context);
stopDatabaseMonitor(context);
stopRebootMonitor(context);
}
// 指定时间重启设备
private static void startRebootMonitor(Context context) {
stopRebootMonitor(context);
Calendar now = Calendar.getInstance(Locale.getDefault());
Calendar future = Calendar.getInstance(Locale.getDefault());
future.add(Calendar.DAY_OF_YEAR, 1);
future.set(Calendar.HOUR_OF_DAY, REBOOT_HOUR_OF_DAY);
future.set(Calendar.MINUTE, REBOOT_MIN);
future.set(Calendar.SECOND, 0);
long delay = future.getTimeInMillis() - now.getTimeInMillis();
LogUtils.d(TAG, delay + "毫秒后将重启设备");
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(ReBootMonitor.class)
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance(context)
.beginUniqueWork(REBOOT_MONITOR_WORKER, ExistingWorkPolicy.REPLACE, request)
.enqueue();
}
// 指定时间重启设备
private static void stopRebootMonitor(Context context) {
WorkManager.getInstance(context)
.cancelUniqueWork(REBOOT_MONITOR_WORKER);
}
public static void startMonitors(Context context) { public static void startMonitors(Context context) {
startLogFilesMonitor(context); startLogFilesMonitor(context);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment