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
bc5f9705
authored
Jul 09, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、完成CommandViewModel的重构
2、部分业务逻辑转移到Repositoy
parent
c18d21a7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
142 deletions
+18
-142
app/src/main/java/com/bgycc/smartcanteen/command/CommandHelper.java
+1
-1
app/src/main/java/com/bgycc/smartcanteen/repository/CommandRepository.java
+14
-0
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.java
+0
-131
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.kt
+3
-10
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/CommandHelper.java
View file @
bc5f9705
...
@@ -59,7 +59,7 @@ public class CommandHelper {
...
@@ -59,7 +59,7 @@ public class CommandHelper {
* true => 数据库已有"未完成"状态的该类指令,不应该重复插入 <br/>
* true => 数据库已有"未完成"状态的该类指令,不应该重复插入 <br/>
* false => 数据库中该类指令都"已完成"或没有该类指令,可插入新的指令 <br/>
* false => 数据库中该类指令都"已完成"或没有该类指令,可插入新的指令 <br/>
*/
*/
public
static
boolean
on
eAt
Time
(
Command
command
)
{
public
static
boolean
on
cePer
Time
(
Command
command
)
{
return
command
.
getAction
().
equals
(
APP_UPDATE
);
return
command
.
getAction
().
equals
(
APP_UPDATE
);
}
}
...
...
app/src/main/java/com/bgycc/smartcanteen/repository/CommandRepository.java
View file @
bc5f9705
...
@@ -4,9 +4,12 @@ import androidx.lifecycle.LiveData;
...
@@ -4,9 +4,12 @@ import androidx.lifecycle.LiveData;
import
com.bgycc.smartcanteen.data.dao.CommandDao
;
import
com.bgycc.smartcanteen.data.dao.CommandDao
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.blankj.utilcode.util.LogUtils
;
import
java.util.List
;
import
java.util.List
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
public
class
CommandRepository
{
public
class
CommandRepository
{
private
CommandDao
dao
;
private
CommandDao
dao
;
...
@@ -26,6 +29,17 @@ public class CommandRepository {
...
@@ -26,6 +29,17 @@ public class CommandRepository {
return
dao
.
insertCommand
(
command
);
return
dao
.
insertCommand
(
command
);
}
}
public
long
insertCommand
(
Command
command
,
boolean
oncePerTime
)
{
if
(
oncePerTime
)
{
int
count
=
queryUndoneCommandCountByAction
(
command
.
getAction
());
if
(
count
>
0
)
{
LogUtils
.
d
(
TAG
,
"该指令已在数据库中记录并未执行完毕,不再插入重复指令"
);
return
0
;
}
}
return
insertCommand
(
command
);
}
public
int
finishCommand
(
Command
command
)
{
public
int
finishCommand
(
Command
command
)
{
command
.
finish
();
command
.
finish
();
return
updateCommand
(
command
);
return
updateCommand
(
command
);
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.java
deleted
100644 → 0
View file @
c18d21a7
package
com
.
bgycc
.
smartcanteen
.
viewModel
;
import
androidx.lifecycle.LiveData
;
import
androidx.lifecycle.MutableLiveData
;
import
androidx.lifecycle.Observer
;
import
androidx.lifecycle.ViewModel
;
import
androidx.work.OneTimeWorkRequest
;
import
com.bgycc.smartcanteen.command.CommandHelper
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.executor.SCTaskExecutor
;
import
com.bgycc.smartcanteen.socket.SCWebSocketClient
;
import
com.bgycc.smartcanteen.socket.SCWebSocketListener
;
import
com.bgycc.smartcanteen.socket.SCWebSocketListenerAdapter
;
import
com.bgycc.smartcanteen.repository.CommandRepository
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonObject
;
import
java.util.List
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
/**
* 监听本地扫码、服务器下发的设置指令 <br/>
* 监听Command数据库的变动,按如下流程进行处理: <br/>
* 从数据库读取 -> 解析执行 -> 更新到数据库
*/
public
class
CommandViewModel
extends
ViewModel
{
private
CommandRepository
commandRepository
;
private
Gson
gson
;
private
String
deviceSN
;
private
MutableLiveData
<
Command
>
commandWorker
=
new
MutableLiveData
<>();
private
LiveData
<
List
<
Command
>>
dataLiveData
;
public
LiveData
<
Command
>
getCommandTask
()
{
return
commandWorker
;
}
public
CommandViewModel
(
CommandRepository
commandRepository
,
Gson
gson
,
String
deviceSN
)
{
this
.
commandRepository
=
commandRepository
;
this
.
gson
=
gson
;
this
.
deviceSN
=
deviceSN
;
// 监听数据库的变动,并执行未完成的指令
this
.
dataLiveData
=
commandRepository
.
queryUndoneCommand
();
this
.
dataLiveData
.
observeForever
(
dataObserver
);
}
public
void
initialize
()
{
SCWebSocketClient
.
getInstance
().
addListener
(
listener
);
}
@Override
protected
void
onCleared
()
{
super
.
onCleared
();
if
(
dataLiveData
!=
null
)
{
dataLiveData
.
removeObserver
(
dataObserver
);
}
}
private
Observer
<
List
<
Command
>>
dataObserver
=
commands
->
{
if
(
commands
==
null
||
commands
.
isEmpty
())
{
return
;
}
Command
first
=
commands
.
get
(
0
);
commandWorker
.
postValue
(
first
);
};
public
OneTimeWorkRequest
getCommandWorker
(
Command
command
)
{
return
CommandHelper
.
createWorker
(
gson
,
command
,
deviceSN
);
}
public
void
commandFinish
(
Command
command
)
{
UpdateDatabaseRunnable
runnable
=
new
UpdateDatabaseRunnable
(
command
);
SCTaskExecutor
.
getInstance
().
executeOnDiskIO
(
runnable
);
}
private
SCWebSocketListener
listener
=
new
SCWebSocketListenerAdapter
()
{
@Override
public
void
onMessage
(
String
action
,
JsonObject
obj
,
String
original
)
{
if
(!
CommandHelper
.
isCommand
(
action
))
return
;
LogUtils
.
d
(
TAG
,
"设备下发指令: "
+
original
);
ResponseRunnable
runnable
=
new
ResponseRunnable
(
original
,
action
);
SCTaskExecutor
.
getInstance
().
executeOnDiskIO
(
runnable
);
}
};
private
class
UpdateDatabaseRunnable
implements
Runnable
{
private
Command
command
;
UpdateDatabaseRunnable
(
Command
command
)
{
this
.
command
=
command
;
}
@Override
public
void
run
()
{
command
.
finish
();
LogUtils
.
d
(
TAG
,
"指令执行完毕: "
+
command
.
toString
());
commandRepository
.
updateCommand
(
command
);
}
}
private
class
ResponseRunnable
implements
Runnable
{
private
String
response
;
private
String
action
;
ResponseRunnable
(
String
response
,
String
action
)
{
this
.
response
=
response
;
this
.
action
=
action
;
}
@Override
public
void
run
()
{
// 指令插入到数据库,则会触发dataObserver
Command
command
=
new
Command
(
response
,
action
);
// 该action同一时间是否只能在数据库存在一条待执行的指令
if
(
CommandHelper
.
oneAtTime
(
command
))
{
int
count
=
commandRepository
.
queryUndoneCommandCountByAction
(
action
);
if
(
count
>
0
)
{
LogUtils
.
d
(
TAG
,
"该指令已在数据库中记录并未执行完毕,不再插入重复指令"
);
return
;
}
}
long
lastInsertId
=
commandRepository
.
insertCommand
(
command
);
if
(
lastInsertId
==
-
1
)
{
LogUtils
.
w
(
TAG
,
"指令插入到数据库失败: "
+
command
.
toString
());
}
}
}
}
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.kt
View file @
bc5f9705
...
@@ -27,8 +27,7 @@ class CommandViewModel(
...
@@ -27,8 +27,7 @@ class CommandViewModel(
if
(
commands
==
null
||
commands
.
isEmpty
())
{
if
(
commands
==
null
||
commands
.
isEmpty
())
{
return
@Observer
return
@Observer
}
}
val
first
=
commands
[
0
]
commandWorker
.
postValue
(
commands
.
first
())
commandWorker
.
postValue
(
first
)
}
}
init
{
init
{
...
@@ -67,15 +66,9 @@ class CommandViewModel(
...
@@ -67,15 +66,9 @@ class CommandViewModel(
viewModelScope
.
launch
(
coroutinesDispatcher
)
{
viewModelScope
.
launch
(
coroutinesDispatcher
)
{
// 指令插入到数据库,则会触发dataObserver
// 指令插入到数据库,则会触发dataObserver
val
command
=
Command
(
original
,
action
)
val
command
=
Command
(
original
,
action
)
val
oncePerTime
=
CommandHelper
.
oncePerTime
(
command
)
// 该action同一时间是否只能在数据库存在一条待执行的指令
// 该action同一时间是否只能在数据库存在一条待执行的指令
if
(
CommandHelper
.
oneAtTime
(
command
))
{
val
lastInsertId
:
Long
=
commandRepository
.
insertCommand
(
command
,
oncePerTime
)
val
count
:
Int
=
commandRepository
.
queryUndoneCommandCountByAction
(
action
)
if
(
count
>
0
)
{
LogUtils
.
d
(
SmartCanteenUtils
.
TAG
,
"该指令已在数据库中记录并未执行完毕,不再插入重复指令"
)
return
@launch
}
}
val
lastInsertId
:
Long
=
commandRepository
.
insertCommand
(
command
)
if
(
lastInsertId
==
-
1L
)
{
if
(
lastInsertId
==
-
1L
)
{
LogUtils
.
w
(
SmartCanteenUtils
.
TAG
,
"指令插入到数据库失败: $command"
)
LogUtils
.
w
(
SmartCanteenUtils
.
TAG
,
"指令插入到数据库失败: $command"
)
}
}
...
...
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