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
4628c6b3
authored
Mar 13, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
各设备指令改为单例的模式(避免多次执行)
parent
8c787126
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
8 deletions
+80
-8
app/src/main/java/com/bgycc/smartcanteen/command/LogCommandHandler.java
+37
-2
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandHandler.java
+40
-3
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.java
+3
-3
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/LogCommandHandler.java
View file @
4628c6b3
...
...
@@ -40,6 +40,7 @@ public class LogCommandHandler extends CommandHandler {
private
static
final
String
UPLOAD_DIR
=
File
.
separator
+
"upload_log"
;
private
static
final
long
DEFAULT_DELAY
=
1000
;
private
volatile
boolean
start
=
false
;
private
SimpleDateFormat
format
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
,
Locale
.
getDefault
());
private
String
deviceSN
;
private
CommandLog
commandLog
;
...
...
@@ -48,7 +49,33 @@ public class LogCommandHandler extends CommandHandler {
private
Date
startTime
;
private
Date
endTime
;
public
LogCommandHandler
(
Command
command
,
Gson
gson
,
String
deviceSN
,
CommandProgressCallback
callback
)
{
private
static
LogCommandHandler
instance
;
public
static
LogCommandHandler
getInstance
(
Command
command
,
Gson
gson
,
String
deviceSN
,
CommandProgressCallback
callback
)
{
if
(
instance
==
null
)
{
synchronized
(
LogCommandHandler
.
class
)
{
if
(
instance
==
null
)
{
instance
=
new
LogCommandHandler
(
command
,
gson
,
deviceSN
,
callback
);
}
else
{
instance
.
init
(
command
,
gson
,
deviceSN
,
callback
);
}
}
}
else
{
instance
.
init
(
command
,
gson
,
deviceSN
,
callback
);
}
return
instance
;
}
private
void
init
(
Command
command
,
Gson
gson
,
String
deviceSN
,
CommandProgressCallback
callback
)
{
this
.
command
=
command
;
this
.
gson
=
gson
;
this
.
commandProgressCallback
=
callback
;
this
.
deviceSN
=
deviceSN
;
this
.
commandLog
=
gson
.
fromJson
(
command
.
getData
(),
CommandLog
.
class
);
parseDate
();
}
private
LogCommandHandler
(
Command
command
,
Gson
gson
,
String
deviceSN
,
CommandProgressCallback
callback
)
{
super
(
command
,
gson
,
callback
);
this
.
deviceSN
=
deviceSN
;
this
.
commandLog
=
gson
.
fromJson
(
command
.
getData
(),
CommandLog
.
class
);
...
...
@@ -179,16 +206,24 @@ public class LogCommandHandler extends CommandHandler {
}
@Override
public
CommandResponse
run
()
throws
InterruptedException
{
public
synchronized
CommandResponse
run
()
throws
InterruptedException
{
if
(
start
)
{
LogUtils
.
w
(
TAG
,
"日志上传任务已启动"
);
return
null
;
}
start
=
true
;
if
(!
checkLogCommand
())
{
start
=
false
;
return
failedResult
(
"日志上传指令不符合规范"
);
}
File
logFile
=
getZipLogs
();
if
(
logFile
!=
null
)
{
wait
(
"上传压缩文件至服务器"
,
60
);
upload
(
logFile
);
start
=
false
;
return
successResult
(
""
);
}
start
=
false
;
return
failedResult
(
""
);
}
}
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandHandler.java
View file @
4628c6b3
...
...
@@ -16,15 +16,49 @@ public class WifiConfigCommandHandler extends CommandHandler {
private
static
final
long
POLLING_DELAY
=
100
;
private
CommandWifiConfig
wifiConfig
;
public
WifiConfigCommandHandler
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
private
volatile
boolean
start
=
false
;
private
static
WifiConfigCommandHandler
instance
;
public
static
WifiConfigCommandHandler
getInstance
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
if
(
instance
==
null
)
{
synchronized
(
WifiConfigCommandHandler
.
class
)
{
if
(
instance
==
null
)
{
instance
=
new
WifiConfigCommandHandler
(
command
,
gson
,
callback
);
}
else
{
instance
.
init
(
command
,
gson
,
callback
);
}
}
}
else
{
instance
.
init
(
command
,
gson
,
callback
);
}
return
instance
;
}
private
WifiConfigCommandHandler
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
super
(
command
,
gson
,
callback
);
this
.
wifiConfig
=
gson
.
fromJson
(
command
.
getData
(),
CommandWifiConfig
.
class
);
}
private
void
init
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
this
.
command
=
command
;
this
.
gson
=
gson
;
this
.
commandProgressCallback
=
callback
;
this
.
wifiConfig
=
gson
.
fromJson
(
command
.
getData
(),
CommandWifiConfig
.
class
);
}
@Override
public
CommandResponse
run
()
throws
InterruptedException
{
public
synchronized
CommandResponse
run
()
throws
InterruptedException
{
if
(
start
)
{
LogUtils
.
w
(
TAG
,
"Wifi配置任务已启动"
);
return
null
;
}
start
=
true
;
CommandWifiConfig
.
CommandWifiConfigData
data
=
wifiConfig
.
getData
();
if
(
data
==
null
)
return
null
;
if
(
data
==
null
)
{
start
=
false
;
return
null
;
}
if
(!
NetworkUtils
.
isWifiEnabled
())
{
wait
(
"正在启动Wifi"
,
5
);
...
...
@@ -33,6 +67,7 @@ public class WifiConfigCommandHandler extends CommandHandler {
wait
(
failedMessage
,
5
);
LogUtils
.
e
(
TAG
,
failedMessage
);
Thread
.
sleep
(
DEFAULT_DELAY
);
start
=
false
;
return
failedResult
(
failedMessage
);
}
}
...
...
@@ -63,6 +98,8 @@ public class WifiConfigCommandHandler extends CommandHandler {
LogUtils
.
e
(
TAG
,
"链接wifi过程出错: "
+
e
.
getMessage
(),
e
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
failedResult
(
e
.
getMessage
());
}
finally
{
start
=
false
;
}
return
failedResult
(
"无法连接Wifi"
);
}
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.java
View file @
4628c6b3
...
...
@@ -129,13 +129,13 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
try
{
switch
(
command
.
getAction
())
{
case
Command
.
LOG_UPLOAD
:
handler
=
new
LogCommandHandler
(
command
,
gson
,
deviceSN
,
CommandViewModel
.
this
);
handler
=
LogCommandHandler
.
getInstance
(
command
,
gson
,
deviceSN
,
CommandViewModel
.
this
);
break
;
case
Command
.
APP_UPDATE
:
handler
=
UpdateCommandHandler
.
getInstance
(
command
,
gson
,
CommandViewModel
.
this
);
break
;
case
Command
.
CONFIG_WIFI
:
handler
=
new
WifiConfigCommandHandler
(
command
,
gson
,
CommandViewModel
.
this
);
handler
=
WifiConfigCommandHandler
.
getInstance
(
command
,
gson
,
CommandViewModel
.
this
);
break
;
case
Command
.
CONFIG_LOG
:
// CONFIG_LOG后直接return
...
...
@@ -156,7 +156,7 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
commandState
.
postValue
(
new
CommandState
(
CommandState
.
WAIT
));
CommandResponse
response
=
handler
.
run
();
if
(
response
==
null
)
{
LogUtils
.
d
(
TAG
,
"指令无返回结果
"
);
LogUtils
.
d
(
TAG
,
"指令无返回结果
: "
+
command
.
toString
()
);
return
;
}
if
(
response
.
success
())
{
...
...
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