Browse Source

设备数据分析

develop
wangsixiang 9 months ago
parent
commit
d54d4eec8e
  1. 119
      zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java
  2. 7
      zc-business/src/main/java/com/zc/business/mapper/DcDeviceMapper.java
  3. 6
      zc-business/src/main/java/com/zc/business/service/IDcDeviceService.java
  4. 11
      zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java
  5. 14
      zc-business/src/main/resources/mapper/business/DcDeviceMapper.xml
  6. 2
      zc-business/src/main/resources/mapper/business/DcPerceivedEventsWarningMapper.xml

119
zc-business/src/main/java/com/zc/business/controller/DcDeviceController.java

@ -1,16 +1,19 @@
package com.zc.business.controller;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zc.business.constant.DeviceTypeConstants;
import com.zc.business.domain.DcDevice;
@ -25,6 +28,7 @@ import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Parameter;
import okhttp3.Response;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
@ -34,6 +38,8 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@ -650,4 +656,117 @@ public class DcDeviceController extends BaseController {
Map<String, Object> map = new ObjectMapper().convertValue(object, Map.class);
return batchInvokedFunction(map);
}
/**
* 分页查询设备名称列表
* @param dcDevice 请求参数
* @return 分页查询结果
*/
@GetMapping("/selectDeviceNameList")
public TableDataInfo selectDeviceNameList(DcDevice dcDevice) {
startPage();
return getDataTable(dcDeviceService.selectDeviceNameList(dcDevice));
}
@GetMapping("/deviceParameter")
public TableDataInfo selectDeviceParameterProperties(DcDevice dcDevice) {
if (dcDevice==null||dcDevice.getDeviceType()==null){
TableDataInfo tableDataInfo = new TableDataInfo();
tableDataInfo.setCode(500);
tableDataInfo.setMsg("参数错误");
return tableDataInfo;
}
startPage();
return getDataTable(dcDeviceService.selectDeviceParameterProperties(dcDevice));
}
/**
* 查询设备指定属性属性列表
* param参数
* deviceId 设备id
* propertyId 属性id
* propertyName 属性名称
* deviceType 设备类型
* dateTime 时间
* @return 属性列表
*/
@PostMapping("/properties/deviceNameData")
public AjaxResult queryDevice(@RequestBody HashMap map) throws HttpException, IOException {
if (map==null||!map.containsKey("deviceId")||!map.containsKey("deviceType")
||!map.containsKey("propertyName")||!map.containsKey("propertyId")
||!map.containsKey("dateTime")){
return AjaxResult.error("参数错误");
}
String deviceType = map.get("deviceType").toString();//设备类型
String deviceId = map.get("deviceId").toString();//设备id
String propertyId = map.get("propertyId").toString();//属性id
String propertyName = map.get("propertyName").toString();//属性名称
HashMap<String, Object> props = new HashMap<>();
// 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内
props.put("terms[0].column", "timestamp$BTW");
ArrayList<String> dateList = new ArrayList<>();
// 添加当前日期的开始和结束时间到列表,用于设定时间范围
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = dateFormat.parse((String) map.get("dateTime"));
dateList.add(DateUtil.beginOfDay(date).toString());
dateList.add(DateUtil.endOfDay(date).toString());
} catch (ParseException e) {
e.printStackTrace();
}
// 将日期列表以逗号分隔并设置为查询条件的值
props.put("terms[0].value", String.join(",", dateList));
props.put("paging", false);
props.put("sorts[0].order", "asc");
props.put("sorts[0].name", "timestamp");
AjaxResult ajaxResult = queryDeviceProperties(deviceId, propertyId, props);
String code = ajaxResult.get("code").toString();
if ("500".equals(code)){
return ajaxResult;
}
if ("13".equals(deviceType) || "16".equals(deviceType)) { //判断是否为设备箱/远端机
JSONObject data = (JSONObject) ajaxResult.get("data");
JSONArray dataVale = (JSONArray) data.get("data");
List<JSONObject> jsonObjectList = dataVale.toJavaList(JSONObject.class);
// 定义一个时间格式转换的工具
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
// 使用Stream API提取每个JSONObject中的timestamp和propertyName值
List<Map<String, String>> results = jsonObjectList.stream()
.map(jsonObject -> {
long timestamp = jsonObject.getLongValue("timestamp"); // 假设timestamp是long类型
Date date = new Date(timestamp); // 将时间戳转换为Date对象
String formattedTime = sdf.format(date); // 将Date对象格式化为字符串
JSONObject formatValue = jsonObject.getJSONObject("formatValue"); // 获取嵌套的JSONObject
String result = formatValue.getString(propertyName); // 从嵌套的JSONObject中获取电压值
Map<String, String> hashMap = new HashMap<>();
hashMap.put("timestamp", formattedTime); // 使用"timestamp"作为键来存储格式化后的时间
hashMap.put("result", result); // 使用"voltage"作为键来存储电压值
return hashMap;
})
.collect(Collectors.toList());
results.sort(Comparator.comparing(hashMap -> hashMap.get("timestamp")));
return AjaxResult.success(results);
}
if ("3".equals(deviceType)||"17".equals(deviceType)){ //气象预警/ups设备
JSONObject data = (JSONObject) ajaxResult.get("data");
JSONArray dataValue = (JSONArray) data.get("data");
List<JSONObject> jsonObjectList = dataValue.toJavaList(JSONObject.class);
// 定义一个时间格式转换的工具
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
List<HashMap<String, String>> results = jsonObjectList.stream()
.map(jsonObject -> {
long timestamp = jsonObject.getLongValue("timestamp"); // 假设timestamp是long类型
Date date = new Date(timestamp); // 将时间戳转换为Date对象
String formattedTime = sdf.format(date); // 将Date对象格式化为字符串
String result = jsonObject.getString(propertyName);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("timestamp", formattedTime); // 使用时间戳作为键可能不是最佳实践,但这里保持与原始逻辑一致
hashMap.put("result", result);
return hashMap;
})
.collect(Collectors.toList());
results.sort(Comparator.comparing(hashMap -> hashMap.get("timestamp")));
return AjaxResult.success(results);
}
return null;
}
}

