Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2df9c9ad
authored
Jun 02, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加指定时间重启设备的功能
parent
75095b78
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
8 deletions
+74
-8
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
+2
-4
app/src/main/java/com/bgycc/smartcanteen/monitor/ReBootMonitor.java
+23
-0
app/src/main/java/com/bgycc/smartcanteen/utils/MonitorUtils.java
+49
-4
No files found.
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
View file @
2df9c9ad
...
@@ -325,8 +325,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
...
@@ -325,8 +325,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
commandViewModel
.
initialize
();
commandViewModel
.
initialize
();
handler
.
post
(
updateTimeRunnable
);
handler
.
post
(
updateTimeRunnable
);
MonitorUtils
.
startLogFilesMonitor
(
this
);
MonitorUtils
.
startMonitors
(
this
);
MonitorUtils
.
startDatabaseMonitor
(
this
);
SCWebSocketClient
.
getInstance
().
tryConnect
();
SCWebSocketClient
.
getInstance
().
tryConnect
();
}
}
...
@@ -400,8 +399,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
...
@@ -400,8 +399,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
TTSHelper
.
release
();
TTSHelper
.
release
();
SCWebSocketClient
.
getInstance
().
realClose
();
SCWebSocketClient
.
getInstance
().
realClose
();
SCTaskExecutor
.
getInstance
().
quit
();
SCTaskExecutor
.
getInstance
().
quit
();
MonitorUtils
.
stopDatabaseMonitor
(
this
);
MonitorUtils
.
stopMonitors
(
this
);
MonitorUtils
.
stopLogFilesMonitor
(
this
);
}
}
private
void
initViews
()
{
private
void
initViews
()
{
...
...
app/src/main/java/com/bgycc/smartcanteen/monitor/ReBootMonitor.java
0 → 100644
View file @
2df9c9ad
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
;
public
class
ReBootMonitor
extends
Worker
{
public
ReBootMonitor
(
@NonNull
Context
context
,
@NonNull
WorkerParameters
workerParams
)
{
super
(
context
,
workerParams
);
}
@NonNull
@Override
public
Result
doWork
()
{
DangerousUtils
.
reboot
();
return
Result
.
success
();
}
}
app/src/main/java/com/bgycc/smartcanteen/utils/MonitorUtils.java
View file @
2df9c9ad
...
@@ -2,23 +2,68 @@ package com.bgycc.smartcanteen.utils;
...
@@ -2,23 +2,68 @@ 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
java.util.Calendar
;
import
java.util.Locale
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
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
();
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
);
}
// 每隔一定时间检查日志文件夹并进行清理
// 每隔一定时间检查日志文件夹并进行清理
p
ublic
static
void
startLogFilesMonitor
(
Context
context
)
{
p
rivate
static
void
startLogFilesMonitor
(
Context
context
)
{
stopLogFilesMonitor
(
context
);
stopLogFilesMonitor
(
context
);
PeriodicWorkRequest
request
=
new
PeriodicWorkRequest
.
Builder
(
LogFileMonitor
.
class
,
LOG_REPEAT_INTERVAL
,
TimeUnit
.
HOURS
)
PeriodicWorkRequest
request
=
new
PeriodicWorkRequest
.
Builder
(
LogFileMonitor
.
class
,
LOG_REPEAT_INTERVAL
,
TimeUnit
.
HOURS
)
.
addTag
(
LOG_MONITOR_WORKER
)
.
addTag
(
LOG_MONITOR_WORKER
)
...
@@ -27,13 +72,13 @@ public class MonitorUtils {
...
@@ -27,13 +72,13 @@ public class MonitorUtils {
.
enqueue
(
request
);
.
enqueue
(
request
);
}
}
p
ublic
static
void
stopLogFilesMonitor
(
Context
context
)
{
p
rivate
static
void
stopLogFilesMonitor
(
Context
context
)
{
WorkManager
.
getInstance
(
context
)
WorkManager
.
getInstance
(
context
)
.
cancelAllWorkByTag
(
LOG_MONITOR_WORKER
);
.
cancelAllWorkByTag
(
LOG_MONITOR_WORKER
);
}
}
// 每隔一定时间检查数据库并进行清理
// 每隔一定时间检查数据库并进行清理
p
ublic
static
void
startDatabaseMonitor
(
Context
context
)
{
p
rivate
static
void
startDatabaseMonitor
(
Context
context
)
{
stopDatabaseMonitor
(
context
);
stopDatabaseMonitor
(
context
);
PeriodicWorkRequest
request
=
new
PeriodicWorkRequest
.
Builder
(
DatabaseMonitor
.
class
,
DATABASE_REPEAT_INTERVAL
,
TimeUnit
.
HOURS
)
PeriodicWorkRequest
request
=
new
PeriodicWorkRequest
.
Builder
(
DatabaseMonitor
.
class
,
DATABASE_REPEAT_INTERVAL
,
TimeUnit
.
HOURS
)
.
addTag
(
DATABASE_MONITOR_WORKER
)
.
addTag
(
DATABASE_MONITOR_WORKER
)
...
@@ -42,7 +87,7 @@ public class MonitorUtils {
...
@@ -42,7 +87,7 @@ public class MonitorUtils {
.
enqueue
(
request
);
.
enqueue
(
request
);
}
}
p
ublic
static
void
stopDatabaseMonitor
(
Context
context
)
{
p
rivate
static
void
stopDatabaseMonitor
(
Context
context
)
{
WorkManager
.
getInstance
(
context
)
WorkManager
.
getInstance
(
context
)
.
cancelAllWorkByTag
(
DATABASE_MONITOR_WORKER
);
.
cancelAllWorkByTag
(
DATABASE_MONITOR_WORKER
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment