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
1d396c11
authored
Jul 11, 2019
by
patpat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改action架构(未完成)
parent
a2b15449
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
126 additions
and
210 deletions
+126
-210
app/src/main/java/com/bgycc/smartcanteen/App.kt
+8
-0
app/src/main/java/com/bgycc/smartcanteen/action/Action.java
+19
-4
app/src/main/java/com/bgycc/smartcanteen/action/PayAction.java
+0
-30
app/src/main/java/com/bgycc/smartcanteen/action/PayOnlineAction.java
+60
-23
app/src/main/java/com/bgycc/smartcanteen/helper/QRCodeHelper.java
+0
-74
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
+32
-34
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
+7
-45
No files found.
app/src/main/java/com/bgycc/smartcanteen/App.kt
View file @
1d396c11
...
...
@@ -2,9 +2,17 @@ package com.bgycc.smartcanteen
import
android.app.Application
import
com.bgycc.smartcanteen.helper.WifiHelpler
import
com.example.zhoukai.modemtooltest.ModemToolTest
import
com.example.zhoukai.modemtooltest.NvConstants
class
App
:
Application
()
{
companion
object
{
fun
getDeviceSN
():
String
{
return
ModemToolTest
.
getItem
(
NvConstants
.
REQUEST_GET_SN
);
}
}
override
fun
onCreate
()
{
super
.
onCreate
()
WifiHelpler
.
initialize
(
this
)
...
...
app/src/main/java/com/bgycc/smartcanteen/action/Action.java
View file @
1d396c11
...
...
@@ -26,6 +26,7 @@ public abstract class Action {
}
private
String
mAction
;
private
State
mState
;
private
ActionResult
mActionResult
;
protected
Action
(
String
action
)
{
mState
=
State
.
UNINIT
;
...
...
@@ -43,12 +44,26 @@ public abstract class Action {
return
mState
;
}
protected
void
setState
(
State
state
)
{
mState
=
state
;
}
protected
void
setActionResult
(
ActionResult
result
)
{
mActionResult
=
result
;
}
boolean
isAction
(
String
action
)
{
if
(
mAction
==
null
||
mAction
.
isEmpty
())
return
false
;
return
mAction
.
equals
(
action
);
}
public
void
response
(
JSONObject
response
)
{
if
(
response
==
null
)
{
fail
(
""
);
}
}
protected
void
response
(
int
sessionId
,
JSONObject
data
)
{
}
...
...
@@ -61,11 +76,11 @@ public abstract class Action {
},
ms
);
}
protected
void
success
(
String
message
,
ActionResult
result
)
{
if
(
result
!=
null
)
r
esult
.
onSuccess
(
message
);
protected
void
success
(
String
message
)
{
if
(
mActionResult
!=
null
)
mActionR
esult
.
onSuccess
(
message
);
}
protected
void
fail
(
String
message
,
ActionResult
result
)
{
if
(
result
!=
null
)
r
esult
.
onFail
(
message
);
protected
void
fail
(
String
message
)
{
if
(
mActionResult
!=
null
)
mActionR
esult
.
onFail
(
message
);
}
}
app/src/main/java/com/bgycc/smartcanteen/action/PayAction.java
deleted
100644 → 0
View file @
a2b15449
package
com
.
bgycc
.
smartcanteen
.
action
;
import
org.json.JSONObject
;
public
class
PayAction
extends
Action
{
public
PayAction
(
String
action
)
{
super
(
action
);
}
@Override
public
void
exec
(
JSONObject
data
,
ActionResult
result
)
{
if
(
isAction
(
ActionEnum
.
PAY_RESULT
.
name
()))
{
if
(
data
==
null
)
{
fail
(
"数据格式错误"
,
result
);
return
;
}
String
payCode
=
data
.
optString
(
"payCode"
,
""
);
if
(
payCode
.
isEmpty
())
{
fail
(
"payCode不能为空"
,
result
);
return
;
}
success
(
""
,
result
);
}
else
{
fail
(
"action无效"
,
result
);
}
}
}
app/src/main/java/com/bgycc/smartcanteen/action/PayOnlineAction.java
View file @
1d396c11
package
com
.
bgycc
.
smartcanteen
.
action
;
import
com.bgycc.smartcanteen.App
;
import
com.bgycc.smartcanteen.event.PayStateEvent
;
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
;
import
org.greenrobot.eventbus.EventBus
;
...
...
@@ -11,53 +12,89 @@ import java.util.Locale;
public
class
PayOnlineAction
extends
Action
{
public
PayOnlineAction
(
String
action
)
{
super
(
action
);
public
PayOnlineAction
()
{
super
(
ActionEnum
.
PAY_ONLINE
.
name
());
MainWebSocket
.
subscribe
(
ActionEnum
.
PAY_RESULT
.
name
(),
mPayResultResponse
);
}
protected
void
setState
(
State
state
,
String
message
)
{
if
(
state
!=
getState
())
{
switch
(
state
)
{
case
INITED:
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
IDLE
,
message
));
break
;
case
STARTED:
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
WAIT
,
message
));
break
;
case
RESPONSE_SUCCESS:
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
SUCCESS
,
message
));
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
setState
(
State
.
INITED
,
""
);
}
},
3000
);
break
;
case
RESQUEST_FAIL:
case
RESQUEST_TIMEOUT:
case
RESPONSE_FAIL:
case
RESPONSE_TIMEOUT:
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
FAIL
,
message
));
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
setState
(
State
.
INITED
,
""
);
}
},
3000
);
break
;
}
}
setState
(
state
);
}
public
void
exec
(
String
payCode
,
String
payCodeType
,
ActionResult
result
)
{
if
(
getState
()
!=
State
.
INITED
)
return
;
setActionResult
(
result
);
final
MainWebSocket
.
Response
response
=
new
MainWebSocket
.
Response
()
{
@Override
protected
void
onSuccess
(
JSONObject
data
,
String
message
)
{
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
SUCCESS
,
message
)
);
setState
(
State
.
RESQUEST_SUCCESS
,
message
);
}
@Override
protected
void
onFail
(
String
code
,
String
message
)
{
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
FAIL
,
message
));
resetStep
();
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
restart
();
}
},
3000
);
setState
(
State
.
RESPONSE_FAIL
,
message
);
}
};
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
response
.
fail
(
null
,
"交易失败"
);
response
.
cancel
();
setState
(
State
.
RESPONSE_FAIL
,
"交易失败"
);
}
},
10000
);
MainWebSocket
.
payOnline
(
event
.
string
,
event
.
payCodeType
,
response
);
try
{
JSONObject
json
=
new
JSONObject
();
json
.
put
(
"equipmentNo"
,
sDeviceSN
);
json
.
put
(
"payCode"
,
payCode
);
json
.
put
(
"terminalType"
,
payCodeType
);
json
.
put
(
"time"
,
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
,
Locale
.
getDefault
()).
format
(
new
Date
()));
MainWebSocket
.
action
(
getAction
(),
json
,
response
);
JSONObject
params
=
new
JSONObject
();
params
.
put
(
"equipmentNo"
,
App
.
Companion
.
getDeviceSN
()
);
params
.
put
(
"payCode"
,
payCode
);
params
.
put
(
"terminalType"
,
payCodeType
);
params
.
put
(
"time"
,
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
,
Locale
.
getDefault
()).
format
(
new
Date
()));
MainWebSocket
.
action
(
getAction
(),
params
,
response
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
void
response
(
JSONObject
response
)
{
}
MainWebSocket
.
Response
mPayResultResponse
=
new
MainWebSocket
.
Response
()
{
@Override
protected
void
onSuccess
(
JSONObject
data
,
String
message
)
{
super
.
onSuccess
(
data
,
message
);
}
@Override
protected
void
onFail
(
String
code
,
String
message
)
{
super
.
onFail
(
code
,
message
);
}
};
}
app/src/main/java/com/bgycc/smartcanteen/helper/QRCodeHelper.java
deleted
100644 → 0
View file @
a2b15449
package
com
.
bgycc
.
smartcanteen
.
helper
;
import
com.bgycc.smartcanteen.QRCodeEvent
;
import
com.szxb.jni.libszxb
;
import
org.greenrobot.eventbus.EventBus
;
import
java.util.regex.Pattern
;
public
class
QRCodeHelper
{
public
static
String
TYPE_ALIPAY
=
"ALIPAY"
;
public
static
String
TYPE_WXPAY
=
"WXPAY"
;
public
static
String
TYPE_BHPAY
=
"BHPAY"
;
private
static
QRCodeHelper
sInstance
;
public
static
synchronized
QRCodeHelper
getInstance
()
{
if
(
sInstance
==
null
)
{
sInstance
=
new
QRCodeHelper
();
}
return
sInstance
;
}
private
LoopThread
mLoopThread
;
private
QRCodeHelper
()
{}
public
boolean
support
()
{
return
true
;
}
public
void
start
()
{
if
(!
support
())
return
;
if
(
mLoopThread
==
null
)
{
mLoopThread
=
new
LoopThread
();
mLoopThread
.
start
();
}
}
public
void
stop
()
{
if
(
mLoopThread
!=
null
)
{
mLoopThread
.
exit
=
true
;
}
}
private
class
LoopThread
extends
Thread
{
boolean
exit
=
false
;
@Override
public
void
run
()
{
while
(
true
)
{
byte
[]
buf
=
new
byte
[
1024
];
int
len
=
libszxb
.
getBarcode
(
buf
);
if
(
len
>
0
)
{
String
str
=
new
String
(
buf
,
0
,
len
);
EventBus
.
getDefault
().
post
(
new
QRCodeEvent
(
str
.
getBytes
(),
str
,
checkPayCodeType
(
str
)));
}
if
(
this
.
exit
)
{
mLoopThread
=
null
;
break
;
}
}
}
}
private
String
checkPayCodeType
(
String
code
)
{
if
(
Pattern
.
matches
(
"^(10|11|12|13|14|15)\\d{16}$"
,
code
))
return
TYPE_WXPAY
;
if
(
Pattern
.
matches
(
"^(25|26|27|28|29|30)\\d{14,22}$"
,
code
))
return
TYPE_ALIPAY
;
if
(
Pattern
.
matches
(
"^(38|39)\\d{16}$"
,
code
))
return
TYPE_BHPAY
;
return
null
;
}
}
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
View file @
1d396c11
...
...
@@ -3,9 +3,7 @@ package com.bgycc.smartcanteen.server.websocket;
import
android.util.Log
;
import
android.util.SparseArray
;
import
com.bgycc.smartcanteen.action.ActionEnum
;
import
com.bgycc.smartcanteen.action.ActionResult
;
import
com.bgycc.smartcanteen.action.Action
;
import
com.bgycc.smartcanteen.action.PayAction
;
import
com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent
;
import
com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
;
import
com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent
;
...
...
@@ -100,31 +98,25 @@ public class MainWebSocket extends WebSocketClient {
sInstance
.
send
(
action
,
params
,
callback
);
}
public
static
boolean
subscribe
(
Action
action
)
{
if
(
action
==
null
)
return
false
;
public
static
boolean
subscribe
(
String
action
,
Response
response
)
{
if
(
action
==
null
||
action
.
isEmpty
()
)
return
false
;
String
a
=
action
.
getAction
();
if
(
a
==
null
||
a
.
isEmpty
())
return
false
;
ArrayList
<
Action
>
list
=
sInstance
.
mSubscribeList
.
get
(
a
);
ArrayList
<
Response
>
list
=
sInstance
.
mSubscribeList
.
get
(
action
);
if
(
list
==
null
)
{
list
=
new
ArrayList
<
Action
>();
sInstance
.
mSubscribeList
.
put
(
a
,
list
);
list
=
new
ArrayList
<
Response
>();
sInstance
.
mSubscribeList
.
put
(
a
ction
,
list
);
}
list
.
add
(
action
);
list
.
add
(
response
);
return
true
;
}
public
static
boolean
unsubscribe
(
Action
action
)
{
if
(
action
==
null
)
return
false
;
String
a
=
action
.
getAction
();
if
(
a
==
null
||
a
.
isEmpty
())
return
false
;
public
static
boolean
unsubscribe
(
String
action
,
Response
response
)
{
if
(
action
==
null
||
action
.
isEmpty
())
return
false
;
ArrayList
<
Action
>
list
=
sInstance
.
mSubscribeList
.
get
(
a
);
ArrayList
<
Response
>
list
=
sInstance
.
mSubscribeList
.
get
(
action
);
if
(
list
==
null
)
return
false
;
return
list
.
remove
(
action
);
return
list
.
remove
(
response
);
}
private
static
void
response
(
int
sessionId
,
String
code
,
String
message
)
{
...
...
@@ -146,7 +138,7 @@ public class MainWebSocket extends WebSocketClient {
private
int
mSessionId
=
0
;
private
final
SparseArray
<
Resquest
>
mResquestList
=
new
SparseArray
<>();
private
final
HashMap
<
String
,
ArrayList
<
Action
>>
mSubscribeList
=
new
HashMap
<>();
private
final
HashMap
<
String
,
ArrayList
<
Response
>>
mSubscribeList
=
new
HashMap
<>();
private
MainWebSocket
(
URI
serverUri
)
{
super
(
serverUri
,
new
Draft_6455
(),
null
,
10000
);
...
...
@@ -158,6 +150,7 @@ public class MainWebSocket extends WebSocketClient {
mSessionId
++;
Resquest
resquest
=
new
Resquest
();
resquest
.
action
=
action
;
resquest
.
time
=
System
.
currentTimeMillis
();
resquest
.
response
=
callback
;
resquest
.
params
=
new
JSONObject
();
...
...
@@ -193,23 +186,15 @@ public class MainWebSocket extends WebSocketClient {
String
action
=
json
.
optString
(
FieldEnum
.
action
.
name
(),
""
);
String
code
=
json
.
optString
(
FieldEnum
.
code
.
name
(),
""
);
if
(
code
.
isEmpty
())
{
if
(
action
.
startsWith
(
ActionEnum
.
PAY_
.
name
()))
{
new
PayAction
(
action
).
exec
(
json
.
optJSONObject
(
FieldEnum
.
data
.
name
()),
new
ActionResult
()
{
@Override
public
void
onSuccess
(
String
message
)
{
response
(
sessionId
,
CODE_OK
,
message
);
}
@Override
public
void
onFail
(
String
message
)
{
response
(
sessionId
,
CODE_FAIL
,
message
);
}
});
ArrayList
<
Response
>
list
=
mSubscribeList
.
get
(
action
);
if
(
list
!=
null
&&
list
.
size
()
>
0
)
{
for
(
Response
res
:
list
)
{
if
(
res
!=
null
)
res
.
parse
(
code
,
json
);
}
}
else
{
Resquest
resquest
=
mResquestList
.
get
(
sessionId
);
if
(
resquest
!=
null
)
{
resquest
.
parseRespon
se
(
code
,
json
);
if
(
resquest
!=
null
&&
resquest
.
response
!=
null
)
{
resquest
.
response
.
par
se
(
code
,
json
);
}
}
}
catch
(
JSONException
e
)
{
...
...
@@ -230,13 +215,15 @@ public class MainWebSocket extends WebSocketClient {
}
public
static
class
Resquest
{
String
action
;
long
time
=
0
;
JSONObject
params
;
Response
response
;
void
parseResponse
(
String
code
,
JSONObject
res
)
{
if
(
res
==
null
)
return
;
if
(
res
==
null
||
response
==
null
)
return
;
response
.
parse
(
code
,
res
);
String
message
=
res
.
optString
(
FieldEnum
.
message
.
name
());
if
(
CODE_OK
.
equals
(
code
))
{
response
.
onSuccess
(
res
.
optJSONObject
(
FieldEnum
.
data
.
name
()),
message
);
...
...
@@ -250,6 +237,17 @@ public class MainWebSocket extends WebSocketClient {
boolean
cancel
=
false
;
void
parse
(
String
code
,
JSONObject
res
)
{
if
(
res
==
null
)
return
;
String
message
=
res
.
optString
(
FieldEnum
.
message
.
name
());
if
(
CODE_OK
.
equals
(
code
))
{
success
(
res
.
optJSONObject
(
FieldEnum
.
data
.
name
()),
message
);
}
else
{
fail
(
code
,
message
);
}
}
public
void
cancel
()
{
this
.
cancel
=
true
;
}
...
...
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
View file @
1d396c11
...
...
@@ -5,6 +5,7 @@ import com.bgycc.smartcanteen.QRCodeEvent;
import
com.bgycc.smartcanteen.action.Action
;
import
com.bgycc.smartcanteen.action.ActionEnum
;
import
com.bgycc.smartcanteen.action.ActionResult
;
import
com.bgycc.smartcanteen.action.PayOnlineAction
;
import
com.bgycc.smartcanteen.event.PayStateEvent
;
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
;
import
com.szxb.jni.libszxb
;
...
...
@@ -59,37 +60,14 @@ public class QRCodeTask {
int
lastLen
=
-
1
;
byte
[]
lastBuf
;
PayOnlineAction
payOnlineAction
=
new
PayOnlineAction
();
AsyncTask
asyncTask
=
mAsyncTask
=
new
AsyncTask
()
{
@Override
protected
void
init
()
{
MainWebSocket
.
subscribe
(
new
Action
(
ActionEnum
.
PAY_RESULT
.
name
())
{
@Override
protected
void
exec
(
JSONObject
data
,
ActionResult
result
)
{
if
(
data
==
null
)
{
fail
(
"数据格式错误"
,
result
);
return
;
}
String
payCode
=
data
.
optString
(
"payCode"
,
""
);
if
(
payCode
.
isEmpty
())
{
fail
(
"payCode不能为空"
,
result
);
return
;
}
success
(
""
,
result
);
}
});
}
@Override
protected
void
run
(
int
step
,
int
progress
)
{
Log
.
i
(
"LoopTimerTask"
,
String
.
format
(
"step: %d progress: %d"
,
step
,
progress
));
switch
(
step
)
{
case
0
:
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
IDLE
));
nextStep
();
break
;
case
1
:
if
(
progress
!=
0
)
break
;
byte
[]
buf
=
new
byte
[
1024
];
...
...
@@ -106,32 +84,16 @@ public class QRCodeTask {
if
(
event
.
payCodeType
==
null
)
{
parseCommand
(
event
);
}
else
{
final
MainWebSocket
.
Response
response
=
new
MainWebSocket
.
Response
()
{
payOnlineAction
.
exec
(
event
.
string
,
event
.
payCodeType
,
new
ActionResult
()
{
@Override
protected
void
onSuccess
(
JSONObject
data
,
String
message
)
{
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
SUCCESS
,
message
));
}
@Override
protected
void
onFail
(
String
code
,
String
message
)
{
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
FAIL
,
message
));
public
void
onSuccess
(
String
message
)
{
resetStep
();
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
restart
();
}
},
3000
);
}
};
timeout
(
new
Runnable
()
{
@Override
public
void
run
()
{
response
.
fail
(
null
,
"交易失败"
);
response
.
cancel
();
public
void
onFail
(
String
message
)
{
resetStep
();
}
},
10000
);
MainWebSocket
.
payOnline
(
event
.
string
,
event
.
payCodeType
,
response
);
EventBus
.
getDefault
().
post
(
new
PayStateEvent
(
PayStateEvent
.
StateEnum
.
WAIT
));
});
nextProgress
();
}
break
;
...
...
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