增加单设备监控的查询接口

ck
liutao 2 weeks ago
parent 15dc437e4e
commit d85744aba4

@ -239,6 +239,14 @@ public class DeviceController {
return success(deviceContactModelDO);
}
@GetMapping("/singleDeviceFrom")
@Operation(summary = "单设备查看")
// @PreAuthorize("@ss.hasPermission('iot:device:query')")
public CommonResult<List<Map<String, Object>>> singleDeviceFrom(@RequestParam("deviceId") Long deviceId) throws JsonProcessingException {
List<Map<String, Object>> deviceContactModelDO=deviceService.singleDeviceFrom(deviceId);
return success(deviceContactModelDO);
}
@GetMapping("/historyRecord")
@Operation(summary = "历史记录查询")

@ -157,4 +157,6 @@ public interface DeviceService {
PageResult<DeviceSelectRespVO> getAvailableDevicePage(DevicePageReqVO pageReqVO);
List<Map<String, Object>> singleDeviceFrom(Long deviceId);
}

@ -64,12 +64,12 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -974,6 +974,85 @@ public class DeviceServiceImpl implements DeviceService {
return resultMap;
}
@Override
public List<Map<String, Object>> singleDeviceFrom(Long deviceId) {
Map<String, List<Map<String, Object>>> resultMap = new LinkedHashMap<>();
List<Map<String, Object>> resultList = new ArrayList<>();
List<DeviceContactModelDO> records = deviceContactModelMapper.selectList(Wrappers.<DeviceContactModelDO>lambdaQuery()
.eq(DeviceContactModelDO::getDeviceId,deviceId)
.orderByDesc(DeviceContactModelDO::getId));
if (records == null || records.isEmpty()) {
return resultList;
}
try {
// 获取最新一行数据
Map<String, Object> latestRow = tdengineService.newSelectLatestRow(deviceId);
if (latestRow == null || latestRow.isEmpty()) {
return resultList;
}
// 属性类型映射
Map<Long, String> idToNameMap = deviceAttributeTypeMapper.selectList()
.stream()
.collect(Collectors.toMap(
DeviceAttributeTypeDO::getId,
DeviceAttributeTypeDO::getName
));
Map<Long, String> attributeTypeCache = new HashMap<>();
// 遍历 records 填充数据
for (DeviceContactModelDO record : records) {
String attributeCode = record.getAttributeCode();
if (attributeCode == null) continue;
Object rawValue = latestRow.get(attributeCode);
Object finalValue = DataTypeParseUtil.parse(rawValue, record.getDataType());
record.setAddressValue(finalValue);
// 属性类型名称
String attributeTypeName = "其他";
Object typeObj = record.getAttributeType();
if (typeObj != null) {
String typeStr = typeObj.toString();
try {
Long typeId = Long.parseLong(typeStr);
attributeTypeName = attributeTypeCache.computeIfAbsent(typeId,
k -> idToNameMap.getOrDefault(typeId, typeStr));
} catch (NumberFormatException e) {
attributeTypeName = typeStr;
}
}
Map<String, Object> simplifiedData = new HashMap<>();
simplifiedData.put("value", record.getAddressValue());
simplifiedData.put("label", record.getAttributeName());
simplifiedData.put("prop", "parpmeter_" + record.getId());
resultMap.computeIfAbsent(attributeTypeName, k -> new ArrayList<>()).add(simplifiedData);
}
if (!resultMap.isEmpty()) {
AtomicInteger a= new AtomicInteger(0);
resultMap.forEach((key, value) -> {
HashMap<String, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("id", a.incrementAndGet());
objectObjectHashMap.put("title", key);
objectObjectHashMap.put("items", value);
resultList.add(objectObjectHashMap);
});
}
} catch (Exception e) {
log.warn("处理设备 {} 最新数据时异常: {}", deviceId, e.getMessage());
}
return resultList;
}
@Override
public PageResult<Map<String, Object>> historyRecord(
@ -1746,6 +1825,7 @@ public class DeviceServiceImpl implements DeviceService {
return new PageResult<>(availableDeviceList, page.getTotal());
}
/**
*
* deviceOperationPage

Loading…
Cancel
Save