feat(数据中心): 新增历史记录和数据分析查询

This commit is contained in:
gx_ma
2026-03-05 16:32:10 +08:00
parent 913dd73f6e
commit 1e362f233b
36 changed files with 1254 additions and 24 deletions

View File

@@ -5,6 +5,8 @@ import com.fastbee.iot.model.ThingsModelItem.*;
import com.fastbee.iot.model.ThingsModels.ThingsModelValueItemDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.fastbee.common.annotation.Excel;
@@ -21,7 +23,9 @@ import java.util.List;
* @author kerwincui
* @date 2022-01-13
*/
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "DeviceLog", description = "设备日志对象 iot_device_log")
@Data
public class DeviceLog extends BaseEntity
{
private static final long serialVersionUID = 1L;
@@ -129,6 +133,8 @@ public class DeviceLog extends BaseEntity
/*是否历史存储*/
private Integer isHistory;
private List<String> identityList;
public Integer getIsHistory() {
return isHistory;
}

View File

@@ -2,11 +2,11 @@ package com.fastbee.iot.mapper;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
/**
@@ -94,4 +94,19 @@ public interface DeviceLogMapper
*/
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog);
/**
* 查询物模型历史数据
* @param deviceLog 设备日志
* @return java.util.List<com.fastbee.iot.model.HistoryModel>
*/
List<HistoryModel> listHistory(DeviceLog deviceLog);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(@Param("dataCenterParam") DataCenterParam dataCenterParam, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);
}

View File

@@ -1,7 +1,13 @@
package com.fastbee.iot.mapper;
import java.util.Date;
import java.util.List;
import com.fastbee.iot.domain.FunctionLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.FunctionLogVO;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
@@ -94,4 +100,19 @@ public interface FunctionLogMapper
* @param log
*/
public void updateByMessageId(FunctionLog log);
/**
* 查询物模型历史数据
* @param functionLogVO 功能日志
* @return java.util.List<com.fastbee.iot.model.HistoryModel>
*/
List<HistoryModel> listHistory(FunctionLogVO functionLogVO);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(@Param("dataCenterParam") DataCenterParam dataCenterParam, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime);
}

View File

@@ -2,7 +2,10 @@ package com.fastbee.iot.mapper;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@@ -35,4 +38,14 @@ public interface IotDbLogMapper {
List<DeviceLog> selectDeviceLogList(@Param("device") DeviceLog deviceLog);
List<DeviceLog> selectEventLogList(@Param("device") DeviceLog deviceLog);
List<HistoryModel> listHistory(@Param("device") DeviceLog deviceLog);
/**
* 统计设备物模型指令下发数量
*
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(@Param("dataCenterParam") DataCenterParam dataCenterParam);
}

View File

@@ -3,8 +3,10 @@ package com.fastbee.iot.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import com.fastbee.iot.tsdb.model.TdLogDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@@ -81,4 +83,14 @@ public interface TDDeviceLogMapper {
int deleteDeviceLogByDeviceNumber(@Param("database") String dbName, @Param("serialNumber") String serialNumber);
List<HistoryModel> listHistory(@Param("database") String database, @Param("device") DeviceLog deviceLog);
/**
* 统计设备物模型指令下发数量
*
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(@Param("database") String database, @Param("dataCenterParam") DataCenterParam dataCenterParam);
}

View File

@@ -0,0 +1,29 @@
package com.fastbee.iot.model;
import lombok.Data;
/**
* @author fastb
* @version 1.0
* @description: 数据中心统一传参类
* @date 2024-06-19 17:07
*/
@Data
public class DataCenterParam {
/**
* 设备编号
*/
private String serialNumber;
/**
* 开始时间
*/
private String beginTime;
/**
* 结束时间
*/
private String endTime;
}

View File

@@ -0,0 +1,45 @@
package com.fastbee.iot.model;
import lombok.Data;
import java.util.List;
/**
* @author fastb
* @version 1.0
* @description: 设备历史数据参数
* @date 2024-06-13 15:15
*/
@Data
public class DeviceHistoryParam {
/**
* 设备id
*/
private Long deviceId;
/**
* 设备编号
*/
private String serialNumber;
private List<IdentifierVO> identifierList;
private String identifierStr;
/** 查询用的开始时间 */
private String beginTime;
/** 查询用的结束时间 */
private String endTime;
@Data
public static class IdentifierVO {
private String identifier;
private Integer type;
}
}

View File

@@ -0,0 +1,129 @@
package com.fastbee.iot.model;
import com.fastbee.common.annotation.Excel;
import com.fastbee.common.core.domain.PageEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.util.List;
/**
* 设备服务下发日志对象 iot_function_log
*
* @author zhuangpeng.li
* @date 2024-11-13
*/
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "FunctionLogVO", description = "设备服务下发日志 iot_function_log")
@Data
public class FunctionLogVO extends PageEntity {
/** 设备功能日志ID */
@Excel(name = "设备功能日志ID")
@ApiModelProperty("设备功能日志ID")
private Long id;
/** 标识符 */
@Excel(name = "标识符")
@ApiModelProperty("标识符")
private String identify;
/** 1==服务下发2=属性获取3.OTA升级 */
@Excel(name = "1==服务下发2=属性获取3.OTA升级")
@ApiModelProperty("1==服务下发2=属性获取3.OTA升级")
private Integer funType;
/** 日志值 */
@Excel(name = "日志值")
@ApiModelProperty("日志值")
private String funValue;
/** 消息id */
@Excel(name = "消息id")
@ApiModelProperty("消息id")
private String messageId;
/** 设备名称 */
@Excel(name = "设备名称")
@ApiModelProperty("设备名称")
private String deviceName;
/** 设备编号 */
@Excel(name = "设备编号")
@ApiModelProperty("设备编号")
private String serialNumber;
/** 模式(1=影子模式2=在线模式3=其他) */
@Excel(name = "模式(1=影子模式2=在线模式3=其他)")
@ApiModelProperty("模式(1=影子模式2=在线模式3=其他)")
private Integer mode;
/** 用户id */
@Excel(name = "用户id")
@ApiModelProperty("用户id")
private Long userId;
/** 下发结果描述 */
@Excel(name = "下发结果描述")
@ApiModelProperty("下发结果描述")
private String resultMsg;
/** 下发结果代码 */
@Excel(name = "下发结果代码")
@ApiModelProperty("下发结果代码")
private Integer resultCode;
/** 创建者 */
@Excel(name = "创建者")
@ApiModelProperty("创建者")
private String createBy;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("创建时间")
@Excel(name = "创建时间")
private Date createTime;
/** 备注 */
@Excel(name = "备注")
@ApiModelProperty("备注")
private String remark;
/** 显示值 */
@Excel(name = "显示值")
@ApiModelProperty("显示值")
private String showValue;
/** 物模型名称 */
@Excel(name = "物模型名称")
@ApiModelProperty("物模型名称")
private String modelName;
/** 设备回复时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("设备回复时间")
@Excel(name = "设备回复时间")
private Date replyTime;
/** 仅用于查询时筛选条件 */
private String prefixIdentify;
private String dataType;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date beginTime;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
private List<Long> list;
private List<String> identifyList;
}

