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
2292a0ea
authored
May 11, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加心跳定时检查离线订单的机制(同时避免离线订单频繁上传的限制)
parent
ffceadd6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
1 deletions
+32
-1
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
+32
-1
No files found.
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
View file @
2292a0ea
...
...
@@ -41,6 +41,7 @@ public class PayOfflineViewModel extends ViewModel {
// 在线支付延迟150ms执行,留出时间给扫码反馈
private
static
final
long
REQUEST_DELAY
=
150
;
private
static
final
long
DEFAULT_DELAY
=
3
*
1000
;
private
static
final
long
PAY_INTERVAL
=
60
*
60
*
1000
;
private
Gson
gson
;
private
String
deviceSN
;
private
PayDataRepository
payDataRepository
;
...
...
@@ -53,6 +54,8 @@ public class PayOfflineViewModel extends ViewModel {
}
private
PayRequest
payRequest
;
private
long
lastExecTime
=
-
1
;
private
ScheduledFuture
<?>
payOfflineFuture
;
private
ScheduledFuture
<?>
timeoutFuture
;
public
PayOfflineViewModel
(
PayDataRepository
payDataRepository
,
...
...
@@ -75,11 +78,25 @@ public class PayOfflineViewModel extends ViewModel {
}
private
void
traversalPayOfflineData
()
{
// 若与上次执行相隔太近,则跳过
long
currentTime
=
System
.
currentTimeMillis
();
if
((
currentTime
-
lastExecTime
)
<
PAY_INTERVAL
)
{
LogUtils
.
d
(
TAG
,
"离线检测过于频繁"
);
return
;
}
lastExecTime
=
currentTime
;
cancelPayOffline
();
cancelTimeout
();
TimeoutRunnable
timeoutRunnable
=
new
TimeoutRunnable
();
timeoutFuture
=
SCTaskExecutor
.
getInstance
().
schedule
(
timeoutRunnable
,
TIMEOUT
,
TimeUnit
.
MILLISECONDS
);
RequestRunnable
runnable
=
new
RequestRunnable
();
SCTaskExecutor
.
getInstance
().
executeOnDiskIO
(
runnable
);
payOfflineFuture
=
SCTaskExecutor
.
getInstance
().
schedule
(
runnable
,
0
,
TimeUnit
.
MILLISECONDS
);
}
private
void
cancelPayOffline
()
{
if
(
payOfflineFuture
==
null
)
return
;
payOfflineFuture
.
cancel
(
true
);
payOfflineFuture
=
null
;
}
private
void
cancelTimeout
()
{
...
...
@@ -92,13 +109,27 @@ public class PayOfflineViewModel extends ViewModel {
private
static
final
String
RESPONSE_MESSAGE
=
"message"
;
private
static
final
String
RESPONSE_OFFLINE_RESULT
=
"操作完成"
;
private
static
final
String
RESPONSE_PAY_OFFLINE_RESULT
=
"PAY_OFFLINE_RESULT"
;
// 每心跳多少次后进行一次离线订单检测
private
static
final
int
PAY_OFFLINE_CHECK
=
30
;
private
int
heartbeatCount
=
0
;
@Override
public
void
onOpen
(
ServerHandshake
data
)
{
heartbeatCount
=
0
;
traversalPayOfflineData
();
}
@Override
public
void
onHeartbeat
()
{
// 在一定心跳次数后开始检查离线订单是否已处理完毕
heartbeatCount
++;
if
(
heartbeatCount
>=
PAY_OFFLINE_CHECK
)
{
heartbeatCount
=
0
;
traversalPayOfflineData
();
}
}
@Override
public
void
onMessage
(
String
action
,
JsonObject
obj
,
String
original
)
{
if
(
action
.
equals
(
RESPONSE_PAY_OFFLINE_RESULT
))
{
// 离线订单已写入到服务器数据库,可以标记为支付成功
...
...
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