Commit a821879d by 罗志长

fix: iot属性同步

parent 6d3dacde
...@@ -14,6 +14,8 @@ public class DeviceInfo { ...@@ -14,6 +14,8 @@ public class DeviceInfo {
private Long timestamp; private Long timestamp;
private String messageId;
private String messageType; private String messageType;
private HeaderInfo headers; private HeaderInfo headers;
......
package com.makeit.service.platform.device; package com.makeit.service.platform.device;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.makeit.common.dto.BaseIdDTO; import com.makeit.common.dto.BaseIdDTO;
...@@ -93,5 +94,5 @@ public interface PlatDeviceService extends IService<PlatDevice> { ...@@ -93,5 +94,5 @@ public interface PlatDeviceService extends IService<PlatDevice> {
void devicePushLog(MultipartFile multipartFile, String deviceId) throws IOException; void devicePushLog(MultipartFile multipartFile, String deviceId) throws IOException;
void syncIotProperties(String deviceId); void syncIotProperties(String deviceId, JSONObject iotProperties);
} }
...@@ -382,7 +382,6 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev ...@@ -382,7 +382,6 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
for (PlatDeviceOther platDeviceOther : platDeviceOtherList) { for (PlatDeviceOther platDeviceOther : platDeviceOtherList) {
BeanUtils.copyProperties(dto, platDeviceOther,"id"); BeanUtils.copyProperties(dto, platDeviceOther,"id");
platDeviceOther.setDeviceId(id); platDeviceOther.setDeviceId(id);
platDeviceOther.setAttribute(JSON.toJSONString(getDeviceBaseAttrDTO(db)));
platDeviceOther.setOriDeviceId(db.getOriDeviceId()); platDeviceOther.setOriDeviceId(db.getOriDeviceId());
platDeviceOtherService.saveOrUpdate(platDeviceOther); platDeviceOtherService.saveOrUpdate(platDeviceOther);
} }
...@@ -766,6 +765,7 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev ...@@ -766,6 +765,7 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
platDeviceOther = new PlatDeviceOther(); platDeviceOther = new PlatDeviceOther();
platDeviceOther.setDeviceId(platDevice.getId()); platDeviceOther.setDeviceId(platDevice.getId());
platDeviceOther.setOriDeviceId(platDevice.getOriDeviceId()); platDeviceOther.setOriDeviceId(platDevice.getOriDeviceId());
platDeviceOther.setAttribute(JSON.toJSONString(getDeviceBaseAttrDTO(platDevice)));
platDeviceOther.setTenantId(platDevice.getTenantId()); platDeviceOther.setTenantId(platDevice.getTenantId());
platDeviceOtherService.save(platDeviceOther); platDeviceOtherService.save(platDeviceOther);
} }
...@@ -942,23 +942,53 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev ...@@ -942,23 +942,53 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
@Override @Override
@TenantIdIgnore @TenantIdIgnore
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void syncIotProperties(String deviceId) { public void syncIotProperties(String deviceId, JSONObject iotProperties) {
PlatDevice device = getOne(new QueryWrapper<PlatDevice>().lambda() PlatDeviceBaseAttrDTO iotAttr = iotProperties.toJavaObject(PlatDeviceBaseAttrDTO.class);
.eq(PlatDevice::getOriDeviceId, deviceId)); List<PlatDeviceOther> deviceOthers = platDeviceOtherService.list(new QueryWrapper<PlatDeviceOther>().lambda()
if (device == null) {
log.warn("iot属性同步,设备不存在,设备id:{}", deviceId);
return;
}
PlatDeviceBaseAttrDTO platDeviceBaseAttrDTO = getDeviceBaseAttrDTO(device);
String attr = JSON.toJSONString(platDeviceBaseAttrDTO);
platDeviceOtherService.update(new UpdateWrapper<PlatDeviceOther>().lambda()
.set(PlatDeviceOther::getAttribute, attr)
.eq(PlatDeviceOther::getOriDeviceId, deviceId)); .eq(PlatDeviceOther::getOriDeviceId, deviceId));
if (platDeviceBaseAttrDTO.getRadarMount() != null) { Integer radarMount = null;
for (PlatDeviceOther deviceOther : deviceOthers) {
PlatDeviceBaseAttrDTO platDeviceBaseAttrDTO = JSON.parseObject(deviceOther.getAttribute(), PlatDeviceBaseAttrDTO.class);
if (platDeviceBaseAttrDTO == null) {
platDeviceBaseAttrDTO = new PlatDeviceBaseAttrDTO();
}
if (iotAttr.getRadarMode() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarMode(), iotAttr.getRadarMode())) {
platDeviceBaseAttrDTO.setRadarMode(iotAttr.getRadarMode());
}
if (iotAttr.getRadarMount() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarMount(), iotAttr.getRadarMount())) {
platDeviceBaseAttrDTO.setRadarMount(iotAttr.getRadarMount());
radarMount = platDeviceBaseAttrDTO.getRadarMount();
}
if (iotAttr.getRadarHight() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarHight(), iotAttr.getRadarHight())) {
platDeviceBaseAttrDTO.setRadarHight(iotAttr.getRadarHight());
}
if (iotAttr.getRadarDelay() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarDelay(), iotAttr.getRadarDelay())) {
platDeviceBaseAttrDTO.setRadarDelay(iotAttr.getRadarDelay());
}
PlatDeviceBaseAttrDTO.DeviceAttrRange radarDistance = platDeviceBaseAttrDTO.getRadarDistance();
if (iotAttr.getRadarDistance() != null && !Objects.equals(radarDistance, iotAttr.getRadarDistance())) {
platDeviceBaseAttrDTO.setRadarDistance(iotAttr.getRadarDistance());
}
PlatDeviceBaseAttrDTO.DeviceAttrRange radarAngle = platDeviceBaseAttrDTO.getRadarAngle();
if (iotAttr.getRadarAngle() != null && !Objects.equals(radarAngle, iotAttr.getRadarAngle())) {
platDeviceBaseAttrDTO.setRadarAngle(iotAttr.getRadarAngle());
}
if (iotAttr.getRadarSence() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarSence(), iotAttr.getRadarSence())) {
platDeviceBaseAttrDTO.setRadarSence(iotAttr.getRadarSence());
}
if (iotAttr.getRadarSPL() != null && !Objects.equals(platDeviceBaseAttrDTO.getRadarSPL(), iotAttr.getRadarSPL())) {
platDeviceBaseAttrDTO.setRadarSPL(iotAttr.getRadarSPL());
}
String attr = JSON.toJSONString(platDeviceBaseAttrDTO);
platDeviceOtherService.update(new UpdateWrapper<PlatDeviceOther>().lambda()
.set(PlatDeviceOther::getAttribute, attr)
.eq(PlatDeviceOther::getId, deviceOther.getId()));
}
if (radarMount != null) {
List<PlatDevice> platDeviceList = list(new QueryWrapper<PlatDevice>().lambda().eq(PlatDevice::getOriDeviceId, deviceId)); List<PlatDevice> platDeviceList = list(new QueryWrapper<PlatDevice>().lambda().eq(PlatDevice::getOriDeviceId, deviceId));
//更新区域设置设备安装方式 //更新区域设置设备安装方式
platRegionSettingService.update(new UpdateWrapper<PlatRegionSetting>().lambda() platRegionSettingService.update(new UpdateWrapper<PlatRegionSetting>().lambda()
.set(PlatRegionSetting::getInstallType, platDeviceBaseAttrDTO.getRadarMount()) .set(PlatRegionSetting::getInstallType, radarMount)
.in(CollectionUtils.isNotEmpty(platDeviceList),PlatRegionSetting::getDeviceId, StreamUtil.map(platDeviceList,BaseEntity::getId))); .in(CollectionUtils.isNotEmpty(platDeviceList),PlatRegionSetting::getDeviceId, StreamUtil.map(platDeviceList,BaseEntity::getId)));
} }
} }
......
...@@ -125,26 +125,26 @@ public class PushCallback implements MqttCallback { ...@@ -125,26 +125,26 @@ public class PushCallback implements MqttCallback {
return; return;
} }
// 记录修改的属性 // 记录修改的属性
String key = DEVICE_WRITE_PROPERTY_KEY + device.getDeviceId(); String redisKey = DEVICE_WRITE_PROPERTY_KEY + device.getMessageId();
if (WRITE_PROPERTY.equals(device.getMessageType())) { if (WRITE_PROPERTY.equals(device.getMessageType())) {
JSONObject properties = device.getProperties(); JSONObject properties = device.getProperties();
String value = properties.keySet().stream().findFirst().orElse("value"); RedisUtil.set(redisKey, properties, 10, TimeUnit.SECONDS);
RedisUtil.set(key, value, 10, TimeUnit.SECONDS);
} }
// 收到修改属性回复 // 收到修改属性回复
if (WRITE_PROPERTY_REPLY.equals(device.getMessageType())) { if (WRITE_PROPERTY_REPLY.equals(device.getMessageType())) {
String value = RedisUtil.get(key); JSONObject properties = RedisUtil.get(redisKey);
if (StringUtils.isBlank(value)) { if (properties == null) {
return; return;
} }
JSONObject properties = device.getProperties(); String key = properties.keySet().stream().findFirst().orElse("key");
int success = properties.getIntValue(value); JSONObject replayProperties = device.getProperties();
if (success != 0) { String success = replayProperties.getString(key);
if (!"0".equals(success)) {
return; return;
} }
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
platDeviceService.syncIotProperties(device.getDeviceId()); platDeviceService.syncIotProperties(device.getDeviceId(), properties);
RedisUtil.delete(key); RedisUtil.delete(redisKey);
}); // 要加线程池 }); // 要加线程池
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment