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
41cfa9b9
authored
Mar 22, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化扫码流程,保证每次扫码成功都会播报提示音
parent
2f0afa13
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
25 deletions
+36
-25
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
+20
-16
app/src/main/java/com/bgycc/smartcanteen/state/QRCodeState.java
+4
-2
app/src/main/java/com/bgycc/smartcanteen/viewModel/QRCodeViewModel.java
+12
-7
No files found.
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
View file @
41cfa9b9
...
...
@@ -190,6 +190,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
qrCodeViewModel
.
getQRCodeStateEvent
().
observe
(
this
,
event
->
{
switch
(
event
.
getState
())
{
case
QRCodeState
.
INIT_FAILED
:
if
(
debugLayoutIsNotInflate
())
return
;
message
.
setVisibility
(
View
.
GONE
);
sendText
.
setTextColor
(
getResources
().
getColor
(
R
.
color
.
serial_port_error
));
sendText
.
setText
(
R
.
string
.
serial_port_init_failed
);
String
logMessage
=
String
.
format
(
Locale
.
getDefault
(),
getString
(
R
.
string
.
log_dir
),
LogUtils
.
getConfig
().
getDir
());
recText
.
setTextColor
(
getResources
().
getColor
(
R
.
color
.
serial_port_error
));
recText
.
setText
(
logMessage
);
break
;
case
QRCodeState
.
IDLE
:
message
.
setTextColor
(
getResources
().
getColor
(
R
.
color
.
pay_idle
));
message
.
setText
(
R
.
string
.
pay_idle
);
...
...
@@ -200,13 +209,17 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
break
;
case
QRCodeState
.
SUCCESS
:
TTSHelper
.
speak
(
"beep"
);
break
;
case
QRCodeState
.
FAILED
:
TTSHelper
.
speak
(
"二维码读取失败"
);
qrCodeViewModel
.
scan
();
break
;
case
QRCodeState
.
PAY
:
PayData
data
=
event
.
getData
();
if
(
data
!=
null
)
{
if
(
SCWebSocketClient
.
getInstance
().
isOpen
())
{
payOnlineViewModel
.
exec
(
data
);
}
else
{
payOfflineViewModel
.
exec
(
data
);
}
if
(
SCWebSocketClient
.
getInstance
().
isOpen
())
{
payOnlineViewModel
.
exec
(
data
);
}
else
{
payOfflineViewModel
.
exec
(
data
);
}
if
(
debugLayoutIsNotInflate
())
return
;
...
...
@@ -221,7 +234,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
qrCodeText
.
setText
(
msg
);
break
;
case
QRCodeState
.
FAILED
:
case
QRCodeState
.
ILLEGAL
:
String
failedText
=
event
.
getMessage
();
if
(
TextUtils
.
isEmpty
(
failedText
))
{
TTSHelper
.
speak
(
"无效二维码"
);
...
...
@@ -230,15 +243,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
qrCodeViewModel
.
scan
();
break
;
case
QRCodeState
.
ILLEGAL
:
if
(
debugLayoutIsNotInflate
())
return
;
message
.
setVisibility
(
View
.
GONE
);
sendText
.
setTextColor
(
getResources
().
getColor
(
R
.
color
.
serial_port_error
));
sendText
.
setText
(
R
.
string
.
serial_port_init_failed
);
String
logMessage
=
String
.
format
(
Locale
.
getDefault
(),
getString
(
R
.
string
.
log_dir
),
LogUtils
.
getConfig
().
getDir
());
recText
.
setTextColor
(
getResources
().
getColor
(
R
.
color
.
serial_port_error
));
recText
.
setText
(
logMessage
);
break
;
}
});
...
...
app/src/main/java/com/bgycc/smartcanteen/state/QRCodeState.java
View file @
41cfa9b9
...
...
@@ -11,9 +11,11 @@ public class QRCodeState {
public
static
final
int
SCANNING
=
1
;
public
static
final
int
SUCCESS
=
2
;
public
static
final
int
FAILED
=
3
;
public
static
final
int
ILLEGAL
=
4
;
public
static
final
int
PAY
=
4
;
public
static
final
int
ILLEGAL
=
5
;
public
static
final
int
INIT_FAILED
=
-
1
;
@IntDef
(
value
=
{
IDLE
,
SCANNING
,
SUCCESS
,
FAILED
,
ILLEGAL
})
@IntDef
(
value
=
{
IDLE
,
SCANNING
,
SUCCESS
,
FAILED
,
PAY
,
ILLEGAL
,
INIT_FAILED
})
public
@interface
QRCODE_STATE
{
}
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/QRCodeViewModel.java
View file @
41cfa9b9
...
...
@@ -34,6 +34,7 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
*/
public
class
QRCodeViewModel
extends
ViewModel
{
private
static
final
String
RESPONSE_ACTION
=
"action"
;
private
static
final
long
SCAN_DELAY
=
100
;
private
static
final
long
DELAY
=
1500
;
private
PayDataRepository
payDataRepository
;
...
...
@@ -63,7 +64,7 @@ public class QRCodeViewModel extends ViewModel {
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"串口初始化失败: "
+
e
.
getMessage
(),
e
);
scan
=
null
;
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
I
LLEGAL
));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
I
NIT_FAILED
));
}
}
...
...
@@ -83,11 +84,16 @@ public class QRCodeViewModel extends ViewModel {
String
scanData
;
try
{
scanData
=
scan
.
scan
();
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
SUCCESS
));
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"读取串口数据失败: "
+
e
.
getMessage
(),
e
);
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
FAILED
));
return
;
}
try
{
Thread
.
sleep
(
SCAN_DELAY
);
}
catch
(
InterruptedException
ignored
)
{
}
// 若存在测试数据,则使用测试数据覆盖
if
(!
TextUtils
.
isEmpty
(
SmartCanteenUtils
.
testQRCode
))
{
scanData
=
SmartCanteenUtils
.
testQRCode
;
...
...
@@ -101,7 +107,7 @@ public class QRCodeViewModel extends ViewModel {
jsonObject
=
JsonParser
.
parseString
(
scanData
).
getAsJsonObject
();
}
catch
(
JsonSyntaxException
jse
)
{
LogUtils
.
e
(
TAG
,
"非法的指令格式: "
+
scanData
);
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
FAILED
));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
ILLEGAL
));
return
;
}
if
(
jsonObject
.
has
(
RESPONSE_ACTION
))
{
...
...
@@ -109,11 +115,10 @@ public class QRCodeViewModel extends ViewModel {
Command
command
=
new
Command
(
scanData
,
action
);
commandRepository
.
insertCommand
(
command
);
LogUtils
.
d
(
TAG
,
"扫码结果: "
+
command
.
toString
());
// 任务写入数据库成功后,主动发送一次IDLE,以重置界面提示语
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
SUCCESS
,
scanData
));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
IDLE
));
againDelay
();
}
else
{
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
FAILED
));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
ILLEGAL
));
}
return
;
}
...
...
@@ -123,7 +128,7 @@ public class QRCodeViewModel extends ViewModel {
PayData
existsPay
=
payDataRepository
.
queryPayDataByPayCode
(
scanData
);
if
(
existsPay
!=
null
)
{
LogUtils
.
w
(
TAG
,
"该订单已存在: "
+
scanData
);
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
FAILED
,
"请不要重复扫码"
));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
ILLEGAL
,
"请不要重复扫码"
));
return
;
}
// 扫描到的二维码保存到数据库
...
...
@@ -132,7 +137,7 @@ public class QRCodeViewModel extends ViewModel {
LogUtils
.
e
(
TAG
,
"数据库插入失败: "
+
newPay
.
toString
());
}
LogUtils
.
d
(
TAG
,
"扫码结果: "
+
newPay
.
toString
());
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
SUCCESS
,
newPay
,
newPay
.
getPayCode
()));
qrCodeState
.
postValue
(
new
QRCodeState
(
QRCodeState
.
PAY
,
newPay
,
newPay
.
getPayCode
()));
};
private
void
againDelay
()
{
...
...
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