View File

@@ -0,0 +1,19 @@
package com.fastbee.iot.model;
import lombok.Data;
/**
* @author fastb
* @version 1.0
* @description: 统计物模型日志数量
* @date 2024-06-17 11:36
*/
@Data
public class ThingsModelLogCountVO {
private String identifier;
private String modelName;
private Integer counts;
}

View File

@@ -0,0 +1,99 @@
package com.fastbee.iot.model.dto;
import com.fastbee.common.core.domain.BaseEntity;
import com.fastbee.iot.model.ThingsModelItem.Datatype;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 物模型对象 iot_things_model
*
* @author kerwincui
* @date 2023-01-14
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class ThingsModelDTO extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 物模型ID */
private Long modelId;
/** 物模型名称 */
private String modelName;
/** 产品ID */
private Long productId;
/** 产品名称 */
private String productName;
/** 租户ID */
private Long tenantId;
/** 租户名称 */
private String tenantName;
/** 标识符,产品下唯一 */
private String identifier;
/** 模型类别1-属性2-功能3-事件) */
private Integer type;
/** 计算公式 */
private String formula;
/** 数据定义 */
private String specs;
/** 是否图表显示0-否1-是) */
private Integer isChart;
/** 是否历史存储0-否1-是) */
private Integer isHistory;
/** 是否实时监测0-否1-是) */
private Integer isMonitor;
/** 是否分享设备权限0-否1-是) */
private Integer isSharePerm;
/** 是否分享设备权限0-否1-是) */
private Integer isApp;
private String unit;
/** 数据定义 */
private Datatype datatype;
private String limitValue;
/** 是否只读数据(0-否1-是) */
private Integer isReadonly;
private Integer modelOrder;
private String value = "";
private String ts;
private String serialNumber;
private Boolean isShadow;
/**
* 是否可下发指令 0 -不可操作 1-可
*/
private Integer canSend;
private String shadow = "";
/**
* 所属设备名称
*/
private String deviceName;
private String address;
}

View File

@@ -0,0 +1,36 @@
package com.fastbee.iot.service;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.iot.model.DeviceHistoryParam;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import java.util.List;
/**
* 数据中心服务类
* @author fastb
* @date 2024-06-13 15:29
* @version 1.0
*/
public interface DataCenterService {
/**
* 查询设备物模型的历史数据
* @param deviceHistoryParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<JSONObject> deviceHistory(DeviceHistoryParam deviceHistoryParam);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam);
List<HistoryModel> queryDeviceHistory(DeviceHistoryParam deviceHistoryParam);
}

View File

@@ -1,8 +1,10 @@
package com.fastbee.iot.service;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import java.util.List;
import java.util.Map;
@@ -40,4 +42,18 @@ public interface IDeviceLogService
*/
public int insertDeviceLog(DeviceLog deviceLog);
/**
* 查询物模型历史数据
* @param deviceLog 设备日志
* @return java.util.List<com.fastbee.iot.model.HistoryModel>
*/
List<HistoryModel> listHistory(DeviceLog deviceLog);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam);
}

