Commit 454f3d96 by huangjy

fix:修改设备属性问题

parent bfc6e8d0
......@@ -10,6 +10,7 @@ import com.makeit.common.response.ApiResponseUtils;
import com.makeit.dto.platform.device.PlatDeviceQueryDTO;
import com.makeit.dto.wechat.device.PlatDeviceAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceEditWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceNetAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceSetupDTO;
import com.makeit.entity.platform.auth.PlatOrg;
import com.makeit.entity.platform.auth.PlatRole;
......@@ -106,5 +107,12 @@ public class PlatDeviceWechatController {
return ApiResponseUtils.success();
}
@ApiOperation("编辑设备网络属性")
@PostMapping("editDeviceNetInfo")
public ApiResponseEntity<?> editDeviceNetInfo(@RequestBody PlatDeviceNetAttrWechatDTO dto) {
platDeviceService.editDeviceNetInfo(dto);
return ApiResponseUtils.success();
}
}
......@@ -25,8 +25,4 @@ public class PlatDeviceAttrWechatDTO extends PlatDeviceBaseAttrDTO {
@ApiModelProperty(value = "读取硬件的系统参数,主要读取服务信息和雷达信息,多个逗号隔开")
private String readProperties;
//private String usrServerInfo;
@ApiModelProperty(value = "服务信息 {\"password\":\"1234576\",\"secureKey\":\"kuangshi\",\"secureId\":\"kuangshi\",\"addr\":\"mqtt://121.1.1.2\",\"username\":\"kuangshi\",\"port\":1883} ")
private UserServerInfo usrServerInfo;
}
......@@ -27,15 +27,5 @@ public class PlatDeviceEditWechatDTO extends BaseTenantDTO {
@ApiModelProperty(value = "设备名称")
private String name;
@ApiModelProperty(value = "设备安装方式")
private String installation;
@ApiModelProperty(value = "开关指示灯")
private String indicatorLight;
@ApiModelProperty(value = "设备属性json")
private String attribute;
}
package com.makeit.dto.wechat.device;
import com.makeit.dto.platform.device.PlatDeviceBaseAttrDTO;
import com.makeit.module.iot.dto.UserServerInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* <p>
* 设备
* </p>
*
* @author eugene young
* @since 2023-09-05
*/
@Data
@ApiModel(value = "PlatDeviceNetAttrWechatDTO", description = "设备网络信息")
public class PlatDeviceNetAttrWechatDTO{
@ApiModelProperty(value = "设备id")
private String deviceId;
//private String usrServerInfo;
@ApiModelProperty(value = "服务信息 {\"password\":\"1234576\",\"secureKey\":\"kuangshi\",\"secureId\":\"kuangshi\",\"addr\":\"mqtt://121.1.1.2\",\"username\":\"kuangshi\",\"port\":1883} ")
private UserServerInfo usrServerInfo;
}
......@@ -12,6 +12,7 @@ import com.makeit.dto.platform.device.PlatDeviceQueryDTO;
import com.makeit.dto.saas.device.PlatDeviceEditSaasDTO;
import com.makeit.dto.wechat.device.PlatDeviceAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceEditWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceNetAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceSetupDTO;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.module.iot.vo.DeviceProperties;
......@@ -75,4 +76,6 @@ public interface PlatDeviceService extends IService<PlatDevice> {
void batchEditProperties(PlatDeviceAttrDTO dto);
boolean updateDeviceStatus(String messageType, String deviceId, String iot_tenantId);
void editDeviceNetInfo(PlatDeviceNetAttrWechatDTO dto);
}
......@@ -19,6 +19,7 @@ import com.makeit.dto.platform.device.*;
import com.makeit.dto.saas.device.PlatDeviceEditSaasDTO;
import com.makeit.dto.wechat.device.PlatDeviceAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceEditWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceNetAttrWechatDTO;
import com.makeit.dto.wechat.device.PlatDeviceSetupDTO;
import com.makeit.entity.platform.auth.PlatOrg;
import com.makeit.entity.platform.auth.PlatRole;
......@@ -220,7 +221,7 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
// 写入设备
PlatDeviceAttrWechatDTO platDeviceAttrWechatDTO = JSON.parseObject(dto.getAttribute(), PlatDeviceAttrWechatDTO.class);
platDeviceAttrWechatDTO.setDeviceId(db.getId());
platDeviceAttrWechatDTO.setDeviceId(db.getOriDeviceId());
editDeviceProperties(platDeviceAttrWechatDTO);
}
......@@ -316,40 +317,10 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
@Transactional(rollbackFor = Exception.class)
public void wechatEdit(PlatDeviceEditWechatDTO dto) {
PlatDevice db = getById(dto.getId());
String id = db.getId();
PlatDeviceOther other = platDeviceOtherService.getOne(new QueryWrapper<PlatDeviceOther>().lambda()
.eq(PlatDeviceOther::getDeviceId, db.getId()));
if (other == null) {
other = new PlatDeviceOther();
}
String otherId = other.getId();
BeanUtils.copyProperties(dto, db);
db.setId(id);
BeanUtils.copyProperties(dto, other);
other.setId(otherId);
other.setDeviceId(id);
other.setOriDeviceId(db.getOriDeviceId());
db.setName(dto.getName());
updateById(db);
deviceCacheUtil.put(db);
platDeviceOtherService.saveOrUpdate(other);
if (StringUtils.isNotEmpty(other.getAttribute())) {
PlatDeviceAttrWechatDTO deviceAttrWechatDTO = JsonUtil.toObj(other.getAttribute(), PlatDeviceAttrWechatDTO.class);
if (deviceAttrWechatDTO != null) {
deviceAttrWechatDTO.setDeviceId(db.getId());
//更新区域设置设备安装方式
editDeviceProperties(deviceAttrWechatDTO);
}
}
}
@Override
......@@ -394,7 +365,7 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
if (StringUtils.isNotEmpty(dto.getAttribute())) {
// 写入设备
PlatDeviceAttrWechatDTO platDeviceAttrWechatDTO = JSON.parseObject(dto.getAttribute(), PlatDeviceAttrWechatDTO.class);
platDeviceAttrWechatDTO.setDeviceId(db.getId());
platDeviceAttrWechatDTO.setDeviceId(db.getOriDeviceId());
editDeviceProperties(platDeviceAttrWechatDTO);
}
}
......@@ -437,13 +408,33 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
log.info("写入设备属性请求参数:{}",JSON.toJSONString(dto));
String deviceId = dto.getDeviceId();
Map<String, Object> map = getAttrMap(dto);
UserServerInfo usrServerInfo = dto.getUsrServerInfo();
boolean usrServerInfoFlag = usrServerInfo != null && (StringUtils.isNotEmpty(usrServerInfo.getUsername()) || StringUtils.isNotEmpty(usrServerInfo.getPassword())
|| StringUtils.isNotEmpty(usrServerInfo.getAddr()) || usrServerInfo.getPort() != null ||
StringUtils.isNotEmpty(usrServerInfo.getSecureId()) || StringUtils.isNotEmpty(usrServerInfo.getSecureKey()));
if (usrServerInfoFlag) {
map.put("usrServerInfo",JSON.toJSONString(usrServerInfo));
String result = devicePropertiesOperateService.deviceWriteAttr(deviceId, map);
if (StringUtils.isNotEmpty(result)) {
throw new RuntimeException("设备写入失败:" + result);
}
PlatDeviceOther other = platDeviceOtherService.getOne(new QueryWrapper<PlatDeviceOther>().lambda()
.eq(PlatDeviceOther::getOriDeviceId, deviceId)
.last("limit 1"));
if (other != null) {
other.setAttribute(JSON.toJSONString(map));
platDeviceOtherService.updateById(other);
}
if (dto.getRadarMount() != null) {
List<PlatDevice> platDeviceList = list(new QueryWrapper<PlatDevice>().lambda().eq(PlatDevice::getOriDeviceId, deviceId));
//更新区域设置设备安装方式
platRegionSettingService.update(new UpdateWrapper<PlatRegionSetting>().lambda()
.set(PlatRegionSetting::getInstallType, dto.getRadarMount())
.in(CollectionUtils.isNotEmpty(platDeviceList),PlatRegionSetting::getDeviceId, StreamUtil.map(platDeviceList,BaseEntity::getId)));
}
}
@Override
public void editDeviceNetInfo(PlatDeviceNetAttrWechatDTO dto) {
String deviceId = dto.getDeviceId();
UserServerInfo usrServerInfo = dto.getUsrServerInfo();
log.info("写入设备属性请求参数:{}",JSON.toJSONString(dto));
Map<String, Object> map = Maps.newHashMap();
map.put("usrServerInfo",JSON.toJSONString(usrServerInfo));
String result = devicePropertiesOperateService.deviceWriteAttr(deviceId, map);
if (StringUtils.isNotEmpty(result)) {
throw new RuntimeException("设备写入失败:" + result);
......@@ -451,25 +442,15 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
PlatDeviceOther other = platDeviceOtherService.getOne(new QueryWrapper<PlatDeviceOther>().lambda()
.eq(PlatDeviceOther::getOriDeviceId, deviceId)
.last("limit 1"));
if (other != null && usrServerInfoFlag) {
if (other != null) {
other.setProtocolAccount(usrServerInfo.getUsername());
other.setProtocolPassword(usrServerInfo.getPassword());
other.setProtocolAddress(usrServerInfo.getAddr());
other.setProtocolPort(usrServerInfo.getPort() == null ? "" : usrServerInfo.getPort().toString());
other.setSecureId(usrServerInfo.getSecureId());
other.setSecureKey(usrServerInfo.getSecureKey());
}
if (other != null && !map.isEmpty() && !usrServerInfoFlag) {
other.setAttribute(JSON.toJSONString(map));
platDeviceOtherService.updateById(other);
}
if (dto.getRadarMount() != null) {
PlatDevice platDevice = getOne(new QueryWrapper<PlatDevice>().lambda().eq(PlatDevice::getOriDeviceId, deviceId));
//更新区域设置设备安装方式
platRegionSettingService.update(new UpdateWrapper<PlatRegionSetting>().lambda()
.set(PlatRegionSetting::getInstallType, dto.getRadarMount())
.eq(PlatRegionSetting::getDeviceId, platDevice.getId()));
}
}
private static Map<String, Object> getAttrMap(PlatDeviceBaseAttrDTO dto) {
......@@ -586,6 +567,9 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
.in(PlatRoomBedDevice::getDeviceId,deviceIdList)
.eq(BaseBusEntity::getTenantId, platTenant.getId()));
}
if (CollectionUtils.isEmpty(iotDeviceIdSet)) {
continue;
}
LambdaQueryWrapper<PlatDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<PlatDevice>().eq(BaseBusEntity::getTenantId, platTenant.getId())
.in(PlatDevice::getOriDeviceId, iotDeviceIdSet);
......@@ -594,14 +578,33 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
List<PlatDevice> deviceList = list(deviceLambdaQueryWrapper);
//更新平台设备
Collection<PlatDevice> platDevices = convertToPlatDevice(iotDeviceList, deviceList, platTenant.getId(), dicNameIdMap);
saveOrUpdateBatch(platDevices);
saveDeviceOtherInfo(platDevices);
deviceCacheUtil.putAll(platDevices);
}
log.info("结束执行同步设备信息接口");
}
private void saveDeviceOtherInfo(Collection<PlatDevice> platDevices) {
List<String> deviceIdList = platDevices.stream().map(BaseEntity::getId).collect(Collectors.toList());
List<PlatDeviceOther> platDeviceOtherList = platDeviceOtherService.list(new QueryWrapper<PlatDeviceOther>().lambda().in(PlatDeviceOther::getDeviceId, deviceIdList));
Map<String, PlatDeviceOther> deviceOtherMap = StreamUtil.toMap(platDeviceOtherList, PlatDeviceOther::getDeviceId);
for (PlatDevice platDevice : platDevices) {
PlatDeviceOther platDeviceOther = deviceOtherMap.get(platDevice.getId());
if (platDeviceOther == null) {
platDeviceOther = new PlatDeviceOther();
platDeviceOther.setDeviceId(platDevice.getId());
platDeviceOther.setOriDeviceId(platDevice.getOriDeviceId());
platDeviceOther.setTenantId(platDevice.getTenantId());
platDeviceOtherService.save(platDeviceOther);
}
}
}
private Collection<PlatDevice> convertToPlatDevice(List<DeviceInstanceEntity> iotDeviceList, List<PlatDevice> deviceList, String tenantId, Map<String, String> dicNameIdMap) {
Map<String, PlatDevice> deviceMap = deviceList.stream().collect(Collectors.toMap(PlatDevice::getOriDeviceId, v -> v, (a, b) -> a));
iotDeviceList.forEach(iotDevice -> {
......
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