数据接口重构

V0.5.x
xiwa 2022-07-14 19:45:27 +08:00
parent 413818248a
commit 61fa50b612
230 changed files with 3939 additions and 3135 deletions

BIN
data/iotkit.mv.db Normal file

Binary file not shown.

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iotkit-parent</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -10,7 +10,7 @@
package cc.iotkit.oauth.controller;
import cc.iotkit.common.utils.JsonUtil;
import cc.iotkit.dao.UserInfoRepository;
import cc.iotkit.data.IUserInfoData;
import cc.iotkit.model.UserInfo;
import cc.iotkit.oauth.service.TokenRequestHandler;
import cc.iotkit.utils.AuthUtil;
@ -33,7 +33,7 @@ import java.util.Map;
public class AuthServerController {
@Autowired
private UserInfoRepository userInfoRepository;
private IUserInfoData userInfoData;
/**
* OAuth
@ -56,7 +56,7 @@ public class AuthServerController {
// 登录处理函数
setDoLoginHandle((name, pwd) -> {
try {
UserInfo userInfo = userInfoRepository.findByUid(name);
UserInfo userInfo = userInfoData.findByUid(name);
if (userInfo != null) {
String secret = userInfo.getSecret();
if (AuthUtil.checkPwd(pwd, secret)) {

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iotkit-parent</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -94,7 +94,17 @@
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-device-dao</artifactId>
<artifactId>iot-data-service</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-temporal-service</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-data-cache</artifactId>
</dependency>
</dependencies>

View File

@ -16,7 +16,7 @@ import cc.iotkit.comp.CompConfig;
import cc.iotkit.comp.IComponent;
import cc.iotkit.comps.config.ComponentConfig;
import cc.iotkit.comps.service.DeviceBehaviourService;
import cc.iotkit.dao.ProtocolComponentRepository;
import cc.iotkit.data.IProtocolComponentData;
import cc.iotkit.model.protocol.ProtocolComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
@ -41,14 +41,14 @@ public class BizComponentManager {
@Autowired
private ComponentConfig componentConfig;
@Autowired
private ProtocolComponentRepository componentRepository;
private IProtocolComponentData protocolComponentData;
@Autowired
private DeviceBehaviourService deviceBehaviourService;
@PostConstruct
public void init() {
try {
List<ProtocolComponent> componentList = componentRepository
List<ProtocolComponent> componentList = protocolComponentData
.findByStateAndType(ProtocolComponent.STATE_RUNNING, ProtocolComponent.TYPE_BIZ);
for (ProtocolComponent component : componentList) {
register(component);

View File

@ -25,7 +25,7 @@ import cc.iotkit.converter.ScriptConverter;
import cc.iotkit.common.thing.ThingService;
import cc.iotkit.dao.DeviceCache;
import cc.iotkit.dao.ProductCache;
import cc.iotkit.dao.ProtocolComponentRepository;
import cc.iotkit.data.IProtocolComponentData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.model.product.Product;
@ -60,7 +60,7 @@ public class DeviceComponentManager {
@Autowired
private ComponentConfig componentConfig;
@Autowired
private ProtocolComponentRepository componentRepository;
private IProtocolComponentData protocolComponentData;
@Autowired
private DeviceCache deviceCache;
@Autowired
@ -71,7 +71,7 @@ public class DeviceComponentManager {
@PostConstruct
public void init() {
try {
List<ProtocolComponent> componentList = componentRepository.findByStateAndType(
List<ProtocolComponent> componentList = protocolComponentData.findByStateAndType(
ProtocolComponent.STATE_RUNNING, ProtocolComponent.TYPE_DEVICE);
for (ProtocolComponent component : componentList) {
register(component);

View File

@ -1,6 +1,5 @@
package cc.iotkit.comps.config;
import cc.iotkit.model.device.message.DeviceReport;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.mq.MqConsumer;
import cc.iotkit.mq.MqProducer;
@ -54,13 +53,4 @@ public class ComponentConfig {
return new VertxMqConsumer<>(ThingModelMessage.class);
}
@Bean("deviceReportProducer")
public MqProducer<DeviceReport> getDeviceReportProducer() {
return new VertxMqProducer<>(DeviceReport.class);
}
@Bean("deviceReportConsumer")
public MqConsumer<DeviceReport> getDeviceReportConsumer() {
return new VertxMqConsumer<>(DeviceReport.class);
}
}

View File

@ -17,6 +17,9 @@ import cc.iotkit.common.utils.UniqueIdUtil;
import cc.iotkit.comp.model.DeviceState;
import cc.iotkit.comp.model.RegisterInfo;
import cc.iotkit.dao.*;
import cc.iotkit.data.IDeviceInfoData;
import cc.iotkit.data.IProductModelData;
import cc.iotkit.data.IProductData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.model.product.Product;
@ -29,24 +32,24 @@ import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Slf4j
@Service
public class DeviceBehaviourService {
@Autowired
private ProductRepository productRepository;
@Autowired
private ProductModelRepository productModelRepository;
private IProductModelData productModelData;
@Autowired
private ProductCache productCache;
@Autowired
private DeviceInfoRepository deviceInfoRepository;
private IDeviceInfoData deviceInfoData;
@Autowired
private DeviceCache deviceCache;
@Autowired
private MqProducer<ThingModelMessage> producer;
@Autowired
private IProductData productData;
public void register(RegisterInfo info) {
try {
@ -80,7 +83,7 @@ public class DeviceBehaviourService {
if (parentId != null) {
//透传设备pk为空、model不为空使用model查询产品
if (StringUtils.isBlank(pk) && StringUtils.isNotBlank(model)) {
ProductModel productModel = productModelRepository.findByModel(model);
ProductModel productModel = productModelData.findByModel(model);
if (productModel == null) {
throw new BizException("product model does not exist");
}
@ -88,13 +91,12 @@ public class DeviceBehaviourService {
}
}
Optional<Product> optProduct = productRepository.findById(pk);
if (optProduct.isEmpty()) {
Product product = productData.findById(pk);
if (product == null) {
throw new BizException("Product does not exist");
}
Product product = optProduct.get();
String uid = product.getUid();
DeviceInfo device = deviceInfoRepository.findByProductKeyAndDeviceName(pk, info.getDeviceName());
DeviceInfo device = deviceInfoData.findByProductKeyAndDeviceName(pk, info.getDeviceName());
boolean reportMsg = false;
if (device != null) {
@ -130,13 +132,14 @@ public class DeviceBehaviourService {
device.setParentId(parentId);
reportMsg = true;
}
deviceInfoRepository.save(device);
deviceInfoData.save(device);
//新设备或更换网关需要产生注册消息
if (reportMsg) {
log.info("device registered:{}", JsonUtil.toJsonString(device));
//新注册设备注册消息
ThingModelMessage modelMessage = new ThingModelMessage(
UUID.randomUUID().toString(),
UniqueIdUtil.newRequestId(), "",
pk, dn,
ThingModelMessage.TYPE_LIFETIME, "register",
@ -154,7 +157,7 @@ public class DeviceBehaviourService {
String deviceName,
String productSecret,
String deviceSecret) {
DeviceInfo deviceInfo = deviceInfoRepository.findByProductKeyAndDeviceName(productKey, deviceName);
DeviceInfo deviceInfo = deviceInfoData.findByProductKeyAndDeviceName(productKey, deviceName);
if (deviceInfo == null) {
throw new BizException("device does not exist");
}
@ -177,7 +180,7 @@ public class DeviceBehaviourService {
public void deviceStateChange(String productKey,
String deviceName,
boolean online) {
DeviceInfo device = deviceInfoRepository.findByProductKeyAndDeviceName(productKey, deviceName);
DeviceInfo device = deviceInfoData.findByProductKeyAndDeviceName(productKey, deviceName);
if (device == null) {
log.warn(String.format("productKey: %s,device: %s,online: %s", productKey, device, online));
throw new BizException("device does not exist");
@ -188,7 +191,7 @@ public class DeviceBehaviourService {
return;
}
List<DeviceInfo> subDevices = deviceInfoRepository.findByParentId(device.getDeviceId());
List<DeviceInfo> subDevices = deviceInfoData.findByParentId(device.getDeviceId());
for (DeviceInfo subDevice : subDevices) {
Product product = productCache.findById(subDevice.getProductKey());
Boolean transparent = product.getTransparent();
@ -207,10 +210,11 @@ public class DeviceBehaviourService {
device.getState().setOnline(false);
device.getState().setOfflineTime(System.currentTimeMillis());
}
deviceInfoRepository.save(device);
deviceInfoData.save(device);
//设备状态变更消息
ThingModelMessage modelMessage = new ThingModelMessage(
UUID.randomUUID().toString(),
UniqueIdUtil.newRequestId(), "",
device.getProductKey(), device.getDeviceName(),
ThingModelMessage.TYPE_STATE,

View File

@ -13,7 +13,7 @@ import cc.iotkit.common.Constants;
import cc.iotkit.common.thing.ThingService;
import cc.iotkit.common.utils.JsonUtil;
import cc.iotkit.comps.DeviceComponentManager;
import cc.iotkit.dao.DeviceConfigRepository;
import cc.iotkit.data.IDeviceConfigData;
import cc.iotkit.model.device.DeviceConfig;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.mq.ConsumerHandler;
@ -30,7 +30,7 @@ import java.util.Map;
*/
@Slf4j
@Service
public class DeviceConfigService implements ConsumerHandler<ThingModelMessage> {
public class DeviceConfigConsumer implements ConsumerHandler<ThingModelMessage> {
@Autowired
private MqConsumer<ThingModelMessage> configMessageConsumer;
@ -39,7 +39,7 @@ public class DeviceConfigService implements ConsumerHandler<ThingModelMessage> {
public DeviceComponentManager deviceComponentManager;
@Autowired
private DeviceConfigRepository deviceConfigRepository;
private IDeviceConfigData deviceConfigData;
@PostConstruct
public void init() {
@ -52,7 +52,7 @@ public class DeviceConfigService implements ConsumerHandler<ThingModelMessage> {
String identifier = msg.getIdentifier();
if (ThingModelMessage.ID_CONFIG_GET.equals(identifier)) {
//收到设备获取配置消息,回复配置信息给设备
DeviceConfig deviceConfig = deviceConfigRepository.findByDeviceId(msg.getDeviceId());
DeviceConfig deviceConfig = deviceConfigData.findByDeviceId(msg.getDeviceId());
if (deviceConfig == null) {
return;
}

View File

@ -10,35 +10,28 @@
package cc.iotkit.comps.service;
import cc.iotkit.common.Constants;
import cc.iotkit.dao.*;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.DeviceReport;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.mq.ConsumerHandler;
import cc.iotkit.mq.MqConsumer;
import cc.iotkit.mq.MqProducer;
import cc.iotkit.temporal.IThingModelMessageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.UUID;
@Slf4j
@Service
public class DeviceMessageConsumer implements ConsumerHandler<ThingModelMessage> {
@Lazy
@Autowired
private ThingModelMessageRepository messageRepository;
@Autowired
private DeviceCache deviceCache;
private IThingModelMessageData thingModelMessageData;
@Autowired
private MqConsumer<ThingModelMessage> thingModelMessageConsumer;
@Autowired
private MqProducer<ThingModelMessage> thingModelMessageMqProducer;
@Autowired
private MqProducer<DeviceReport> deviceReportProducer;
@PostConstruct
public void init() {
@ -59,29 +52,12 @@ public class DeviceMessageConsumer implements ConsumerHandler<ThingModelMessage>
thingModelMessageMqProducer.publish(Constants.DEVICE_CONFIG_TOPIC, msg);
}
//重新发布设备上报记录,不包含消息内容,用于数据统计
deviceReportProducer.publish(Constants.DEVICE_REPORT_RECORD_TOPIC, getDeviceReport(msg));
//设备消息入库
messageRepository.save(msg);
thingModelMessageData.add(msg);
} catch (Throwable e) {
//不能重复消费
log.error("device message consumer error", e);
}
}
private DeviceReport getDeviceReport(ThingModelMessage message) {
DeviceInfo device = deviceCache.get(message.getDeviceId());
return DeviceReport.builder()
.id(UUID.randomUUID().toString())
.deviceId(message.getDeviceId())
.productKey(message.getProductKey())
.deviceName(message.getDeviceName())
.uid(device.getUid())
.identifier(message.getIdentifier())
.type(message.getType())
.code(message.getCode())
.time(message.getTime())
.build();
}
}

View File

@ -3,11 +3,13 @@ package cc.iotkit.comps.service;
import cc.iotkit.common.Constants;
import cc.iotkit.common.utils.JsonUtil;
import cc.iotkit.dao.DeviceDao;
import cc.iotkit.dao.DevicePropertyRepository;
import cc.iotkit.data.IDeviceInfoData;
import cc.iotkit.data.cache.DeviceCacheService;
import cc.iotkit.model.device.message.DeviceProperty;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.mq.ConsumerHandler;
import cc.iotkit.mq.MqConsumer;
import cc.iotkit.temporal.IDevicePropertyData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -22,14 +24,18 @@ import java.util.Map;
*/
@Slf4j
@Service
public class PropertyPersistService implements ConsumerHandler<ThingModelMessage> {
public class DevicePropertyConsumer implements ConsumerHandler<ThingModelMessage> {
@Autowired
private MqConsumer<ThingModelMessage> thingModelMessageMqConsumer;
@Autowired
private DeviceDao deviceDao;
@Autowired
private DevicePropertyRepository propertyRepository;
private IDevicePropertyData devicePropertyData;
@Autowired
private IDeviceInfoData deviceInfoData;
@Autowired
private DeviceCacheService deviceCacheService;
@PostConstruct
public void init() {
@ -63,7 +69,7 @@ public class PropertyPersistService implements ConsumerHandler<ThingModelMessage
//批量保存
try {
propertyRepository.saveAll(batch);
devicePropertyData.addProperties(batch);
} catch (Throwable e) {
log.warn("save property data error", e);
}
@ -74,9 +80,8 @@ public class PropertyPersistService implements ConsumerHandler<ThingModelMessage
*/
private void updateDeviceCurrentProperties(String deviceId, Map<String, Object> properties) {
try {
log.info("update device property,deviceId:{},property:{}",
deviceId, JsonUtil.toJsonString(properties));
deviceDao.updateProperties(deviceId, properties);
log.info("save device property,deviceId:{},property:{}", deviceId, JsonUtil.toJsonString(properties));
deviceCacheService.saveProperties(deviceId, properties);
} catch (Throwable e) {
log.error("save device current properties error", e);
}

View File

@ -1,40 +0,0 @@
package cc.iotkit.comps.service;
import cc.iotkit.common.Constants;
import cc.iotkit.dao.DeviceReportRepository;
import cc.iotkit.model.device.message.DeviceReport;
import cc.iotkit.mq.ConsumerHandler;
import cc.iotkit.mq.MqConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/**
*
*/
@Slf4j
@Service
public class ReportRecordPersistService implements ConsumerHandler<DeviceReport> {
@Autowired
private MqConsumer<DeviceReport> deviceReportMqConsumer;
@Autowired
private DeviceReportRepository deviceReportRepository;
@PostConstruct
public void init() {
deviceReportMqConsumer.consume(Constants.DEVICE_REPORT_RECORD_TOPIC, this);
}
@Override
public void handler(DeviceReport msg) {
try {
deviceReportRepository.save(msg);
} catch (Throwable e) {
log.warn("save report record error", e);
}
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -3,7 +3,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-emqx-component</artifactId>
@ -82,25 +82,25 @@
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-model</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-dao</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-component-base</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@ import cc.iotkit.comp.model.DeviceState;
import cc.iotkit.comp.utils.SpringUtils;
import cc.iotkit.converter.DeviceMessage;
import cc.iotkit.common.thing.ThingService;
import cc.iotkit.dao.DeviceInfoRepository;
import cc.iotkit.data.IDeviceInfoData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.ThingModelMessage;
import io.netty.handler.codec.mqtt.MqttQoS;
@ -173,9 +173,9 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
if (parent == null) {
return;
}
DeviceInfoRepository deviceInfoRepository = SpringUtils.getBean(DeviceInfoRepository.class);
IDeviceInfoData deviceInfoService = SpringUtils.getBean(IDeviceInfoData.class);
DeviceInfo deviceInfo = deviceInfoRepository.findByProductKeyAndDeviceName(state.getProductKey(), state.getDeviceName());
DeviceInfo deviceInfo = deviceInfoService.findByProductKeyAndDeviceName(state.getProductKey(), state.getDeviceName());
if (deviceInfo != null) {
boolean isOnline = DeviceState.STATE_ONLINE.equals(state.getState());
deviceInfo.getState().setOnline(isOnline);
@ -185,7 +185,7 @@ public class EmqxDeviceComponent extends AbstractDeviceComponent {
if (isOnline) {
deviceInfo.getState().setOnlineTime(System.currentTimeMillis());
}
deviceInfoRepository.save(deviceInfo);
deviceInfoService.save(deviceInfo);
}
}

View File

@ -3,7 +3,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-http-biz-component</artifactId>
@ -58,7 +58,7 @@
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-component-base</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -3,7 +3,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-mqtt-component</artifactId>
@ -80,19 +80,19 @@
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-component-base</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-dao</artifactId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iot-components</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>iotkit-parent</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -1,27 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.aligenie.AligenieDevice;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface AligenieDeviceRepository extends ElasticsearchRepository<AligenieDevice, String> {
void deleteByUid(String uid);
List<AligenieDevice> findByUid(String uid);
AligenieDevice findByUidAndDeviceId(String uid, String deviceId);
List<AligenieDevice> findByDeviceId(String deviceId);
}

View File

@ -1,22 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.aligenie.AligenieProduct;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface AligenieProductRepository extends ElasticsearchRepository<AligenieProduct, String> {
List<AligenieProduct> findByUid(String uid);
AligenieProduct findByProductKey(String productKey);
}

View File

@ -1,23 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.product.AppDesign;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface AppDesignRepository extends ElasticsearchRepository<AppDesign, String> {
AppDesign findByProductKey(String productKey);
List<AppDesign> findByUid(String uid);
}

View File

@ -1,61 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public class CommonDao {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
*
*/
public <T> Paging<T> pagedFind(Class<T> cls, Criteria condition, Sort.Order order, int size, int page) {
Query query = new CriteriaQuery(condition);
long total = elasticsearchRestTemplate.count(query, cls);
query = query.setPageable(PageRequest.of(page - 1, size, Sort.by(order)));
SearchHits<T> searchHits = elasticsearchRestTemplate.search(query, cls);
List<T> list = new ArrayList<>();
for (SearchHit<T> searchHit : searchHits) {
list.add(searchHit.getContent());
}
return new Paging<>(total, list);
}
/**
*
*/
public <T> List<T> find(Class<T> cls, Criteria condition) {
Query query = new CriteriaQuery(condition);
SearchHits<T> searchHits = elasticsearchRestTemplate.search(query, cls);
List<T> list = new ArrayList<>();
for (SearchHit<T> searchHit : searchHits) {
list.add(searchHit.getContent());
}
return list;
}
}

View File

@ -1,270 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.dao.config.EmbeddedEs;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.product.Category;
import cc.iotkit.model.product.Product;
import cc.iotkit.model.stats.DataItem;
import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ScriptType;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.clients.elasticsearch7.ElasticsearchAggregations;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Repository
public class DeviceDao {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private DeviceInfoRepository deviceInfoRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private CategoryRepository categoryRepository;
public Paging<DeviceInfo> find(Criteria condition, int size, int page) {
Query query = new CriteriaQuery(condition);
long total = elasticsearchRestTemplate.count(query, DeviceInfo.class);
query = query.setPageable(PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt"))));
SearchHits<DeviceInfo> searchHits = elasticsearchRestTemplate.search(query, DeviceInfo.class);
List<DeviceInfo> list = new ArrayList<>();
for (SearchHit<DeviceInfo> searchHit : searchHits) {
list.add(searchHit.getContent());
}
return new Paging<>(total, list);
}
/**
*
*/
public void updateProperties(String deviceId, Map<String, Object> properties) {
if (properties == null) {
return;
}
//外置es采用脚本更新
if (EmbeddedEs.disabled) {
Map<String, Object> param = new HashMap<>();
param.put("property", BeanUtil.beanToMap(properties));
param.put("keys", properties.keySet());
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(new Criteria()
.and("deviceId").is(deviceId)))
.withParams(param)
.withScript("for(key in params.keys){ctx._source.property[key]=params.property[key];}")
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.updateByQuery(updateQuery, IndexCoordinates.of("device_info"));
} else {
//内置es采用文档更新
DeviceInfo deviceInfo = deviceInfoRepository.findByDeviceId(deviceId);
Map<String, Object> oldProps = deviceInfo.getProperty();
oldProps.putAll(properties);
deviceInfoRepository.save(deviceInfo);
}
}
/**
*
*/
public void updateTag(String deviceId, DeviceInfo.Tag tag) {
Map<String, Object> param = new HashMap<>();
param.put("tag", BeanUtil.beanToMap(tag));
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(new Criteria()
.and("deviceId").is(deviceId)))
.withParams(param)
.withScript(String.format("ctx._source.tag.%s=params.tag", tag.getId()))
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.updateByQuery(updateQuery, IndexCoordinates.of("device_info"));
}
/**
*
*/
public void setTagNull(String deviceId, String tagId) {
// Query query = Query.query(new Criteria().and("deviceId").is(deviceId));
// Update update = new Update();
// update.set("tag." + tagId, null);
// mongoTemplate.updateFirst(query, update, DeviceInfo.class);
}
/**
*
*/
public List<DataItem> getDeviceStatsByCategory(String uid) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(uid)) {
queryBuilder =
queryBuilder.must(QueryBuilders.termQuery("uid.keyword", uid));
}
//先按产品分组统计
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withAggregations(
AggregationBuilders.terms("countByPk").field("productKey.keyword")
.size(1000)
.subAggregation(AggregationBuilders.count("count").field("productKey.keyword"))
)
.build();
ElasticsearchAggregations result = (ElasticsearchAggregations) elasticsearchRestTemplate
.search(query, DeviceInfo.class).getAggregations();
ParsedStringTerms terms = result.aggregations().get("countByPk");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
Map<String, Long> productCount = new HashMap<>();
for (Terms.Bucket bucket : buckets) {
productCount.put(bucket.getKeyAsString(), bucket.getDocCount());
}
//取用户下产品列表
Iterable<Product> products;
if (StringUtils.isNotBlank(uid)) {
products = productRepository.findByUid(uid);
} else {
products = productRepository.findAll();
}
Map<String, String> pkCateMap = new HashMap<>();
for (Product product : products) {
pkCateMap.put(product.getId(), product.getCategory());
}
//取品类列表
Map<String, String> cateNames = new HashMap<>();
for (Category category : categoryRepository.findAll()) {
cateNames.put(category.getId(), category.getName());
}
Map<String, Long> cateStats = new HashMap<>();
productCount.forEach((key, val) -> {
String cateName = cateNames.get(pkCateMap.get(key));
//按品类汇总
long total = cateStats.getOrDefault(cateName, 0L);
total += val;
cateStats.put(cateName, total);
});
List<DataItem> items = new ArrayList<>();
cateStats.forEach((key, val) -> {
items.add(new DataItem(key, val));
});
return items;
}
/**
*
*/
public List<DataItem> getDeviceStatsByCategory() {
return getDeviceStatsByCategory(null);
}
/**
* id
*/
public List<DeviceInfo> findByGroupId(String groupId) {
Query query = new CriteriaQuery(new Criteria().and("group." + groupId).exists());
SearchHits<DeviceInfo> searchHits = elasticsearchRestTemplate.search(query, DeviceInfo.class);
return searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
}
/**
* id
*/
public long countByGroupId(String groupId) {
Query query = new CriteriaQuery(new Criteria().and("group." + groupId).exists());
return elasticsearchRestTemplate.count(query, DeviceInfo.class);
}
/**
* id
*/
public void updateGroupByDeviceId(String deviceId, DeviceInfo.Group group) {
Map<String, Object> param = new HashMap<>();
param.put("group", BeanUtil.beanToMap(group));
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(new Criteria()
.and("deviceId").is(deviceId)))
.withParams(param)
.withScript(String.format("ctx._source.group.%s=params.group", group.getId()))
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.updateByQuery(updateQuery, IndexCoordinates.of("device_info"));
}
/**
* id
*/
public void updateGroup(String groupId, DeviceInfo.Group group) {
Map<String, Object> param = new HashMap<>();
param.put("group", BeanUtil.beanToMap(group));
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(new Criteria()
.and("group." + groupId).exists()))
.withParams(param)
.withScript(String.format("ctx._source.group.%s=params.group", groupId))
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("device_info"));
}
/**
*
*/
public void removeGroup(String deviceId, String groupId) {
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(
Criteria.where("deviceId").is(deviceId)))
.withScript(String.format("ctx._source.group.remove('%s')", groupId))
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.updateByQuery(updateQuery, IndexCoordinates.of("device_info"));
}
/**
*
*/
public void removeGroup(String groupId) {
UpdateQuery updateQuery = UpdateQuery.builder(new CriteriaQuery(new Criteria()
.and("group." + groupId).exists()))
.withScript(String.format("ctx._source.group.remove('%s')", groupId))
.withScriptType(ScriptType.INLINE)
.build();
elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("device_info"));
}
}

View File

@ -1,21 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
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.elasticsearch.repository.ElasticsearchRepository;
public interface DeviceGroupRepository extends ElasticsearchRepository<DeviceGroup, String> {
Page<DeviceGroup> findByNameLike(String name, Pageable pageable);
}

View File

@ -1,31 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.DeviceInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface DeviceInfoRepository extends ElasticsearchRepository<DeviceInfo, String> {
DeviceInfo findByProductKeyAndDeviceName(String productKey, String deviceName);
DeviceInfo findByDeviceId(String deviceId);
List<DeviceInfo> findByParentId(String parentId);
List<DeviceInfo> findByParentIdAndUid(String parentId, String uid);
List<DeviceInfo> findByDeviceName(String deviceName);
long countByUid(String uid);
}

View File

@ -1,18 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.message.DeviceProperty;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface DevicePropertyRepository extends ElasticsearchRepository<DeviceProperty, String> {
}

View File

@ -1,79 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.message.DeviceReport;
import cc.iotkit.model.stats.TimeData;
import lombok.SneakyThrows;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.clients.elasticsearch7.ElasticsearchAggregations;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
@Repository
public class DeviceReportDao {
@Autowired
private ElasticsearchRestTemplate template;
/**
*
*/
public List<TimeData> getDeviceMessageStatsWithUid(String uid, long start, long end) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.rangeQuery("time")
.from(start, true).to(end, true));
if (uid != null) {
queryBuilder =
queryBuilder.must(QueryBuilders.termQuery("uid", uid));
}
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withAggregations(AggregationBuilders.dateHistogram("agg")
.field("time")
.calendarInterval(DateHistogramInterval.HOUR)
.calendarInterval(DateHistogramInterval.hours(1))
)
.build();
ElasticsearchAggregations result = (ElasticsearchAggregations) template
.search(query, DeviceReport.class).getAggregations();
ParsedDateHistogram histogram = result.aggregations().get("agg");
List<TimeData> data = new ArrayList<>();
for (Histogram.Bucket bucket : histogram.getBuckets()) {
long seconds = ((ZonedDateTime) bucket.getKey()).toInstant().getEpochSecond();
data.add(new TimeData(seconds * 1000, bucket.getDocCount()));
}
return data;
}
/**
*
*/
@SneakyThrows
public List<TimeData> getDeviceMessageStats(long start, long end) {
return getDeviceMessageStatsWithUid(null, start, end);
}
}

View File

@ -1,23 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.protocol.ProtocolComponent;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ProtocolComponentRepository extends ElasticsearchRepository<ProtocolComponent, String> {
List<ProtocolComponent> findByState(String state);
List<ProtocolComponent> findByStateAndType(String state, String type);
}

View File

@ -1,16 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.protocol.ProtocolConverter;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ProtocolConverterRepository extends ElasticsearchRepository<ProtocolConverter, String> {
}

View File

@ -1,27 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.rule.RuleInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface RuleInfoRepository extends ElasticsearchRepository<RuleInfo, String> {
List<RuleInfo> findByUidAndType(String uid, String type);
Page<RuleInfo> findByUidAndType(String uid, String type, Pageable pageable);
Page<RuleInfo> findByType(String type, Pageable pageable);
}

View File

@ -1,23 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.rule.RuleLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface RuleLogRepository extends ElasticsearchRepository<RuleLog, String> {
void deleteByRuleId(String ruleId);
Page<RuleLog> findByRuleId(String ruleId, Pageable pageable);
}

View File

@ -1,32 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.space.SpaceDevice;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface SpaceDeviceRepository extends ElasticsearchRepository<SpaceDevice, String> {
List<SpaceDevice> findByUidOrderByUseAtDesc(String uid);
List<SpaceDevice> findByUidOrderByAddAtDesc(String uid);
List<SpaceDevice> findByUid(String uid);
List<SpaceDevice> findBySpaceIdOrderByAddAtDesc(String spaceId);
List<SpaceDevice> findByUidAndSpaceIdOrderByAddAtDesc(String uid, String spaceId);
SpaceDevice findByDeviceId(String deviceId);
SpaceDevice findByDeviceIdAndUid(String deviceId, String uid);
}

View File

@ -1,25 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.space.Space;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface SpaceRepository extends ElasticsearchRepository<Space, String> {
List<Space> findByUidOrderByCreateAtDesc(String uid);
List<Space> findByUidAndHomeIdOrderByCreateAtDesc(String uid, String homeId);
List<Space> findByHomeId(String homeId);
}

View File

@ -1,21 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.rule.TaskInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface TaskInfoRepository extends ElasticsearchRepository<TaskInfo, String> {
List<TaskInfo> findByUid(String uid);
}

View File

@ -1,23 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.rule.TaskLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface TaskLogRepository extends ElasticsearchRepository<TaskLog, String> {
void deleteByTaskId(String taskId);
Page<TaskLog> findByTaskId(String taskId, Pageable pageable);
}

View File

@ -1,21 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.message.ThingModelMessage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ThingModelMessageRepository extends ElasticsearchRepository<ThingModelMessage, String> {
Page<ThingModelMessage> findByTypeAndIdentifier(String type, String identifier, Pageable pageable);
}

View File

@ -1,19 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.product.ThingModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ThingModelRepository extends ElasticsearchRepository<ThingModel, String> {
ThingModel findByProductKey(String productKey);
}

View File

@ -1,9 +0,0 @@
package cc.iotkit.dao;
import cc.iotkit.model.ThirdUserSession;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ThirdUserSessionRepository extends ElasticsearchRepository<ThirdUserSession, String> {
}

View File

@ -1,17 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.UserActionLog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface UserActionLogRepository extends ElasticsearchRepository<UserActionLog, String> {
}

View File

@ -1,25 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.UserInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface UserInfoRepository extends ElasticsearchRepository<UserInfo, String> {
UserInfo findByUid(String uid);
List<UserInfo> findByType(int type);
List<UserInfo> findByTypeAndOwnerId(int type, String ownerId);
}

View File

@ -1,21 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.VirtualDeviceLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface VirtualDeviceLogRepository extends ElasticsearchRepository<VirtualDeviceLog, String> {
Page<VirtualDeviceLog> findByVirtualDeviceId(String virtualDeviceId, Pageable pageable);
}

View File

@ -1,27 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.model.device.VirtualDevice;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface VirtualDeviceRepository extends ElasticsearchRepository<VirtualDevice, String> {
Page<VirtualDevice> findByUid(String uid, Pageable pageable);
List<VirtualDevice> findByUidAndState(String uid, String state);
List<VirtualDevice> findByTriggerAndState(String trigger, String state);
}

View File

@ -1,25 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Lazy
@Configuration
@EnableElasticsearchRepositories(basePackages = "cc.iotkit.dao", includeFilters =
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ElasticsearchRepository.class))
public class ElasticsearchConfiguration {
}

