mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善环保设备监控
This commit is contained in:
@@ -19,7 +19,12 @@ public class LocalDateTImeConverter implements Converter<String, LocalDateTime>
|
||||
|
||||
return LocalDateTime.parse(source);
|
||||
} catch (Exception e) {
|
||||
try{
|
||||
|
||||
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
}catch (Exception exception){
|
||||
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,5 +35,8 @@ public class CreateEmDeviceDto extends OrgCommonDto {
|
||||
private String location;
|
||||
|
||||
private List<EmDeviceItemConfig> itemConfig;
|
||||
|
||||
@Comment("采集间隔(分钟)")
|
||||
private Integer interval;
|
||||
|
||||
}
|
||||
|
||||
@@ -31,4 +31,7 @@ public class EmDeviceDto extends OrgCommonDto {
|
||||
private String archiveStatusName;
|
||||
|
||||
private List<EmDeviceItemConfig> itemConfig;
|
||||
@Comment("采集间隔(分钟)")
|
||||
private Integer interval;
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,7 @@ public class UpdateEmDeviceDto extends OrgCommonDto {
|
||||
private String location;
|
||||
|
||||
private List<EmDeviceItemConfig> itemConfig;
|
||||
@Comment("采集间隔(分钟)")
|
||||
private Integer interval;
|
||||
|
||||
}
|
||||
|
||||
@@ -54,4 +54,8 @@ public class EmDeviceEntity extends OrgCommonEntity {
|
||||
|
||||
@ElementCollection
|
||||
private List<EmDeviceItemConfig> itemConfig;
|
||||
|
||||
|
||||
@Comment("采集间隔(分钟)")
|
||||
private Integer interval;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,18 @@ package cn.lihongjie.coal.emDevice.repository;
|
||||
import cn.lihongjie.coal.base.dao.BaseRepository;
|
||||
import cn.lihongjie.coal.emDevice.entity.EmDeviceEntity;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface EmDeviceRepository extends BaseRepository<EmDeviceEntity> {}
|
||||
public interface EmDeviceRepository extends BaseRepository<EmDeviceEntity> {
|
||||
@Query(
|
||||
value =
|
||||
"select d.id\n"
|
||||
+ "from t_em_device d\n"
|
||||
+ "where (select max(time) from t_em_device_data data where data.device_id = d.id) + (d.\"interval\" * interval '1 minute') >= now()",
|
||||
nativeQuery = true)
|
||||
List<String> findNeedToFetchDevice();
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ import cn.lihongjie.coal.emDeviceData.mapper.EmDeviceDataMapper;
|
||||
import cn.lihongjie.coal.emDeviceData.service.EmDeviceDataService;
|
||||
import cn.lihongjie.coal.exception.BizException;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
@@ -137,14 +140,13 @@ public class EmDeviceService extends BaseService<EmDeviceEntity, EmDeviceReposit
|
||||
|
||||
@Autowired EmDeviceDataService emDeviceDataService;
|
||||
|
||||
@Autowired
|
||||
EmDeviceDataMapper emDeviceDataMapper;
|
||||
|
||||
@Autowired EmDeviceDataMapper emDeviceDataMapper;
|
||||
|
||||
@PersistenceContext private EntityManager em;
|
||||
|
||||
public void fetchHisData(FetchHisDataRequest request) {
|
||||
|
||||
if (CollectionUtils.isEmpty(request.getIds())){
|
||||
if (CollectionUtils.isEmpty(request.getIds())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,16 +158,27 @@ public class EmDeviceService extends BaseService<EmDeviceEntity, EmDeviceReposit
|
||||
.filter(x -> StringUtils.isNotEmpty(x.getCode()))
|
||||
.collect(Collectors.groupingBy(d -> d.getThirdAccount().getId()));
|
||||
|
||||
Map<String, EmDeviceEntity> codeMap = all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
Map<String, EmDeviceEntity> codeMap =
|
||||
all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
|
||||
for (List<EmDeviceEntity> devices : groupByAccount.values()) {
|
||||
|
||||
DustApi.GetRealtimeDataResponse hisData;
|
||||
String deviceIds = devices.stream().map(CommonEntity::getCode).collect(Collectors.joining(","));
|
||||
String deviceIds =
|
||||
devices.stream().map(CommonEntity::getCode).collect(Collectors.joining(","));
|
||||
|
||||
try {
|
||||
|
||||
hisData = dustApi.getHistoryData(devices.get(0).getThirdAccount().getToken(), deviceIds, request.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), request.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
hisData =
|
||||
dustApi.getHistoryData(
|
||||
devices.get(0).getThirdAccount().getToken(),
|
||||
deviceIds,
|
||||
request.getStartTime()
|
||||
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
|
||||
request.getEndTime()
|
||||
.format(
|
||||
DateTimeFormatter.ofPattern(
|
||||
"yyyy-MM-dd HH:mm:ss")));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("get his data error {} ", deviceIds, e);
|
||||
@@ -195,107 +208,148 @@ public class EmDeviceService extends BaseService<EmDeviceEntity, EmDeviceReposit
|
||||
|
||||
if (data.size() != devices.size()) {
|
||||
|
||||
List<String> realTimeCode = data.stream().map(DustApi.DeviceData::deviceId).collect(Collectors.toList());
|
||||
List<String> deviceCode = devices.stream().map(CommonEntity::getCode).collect(Collectors.toList());
|
||||
List<String> realTimeCode =
|
||||
data.stream()
|
||||
.map(DustApi.DeviceData::deviceId)
|
||||
.collect(Collectors.toList());
|
||||
List<String> deviceCode =
|
||||
devices.stream().map(CommonEntity::getCode).collect(Collectors.toList());
|
||||
|
||||
log.info("device code not match {} - {} = {}", deviceCode, realTimeCode, CollectionUtils.removeAll(deviceCode, realTimeCode));
|
||||
log.info(
|
||||
"device code not match {} - {} = {}",
|
||||
deviceCode,
|
||||
realTimeCode,
|
||||
CollectionUtils.removeAll(deviceCode, realTimeCode));
|
||||
}
|
||||
|
||||
List<EmDeviceDataEntity> collect = data.stream().map(x -> {
|
||||
List<EmDeviceDataEntity> collect =
|
||||
data.stream()
|
||||
.map(
|
||||
x -> {
|
||||
EmDeviceDataEntity emDeviceData = new EmDeviceDataEntity();
|
||||
emDeviceData.setDevice(
|
||||
em.getReference(
|
||||
EmDeviceEntity.class,
|
||||
codeMap.get(x.deviceId()).getId()));
|
||||
emDeviceData.setOrganizationId(
|
||||
codeMap.get(x.deviceId()).getOrganizationId());
|
||||
emDeviceData.setTime(LocalDateTime.now());
|
||||
emDeviceDataMapper.updateEntity(emDeviceData, x);
|
||||
|
||||
|
||||
EmDeviceDataEntity emDeviceData = new EmDeviceDataEntity();
|
||||
emDeviceData.setDeviceId(codeMap.get(x.deviceId()).getId());
|
||||
emDeviceData.setOrganizationId(codeMap.get(x.deviceId()).getOrganizationId());
|
||||
emDeviceData.setTime(LocalDateTime.now());
|
||||
emDeviceDataMapper.updateEntity(emDeviceData, x);
|
||||
|
||||
|
||||
return emDeviceData;
|
||||
|
||||
}).collect(Collectors.toList());
|
||||
return emDeviceData;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
emDeviceDataService.saveAll(collect);
|
||||
|
||||
log.info("get his data success {} ", deviceIds);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void queryRealtimeData() {
|
||||
|
||||
List<EmDeviceEntity> all = this.repository.findAll();
|
||||
try {
|
||||
|
||||
Map<String, List<EmDeviceEntity>> groupByAccount =
|
||||
all.stream()
|
||||
.filter(x -> x.getThirdAccount() != null)
|
||||
.filter(x -> StringUtils.isNotEmpty(x.getCode()))
|
||||
.collect(Collectors.groupingBy(d -> d.getThirdAccount().getId()));
|
||||
List<String> ids = this.repository.findNeedToFetchDevice();
|
||||
|
||||
Map<String, EmDeviceEntity> codeMap = all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
|
||||
for (List<EmDeviceEntity> devices : groupByAccount.values()) {
|
||||
|
||||
DustApi.GetRealtimeDataResponse realtimeData;
|
||||
String deviceIds = devices.stream().map(CommonEntity::getCode).collect(Collectors.joining(","));
|
||||
|
||||
try {
|
||||
|
||||
realtimeData = dustApi.getRealtimeData(devices.get(0).getThirdAccount().getToken() ,deviceIds);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("get realtime data error {} ", deviceIds, e);
|
||||
continue;
|
||||
if (CollectionUtils.isEmpty(ids)) {
|
||||
log.info("no need to fetch data");
|
||||
return;
|
||||
}
|
||||
var all = this.repository.findAllById(ids);
|
||||
|
||||
String code = realtimeData.code();
|
||||
if (ObjectUtils.notEqual(code, "1000")) {
|
||||
log.error(
|
||||
"get realtime data error {} {} {}",
|
||||
realtimeData.code(),
|
||||
realtimeData.message(),
|
||||
realtimeData.data());
|
||||
continue;
|
||||
Map<String, List<EmDeviceEntity>> groupByAccount =
|
||||
all.stream()
|
||||
.filter(x -> x.getThirdAccount() != null)
|
||||
.filter(x -> StringUtils.isNotEmpty(x.getCode()))
|
||||
.collect(Collectors.groupingBy(d -> d.getThirdAccount().getId()));
|
||||
|
||||
Map<String, EmDeviceEntity> codeMap =
|
||||
all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
|
||||
for (List<EmDeviceEntity> devices : groupByAccount.values()) {
|
||||
|
||||
DustApi.GetRealtimeDataResponse realtimeData;
|
||||
String deviceIds =
|
||||
devices.stream()
|
||||
.map(CommonEntity::getCode)
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
try {
|
||||
|
||||
realtimeData =
|
||||
dustApi.getRealtimeData(
|
||||
devices.get(0).getThirdAccount().getToken(), deviceIds);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("get realtime data error {} ", deviceIds, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
String code = realtimeData.code();
|
||||
if (ObjectUtils.notEqual(code, "1000")) {
|
||||
log.error(
|
||||
"get realtime data error {} {} {}",
|
||||
realtimeData.code(),
|
||||
realtimeData.message(),
|
||||
realtimeData.data());
|
||||
continue;
|
||||
}
|
||||
|
||||
List<DustApi.DeviceData> data = realtimeData.data();
|
||||
|
||||
if (CollectionUtils.isEmpty(data)) {
|
||||
log.error(
|
||||
"get realtime data empty {} {} {}",
|
||||
realtimeData.code(),
|
||||
realtimeData.message(),
|
||||
realtimeData.data());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data.size() != devices.size()) {
|
||||
|
||||
List<String> realTimeCode =
|
||||
data.stream()
|
||||
.map(DustApi.DeviceData::deviceId)
|
||||
.collect(Collectors.toList());
|
||||
List<String> deviceCode =
|
||||
devices.stream()
|
||||
.map(CommonEntity::getCode)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
log.info(
|
||||
"device code not match {} - {} = {}",
|
||||
deviceCode,
|
||||
realTimeCode,
|
||||
CollectionUtils.removeAll(deviceCode, realTimeCode));
|
||||
}
|
||||
|
||||
List<EmDeviceDataEntity> collect =
|
||||
data.stream()
|
||||
.map(
|
||||
x -> {
|
||||
EmDeviceDataEntity emDeviceData =
|
||||
new EmDeviceDataEntity();
|
||||
emDeviceData.setDevice(
|
||||
em.getReference(
|
||||
EmDeviceEntity.class,
|
||||
codeMap.get(x.deviceId()).getId()));
|
||||
emDeviceData.setOrganizationId(
|
||||
codeMap.get(x.deviceId()).getOrganizationId());
|
||||
emDeviceData.setTime(LocalDateTime.now());
|
||||
emDeviceDataMapper.updateEntity(emDeviceData, x);
|
||||
|
||||
return emDeviceData;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
emDeviceDataService.saveAll(collect);
|
||||
|
||||
log.info("get realtime data success {} ", deviceIds);
|
||||
}
|
||||
|
||||
List<DustApi.DeviceData> data = realtimeData.data();
|
||||
|
||||
if (CollectionUtils.isEmpty(data)) {
|
||||
log.error(
|
||||
"get realtime data empty {} {} {}",
|
||||
realtimeData.code(),
|
||||
realtimeData.message(),
|
||||
realtimeData.data());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data.size() != devices.size()){
|
||||
|
||||
List<String> realTimeCode = data.stream().map(DustApi.DeviceData::deviceId).collect(Collectors.toList());
|
||||
List<String> deviceCode = devices.stream().map(CommonEntity::getCode).collect(Collectors.toList());
|
||||
|
||||
log.info("device code not match {} - {} = {}",deviceCode, realTimeCode, CollectionUtils.removeAll(deviceCode, realTimeCode));
|
||||
}
|
||||
|
||||
List<EmDeviceDataEntity> collect = data.stream().map(x -> {
|
||||
|
||||
|
||||
EmDeviceDataEntity emDeviceData = new EmDeviceDataEntity();
|
||||
emDeviceData.setDeviceId(codeMap.get(x.deviceId()).getId());
|
||||
emDeviceData.setOrganizationId(codeMap.get(x.deviceId()).getOrganizationId());
|
||||
emDeviceData.setTime(LocalDateTime.now());
|
||||
emDeviceDataMapper.updateEntity(emDeviceData, x);
|
||||
|
||||
|
||||
return emDeviceData;
|
||||
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
emDeviceDataService.saveAll(collect);
|
||||
|
||||
log.info("get realtime data success {} ", deviceIds);
|
||||
} catch (Exception e) {
|
||||
log.error("query realtime data error", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.time.LocalDateTime;
|
||||
@Data
|
||||
public class CreateEmDeviceDataDto extends OrgCommonDto {
|
||||
|
||||
private String deviceId;
|
||||
private String device;
|
||||
|
||||
private LocalDateTime time;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.lihongjie.coal.emDeviceData.dto;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.OrgCommonDto;
|
||||
import cn.lihongjie.coal.emDevice.dto.EmDeviceDto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -11,7 +12,7 @@ import java.time.LocalDateTime;
|
||||
@Data
|
||||
public class EmDeviceDataDto extends OrgCommonDto {
|
||||
|
||||
private String deviceId;
|
||||
private EmDeviceDto device;
|
||||
|
||||
private LocalDateTime time;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class UpdateEmDeviceDataDto extends OrgCommonDto {
|
||||
private String deviceId;
|
||||
private String device;
|
||||
|
||||
private LocalDateTime time;
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package cn.lihongjie.coal.emDeviceData.entity;
|
||||
|
||||
import cn.lihongjie.coal.annotation.HyperTable;
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
import cn.lihongjie.coal.emDevice.entity.EmDeviceEntity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -17,9 +21,12 @@ import java.time.LocalDateTime;
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@HyperTable
|
||||
public class EmDeviceDataEntity extends OrgCommonEntity {
|
||||
|
||||
private String deviceId;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private EmDeviceEntity device;
|
||||
|
||||
private LocalDateTime time;
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ public interface EmDeviceDataMapper
|
||||
UpdateEmDeviceDataDto> {
|
||||
@Mappings(
|
||||
{ @Mapping(target = "status", ignore = true),
|
||||
@Mapping(target = "deviceId", ignore = true)}
|
||||
@Mapping(target = "device", ignore = true)}
|
||||
)
|
||||
void updateEntity(@MappingTarget EmDeviceDataEntity emDeviceData, DustApi.DeviceData x);
|
||||
}
|
||||
|
||||
@@ -94,13 +94,13 @@ public class EmDeviceDataService extends BaseService<EmDeviceDataEntity, EmDevic
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (EmDeviceDataEntity data : emDeviceDataEntities) {
|
||||
EmDeviceEntity device = emDeviceService.get(data.getDeviceId());
|
||||
EmDeviceEntity device = emDeviceService.get(data.getDevice().getId());
|
||||
|
||||
List<EmDeviceItemConfig> itemConfig = device.getItemConfig();
|
||||
|
||||
for (EmDeviceItemConfig config : itemConfig) {
|
||||
|
||||
if (!config.getEnable() || !config.getEnableAlter()) {
|
||||
if (config.getEnable()!=null && !config.getEnable() || !config.getEnableAlter()) {
|
||||
continue;
|
||||
}
|
||||
Object value = PropertyUtils.getProperty(data, config.getCode());
|
||||
|
||||
@@ -43,6 +43,7 @@ public class ThirdAccountService extends BaseService<ThirdAccountEntity, ThirdAc
|
||||
ThirdAccountEntity entity = mapper.toEntity(request);
|
||||
|
||||
this.repository.save(entity);
|
||||
refresh(entity);
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
@@ -52,6 +53,8 @@ public class ThirdAccountService extends BaseService<ThirdAccountEntity, ThirdAc
|
||||
|
||||
this.repository.save(entity);
|
||||
|
||||
refresh(entity);
|
||||
|
||||
return getById(entity.getId());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user