View File

@@ -5,9 +5,11 @@ import com.fastbee.common.core.thingsModel.ThingsModelSimpleItem;
import com.fastbee.common.enums.DeviceStatus;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.model.*;
import com.fastbee.iot.model.ThingsModelItem.ThingsModel;
import com.fastbee.iot.model.ThingsModels.ThingsModelShadow;
import com.fastbee.iot.model.ThingsModels.ThingsModelValueItem;
import com.fastbee.common.core.thingsModel.ThingsModelValuesInput;
import com.fastbee.iot.model.dto.ThingsModelDTO;
import org.quartz.SchedulerException;
import java.util.List;
@@ -273,4 +275,6 @@ public interface IDeviceService
* @return
*/
public String[] getDeviceNumsByProductId(Long productId);
List<ThingsModel> listThingsModel(Long deviceId);
}

View File

@@ -2,6 +2,10 @@ package com.fastbee.iot.service;
import java.util.List;
import com.fastbee.iot.domain.FunctionLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.FunctionLogVO;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
/**
* 设备服务下发日志Service接口
@@ -84,4 +88,18 @@ public interface IFunctionLogService
* @param log
*/
public void updateByMessageId(FunctionLog log);
/**
* 查询物模型历史数据
* @param functionLogVO 功能日志
* @return java.util.List<com.fastbee.iot.model.HistoryModel>
*/
List<HistoryModel> listHistory(FunctionLogVO functionLogVO);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam);
}

View File