View File

@ -1,28 +0,0 @@
package cc.iotkit.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* oauth2client
*/
@Data
@Document(indexName = "oauth_client")
public class OauthClient {
@Id
private String clientId;
private String name;
private String clientSecret;
private String allowUrl;
@Field(type = FieldType.Date)
private Long createAt;
}

View File

@ -1,43 +0,0 @@
package cc.iotkit.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
*
*/
@Data
@Document(indexName = "third_user_session")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ThirdUserSession {
/**
* id
*/
@Id
private String uid;
/**
*
*/
private String type;
/**
* token
*/
private String token;
/**
*
*/
@Field(type = FieldType.Date)
private Long authAt;
}

View File

@ -1,87 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
*
*/
@Document(indexName = "user_action_log")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserActionLog {
@Id
private String id;
private String uid;
/**
*
* 0:
* 1:
* 2:
* 3:
* 4:
*/
private int type;
/**
*
*/
private String target;
/**
*
*/
private Object log;
/**
*
*/
private String result;
@Field(type = FieldType.Date)
private Long createAt;
public enum Type {
DEVICE_CONTROL("设备控制", 0),
DEVICE_ADD("添加设备", 1),
DEVICE_SHARED("分享设备", 2),
SPACE_ADD("创建空间", 3),
HOME_SHARED("分享家庭", 4);
private String name;
private int value;
private Type(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
}
}

View File

@ -1,72 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.model.aligenie;
import cc.iotkit.model.Owned;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.List;
/**
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "aligenie_product")
public class AligenieProduct implements Owned {
@Id
private String productId;
private String deviceType;
private String brand;
private String model;
private String icon;
private List<Property> properties;
private List<String> actions;
/**
* pk
*/
private String productKey;
/**
*
*/
private String transform;
/**
*
*/
private String uid;
@Field(type = FieldType.Date)
private Long createAt;
@Override
public String getId() {
return productId;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Property {
private String name;
private String value;
}
}

View File

@ -1,59 +0,0 @@
package cc.iotkit.model.device.message;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* -
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "device_report")
public class DeviceReport {
@Id
private String id;
private String deviceId;
private String productKey;
private String deviceName;
/**
*
*/
private String uid;
/**
*
* lifetime:
* state:
* property:
* event:
* service:
*/
private String type;
private String identifier;
/**
*
*/
private int code;
/**
*
*/
@Field(type = FieldType.Date)
private Long time;
}