7
zc-business/src/main/java/com/zc/business/mapper/DcDeviceMapper.java

@ -5,6 +5,7 @@ import com.zc.business.domain.DcDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -25,4 +26,10 @@ public interface DcDeviceMapper extends BaseMapper<DcDevice> {
List<DcDevice> selectNearBoard(@Param("direction") String direction,@Param("startMileage") Integer startMileage,@Param("endMileage") Integer endMileage);
List<Map<String,String>> countTheNumberOfEligibleItems();
//设备查询-设备名称列表
public List<HashMap<String,Object>> selectDeviceNameList(DcDevice dcDevice);
//设备查询-设备参数属性列表
public List<HashMap<String,Object>> selectDeviceParameterProperties(DcDevice dcDevice);
}

6
zc-business/src/main/java/com/zc/business/service/IDcDeviceService.java

@ -6,6 +6,7 @@ import com.ruoyi.common.core.domain.AjaxResult;
import com.zc.business.domain.DcDevice;
import com.zc.business.domain.DcStakeMark;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -104,4 +105,9 @@ public interface IDcDeviceService extends IService<DcDevice> {
* 查询上游10公里内的情报板
*/
AjaxResult selectNearBoard(String stakeMark, String direction);
//设备查询-设备名称列表
public List<HashMap<String,Object>> selectDeviceNameList(DcDevice dcDevice);
//设备查询-设备参数属性列表
public List<HashMap<String,Object>> selectDeviceParameterProperties(DcDevice dcDevice);
}

11
zc-business/src/main/java/com/zc/business/service/impl/DcDeviceServiceImpl.java

@ -533,5 +533,16 @@ public class DcDeviceServiceImpl extends ServiceImpl<DcDeviceMapper, DcDevice> i
}
return AjaxResult.success(boardList);
}
//设备查询-设备名称列表
@Override
public List<HashMap<String, Object>> selectDeviceNameList(DcDevice dcDevice) {
return dcDeviceMapper.selectDeviceNameList(dcDevice);
}
//设备查询-设备参数属性列表
@Override
public List<HashMap<String, Object>> selectDeviceParameterProperties(DcDevice dcDevice) {
return dcDeviceMapper.selectDeviceParameterProperties(dcDevice);
}
}

14
zc-business/src/main/resources/mapper/business/DcDeviceMapper.xml

@ -153,5 +153,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
GROUP BY
dc_road_section.id;
</select>
<select id="selectDeviceNameList" resultType="java.util.HashMap">
select iot_device_id deviceId, device_type deviceType,device_name deviceName,device_state deviceState from dc_device
<where>
<if test="deviceState != null and deviceState != ''">and device_state = #{deviceState}</if>
<if test="deviceName != null and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')</if>
and (device_type=3||device_type=13||device_type=16||device_type=17) and iot_device_id is not null
</where>
</select>
<select id="selectDeviceParameterProperties" resultType="java.util.HashMap">
select id, device_type deviceType,
property_id propertyId,property_name propertyName,
name from dc_device_attribute
where device_type=#{deviceType}
</select>
</mapper>

2
zc-business/src/main/resources/mapper/business/DcPerceivedEventsWarningMapper.xml

@ -543,4 +543,4 @@
<!-- </select>-->
</mapper>
</mapper>

Loading…
Cancel
Save