@@ -0,0 +1,145 @@
package com.fastbee.iot.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.*;
import com.fastbee.iot.model.ThingsModelItem.ThingsModel;
import com.fastbee.iot.service.*;
import com.fastbee.iot.tsdb.service.ILogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author fastb
* @version 1.0
* @description: 数据中心服务类
* @date 2024-06-13 15:29
*/
@Service
public class DataCenterServiceImpl implements DataCenterService {
@Resource
private IDeviceLogService deviceLogService;
@Resource
private IFunctionLogService functionLogService;
@Resource
private ILogService logService;
@Resource
private IDeviceService deviceService;
@Override
public List<JSONObject> deviceHistory(DeviceHistoryParam deviceHistoryParam) {
List<JSONObject> resultList = new ArrayList<>();
List<DeviceHistoryParam.IdentifierVO> identifierVOList = new ArrayList<>();
if (CollectionUtils.isEmpty(deviceHistoryParam.getIdentifierList())) {
List<ThingsModel> list = deviceService.listThingsModel(deviceHistoryParam.getDeviceId());
for (ThingsModel thingsModel : list) {
DeviceHistoryParam.IdentifierVO identifierVO = new DeviceHistoryParam.IdentifierVO();
identifierVO.setIdentifier(thingsModel.getId());
identifierVO.setType(thingsModel.getType());
identifierVOList.add(identifierVO);
}
} else {
identifierVOList = deviceHistoryParam.getIdentifierList();
}
if (CollectionUtils.isEmpty(identifierVOList)) {
return resultList;
}
List<String> identifierList = identifierVOList.stream().map(DeviceHistoryParam.IdentifierVO::getIdentifier).collect(Collectors.toList());
deviceHistoryParam.setIdentifierList(identifierVOList);
List<HistoryModel> historyModelList = this.queryDeviceHistory(deviceHistoryParam);
historyModelList.sort(Comparator.comparing(HistoryModel::getTime));
return this.handleData(identifierList, historyModelList);
}
@Override
public List<HistoryModel> queryDeviceHistory(DeviceHistoryParam deviceHistoryParam) {
List<HistoryModel> historyModelList = new ArrayList<>();
List<DeviceHistoryParam.IdentifierVO> identifierVOList = deviceHistoryParam.getIdentifierList();
List<String> propertyIdentifierList = identifierVOList.stream().filter(t -> 1 == t.getType()).map(DeviceHistoryParam.IdentifierVO::getIdentifier).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(propertyIdentifierList)) {
DeviceLog deviceLog = new DeviceLog();
deviceLog.setIdentityList(propertyIdentifierList);
deviceLog.setSerialNumber(deviceHistoryParam.getSerialNumber());
deviceLog.setBeginTime(deviceHistoryParam.getBeginTime());
deviceLog.setEndTime(deviceHistoryParam.getEndTime());
List<HistoryModel> historyModelList1 = deviceLogService.listHistory(deviceLog);
historyModelList.addAll(historyModelList1);
}
List<String> functionIdentifierList = identifierVOList.stream().filter(t -> 2 == t.getType()).map(DeviceHistoryParam.IdentifierVO::getIdentifier).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(functionIdentifierList)) {
FunctionLogVO functionLogVO = new FunctionLogVO();
functionLogVO.setIdentifyList(functionIdentifierList);
functionLogVO.setSerialNumber(deviceHistoryParam.getSerialNumber());
functionLogVO.setBeginTime(DateUtils.dateTime(DateUtils.YY_MM_DD_HH_MM_SS, deviceHistoryParam.getBeginTime()));
functionLogVO.setEndTime(DateUtils.dateTime(DateUtils.YY_MM_DD_HH_MM_SS, deviceHistoryParam.getEndTime()));
historyModelList.addAll(functionLogService.listHistory(functionLogVO));
}
List<String> eventIdentifierList = identifierVOList.stream().filter(t -> 3 == t.getType()).map(DeviceHistoryParam.IdentifierVO::getIdentifier).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(eventIdentifierList)) {
DeviceLog eventLog = new DeviceLog();
eventLog.setIdentityList(eventIdentifierList);
eventLog.setSerialNumber(deviceHistoryParam.getSerialNumber());
eventLog.setBeginTime(deviceHistoryParam.getBeginTime());
eventLog.setEndTime(deviceHistoryParam.getEndTime());
historyModelList.addAll(logService.listHistory(eventLog));
}
return historyModelList;
}
private List<JSONObject> handleData(List<String> identifierList, List<HistoryModel> historyModelList) {
List<JSONObject> resultList = new ArrayList<>();
if (CollectionUtils.isEmpty(historyModelList)) {
return resultList;
}
LinkedHashMap<Date, List<HistoryModel>> map = historyModelList.stream()
.collect(Collectors.groupingBy(HistoryModel::getTime, LinkedHashMap::new, Collectors.toList()));
// Map<String, HistoryModel> oldHistoryModelMap = new HashMap<>(2);
for (Map.Entry<Date, List<HistoryModel>> entry : map.entrySet()) {
JSONObject jsonObject = new JSONObject();
List<HistoryModel> value = entry.getValue();
Map<String, HistoryModel> historyModelMap = value.stream().collect(Collectors.toMap(HistoryModel::getIdentify, Function.identity(), (o, n) -> n));
List<JSONObject> jsonObjectList = new ArrayList<>();
for (String identifier : identifierList) {
JSONObject jsonObject1 = new JSONObject();
HistoryModel historyModel = historyModelMap.get(identifier);
if (null != historyModel) {
// oldHistoryModelMap.put(identifier, historyModel);
jsonObject1.put(historyModel.getIdentify(), historyModel.getValue());
} else {
// HistoryModel oldHistoryModel = oldHistoryModelMap.get(identifier);
// if (null != oldHistoryModel) {
// jsonObject1.put(identifier, oldHistoryModel.getValue());
// } else {
// jsonObject1.put(identifier, null);
// }
jsonObject1.put(identifier, null);
}
jsonObjectList.add(jsonObject1);
}
jsonObject.put(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, entry.getKey()), jsonObjectList);
resultList.add(jsonObject);
}
return resultList;
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
List<ThingsModelLogCountVO> resultList = new ArrayList<>();
resultList.addAll(deviceLogService.countThingsModelInvoke(dataCenterParam));
resultList.addAll(functionLogService.countThingsModelInvoke(dataCenterParam));
return resultList;
}
}

View File

@@ -4,6 +4,9 @@ import com.fastbee.common.core.domain.model.LoginUser;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.SecurityUtils;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import com.fastbee.iot.tsdb.service.ILogService;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.service.IDeviceLogService;
@@ -67,4 +70,14 @@ public class DeviceLogServiceImpl implements IDeviceLogService
deviceLog.setCreateBy(loginUser.getUsername());
return logService.saveDeviceLog(deviceLog);
}
@Override
public List<HistoryModel> listHistory(DeviceLog deviceLog) {
return logService.listHistory(deviceLog);
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
return logService.countThingsModelInvoke(dataCenterParam);
}
}

View File