View File

@ -1,28 +0,0 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.model.product;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "category")
public class Category {
@Id
private String id;
private String name;
@Field(type = FieldType.Date)
private Long createAt;
}

View File

@ -1 +0,0 @@
与数据存取相关内容的模块

View File

@ -3,9 +3,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iot-data-service</artifactId>
<artifactId>iot-data</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -33,6 +33,11 @@
<artifactId>iot-model</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-data-service</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -10,6 +10,7 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.ICategoryData;
import cc.iotkit.model.product.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
@ -22,7 +23,7 @@ import javax.annotation.PostConstruct;
public class CategoryCache {
@Autowired
private CategoryRepository categoryRepository;
private ICategoryData categoryData;
private static CategoryCache INSTANCE;
@ -37,6 +38,6 @@ public class CategoryCache {
@Cacheable(value = Constants.CATEGORY_CACHE, key = "#id")
public Category getById(String id) {
return categoryRepository.findById(id).orElse(null);
return categoryData.findById(id);
}
}

View File

@ -10,6 +10,7 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.IDeviceInfoData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.stats.DataItem;
import org.springframework.beans.factory.annotation.Autowired;
@ -23,7 +24,7 @@ import java.util.List;
public class DeviceCache {
@Autowired
private DeviceInfoRepository deviceInfoRepository;
private IDeviceInfoData deviceInfoData;
@Autowired
private DeviceDao deviceDao;
@ -40,17 +41,17 @@ public class DeviceCache {
@Cacheable(value = Constants.DEVICE_CACHE, key = "#pk+'_'+#dn")
public DeviceInfo getDeviceInfo(String pk, String dn) {
return deviceInfoRepository.findByProductKeyAndDeviceName(pk, dn);
return deviceInfoData.findByProductKeyAndDeviceName(pk, dn);
}
@Cacheable(value = Constants.DEVICE_CACHE, key = "#deviceId")
public DeviceInfo get(String deviceId) {
return deviceInfoRepository.findById(deviceId).orElse(null);
return deviceInfoData.findByDeviceId(deviceId);
}
@Cacheable(value = Constants.DEVICE_STATS_CACHE, key = "#uid")
public List<DataItem> getDeviceStatsByCategory(String uid) {
return deviceDao.getDeviceStatsByCategory(uid);
return deviceInfoData.getDeviceStatsByCategory(uid);
}
}

View File

@ -0,0 +1,122 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
import cc.iotkit.data.ICategoryData;
import cc.iotkit.data.IDeviceInfoData;
import cc.iotkit.data.IProductData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.product.Category;
import cc.iotkit.model.product.Product;
import cc.iotkit.model.stats.DataItem;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.clients.elasticsearch7.ElasticsearchAggregations;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class DeviceDao {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Autowired
private IDeviceInfoData deviceInfoData;
@Autowired
private IProductData productData;
@Autowired
private ICategoryData categoryData;
/**
*
*/
public List<DataItem> getDeviceStatsByCategory(String uid) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
if (StringUtils.isNotBlank(uid)) {
queryBuilder =
queryBuilder.must(QueryBuilders.termQuery("uid.keyword", uid));
}
//先按产品分组统计
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withAggregations(
AggregationBuilders.terms("countByPk").field("productKey.keyword")
.size(1000)
.subAggregation(AggregationBuilders.count("count").field("productKey.keyword"))
)
.build();
ElasticsearchAggregations result = (ElasticsearchAggregations) elasticsearchRestTemplate
.search(query, DeviceInfo.class).getAggregations();
ParsedStringTerms terms = result.aggregations().get("countByPk");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
Map<String, Long> productCount = new HashMap<>();
for (Terms.Bucket bucket : buckets) {
productCount.put(bucket.getKeyAsString(), bucket.getDocCount());
}
//取用户下产品列表
Iterable<Product> products;
if (StringUtils.isNotBlank(uid)) {
products = productData.findByUid(uid);
} else {
products = productData.findAll();
}
Map<String, String> pkCateMap = new HashMap<>();
for (Product product : products) {
pkCateMap.put(product.getId(), product.getCategory());
}
//取品类列表
Map<String, String> cateNames = new HashMap<>();
for (Category category : categoryData.findAll()) {
cateNames.put(category.getId(), category.getName());
}
Map<String, Long> cateStats = new HashMap<>();
productCount.forEach((key, val) -> {
String cateName = cateNames.get(pkCateMap.get(key));
//按品类汇总
long total = cateStats.getOrDefault(cateName, 0L);
total += val;
cateStats.put(cateName, total);
});
List<DataItem> items = new ArrayList<>();
cateStats.forEach((key, val) -> {
items.add(new DataItem(key, val));
});
return items;
}
/**
*
*/
public List<DataItem> getDeviceStatsByCategory() {
return getDeviceStatsByCategory(null);
}
}

View File

@ -10,6 +10,7 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.IOauthClientData;
import cc.iotkit.model.OauthClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
@ -21,7 +22,7 @@ import javax.annotation.PostConstruct;
public class OauthClientCache {
@Autowired
private OauthClientRepository oauthClientRepository;
private IOauthClientData oauthClientData;
private static OauthClientCache INSTANCE;
@ -36,7 +37,7 @@ public class OauthClientCache {
@Cacheable(value = Constants.OAUTH_CLIENT_CACHE, key = "#clientId")
public OauthClient getClient(String clientId) {
return oauthClientRepository.findById(clientId).orElse(null);
return oauthClientData.findByClientId(clientId);
}
}

View File

@ -10,6 +10,9 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.IProductModelData;
import cc.iotkit.data.IProductData;
import cc.iotkit.data.IThingModelData;
import cc.iotkit.model.product.Product;
import cc.iotkit.model.product.ProductModel;
import cc.iotkit.model.product.ThingModel;
@ -23,11 +26,11 @@ import javax.annotation.PostConstruct;
public class ProductCache {
@Autowired
private ProductRepository productRepository;
private IProductData productData;
@Autowired
private ThingModelRepository thingModelRepository;
private IThingModelData thingModelData;
@Autowired
private ProductModelRepository productModelRepository;
private IProductModelData productModelData;
private static ProductCache INSTANCE;
@ -42,17 +45,17 @@ public class ProductCache {
@Cacheable(value = Constants.PRODUCT_CACHE, key = "'product'+#pk")
public Product findById(String pk) {
return productRepository.findById(pk).orElse(new Product());
return productData.findById(pk);
}
@Cacheable(value = Constants.THING_MODEL_CACHE, key = "'thing_model'+#pk")
public ThingModel getThingModel(String pk) {
return thingModelRepository.findByProductKey(pk);
return thingModelData.findByProductKey(pk);
}
@Cacheable(value = Constants.PRODUCT_SCRIPT_CACHE, key = "'product_script'+#model")
public ProductModel getProductScriptByModel(String model) {
return productModelRepository.findByModel(model);
return productModelData.findByModel(model);
}

View File

@ -1,6 +1,7 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.ISpaceData;
import cc.iotkit.model.space.Space;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
@ -12,7 +13,7 @@ import javax.annotation.PostConstruct;
public class SpaceCache {
@Autowired
private SpaceRepository spaceRepository;
private ISpaceData spaceData;
private static SpaceCache INSTANCE;
@ -27,7 +28,7 @@ public class SpaceCache {
@Cacheable(value = Constants.SPACE_CACHE, key = "#spaceId")
public Space getSpace(String spaceId) {
return spaceRepository.findById(spaceId).orElse(null);
return spaceData.findById(spaceId);
}
}

View File

@ -10,6 +10,7 @@
package cc.iotkit.dao;
import cc.iotkit.common.Constants;
import cc.iotkit.data.IUserInfoData;
import cc.iotkit.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
@ -21,7 +22,7 @@ import javax.annotation.PostConstruct;
public class UserInfoCache {
@Autowired
private UserInfoRepository userInfoRepository;
private IUserInfoData userInfoData;
private static UserInfoCache INSTANCE;
@ -36,7 +37,7 @@ public class UserInfoCache {
@Cacheable(value = Constants.USER_CACHE, key = "#uid")
public UserInfo getUserInfo(String uid) {
return userInfoRepository.findById(uid).orElse(null);
return userInfoData.findById(uid);
}
}

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iotkit-parent</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.3-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-data-cache</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,41 @@
package cc.iotkit.data.cache;
import cc.iotkit.common.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class DeviceCacheService {
private static final String PROPERTY_CACHE_KEY = "str:device:property:%s";
@Autowired
private StringRedisTemplate redisTemplate;
private String getPropertyCacheKey(String deviceId) {
return String.format(PROPERTY_CACHE_KEY, deviceId);
}
/**
* redis
*
* @param deviceId id
* @param properties map
*/
public void saveProperties(String deviceId, Map<String, Object> properties) {
redisTemplate.opsForValue().set(getPropertyCacheKey(deviceId), JsonUtil.toJsonString(properties));
}
/**
* map
*
* @param deviceId id
*/
public Map<String, Object> getProperties(String deviceId) {
return JsonUtil.parse(redisTemplate.opsForValue().get(getPropertyCacheKey(deviceId)), Map.class);
}
}

View File

@ -3,20 +3,20 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iotkit-parent</artifactId>
<artifactId>iot-data</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>iot-model</module>
<module>iot-dao</module>
<module>iot-device-dao</module>
</modules>
<artifactId>iot-data-service</artifactId>
<dependencies>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-model</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -7,10 +7,9 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao.config;
package cc.iotkit.data;
public interface EmbeddedEs {
boolean disabled = "true".equals(System.getProperty("disabledEmbeddedEs"));
import cc.iotkit.model.product.Category;
public interface ICategoryData extends ICommonData<Category, String> {
}

View File

@ -9,43 +9,51 @@
*/
package cc.iotkit.data;
import cc.iotkit.model.product.Product;
import cc.iotkit.model.Id;
import cc.iotkit.model.Paging;
import java.util.List;
/**
*
*
*/
public interface ProductDao {
public interface ICommonData<T extends Id<ID>, ID> {
/**
* id
* ID
*/
Product findById(String id);
T findById(ID id);
/**
*
* id
*/
void add(Product product);
T save(T data);
/**
* id
*
*/
void updateById(Product product);
T add(T data);
/**
* id
* id
*/
long countByUid(String uid);
void deleteById(ID id);
/**
* id
*
*/
List<Product> findByUid(String uid);
long count();
/**
*
*
*/
List<Product> findByCategory(String category);
List<T> findAll();
/**
*
*
* @param page 0
* @param size
*/
Paging<T> findAll(int page, int size);
}

View File

@ -7,12 +7,11 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.device.DeviceConfig;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface DeviceConfigRepository extends ElasticsearchRepository<DeviceConfig, String> {
public interface IDeviceConfigData extends ICommonData<DeviceConfig, String> {
DeviceConfig findByProductKeyAndDeviceName(String productKey, String deviceName);

View File

@ -7,12 +7,13 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.OauthClient;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.DeviceGroup;
public interface OauthClientRepository extends ElasticsearchRepository<OauthClient, String> {
public interface IDeviceGroupData extends ICommonData<DeviceGroup, String> {
Paging<DeviceGroup> findByNameLike(String name, int page, int size);
}

View File

@ -0,0 +1,108 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.data;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.stats.DataItem;
import java.util.List;
public interface IDeviceInfoData extends IOwnedData<DeviceInfo, String> {
/**
* ID
*
* @param deviceId ID
*/
DeviceInfo findByDeviceId(String deviceId);
/**
* productKeydeviceName
*/
DeviceInfo findByProductKeyAndDeviceName(String productKey, String deviceName);
/**
* ID
*
* @param parentId ID
*/
List<DeviceInfo> findByParentId(String parentId);
/**
* deviceName
*/
List<DeviceInfo> findByDeviceName(String deviceName);
/**
*
*
* @param uid id
* @param subUid id
* @param productKey key
* @param groupId
* @param state 线:online线,offline线
* @param keyword
* @param page
* @param size
*/
Paging<DeviceInfo> findByConditions(String uid, String subUid, String productKey,
String groupId, String state, String keyword,
int page, int size);
/**
*
*
* @param deviceId ID
* @param tag
*/
void updateTag(String deviceId, DeviceInfo.Tag tag);
/**
*
*/
List<DataItem> getDeviceStatsByCategory(String uid);
/**
* id
*/
long countByGroupId(String groupId);
/**
*
*
* @param deviceId ID
* @param group
*/
void addToGroup(String deviceId, DeviceInfo.Group group);
/**
* id
*
* @param groupId ID
* @param group
*/
void updateGroup(String groupId, DeviceInfo.Group group);
/**
*
*
* @param deviceId ID
* @param groupId ID
*/
void removeGroup(String deviceId, String groupId);
/**
*
*
* @param groupId ID
*/
void removeGroup(String groupId);
}

View File

@ -7,16 +7,12 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.space.Home;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface HomeRepository extends ElasticsearchRepository<Home, String> {
List<Home> findByUid(String uid);
public interface IHomeData extends IOwnedData<Home, String> {
Home findByUidAndCurrent(String uid, boolean current);

View File

@ -0,0 +1,9 @@
package cc.iotkit.data;
import cc.iotkit.model.OauthClient;
public interface IOauthClientData extends ICommonData<OauthClient, String> {
OauthClient findByClientId(String clientId);
}

View File

@ -7,13 +7,28 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.device.message.DeviceReport;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import cc.iotkit.model.Owned;
import cc.iotkit.model.Paging;
public interface DeviceReportRepository extends ElasticsearchRepository<DeviceReport, String> {
import java.util.List;
/**
*
*/
public interface IOwnedData<T extends Owned<ID>, ID> extends ICommonData<T, ID> {
/**
*
*/
List<T> findByUid(String uid);
Paging<T> findByUid(String uid, int page, int size);
/**
*
*/
long countByUid(String uid);
}

View File

@ -7,19 +7,20 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.product.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
long countByUid(String uid);
List<Product> findByUid(String uid);
/**
*
*/
public interface IProductData extends IOwnedData<Product, String> {
/**
*
*/
List<Product> findByCategory(String category);
}

View File

@ -7,14 +7,13 @@
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.data;
import cc.iotkit.model.product.ProductModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ProductModelRepository extends ElasticsearchRepository<ProductModel, String> {
public interface IProductModelData extends ICommonData<ProductModel, String> {
ProductModel findByModel(String model);

View File

@ -0,0 +1,13 @@
package cc.iotkit.data;
import cc.iotkit.model.protocol.ProtocolComponent;
import java.util.List;
public interface IProtocolComponentData extends IOwnedData<ProtocolComponent, String> {
List<ProtocolComponent> findByState(String state);
List<ProtocolComponent> findByStateAndType(String state, String type);
}

View File

@ -0,0 +1,6 @@
package cc.iotkit.data;
import cc.iotkit.model.protocol.ProtocolConverter;
public interface IProtocolConverterData extends IOwnedData<ProtocolConverter, String> {
}

View File

@ -0,0 +1,18 @@
package cc.iotkit.data;
import cc.iotkit.model.Paging;
import cc.iotkit.model.rule.RuleInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
public interface IRuleInfoData extends IOwnedData<RuleInfo, String> {
List<RuleInfo> findByUidAndType(String uid, String type);
Paging<RuleInfo> findByUidAndType(String uid, String type, int page, int size);
Paging<RuleInfo> findByType(String type, int page, int size);
}

View File

@ -0,0 +1,15 @@
package cc.iotkit.data;
import cc.iotkit.model.space.Space;
import java.util.List;
public interface ISpaceData extends IOwnedData<Space,String> {
List<Space> findByUidOrderByCreateAtDesc(String uid);
List<Space> findByUidAndHomeIdOrderByCreateAtDesc(String uid, String homeId);
List<Space> findByHomeId(String homeId);
}

View File

@ -0,0 +1,20 @@
package cc.iotkit.data;
import cc.iotkit.model.space.SpaceDevice;
import java.util.List;
public interface ISpaceDeviceData extends IOwnedData<SpaceDevice, String> {
List<SpaceDevice> findByUidOrderByUseAtDesc(String uid);
List<SpaceDevice> findByUidOrderByAddAtDesc(String uid);
List<SpaceDevice> findBySpaceIdOrderByAddAtDesc(String spaceId);
List<SpaceDevice> findByUidAndSpaceIdOrderByAddAtDesc(String uid, String spaceId);
SpaceDevice findByDeviceId(String deviceId);
SpaceDevice findByDeviceIdAndUid(String deviceId, String uid);
}

View File

@ -0,0 +1,6 @@
package cc.iotkit.data;
import cc.iotkit.model.rule.TaskInfo;
public interface ITaskInfoData extends IOwnedData<TaskInfo, String> {
}

View File

@ -0,0 +1,9 @@
package cc.iotkit.data;
import cc.iotkit.model.product.ThingModel;
public interface IThingModelData extends ICommonData<ThingModel, String> {
ThingModel findByProductKey(String productKey);
}

View File

@ -0,0 +1,14 @@
package cc.iotkit.data;
import cc.iotkit.model.UserInfo;
import java.util.List;
public interface IUserInfoData extends ICommonData<UserInfo, String> {
UserInfo findByUid(String uid);
List<UserInfo> findByType(int type);
List<UserInfo> findByTypeAndOwnerId(int type, String ownerId);
}

View File

@ -0,0 +1,16 @@
package cc.iotkit.data;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.VirtualDevice;
import java.util.List;
public interface IVirtualDeviceData extends IOwnedData<VirtualDevice, String> {
Paging<VirtualDevice> findByUid(String uid, int size, int page);
List<VirtualDevice> findByUidAndState(String uid, String state);
List<VirtualDevice> findByTriggerAndState(String trigger, String state);
}

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iot-data</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-es-temporal-service</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-temporal-service</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,43 @@
package cc.iotkit.temporal.es.document;
import cc.iotkit.model.device.message.DeviceProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "device_property")
public class DevicePropertyDoc {
@Id
private String id;
private String deviceId;
private String name;
private Object value;
@Field(type = FieldType.Date)
private Long time;
public DeviceProperty de() {
return new DeviceProperty(id, deviceId, name, value, time);
}
public DevicePropertyDoc(DeviceProperty raw) {
this.id = raw.getId();
this.deviceId = raw.getDeviceId();
this.name = raw.getName();
this.value = raw.getValue();
this.time = raw.getTime();
}
}

View File

@ -1,31 +1,23 @@
/*
* +----------------------------------------------------------------------
* | Copyright (c) 2021-2022 All rights reserved.
* +----------------------------------------------------------------------
* | Licensed
* +----------------------------------------------------------------------
* | Author: xw2sy@163.com
* +----------------------------------------------------------------------
*/
package cc.iotkit.dao;
package cc.iotkit.temporal.es.service;
import cc.iotkit.model.device.message.DeviceProperty;
import cc.iotkit.temporal.IDevicePropertyData;
import cc.iotkit.temporal.es.document.DevicePropertyDoc;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Repository
public class DevicePropertyDao {
@Service
public class DevicePropertyDataImpl implements IDevicePropertyData {
@Autowired
private ElasticsearchRestTemplate template;
@ -41,9 +33,17 @@ public class DevicePropertyDao {
)
.withSorts(new FieldSortBuilder("time").order(SortOrder.ASC))
.build();
SearchHits<DeviceProperty> result = template.search(query, DeviceProperty.class);
SearchHits<DevicePropertyDoc> result = template.search(query, DevicePropertyDoc.class);
return result.getSearchHits().stream()
.map(SearchHit::getContent).collect(Collectors.toList());
.map(h -> h.getContent().de())
.collect(Collectors.toList());
}
@Override
public void addProperties(List<DeviceProperty> properties) {
template.save(properties.stream().map(DevicePropertyDoc::new)
.collect(Collectors.toList()));
}
}

View File

@ -0,0 +1,24 @@
package cc.iotkit.temporal.es.service;
import cc.iotkit.model.Paging;
import cc.iotkit.model.rule.RuleLog;
import cc.iotkit.temporal.IRuleLogData;
import org.springframework.stereotype.Service;
@Service
public class RuleLogDataImpl implements IRuleLogData {
@Override
public void deleteByRuleId(String ruleId) {
}
@Override
public Paging<RuleLog> findByRuleId(String ruleId, int page, int size) {
return null;
}
@Override
public void add(RuleLog log) {
}
}

View File

@ -0,0 +1,24 @@
package cc.iotkit.temporal.es.service;
import cc.iotkit.model.Paging;
import cc.iotkit.model.rule.TaskLog;
import cc.iotkit.temporal.ITaskLogData;
import org.springframework.stereotype.Service;
@Service
public class TaskLogDataImpl implements ITaskLogData {
@Override
public void deleteByTaskId(String taskId) {
}
@Override
public Paging<TaskLog> findByTaskId(String taskId, int page, int size) {
return null;
}
@Override
public void add(TaskLog log) {
}
}

View File

@ -1,7 +1,9 @@
package cc.iotkit.dao;
package cc.iotkit.temporal.es.service;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.message.ThingModelMessage;
import cc.iotkit.model.stats.TimeData;
import cc.iotkit.temporal.IThingModelMessageData;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
@ -13,12 +15,13 @@ import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Repository
public class ThingModelMessageDao {
@Service
public class ThingModelMessageDataImpl implements IThingModelMessageData {
@Autowired
private ElasticsearchRestTemplate template;
@ -41,4 +44,19 @@ public class ThingModelMessageDao {
return new Paging<>(result.getTotalHits(), result.getSearchHits().stream()
.map(SearchHit::getContent).collect(Collectors.toList()));
}
@Override
public List<TimeData> getDeviceMessageStatsWithUid(String uid, long start, long end) {
return null;
}
@Override
public void add(ThingModelMessage msg) {
}
@Override
public long count() {
return 0;
}
}

View File

@ -0,0 +1,19 @@
package cc.iotkit.temporal.es.service;
import cc.iotkit.model.Paging;
import cc.iotkit.model.device.VirtualDeviceLog;
import cc.iotkit.temporal.IVirtualDeviceLogData;
import org.springframework.stereotype.Service;
@Service
public class VirtualDeviceLogDataImpl implements IVirtualDeviceLogData {
@Override
public Paging<VirtualDeviceLog> findByVirtualDeviceId(String virtualDeviceId, int page, int size) {
return null;
}
@Override
public void add(VirtualDeviceLog log) {
}
}

View File

@ -3,11 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>iot-data-service</artifactId>
<artifactId>iot-data</artifactId>
<groupId>cc.iotkit</groupId>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
</parent>
<version>0.3.2-SNAPSHOT</version>
<version>0.3.3-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>iot-model</artifactId>

View File

@ -0,0 +1,9 @@
package cc.iotkit.model;
public interface Id<T> {
T getId();
void setId(T id);
}

Some files were not shown because too many files have changed in this diff Show More