增加设备分组功能

V0.5.x
xiwa 2022-06-17 05:31:26 +08:00
parent 7bcd19fa91
commit 744a7cae19
6 changed files with 300 additions and 4 deletions

View File

@ -140,4 +140,59 @@ public class DeviceDao {
return getDeviceStatsByCategory(null);
}
/**
* id
*/
public List<DeviceInfo> findByGroupId(String groupId) {
Query query = Query.query(new Criteria().and("group." + groupId).exists(true));
return mongoTemplate.find(query, DeviceInfo.class);
}
/**
* id
*/
public long countByGroupId(String groupId) {
Query query = Query.query(new Criteria().and("group." + groupId).exists(true));
return mongoTemplate.count(query, DeviceInfo.class);
}
/**
* id
*/
public void updateGroupByDeviceId(String deviceId, DeviceInfo.Group group) {
Query query = Query.query(new Criteria().and("deviceId").is(deviceId));
Update update = new Update();
update.set("group." + group.getId(), group);
mongoTemplate.updateFirst(query, update, DeviceInfo.class);
}
/**
* id
*/
public void updateGroup(String groupId, DeviceInfo.Group group) {
Query query = Query.query(new Criteria().and("group." + groupId).exists(true));
Update update = new Update();
update.set("group." + group.getId(), group);
mongoTemplate.updateMulti(query, update, DeviceInfo.class);
}
/**
*
*/
public void removeGroup(String deviceId, String groupId) {
Query query = Query.query(new Criteria().and("deviceId").is(deviceId).and("group." + groupId).exists(true));
Update update = new Update();
update.unset("group." + groupId);
mongoTemplate.updateFirst(query, update, DeviceInfo.class);
}
/**
*
*/
public void removeGroup(String groupId) {
Query query = Query.query(new Criteria().and("group." + groupId).exists(true));
Update update = new Update();
update.unset("group." + groupId);
mongoTemplate.updateMulti(query, update, DeviceInfo.class);
}
}

View File

@ -0,0 +1,14 @@
package cc.iotkit.dao;
import cc.iotkit.model.device.DeviceGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DeviceGroupRepository extends MongoRepository<DeviceGroup, String> {
Page<DeviceGroup> findByNameLike(String name, Pageable pageable);
}

View File