@@ -34,6 +34,7 @@ import com.fastbee.iot.model.ThingsModels.PropertyDto;
import com.fastbee.iot.model.ThingsModels.ThingsModelShadow;
import com.fastbee.iot.model.ThingsModels.ThingsModelValueItem;
import com.fastbee.iot.model.ThingsModels.ValueItem;
import com.fastbee.iot.model.dto.ThingsModelDTO;
import com.fastbee.iot.service.*;
import com.fastbee.iot.service.cache.IDeviceCache;
import com.fastbee.iot.tsdb.service.ILogService;
@@ -52,6 +53,7 @@ import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.fastbee.common.utils.SecurityUtils.getLoginUser;
@@ -1413,4 +1415,22 @@ public class DeviceServiceImpl implements IDeviceService {
return deviceMapper.getDeviceNumsByProductId(productId);
}
@Override
public List<ThingsModel> listThingsModel(Long deviceId) {
Device device = deviceMapper.selectDeviceByDeviceId(deviceId);
JSONObject thingsModelObject = JSONObject.parseObject(thingsModelService.getCacheThingsModelByProductId(device.getProductId()));
JSONArray properties = thingsModelObject.getJSONArray("properties");
JSONArray functions = thingsModelObject.getJSONArray("functions");
List<ThingsModelValueItem> thingsModelValueItems = getCacheDeviceStatus(device.getProductId(), device.getSerialNumber());
// 物模型转换赋值
List<ThingsModel> thingsList = new ArrayList<>();
if (!CollectionUtils.isEmpty(properties)) {
thingsList.addAll(convertJsonToThingsList(properties, thingsModelValueItems, 1));
}
if (!CollectionUtils.isEmpty(functions)) {
thingsList.addAll(convertJsonToThingsList(functions, thingsModelValueItems, 2));
}
return thingsList;
}
}

View File

@@ -1,7 +1,14 @@
package com.fastbee.iot.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.model.DataCenterParam;
import com.fastbee.iot.model.FunctionLogVO;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.iot.mapper.FunctionLogMapper;
@@ -131,4 +138,29 @@ public class FunctionLogServiceImpl implements IFunctionLogService
public void updateByMessageId(FunctionLog log){
functionLogMapper.updateByMessageId(log);
}
@Override
public List<HistoryModel> listHistory(FunctionLogVO functionLogVO) {
return functionLogMapper.listHistory(functionLogVO);
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
Date beginTime = null;
Date endTime = null;
if (dataCenterParam.getBeginTime() != null && dataCenterParam.getBeginTime() != "" && dataCenterParam.getEndTime() != null && dataCenterParam.getEndTime() != "") {
beginTime = parseTime(dataCenterParam.getBeginTime());
endTime = parseTime(dataCenterParam.getEndTime());
}
return functionLogMapper.countThingsModelInvoke(dataCenterParam, beginTime, endTime);
}
private Date parseTime(String time) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(time);
} catch (ParseException e) {
throw new IllegalArgumentException("时间格式错误: " + time, e);
}
}
}

View File

@@ -3,8 +3,7 @@ package com.fastbee.iot.tsdb.service;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.*;
import com.fastbee.iot.tsdb.model.TdLogDto;
import java.util.List;
@@ -40,5 +39,17 @@ public interface ILogService {
/** 查询物模型日志列表 **/
List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog);
/**
* 查询物模型历史数据
* @param deviceLog 设备日志
* @return java.util.List<com.fastbee.iot.model.HistoryModel>
*/
List<HistoryModel> listHistory(DeviceLog deviceLog);
/**
* 统计设备物模型指令下发数量
* @param dataCenterParam 传参
* @return com.fastbee.common.core.domain.AjaxResult
*/
List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam);
}

View File

