|
|
@@ -21,6 +21,7 @@ import com.nb.bus.service.dto.DeviceQuery;
|
|
|
import com.nb.bus.service.dto.DeviceResult;
|
|
|
import com.nb.common.crud.BaseService;
|
|
|
import com.nb.common.exception.CustomException;
|
|
|
+import com.nb.common.util.ExceptionUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
@@ -48,9 +49,6 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
@Lazy
|
|
|
private DeviceRegistry deviceRegistry;
|
|
|
|
|
|
- @Autowired
|
|
|
- private BusDeviceMapper deviceMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
private AliyunIotSdk aliyunIotSdk;
|
|
|
|
|
|
@@ -142,14 +140,15 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
* 保存一个设备,系统中存在时就更新,不存在时则插入。
|
|
|
* @param entity
|
|
|
*/
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public boolean saveDevice(BusDeviceEntity entity) {
|
|
|
// 查询设备是否存在,无视逻辑删除
|
|
|
- BusDeviceEntity device = deviceMapper.selectOneByDeviceId(entity.getDeviceId());
|
|
|
+ BusDeviceEntity device = this.baseMapper.selectOneByDeviceId(entity.getDeviceId());
|
|
|
// 判断设备是否存在
|
|
|
boolean isExists = Objects.nonNull(device);
|
|
|
// 设备存在,且处于删除状态,首先去掉逻辑删除标志 ?? 逻辑删除后设备数据是否不再接收
|
|
|
if (isExists && device.getIsDelete() == 1){
|
|
|
- deviceMapper.notDelete(entity.getDeviceId());
|
|
|
+ this.baseMapper.notDelete(Collections.singleton(entity.getDeviceId()));
|
|
|
}
|
|
|
// 设备存在
|
|
|
if (isExists){
|
|
|
@@ -160,7 +159,7 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
try {
|
|
|
return this.save(entity);
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("新增设备失败,【{}】",ExceptionUtil.getExceptionMsg(e));
|
|
|
}
|
|
|
return false;
|
|
|
|
|
|
@@ -243,7 +242,7 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
* @param
|
|
|
* @return int
|
|
|
*/
|
|
|
- public void syncDeviceFromIot() throws ExecutionException, InterruptedException {
|
|
|
+ public void syncDeviceFromIot(){
|
|
|
log.info("从阿里云同步所有设备信息");
|
|
|
HashMap<String, BusDeviceEntity> deviceById = new HashMap<>();
|
|
|
ArrayList<BusDeviceEntity> devices = new ArrayList<>();
|
|
|
@@ -262,7 +261,12 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
Set<String> pollDeviceIds = devices.stream().filter(info -> StrUtil.isNotBlank(info.getDeviceId()))
|
|
|
.map(BusDeviceEntity::getDeviceId)
|
|
|
.collect(Collectors.toSet());
|
|
|
- List<BusDeviceEntity> existDevices = this.list(new QueryWrapper<BusDeviceEntity>().lambda().in(BusDeviceEntity::getDeviceId, pollDeviceIds));
|
|
|
+ List<BusDeviceEntity> existDevices=this.baseMapper.ignoreLogicAll(pollDeviceIds);
|
|
|
+ //将所有删除设备恢复
|
|
|
+ List<String> delDeviceIds = existDevices.stream().filter(device -> device.getIsDelete() == 1).map(BusDeviceEntity::getDeviceId).collect(Collectors.toList());
|
|
|
+ if (CollUtil.isNotEmpty(delDeviceIds)) {
|
|
|
+ this.baseMapper.notDelete(delDeviceIds);
|
|
|
+ }
|
|
|
if (CollUtil.size(pollDeviceIds) != CollUtil.size(existDevices)) {
|
|
|
//有新的设备产生
|
|
|
Set<String> existDeviceIds = existDevices.stream()
|
|
|
@@ -280,9 +284,9 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
|
|
|
})
|
|
|
.whenComplete((i,e)->{
|
|
|
if(e!=null){
|
|
|
- log.error("同步设备失败",e);
|
|
|
+ log.error("同步设备失败:【{}】", ExceptionUtil.getExceptionMsg(e));
|
|
|
}
|
|
|
- });
|
|
|
+ }).join();
|
|
|
}
|
|
|
}
|
|
|
|