Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
ne-shield-devicemonitor
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit cb940650
authored
Jun 02, 2022
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加设备批量同步接口
1 parent
f7f0628b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
183 additions
and
2 deletions
src/main/java/cn/com/glsx/devicemonitor/modules/api/FeignDeviceController.java
src/main/java/cn/com/glsx/devicemonitor/modules/model/TrackInfoDTO.java
src/main/java/cn/com/glsx/devicemonitor/modules/model/response/GetEShieldTrackInfoListResp.java
src/main/java/cn/com/glsx/devicemonitor/modules/service/DeviceService.java
src/main/java/cn/com/glsx/devicemonitor/modules/service/impl/DeviceServiceImpl.java
src/main/java/cn/com/glsx/devicemonitor/modules/service/impl/EShieldService.java
src/test/java/cn/com/glsx/devicemonitor/modules/service/impl/DeviceServiceImplTest.java
src/test/java/cn/com/glsx/devicemonitor/modules/service/impl/EShieldServiceTest.java
src/main/java/cn/com/glsx/devicemonitor/modules/api/FeignDeviceController.java
View file @
cb94065
...
...
@@ -50,7 +50,7 @@ public class FeignDeviceController implements DeviceFeignService {
}
@Override
@GetMapping
(
{
"/getOrStoreDeviceBySn"
}
)
@GetMapping
(
value
=
"/getOrStoreDeviceBySn"
)
public
R
<
DeviceDTO
>
getOrStoreDeviceBySn
(
@RequestParam
(
value
=
"sn"
)
String
sn
,
@RequestParam
(
value
=
"storeIfNotExists"
)
Boolean
storeIfNotExists
)
{
if
(
storeIfNotExists
==
null
)
{
storeIfNotExists
=
false
;
...
...
@@ -59,6 +59,13 @@ public class FeignDeviceController implements DeviceFeignService {
return
R
.
ok
().
data
(
deviceDTO
);
}
@Override
@PostMapping
(
value
=
"/getOrStoreDeviceBySns"
)
public
R
<
List
<
DeviceDTO
>>
getOrStoreDeviceBySns
(
List
<
String
>
sns
)
{
List
<
DeviceDTO
>
deviceDTOList
=
deviceService
.
getOrStoreDeviceBySns
(
sns
);
return
R
.
ok
().
data
(
deviceDTOList
);
}
/**
* 获取设备的状态
*
...
...
src/main/java/cn/com/glsx/devicemonitor/modules/model/TrackInfoDTO.java
View file @
cb94065
...
...
@@ -42,4 +42,8 @@ public class TrackInfoDTO {
private
String
partner
;
/**
* 设备类型名称
*/
private
String
deviceClassifyName
;
}
\ No newline at end of file
src/main/java/cn/com/glsx/devicemonitor/modules/model/response/GetEShieldTrackInfoListResp.java
0 → 100644
View file @
cb94065
package
cn
.
com
.
glsx
.
devicemonitor
.
modules
.
model
.
response
;
import
cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
GetEShieldTrackInfoListResp
extends
AbstractEShieldResponse
{
/**
* 设备集合
*/
private
List
<
TrackInfoDTO
>
data
;
}
src/main/java/cn/com/glsx/devicemonitor/modules/service/DeviceService.java
View file @
cb94065
...
...
@@ -49,6 +49,15 @@ public interface DeviceService {
*/
DeviceDTO
getDeviceDTOBySn
(
String
sn
,
Boolean
storeIfNotExists
);
/**
* 根据设备号批量查询设备信息,不存在就同步
*
* @param sns 设备号集合
* @return
*/
List
<
DeviceDTO
>
getOrStoreDeviceBySns
(
List
<
String
>
sns
);
/**
* 获取设备的在线状态
*
...
...
@@ -66,6 +75,14 @@ public interface DeviceService {
*/
Device
getDeviceOrRemote
(
String
sn
,
Boolean
storeIfNotExists
);
/**
* 批量查询设备(本地库没有,查询旧E盾)
*
* @param sns 设备号集合
* @return
*/
List
<
Device
>
getDeviceListOrRemote
(
List
<
String
>
sns
);
/**
* 获取设备上点总次数
...
...
@@ -100,5 +117,4 @@ public interface DeviceService {
* @return
*/
Map
<
String
,
DeviceTypeRelationEnum
>
getDeviceTypes
(
List
<
String
>
snList
);
}
src/main/java/cn/com/glsx/devicemonitor/modules/service/impl/DeviceServiceImpl.java
View file @
cb94065
...
...
@@ -15,8 +15,10 @@ import cn.com.glsx.devicemonitor.modules.mapper.DeviceMapper;
import
cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO
;
import
cn.com.glsx.devicemonitor.modules.service.DeviceService
;
import
cn.com.glsx.devicemonitor.modules.utils.DeviceUtils
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.alibaba.nacos.common.utils.CollectionUtils
;
import
com.alibaba.nacos.common.utils.StringUtils
;
import
com.google.common.collect.Lists
;
import
com.google.common.collect.Maps
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -48,6 +50,7 @@ public class DeviceServiceImpl implements DeviceService {
return
deviceMapper
.
selectBySn
(
sn
);
}
@Override
public
int
batchAdd
(
List
<
DeviceBO
>
deviceBOList
)
{
log
.
info
(
"设备数据:{}"
,
JSONUtil
.
toJsonStr
(
deviceBOList
));
if
(
CollectionUtils
.
isEmpty
(
deviceBOList
))
{
...
...
@@ -83,6 +86,19 @@ public class DeviceServiceImpl implements DeviceService {
}
@Override
public
List
<
DeviceDTO
>
getOrStoreDeviceBySns
(
List
<
String
>
sns
)
{
if
(
CollectionUtil
.
isEmpty
(
sns
))
{
return
null
;
}
List
<
Device
>
deviceList
=
getDeviceListOrRemote
(
sns
);
return
deviceList
.
stream
().
map
(
device
->
{
DeviceDTO
deviceDTO
=
new
DeviceDTO
();
BeanUtils
.
copyProperties
(
device
,
deviceDTO
);
return
deviceDTO
;
}).
collect
(
Collectors
.
toList
());
}
@Override
public
DeviceStatusModel
getDeviceStatusBySn
(
String
sn
,
Long
offlineTime
)
{
Device
device
=
getDeviceOrRemote
(
sn
,
false
);
if
(
device
==
null
)
{
...
...
@@ -178,12 +194,44 @@ public class DeviceServiceImpl implements DeviceService {
return
device
;
}
@Override
public
List
<
Device
>
getDeviceListOrRemote
(
List
<
String
>
sns
)
{
List
<
Device
>
deviceList
=
deviceMapper
.
selectBySns
(
sns
);
List
<
String
>
dbSnList
=
deviceList
.
stream
().
map
(
Device:
:
getSn
).
collect
(
Collectors
.
toList
());
// 获取不在数据库中的设备号集合(老E盾没有同步过来的设备号)
List
<
String
>
remoteSnList
=
sns
.
stream
().
filter
(
sn
->
!
dbSnList
.
contains
(
sn
)).
collect
(
Collectors
.
toList
());
List
<
Device
>
remoteDeviceList
=
getRemoteDeviceList
(
StringUtils
.
join
(
remoteSnList
,
","
));
if
(
CollectionUtil
.
isNotEmpty
(
remoteDeviceList
))
{
//如果老E盾存在 则同步老E盾中的设备数据到新E盾
deviceMapper
.
insertList
(
remoteDeviceList
);
deviceList
.
addAll
(
remoteDeviceList
);
}
return
deviceList
;
}
/**
* 通过设备号获取老E盾设备号数据
*
* @param sn 设备号
* @return
*/
public
Device
getRemoteDevice
(
String
sn
)
{
TrackInfoDTO
trackInfoDTO
=
eShieldService
.
getDeviceBySn
(
sn
);
return
convertDevice
(
trackInfoDTO
,
0L
);
}
/**
* 根据设备号集合获取老E盾设备号数据
*
* @param sns 设备号集合
* @return
*/
public
List
<
Device
>
getRemoteDeviceList
(
String
sns
)
{
List
<
TrackInfoDTO
>
trackInfoDTOList
=
eShieldService
.
getDeviceListBySns
(
sns
);
return
convertDeviceList
(
trackInfoDTOList
,
0L
);
}
/**
* 保存从E盾过来的设备
*
* @param trackInfo
...
...
@@ -204,6 +252,32 @@ public class DeviceServiceImpl implements DeviceService {
return
device
;
}
/**
* 保存从E盾过来的设备
*
* @param trackInfoList 设备号集合
* @param createId 默认创建人
*/
public
List
<
Device
>
convertDeviceList
(
List
<
TrackInfoDTO
>
trackInfoList
,
Long
createId
)
{
if
(
CollectionUtil
.
isEmpty
(
trackInfoList
))
{
return
null
;
}
List
<
Device
>
deviceList
=
Lists
.
newArrayList
();
for
(
TrackInfoDTO
trackInfo
:
trackInfoList
)
{
Device
device
=
new
Device
();
BeanUtils
.
copyProperties
(
trackInfo
,
device
);
device
.
setManufacturer
(
trackInfo
.
getPartner
());
device
.
setModelName
(
trackInfo
.
getDeviceClassifyName
());
device
.
setSource
(
trackInfo
.
getSource
());
device
.
setActivateStatus
(
trackInfo
.
getIsActive
());
device
.
setActivateTime
(
trackInfo
.
getActiveDate
());
device
.
setCreatedBy
(
createId
);
device
.
setCreatedDate
(
new
Date
());
deviceList
.
add
(
device
);
}
return
deviceList
;
}
@Override
public
Integer
getDevicesTotalCount
(
String
sn
)
{
DevicesTotalCountResHolder
res
=
new
DevicesTotalCountResHolder
();
...
...
src/main/java/cn/com/glsx/devicemonitor/modules/service/impl/EShieldService.java
View file @
cb94065
...
...
@@ -3,6 +3,7 @@ package cn.com.glsx.devicemonitor.modules.service.impl;
import
cn.com.glsx.devicemonitor.config.EShieldConfig
;
import
cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO
;
import
cn.com.glsx.devicemonitor.modules.model.response.GetDeviceClassifyNameResp
;
import
cn.com.glsx.devicemonitor.modules.model.response.GetEShieldTrackInfoListResp
;
import
cn.com.glsx.devicemonitor.modules.model.response.GetEShieldTrackInfoResp
;
import
cn.com.glsx.devicemonitor.modules.utils.GopUtil
;
import
com.alibaba.fastjson.JSONObject
;
...
...
@@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
import
java.util.TreeMap
;
@Slf4j
...
...
@@ -44,6 +46,29 @@ public class EShieldService {
}
/**
* 调用e盾标准接口批量获取设备信息
*
* @param sns 设备号集合 用 , 分隔
*/
public
List
<
TrackInfoDTO
>
getDeviceListBySns
(
String
sns
)
{
String
url
=
config
.
getApiUrl
()
+
"/api/device/getInfoListBySns"
;
TreeMap
<
String
,
Object
>
tparams
=
new
TreeMap
<>();
tparams
.
put
(
"appKey"
,
config
.
getAppId
());
tparams
.
put
(
"sns"
,
sns
);
//加密得到sign
String
sign
=
GopUtil
.
sign
(
tparams
,
config
.
getAppSecret
());
tparams
.
put
(
"sign"
,
sign
);
String
result
=
HttpUtils
.
get
(
url
,
tparams
);
log
.
info
(
"getInfoListBySns result {}"
,
result
);
GetEShieldTrackInfoListResp
resp
=
JSONObject
.
parseObject
(
result
,
GetEShieldTrackInfoListResp
.
class
);
if
(!
"0"
.
equals
(
resp
.
getReturnCode
()))
{
log
.
info
(
"remote getInfoListBySns fail! returnCode:"
+
resp
.
getReturnCode
()
+
"; message:"
+
resp
.
getMessage
());
return
null
;
}
return
resp
.
getData
();
}
/**
* 调用e盾标准接口获取设备型号
*
* @param sn 设备号
...
...
src/test/java/cn/com/glsx/devicemonitor/modules/service/impl/DeviceServiceImplTest.java
0 → 100644
View file @
cb94065
package
cn
.
com
.
glsx
.
devicemonitor
.
modules
.
service
.
impl
;
import
cn.com.glsx.Application
;
import
cn.com.glsx.devicemonitor.modules.entity.Device
;
import
cn.com.glsx.devicemonitor.modules.service.DeviceService
;
import
cn.hutool.json.JSONUtil
;
import
com.google.common.collect.Lists
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.util.List
;
@Slf4j
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
Application
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
public
class
DeviceServiceImplTest
{
@Autowired
private
DeviceService
deviceService
;
@Test
public
void
getDeviceListOrRemote
()
{
List
<
Device
>
deviceListOrRemote
=
deviceService
.
getDeviceListOrRemote
(
Lists
.
newArrayList
(
"8130902004"
));
System
.
out
.
println
(
"deviceListOrRemote = "
+
JSONUtil
.
toJsonStr
(
deviceListOrRemote
));
}
}
\ No newline at end of file
src/test/java/cn/com/glsx/devicemonitor/modules/service/impl/EShieldServiceTest.java
View file @
cb94065
...
...
@@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
java.util.List
;
@Slf4j
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
Application
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
...
...
@@ -22,4 +24,10 @@ class EShieldServiceTest {
TrackInfoDTO
deviceBySn
=
eShieldService
.
getDeviceBySn
(
"45106800001"
);
System
.
out
.
println
(
"deviceBySn = "
+
JSONUtil
.
toJsonStr
(
deviceBySn
));
}
@Test
public
void
TestGetDeviceListBySns
()
{
List
<
TrackInfoDTO
>
deviceBySnList
=
eShieldService
.
getDeviceListBySns
(
"45106800001,8130902003"
);
System
.
out
.
println
(
"deviceBySn = "
+
JSONUtil
.
toJsonStr
(
deviceBySnList
));
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
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 post a comment