@@ -4,9 +4,7 @@ import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.*;
import com.fastbee.iot.tsdb.config.InfluxConfig;
import com.fastbee.iot.tsdb.service.ILogService;
import com.fastbee.iot.tsdb.model.TdLogDto;
@@ -511,4 +509,169 @@ public class InfluxLogService implements ILogService {
return monitorList;
}
@Override
public List<HistoryModel> listHistory(DeviceLog deviceLog) {
QueryApi queryApi = influxDBClient.getQueryApi();
StringBuilder fluxQuery = new StringBuilder();
fluxQuery.append("from(bucket: \"").append(influxConfig.getBucket()).append("\") ");
// 处理时间范围
if (deviceLog.getBeginTime() != null && !deviceLog.getBeginTime().isEmpty()
&& deviceLog.getEndTime() != null && !deviceLog.getEndTime().isEmpty()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date beginDate = sdf.parse(deviceLog.getBeginTime());
Date endDate = sdf.parse(deviceLog.getEndTime());
// 转换为RFC3339格式时间字符串
String startRFC3339 = beginDate.toInstant().toString();
String stopRFC3339 = endDate.toInstant().toString();
fluxQuery.append("|> range(start: ")
.append(startRFC3339)
.append(", stop: ")
.append(stopRFC3339)
.append(") ");
} catch (ParseException e) {
e.printStackTrace();
// 若解析失败,可使用默认时间范围
fluxQuery.append("|> range(start: 0) ");
}
} else {
fluxQuery.append("|> range(start: 0) ");
}
fluxQuery.append("|> filter(fn: (r) => r._measurement == \"").append(influxConfig.getMeasurement()).append("\") ");
fluxQuery.append("|> pivot(\n" +
" rowKey:[\"_time\"], \n" +
" columnKey: [\"_field\"], \n" +
" valueColumn: \"_value\"\n" +
" )");
List<String> filterConditions = new ArrayList<>();
if (deviceLog.getSerialNumber() != null && !deviceLog.getSerialNumber().isEmpty()) {
filterConditions.add("r.serialNumber == \"" + deviceLog.getSerialNumber() + "\"");
}
if (deviceLog.getIdentityList() != null && !deviceLog.getIdentityList().isEmpty()) {
StringBuilder identityFilter = new StringBuilder("r.identify =~ /^(");
for (int i = 0; i < deviceLog.getIdentityList().size(); i++) {
if (i > 0) {
identityFilter.append("|");
}
identityFilter.append(deviceLog.getIdentityList().get(i));
}
identityFilter.append(")$/");
filterConditions.add(identityFilter.toString());
}
if (deviceLog.getLogType() != null) {
filterConditions.add("r.logType == " + deviceLog.getLogType());
}
if (!filterConditions.isEmpty()) {
fluxQuery.append("|> filter(fn: (r) => ");
for (int i = 0; i < filterConditions.size(); i++) {
if (i > 0) {
fluxQuery.append(" and ");
}
fluxQuery.append(filterConditions.get(i));
}
fluxQuery.append(") ");
}
fluxQuery.append("|> sort(columns: [\"_time\"], desc: true) ");
// 分页处理
int offset = (deviceLog.getPageNum() - 1) * deviceLog.getPageSize();
fluxQuery.append("|> limit(n: ").append(deviceLog.getPageSize()).append(", offset: ").append(offset).append(") ");
fluxQuery.append("|> keep(columns: [\"logValue\", \"_time\", \"identify\"]) ");
List<FluxTable> tables = queryApi.query(fluxQuery.toString());
List<HistoryModel> historyList = new ArrayList<>();
for (FluxTable table : tables) {
for (FluxRecord record : table.getRecords()) {
HistoryModel historyModel = new HistoryModel();
historyModel.setValue((String) record.getValueByKey("logValue"));
historyModel.setTime(new Date(record.getTime().getEpochSecond() * 1000));
historyModel.setIdentify((String) record.getValueByKey("identify"));
historyList.add(historyModel);
}
}
return historyList;
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
QueryApi queryApi = influxDBClient.getQueryApi();
StringBuilder fluxQuery = new StringBuilder();
fluxQuery.append("from(bucket: \"").append(influxConfig.getBucket()).append("\") ");
// 处理时间范围
if (dataCenterParam.getBeginTime() != null && !dataCenterParam.getBeginTime().isEmpty()
&& dataCenterParam.getEndTime() != null && !dataCenterParam.getEndTime().isEmpty()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date beginDate = sdf.parse(dataCenterParam.getBeginTime());
Date endDate = sdf.parse(dataCenterParam.getEndTime());
// 转换为RFC3339格式时间字符串
String startRFC3339 = beginDate.toInstant().toString();
String stopRFC3339 = endDate.toInstant().toString();
fluxQuery.append("|> range(start: ")
.append(startRFC3339)
.append(", stop: ")
.append(stopRFC3339)
.append(") ");
} catch (ParseException e) {
e.printStackTrace();
// 若解析失败,可使用默认时间范围
fluxQuery.append("|> range(start: 0) ");
}
} else {
fluxQuery.append("|> range(start: 0) ");
}
fluxQuery.append("|> filter(fn: (r) => r._measurement == \"").append(influxConfig.getMeasurement()).append("\") ");
fluxQuery.append("|> pivot(\n" +
" rowKey:[\"_time\"], \n" +
" columnKey: [\"_field\"], \n" +
" valueColumn: \"_value\"\n" +
" )");
fluxQuery.append("|> filter(fn: (r) => r.log_type == 2) ");
List<String> filterConditions = new ArrayList<>();
if (dataCenterParam.getSerialNumber() != null && !dataCenterParam.getSerialNumber().isEmpty()) {
filterConditions.add("r.serial_number == \"" + dataCenterParam.getSerialNumber() + "\"");
}
if (!filterConditions.isEmpty()) {
fluxQuery.append("|> filter(fn: (r) => ");
for (int i = 0; i < filterConditions.size(); i++) {
if (i > 0) {
fluxQuery.append(" and ");
}
fluxQuery.append(filterConditions.get(i));
}
fluxQuery.append(") ");
}
fluxQuery.append("|> group() ");
int counts = 0;
List<FluxTable> tables = queryApi.query(fluxQuery.toString());
if (!tables.isEmpty() && !tables.get(0).getRecords().isEmpty()) {
counts = tables.get(0).getRecords().size();
}
List<ThingsModelLogCountVO> resultList = new ArrayList<>();
for (FluxTable table : tables) {
for (FluxRecord record : table.getRecords()) {
ThingsModelLogCountVO vo = new ThingsModelLogCountVO();
vo.setIdentifier((String) record.getValueByKey("identify"));
vo.setCounts(counts);
resultList.add(vo);
}
}
return resultList;
}
}

View File