@ -3,6 +3,7 @@ package cc.iotkit.manager.controller;
import cc.iotkit.common.Constants;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.utils.DeviceUtil;
import cc.iotkit.common.utils.ReflectUtil;
import cc.iotkit.common.utils.UniqueIdUtil;
import cc.iotkit.comps.service.DeviceBehaviourService;
import cc.iotkit.dao.*;
@ -10,6 +11,7 @@ import cc.iotkit.manager.model.query.DeviceQuery;
import cc.iotkit.manager.service.DataOwnerService;
import cc.iotkit.manager.service.DeferredDataConsumer;
import cc.iotkit.manager.service.DeviceService;
import cc.iotkit.model.device.DeviceGroup;
import cc.iotkit.utils.AuthUtil;
import cc.iotkit.model.InvokeResult;
import cc.iotkit.model.Paging;
@ -23,6 +25,9 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
@ -30,6 +35,7 @@ import org.springframework.web.context.request.async.DeferredResult;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
@Slf4j
@RestController
@ -58,6 +64,10 @@ public class DeviceController {
private DeviceBehaviourService behaviourService;
@Autowired
DeferredDataConsumer deferredDataConsumer;
@Autowired
private DeviceGroupRepository deviceGroupRepository;
@Autowired
private DeviceCache deviceCache;
@PostMapping(Constants.API_DEVICE.INVOKE_SERVICE)
public InvokeResult invokeService(@PathVariable("deviceId") String deviceId,
@ -96,9 +106,18 @@ public class DeviceController {
if (StringUtils.isNotBlank(pk)) {
condition.and("productKey").is(pk);
}
String dn = query.getDeviceName();
if (StringUtils.isNotBlank(dn)) {
condition.and("deviceName").regex(".*" + dn + ".*");
//关键字查询
String keyword = query.getKeyword();
if (StringUtils.isNotBlank(keyword)) {
Pattern pattern = Pattern.compile("^.*" + keyword + ".*$", Pattern.CASE_INSENSITIVE);
condition.orOperator(
Criteria.where("deviceName").regex(pattern),
Criteria.where("deviceId").regex(pattern)
);
}
String group = query.getGroup();
if (StringUtils.isNotBlank(group)) {
condition.and("group." + group).exists(true);
}
String state = query.getState();
if (StringUtils.isNotBlank(state)) {
@ -233,4 +252,145 @@ public class DeviceController {
return deferredDataConsumer.newConsumer(uid + clientId,
Constants.HTTP_CONSUMER_DEVICE_INFO_TOPIC + deviceId);
}
/**
*
*/
@PostMapping("/groups/{size}/{page}")
public Paging<DeviceGroup> getDevices(
@PathVariable("size") int size,
@PathVariable("page") int page,
String name
) {
Page<DeviceGroup> groupPage = deviceGroupRepository.findByNameLike(name,
PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt"))));
return new Paging<>(groupPage.getTotalElements(), groupPage.getContent());
}
/**
*
*/
@PostMapping("/group/add")
public void addGroup(DeviceGroup group) {
group.setUid(AuthUtil.getUserId());
if (deviceGroupRepository.existsById(group.getId())) {
throw new BizException("group id already exists");
}
deviceGroupRepository.save(group);
}
/**
*
*/
@PostMapping("/group/save")
public void saveGroup(DeviceGroup group) {
Optional<DeviceGroup> optGroup = deviceGroupRepository.findById(group.getId());
if (optGroup.isEmpty()) {
throw new BizException("group id does not exists");
}
DeviceGroup dbGroup = optGroup.get();
dataOwnerService.checkOwner(dbGroup);
ReflectUtil.copyNoNulls(group, dbGroup);
deviceGroupRepository.save(dbGroup);
//更新设备中的组信息
deviceDao.updateGroup(dbGroup.getId(), new DeviceInfo.Group(dbGroup.getId(), dbGroup.getName()));
}
/**
*
*/
@DeleteMapping("/group/delete/{id}")
public void deleteGroup(@PathVariable("id") String id) {
Optional<DeviceGroup> optGroup = deviceGroupRepository.findById(id);
if (optGroup.isEmpty()) {
throw new BizException("device group does not exist");
}
DeviceGroup group = optGroup.get();
dataOwnerService.checkOwner(group);
//删除分组
deviceGroupRepository.deleteById(id);
//移除设备信息中的分组
deviceDao.removeGroup(group.getId());
}
/**
*
*/
@PostMapping("/group/clear/{id}")
public void clearGroup(@PathVariable("id") String id) {
Optional<DeviceGroup> optGroup = deviceGroupRepository.findById(id);
if (optGroup.isEmpty()) {
throw new BizException("device group does not exist");
}
DeviceGroup group = optGroup.get();
dataOwnerService.checkOwner(group);
//设备数量清零
group.setDeviceQty(0);
deviceGroupRepository.save(group);
//移除设备信息中的分组
deviceDao.removeGroup(group.getId());
}
/**
*
*/
@PostMapping("/group/addDevices/{group}")
public void addToGroup(@PathVariable("group") String group, @RequestBody List<String> devices) {
Optional<DeviceGroup> optGroup = deviceGroupRepository.findById(group);
if (optGroup.isEmpty()) {
throw new BizException("device group does not exists");
}
DeviceGroup deviceGroup = optGroup.get();
dataOwnerService.checkOwner(deviceGroup);
for (String device : devices) {
DeviceInfo deviceInfo = deviceCache.get(device);
if (deviceInfo == null) {
continue;
}
dataOwnerService.checkOwner(deviceInfo);
//更新设备所在组
deviceDao.updateGroupByDeviceId(device, new DeviceInfo.Group(group, deviceGroup.getName()));
}
//统计组下设备数量
long qty = deviceDao.countByGroupId(group);
//更新组信息
deviceGroup.setDeviceQty((int) qty);
deviceGroupRepository.save(deviceGroup);
}
/**
*
*/
@PostMapping("/group/removeDevices/{group}")
public void removeDevices(@PathVariable("group") String group, @RequestBody List<String> devices) {
Optional<DeviceGroup> optGroup = deviceGroupRepository.findById(group);
if (optGroup.isEmpty()) {
throw new BizException("device group does not exists");
}
DeviceGroup deviceGroup = optGroup.get();
dataOwnerService.checkOwner(deviceGroup);
for (String device : devices) {
DeviceInfo deviceInfo = deviceCache.get(device);
if (deviceInfo == null) {
continue;
}
dataOwnerService.checkOwner(deviceInfo);
//删除设备所在组
deviceDao.removeGroup(device, group);
}
//统计组下设备数量
long qty = deviceDao.countByGroupId(group);
//更新组信息
deviceGroup.setDeviceQty((int) qty);
deviceGroupRepository.save(deviceGroup);
}
}

View File

@ -7,7 +7,9 @@ public class DeviceQuery {
private String productKey;
private String deviceName;
private String keyword;
private String group;
private String state;

View File

@ -0,0 +1,46 @@
package cc.iotkit.model.device;
import cc.iotkit.model.Owned;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
*
*/
@Data
@Document
public class DeviceGroup implements Owned {
/**
* id
*/
@Id
private String id;
/**
*
*/
private String name;
/**
*
*/
private String uid;
/**
*
*/
private String remark;
/**
*
*/
private int deviceQty;
/**
*
*/
private long createAt;
}

View File

@ -54,8 +54,14 @@ public class DeviceInfo implements Owned {
*/
private List<String> subUid = new ArrayList<>();
/**
* 线
*/
private State state = new State();
/**
*
*/
private Map<String, Object> property = new HashMap<>();
/**
@ -63,6 +69,11 @@ public class DeviceInfo implements Owned {
*/
private Map<String, Tag> tag = new HashMap<>();
/**
*
*/
private Map<String, Group> group = new HashMap<>();
private Long createAt;
@Data
@ -87,4 +98,12 @@ public class DeviceInfo implements Owned {
private Object value;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Group {
private String id;
private String name;
}
}