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
c8a3b955
authored
May 13, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、现在服务器在返回"写入成功"的通知后,会判断其code是否成功
2、优化离线订单上传的逻辑,保证上传的离线订单和设备号一致
parent
9fca2e6b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
11 deletions
+64
-11
app/src/main/java/com/bgycc/smartcanteen/socket/SCWebSocketClient.java
+3
-0
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
+61
-11
No files found.
app/src/main/java/com/bgycc/smartcanteen/socket/SCWebSocketClient.java
View file @
c8a3b955
...
@@ -159,6 +159,9 @@ public class SCWebSocketClient extends WebSocketClient {
...
@@ -159,6 +159,9 @@ public class SCWebSocketClient extends WebSocketClient {
@Override
@Override
public
void
onMessage
(
String
response
)
{
public
void
onMessage
(
String
response
)
{
// 由于项目设计阶段没有规范返回的结果
// 导致做统一处理比较麻烦(需要考虑到很多情况)
// 因此目前只采取通过JsonObject来逐个字段判断的低效方案
JsonObject
jsonObject
;
JsonObject
jsonObject
;
String
action
=
""
;
String
action
=
""
;
try
{
try
{
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
View file @
c8a3b955
...
@@ -22,6 +22,8 @@ import com.google.gson.JsonObject;
...
@@ -22,6 +22,8 @@ import com.google.gson.JsonObject;
import
org.java_websocket.handshake.ServerHandshake
;
import
org.java_websocket.handshake.ServerHandshake
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.concurrent.ScheduledFuture
;
import
java.util.concurrent.ScheduledFuture
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
...
@@ -100,6 +102,7 @@ public class PayOfflineViewModel extends ViewModel {
...
@@ -100,6 +102,7 @@ public class PayOfflineViewModel extends ViewModel {
private
static
final
String
RESPONSE_MESSAGE
=
"message"
;
private
static
final
String
RESPONSE_MESSAGE
=
"message"
;
private
static
final
String
RESPONSE_OFFLINE_RESULT
=
"操作完成"
;
private
static
final
String
RESPONSE_OFFLINE_RESULT
=
"操作完成"
;
private
static
final
String
RESPONSE_PAY_OFFLINE_RESULT
=
"PAY_OFFLINE_RESULT"
;
private
static
final
String
RESPONSE_PAY_OFFLINE_RESULT
=
"PAY_OFFLINE_RESULT"
;
private
static
final
String
RESPONSE_SUCCESS
=
"1"
;
// 每心跳多少次后进行一次离线订单检测
// 每心跳多少次后进行一次离线订单检测
private
static
final
int
PAY_OFFLINE_CHECK
=
30
;
private
static
final
int
PAY_OFFLINE_CHECK
=
30
;
// 当链接频繁断开时,给予离线订单检测一定缓冲时间
// 当链接频繁断开时,给予离线订单检测一定缓冲时间
...
@@ -126,12 +129,6 @@ public class PayOfflineViewModel extends ViewModel {
...
@@ -126,12 +129,6 @@ public class PayOfflineViewModel extends ViewModel {
heartbeatCount
++;
heartbeatCount
++;
if
(
heartbeatCount
>=
PAY_OFFLINE_CHECK
)
{
if
(
heartbeatCount
>=
PAY_OFFLINE_CHECK
)
{
heartbeatCount
=
0
;
heartbeatCount
=
0
;
long
currentTime
=
System
.
currentTimeMillis
();
if
((
currentTime
-
lastPayTime
)
<
PAY_OFFLINE_INTERVAL
)
{
LogUtils
.
w
(
TAG
,
"心跳引发离线检测冲突,跳过该次检测"
);
return
;
}
lastPayTime
=
currentTime
;
traversalPayOfflineData
();
traversalPayOfflineData
();
}
}
}
}
...
@@ -139,10 +136,17 @@ public class PayOfflineViewModel extends ViewModel {
...
@@ -139,10 +136,17 @@ public class PayOfflineViewModel extends ViewModel {
@Override
@Override
public
void
onMessage
(
String
action
,
JsonObject
obj
,
String
original
)
{
public
void
onMessage
(
String
action
,
JsonObject
obj
,
String
original
)
{
if
(
action
.
equals
(
RESPONSE_PAY_OFFLINE_RESULT
))
{
if
(
action
.
equals
(
RESPONSE_PAY_OFFLINE_RESULT
))
{
// 离线订单已写入到服务器数据库,可以标记为支付成功
String
code
=
null
;
LogUtils
.
d
(
TAG
,
"离线支付结果响应: "
+
original
);
// 这里为了兼容新的消息体需要单独对code作判断
ResponseRunnable
runnable
=
new
ResponseRunnable
(
original
);
if
(
obj
.
has
(
"code"
))
{
SCTaskExecutor
.
getInstance
().
executeOnDiskIO
(
runnable
);
code
=
obj
.
get
(
"code"
).
getAsString
();
}
if
(
code
!=
null
&&
code
.
equals
(
RESPONSE_SUCCESS
))
{
// 离线订单已写入到服务器数据库,可以标记为支付成功
LogUtils
.
d
(
TAG
,
"离线支付结果响应: "
+
original
);
ResponseRunnable
runnable
=
new
ResponseRunnable
(
original
);
SCTaskExecutor
.
getInstance
().
executeOnDiskIO
(
runnable
);
}
return
;
return
;
}
}
...
@@ -201,12 +205,58 @@ public class PayOfflineViewModel extends ViewModel {
...
@@ -201,12 +205,58 @@ public class PayOfflineViewModel extends ViewModel {
LogUtils
.
d
(
TAG
,
"所有离线订单处理完毕"
);
LogUtils
.
d
(
TAG
,
"所有离线订单处理完毕"
);
return
;
return
;
}
}
payRequest
=
new
PayRequest
(
deviceSN
,
payOfflineData
);
// 由于离线支付订单有可能和设备码不一致,需要兼容该情况
// 保证离线订单的equipmentId与equipmentNo一致
// 去除不一致的订单(留待下次上传)
String
mainEquipmentNo
=
acquireMaxEquipmentNo
(
payOfflineData
);
if
(
TextUtils
.
isEmpty
(
mainEquipmentNo
))
{
mainEquipmentNo
=
deviceSN
;
}
ArrayList
<
PayData
>
theSameEquipmentNoData
=
new
ArrayList
<>();
for
(
PayData
t
:
payOfflineData
)
{
if
(
t
.
getEquipmentNo
().
equals
(
mainEquipmentNo
))
{
theSameEquipmentNoData
.
add
(
t
);
}
}
payRequest
=
new
PayRequest
(
mainEquipmentNo
,
theSameEquipmentNoData
);
String
requestStr
=
gson
.
toJson
(
payRequest
);
String
requestStr
=
gson
.
toJson
(
payRequest
);
payOfflineState
.
postValue
(
new
PayOfflineState
(
PayOfflineState
.
SEND
,
requestStr
));
payOfflineState
.
postValue
(
new
PayOfflineState
(
PayOfflineState
.
SEND
,
requestStr
));
SCWebSocketClient
.
getInstance
().
send
(
requestStr
);
SCWebSocketClient
.
getInstance
().
send
(
requestStr
);
LogUtils
.
d
(
TAG
,
"离线支付: "
+
payRequest
.
toString
());
LogUtils
.
d
(
TAG
,
"离线支付: "
+
payRequest
.
toString
());
}
}
private
String
acquireMaxEquipmentNo
(
List
<
PayData
>
list
)
{
HashMap
<
String
,
Integer
>
tMap
=
new
HashMap
<>();
String
mainEquipmentNo
=
""
;
int
size
=
list
.
size
();
int
max
=
0
;
for
(
PayData
t
:
list
)
{
String
key
=
t
.
getEquipmentNo
();
int
value
;
if
(
tMap
.
containsKey
(
key
))
{
Integer
vWrapper
=
tMap
.
get
(
key
);
if
(
vWrapper
==
null
)
{
value
=
1
;
}
else
{
value
=
vWrapper
+
1
;
}
if
(
value
>
(
size
/
2
))
{
return
key
;
}
}
else
{
value
=
1
;
}
if
(
value
>=
max
)
{
max
=
value
;
mainEquipmentNo
=
key
;
}
tMap
.
put
(
key
,
value
);
}
tMap
.
clear
();
return
mainEquipmentNo
;
}
}
}
private
class
UpdateUploadTimeRunnable
implements
Runnable
{
private
class
UpdateUploadTimeRunnable
implements
Runnable
{
...
...
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