@@ -5,9 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.mapper.IotDbLogMapper;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.model.*;
import com.fastbee.iot.tsdb.service.ILogService;
import com.fastbee.iot.tsdb.model.TdLogDto;
import lombok.extern.slf4j.Slf4j;
@@ -117,4 +115,16 @@ public class IotDbLogService implements ILogService {
return iotDbLogMapper.selectMonitorList(deviceLog);
}
@Override
public List<HistoryModel> listHistory(DeviceLog deviceLog) {
return iotDbLogMapper.listHistory(deviceLog);
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
List<ThingsModelLogCountVO> thingsModelLogCountVOS = iotDbLogMapper.countThingsModelInvoke(dataCenterParam);
thingsModelLogCountVOS.forEach(vo -> {vo.setCounts(thingsModelLogCountVOS.size());});
return thingsModelLogCountVOS;
}
}

View File

@@ -5,14 +5,16 @@ import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.domain.EventLog;
import com.fastbee.iot.mapper.EventLogMapper;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.*;
import com.fastbee.iot.tsdb.model.TdLogDto;
import com.fastbee.iot.tsdb.service.ILogService;
import com.fastbee.iot.mapper.DeviceLogMapper;
import com.fastbee.iot.model.MonitorModel;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
@@ -106,4 +108,29 @@ public class MySqlLogServiceImpl implements ILogService {
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
return deviceLogMapper.selectDeviceLogList(deviceLog);
}
@Override
public List<HistoryModel> listHistory(DeviceLog deviceLog) {
return deviceLogMapper.listHistory(deviceLog);
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
Date beginTime = null;
Date endTime = null;
if (dataCenterParam.getBeginTime() != null && dataCenterParam.getEndTime() != null) {
beginTime = parseTime(dataCenterParam.getBeginTime());
endTime = parseTime(dataCenterParam.getEndTime());
}
return deviceLogMapper.countThingsModelInvoke(dataCenterParam, beginTime, endTime);
}
private Date parseTime(String time) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(time);
} catch (ParseException e) {
throw new IllegalArgumentException("时间格式错误: " + time, e);
}
}
}

View File

@@ -1,11 +1,11 @@
package com.fastbee.iot.tsdb.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.*;
import com.fastbee.iot.tsdb.service.ILogService;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.mapper.TDDeviceLogMapper;
import com.fastbee.iot.tsdb.model.TdLogDto;
import com.fastbee.iot.util.SnowflakeIdWorker;
@@ -123,4 +123,18 @@ public class TdengineLogServiceImpl implements ILogService {
return tDDeviceLogMapper.deleteDeviceLogByDeviceNumber(dbName, deviceNumber);
}
@Override
public List<HistoryModel> listHistory(DeviceLog deviceLog) {
List<HistoryModel> historyModelList = tDDeviceLogMapper.listHistory(dbName, deviceLog);
for (HistoryModel historyModel : historyModelList) {
historyModel.setTime(DateUtils.dateRemoveMs(historyModel.getTime()));
}
return historyModelList;
}
@Override
public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
return tDDeviceLogMapper.countThingsModelInvoke(dbName, dataCenterParam);
}
}

View File

@@ -194,4 +194,42 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by create_time desc
</select>
<select id="listHistory" parameterType="com.fastbee.iot.domain.DeviceLog" resultMap="HistoryResult">
select log_value,
create_time,
identify
from iot_device_log
<where>
<if test="serialNumber != null and serialNumber != ''">
and serial_number = #{serialNumber}
</if>
<if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
and create_time between #{beginTime} and #{endTime}
</if>
<if test="identityList != null and identityList != ''">
and identify in
<foreach collection="identityList" item="identify" open="(" separator="," close=")">
#{identify}
</foreach>
</if>
<if test="logType != null">
and log_type = #{logType}
</if>
</where>
order by create_time desc
</select>
<select id="countThingsModelInvoke" resultType="com.fastbee.iot.model.ThingsModelLogCountVO">
select identify identifier, min(model_name) modelName, count(identify) counts
from iot_device_log
where log_type = 2
<if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
and serial_number = #{dataCenterParam.serialNumber}
</if>
<if test="beginTime != null and endTime != null ">
and create_time between #{beginTime} and #{endTime}
</if>
group by identify
</select>
</mapper>

View File

@@ -182,4 +182,40 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</trim>
where message_id = #{messageId}
</update>
<select id="listHistory" parameterType="com.fastbee.iot.domain.FunctionLog" resultType="com.fastbee.iot.model.HistoryModel">
select create_time as time,
fun_value as value,
identify as identify,
model_name as moderName
from iot_function_log
<where>
<if test="serialNumber != null and serialNumber != ''">
and serial_number = #{serialNumber}
</if>
<if test="beginTime != null and endTime != null">
and create_time between #{beginTime} and #{endTime}
</if>
<if test="identifyList != null and identifyList != ''">
and identify in
<foreach collection="identifyList" item="identify" open="(" separator="," close=")">
#{identify}
</foreach>
</if>
</where>
order by create_time desc
</select>
<select id="countThingsModelInvoke" resultType="com.fastbee.iot.model.ThingsModelLogCountVO">
select identify identifier, min(model_name) modelName, count(identify) counts
from iot_function_log
where fun_type = 1
<if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
and serial_number = #{dataCenterParam.serialNumber}
</if>
<if test="beginTime != null and endTime != null ">
and create_time between #{beginTime} and #{endTime}
</if>
group by identify
</select>
</mapper>

