Commit cb940650 by 杨林龙

增加设备批量同步接口

1 parent f7f0628b
...@@ -50,7 +50,7 @@ public class FeignDeviceController implements DeviceFeignService { ...@@ -50,7 +50,7 @@ public class FeignDeviceController implements DeviceFeignService {
} }
@Override @Override
@GetMapping({"/getOrStoreDeviceBySn"}) @GetMapping(value = "/getOrStoreDeviceBySn")
public R<DeviceDTO> getOrStoreDeviceBySn(@RequestParam(value = "sn") String sn, @RequestParam(value = "storeIfNotExists") Boolean storeIfNotExists) { public R<DeviceDTO> getOrStoreDeviceBySn(@RequestParam(value = "sn") String sn, @RequestParam(value = "storeIfNotExists") Boolean storeIfNotExists) {
if (storeIfNotExists == null) { if (storeIfNotExists == null) {
storeIfNotExists = false; storeIfNotExists = false;
...@@ -59,6 +59,13 @@ public class FeignDeviceController implements DeviceFeignService { ...@@ -59,6 +59,13 @@ public class FeignDeviceController implements DeviceFeignService {
return R.ok().data(deviceDTO); 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);
}
/** /**
* 获取设备的状态 * 获取设备的状态
* *
......
...@@ -42,4 +42,8 @@ public class TrackInfoDTO { ...@@ -42,4 +42,8 @@ public class TrackInfoDTO {
private String partner; private String partner;
/**
* 设备类型名称
*/
private String deviceClassifyName;
} }
\ No newline at end of file \ No newline at end of file
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;
}
...@@ -49,6 +49,15 @@ public interface DeviceService { ...@@ -49,6 +49,15 @@ public interface DeviceService {
*/ */
DeviceDTO getDeviceDTOBySn(String sn, Boolean storeIfNotExists); DeviceDTO getDeviceDTOBySn(String sn, Boolean storeIfNotExists);
/**
* 根据设备号批量查询设备信息,不存在就同步
*
* @param sns 设备号集合
* @return
*/
List<DeviceDTO> getOrStoreDeviceBySns(List<String> sns);
/** /**
* 获取设备的在线状态 * 获取设备的在线状态
* *
...@@ -66,6 +75,14 @@ public interface DeviceService { ...@@ -66,6 +75,14 @@ public interface DeviceService {
*/ */
Device getDeviceOrRemote(String sn, Boolean storeIfNotExists); Device getDeviceOrRemote(String sn, Boolean storeIfNotExists);
/**
* 批量查询设备(本地库没有,查询旧E盾)
*
* @param sns 设备号集合
* @return
*/
List<Device> getDeviceListOrRemote(List<String> sns);
/** /**
* 获取设备上点总次数 * 获取设备上点总次数
...@@ -100,5 +117,4 @@ public interface DeviceService { ...@@ -100,5 +117,4 @@ public interface DeviceService {
* @return * @return
*/ */
Map<String, DeviceTypeRelationEnum> getDeviceTypes(List<String> snList); Map<String, DeviceTypeRelationEnum> getDeviceTypes(List<String> snList);
} }
...@@ -15,8 +15,10 @@ import cn.com.glsx.devicemonitor.modules.mapper.DeviceMapper; ...@@ -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.model.TrackInfoDTO;
import cn.com.glsx.devicemonitor.modules.service.DeviceService; import cn.com.glsx.devicemonitor.modules.service.DeviceService;
import cn.com.glsx.devicemonitor.modules.utils.DeviceUtils; import cn.com.glsx.devicemonitor.modules.utils.DeviceUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.common.utils.CollectionUtils; 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.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -48,6 +50,7 @@ public class DeviceServiceImpl implements DeviceService { ...@@ -48,6 +50,7 @@ public class DeviceServiceImpl implements DeviceService {
return deviceMapper.selectBySn(sn); return deviceMapper.selectBySn(sn);
} }
@Override
public int batchAdd(List<DeviceBO> deviceBOList) { public int batchAdd(List<DeviceBO> deviceBOList) {
log.info("设备数据:{}", JSONUtil.toJsonStr(deviceBOList)); log.info("设备数据:{}", JSONUtil.toJsonStr(deviceBOList));
if (CollectionUtils.isEmpty(deviceBOList)) { if (CollectionUtils.isEmpty(deviceBOList)) {
...@@ -83,6 +86,19 @@ public class DeviceServiceImpl implements DeviceService { ...@@ -83,6 +86,19 @@ public class DeviceServiceImpl implements DeviceService {
} }
@Override @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) { public DeviceStatusModel getDeviceStatusBySn(String sn, Long offlineTime) {
Device device = getDeviceOrRemote(sn, false); Device device = getDeviceOrRemote(sn, false);
if (device == null) { if (device == null) {
...@@ -178,12 +194,44 @@ public class DeviceServiceImpl implements DeviceService { ...@@ -178,12 +194,44 @@ public class DeviceServiceImpl implements DeviceService {
return device; 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) { public Device getRemoteDevice(String sn) {
TrackInfoDTO trackInfoDTO = eShieldService.getDeviceBySn(sn); TrackInfoDTO trackInfoDTO = eShieldService.getDeviceBySn(sn);
return convertDevice(trackInfoDTO, 0L); 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盾过来的设备 * 保存从E盾过来的设备
* *
* @param trackInfo * @param trackInfo
...@@ -204,6 +252,32 @@ public class DeviceServiceImpl implements DeviceService { ...@@ -204,6 +252,32 @@ public class DeviceServiceImpl implements DeviceService {
return device; 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 @Override
public Integer getDevicesTotalCount(String sn) { public Integer getDevicesTotalCount(String sn) {
DevicesTotalCountResHolder res = new DevicesTotalCountResHolder(); DevicesTotalCountResHolder res = new DevicesTotalCountResHolder();
......
...@@ -3,6 +3,7 @@ package cn.com.glsx.devicemonitor.modules.service.impl; ...@@ -3,6 +3,7 @@ package cn.com.glsx.devicemonitor.modules.service.impl;
import cn.com.glsx.devicemonitor.config.EShieldConfig; import cn.com.glsx.devicemonitor.config.EShieldConfig;
import cn.com.glsx.devicemonitor.modules.model.TrackInfoDTO; 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.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.model.response.GetEShieldTrackInfoResp;
import cn.com.glsx.devicemonitor.modules.utils.GopUtil; import cn.com.glsx.devicemonitor.modules.utils.GopUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
...@@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
@Slf4j @Slf4j
...@@ -44,6 +46,29 @@ public class EShieldService { ...@@ -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盾标准接口获取设备型号 * 调用e盾标准接口获取设备型号
* *
* @param sn 设备号 * @param sn 设备号
......
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 \ No newline at end of file
...@@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@Slf4j @Slf4j
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
...@@ -22,4 +24,10 @@ class EShieldServiceTest { ...@@ -22,4 +24,10 @@ class EShieldServiceTest {
TrackInfoDTO deviceBySn = eShieldService.getDeviceBySn("45106800001"); TrackInfoDTO deviceBySn = eShieldService.getDeviceBySn("45106800001");
System.out.println("deviceBySn = " + JSONUtil.toJsonStr(deviceBySn)); 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 \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!