View File

@@ -35,9 +35,9 @@
<!-- <result property="identify" column="root.ln.device_log.identify" />-->
<!-- </resultMap>-->
<!-- <resultMap type="com.fastbee.iot.model.vo.ThingsModelLogCountVO" id="ThingsModelLogCountVO">-->
<!-- <result property="identifier" column="root.ln.device_log.identify" />-->
<!-- </resultMap>-->
<resultMap type="com.fastbee.iot.model.ThingsModelLogCountVO" id="ThingsModelLogCountVO">
<result property="identifier" column="root.ln.device_log.identify" />
</resultMap>
<update id="createDB">
create database ${database}
@@ -177,6 +177,40 @@
order by time desc
</select>
<select id="listHistory" parameterType="com.fastbee.iot.domain.DeviceLog" resultMap="HistoryResult">
select log_value,
identify
FROM root.ln.device_log
<where>
<if test="device.serialNumber != null and device.serialNumber != ''">
and serial_number = '${device.serialNumber}'
</if>
<if test="device.beginTime != null and device.beginTime != '' and device.endTime != null and device.endTime != ''">
and time <![CDATA[ >= ]]> ${device.beginTime} and time <![CDATA[ <= ]]> ${device.endTime}
</if>
<if test="device.identityList != null and device.identityList.size > 0">
and identify in
<foreach collection="device.identityList" item="identify" open="(" separator="," close=")">
'${identify}'
</foreach>
</if>
<if test="device.logType != null">
and log_type = #{device.logType}
</if>
</where>
order by time desc
</select>
<select id="countThingsModelInvoke" resultMap="ThingsModelLogCountVO">
select identify
FROM root.ln.device_log
where log_type = 2
<if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
and serial_number = '${dataCenterParam.serialNumber}'
</if>
<if test="dataCenterParam.beginTime != null and dataCenterParam.beginTime != '' and dataCenterParam.endTime != null and dataCenterParam.endTime != ''">
and time <![CDATA[ >= ]]> ${dataCenterParam.beginTime} and time <![CDATA[ <= ]]> ${dataCenterParam.endTime}
</if>
</select>
</mapper>

View File

@@ -27,12 +27,11 @@
<result property="remark" column="remark" />
</resultMap>
<!-- <resultMap type="com.fastbee.iot.model.HistoryModel" id="HistoryResult">-->
<!-- <result property="time" column="ts" />-->
<!-- <result property="value" column="log_value" />-->
<!-- <result property="identify" column="identify" />-->
<!-- <result property="moderName" column="mode" />-->
<!-- </resultMap>-->
<resultMap type="com.fastbee.iot.model.HistoryModel" id="HistoryResult">
<result property="time" column="ts" />
<result property="value" column="log_value" />
<result property="identify" column="identify" />
</resultMap>
<!-- <resultMap type="com.fastbee.iot.model.HistoryBo" id="HistoryResultBo">-->
<!-- <result property="value" column="log_value" />-->
@@ -173,4 +172,42 @@
order by ts desc
</select>
<select id="listHistory" parameterType="com.fastbee.iot.domain.DeviceLog" resultMap="HistoryResult">
select ts,
log_value,
identify
from ${database}.device_log
<where>
<if test="device.beginTime != null and device.beginTime != '' and device.endTime != null and device.endTime != ''">
and ts between #{device.beginTime} and #{device.endTime}
</if>
<if test="device.serialNumber != null and device.serialNumber !=''">
and serial_number = #{device.serialNumber}
</if>
<if test="device.identityList != null and device.identityList.size > 0">
and identify in
<foreach collection="device.identityList" item="identify" open="(" separator="," close=")">
#{identify}
</foreach>
</if>
<if test="device.logType != null">
and log_type = #{device.logType}
</if>
</where>
order by ts desc
</select>
<select id="countThingsModelInvoke" resultType="com.fastbee.iot.model.ThingsModelLogCountVO">
select identify identifier, count(identify) counts
from ${database}.device_log
where log_type = 2
<if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
and serial_number = #{dataCenterParam.serialNumber}
</if>
<if test="dataCenterParam.beginTime != null and dataCenterParam.beginTime != '' and dataCenterParam.endTime != null and dataCenterParam.endTime != ''">
and ts between #{dataCenterParam.beginTime} and #{dataCenterParam.endTime}
</if>
group by identify
</select>
</mapper>