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; import com.zc.business.interfaces.OperationLog; import com.zc.business.request.DeviceGetPropertiesOperateRequest; import com.zc.business.service.IDcDeviceService; import com.zc.common.core.httpclient.OkHttp; import com.zc.common.core.httpclient.exception.HttpException; import com.zc.common.core.httpclient.request.RequestParams; import io.swagger.annotations.Api; 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; import org.springframework.web.bind.annotation.*; 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; /** * 设备Controller * * @author zhaoxianglong */ @Api(tags = {"设备"}) @RestController @RequestMapping("/business/device") public class DcDeviceController extends BaseController { @Resource private IDcDeviceService dcDeviceService; @Value("${iot.address}") private String iotAddress; @Resource private BroadcastController broadcastController; //*********************************设备增删改查****************************************** /** * 分页查询设备列表 * * @param dcDevice 请求参数 * @return 分页查询结果 */ @ApiOperation("分页查询设备列表") @PreAuthorize("@ss.hasPermi('iot:device:list')") @GetMapping("list") public TableDataInfo listDevice(DcDevice dcDevice) { return getDataTable(dcDeviceService.pageDevice(dcDevice)); } /** * 统计异常设备 * * @return 查询结果 */ @ApiOperation("统计异常设备") @PreAuthorize("@ss.hasPermi('iot:device:list')") @GetMapping("abnormalDevice") public AjaxResult statisticalAnomalyDevice() { return AjaxResult.success(dcDeviceService.statisticalAnomalyDevice()); } /** * 无分页查询设备列表 * * @param dcDevice 请求参数 * @return 查询结果 */ @ApiOperation("无分页查询设备列表") @PreAuthorize("@ss.hasPermi('iot:device:query')") @GetMapping("query") public AjaxResult queryDevice(DcDevice dcDevice) { return AjaxResult.success(dcDeviceService.listDevice(dcDevice)); } /** * 无分页根据设备桩号查询设备列表 * * @param parameter 请求参数 * @return 查询结果 */ @ApiOperation("无分页根据设备桩号查询设备列表") @PreAuthorize("@ss.hasPermi('iot:device:query')") @PostMapping("pileNumberQuery") public AjaxResult devicePileNumberQueryDevice(@RequestBody Map parameter) { return AjaxResult.success(dcDeviceService.devicePileNumberQueryDevice(parameter)); } /** * 根据id查询设备信息 * * @param id id * @return 查询结果 */ @ApiOperation("根据id查询设备信息") @PreAuthorize("@ss.hasPermi('iot:device:query')") @GetMapping("{id}") public AjaxResult getDevice(@PathVariable String id) { return AjaxResult.success(dcDeviceService.getDevice(id)); } /** * 根据网段查询设备 */ @ApiOperation("根据网段查询设备") @GetMapping("/networkSegment/{networkSegment}") public AjaxResult getDeviceByNetworkSegment(@PathVariable String networkSegment) { return AjaxResult.success(dcDeviceService.getDeviceByNetworkSegment(networkSegment)); } /** * 根据网段分组查询分组设备 */ @ApiOperation("根据网段分组查询分组设备") @GetMapping("/networkSegment") public AjaxResult getGroupingDeviceByNetworkSegment() { return AjaxResult.success(dcDeviceService.getGroupingDeviceByNetworkSegment()); } /** * 新增 * * @param dcDevice 新增参数 * @return 新增操作结果 */ @ApiOperation("新增") @PreAuthorize("@ss.hasPermi('iot:device:add')") @Log(title = "新增设备", businessType = BusinessType.INSERT) @PostMapping public AjaxResult addDevice(@Valid @RequestBody DcDevice dcDevice) { return toAjax(dcDeviceService.addDevice(dcDevice)); } /** * 修改 * * @param dcDevice 修改参数 * @return 修改操作结果 */ @ApiOperation("修改") @PreAuthorize("@ss.hasPermi('iot:device:edit')") @Log(title = "修改设备", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult editDevice(@Valid @RequestBody DcDevice dcDevice) { return toAjax(dcDeviceService.editDevice(dcDevice)); } /** * 删除 * * @param ids id集 * @return 删除操作结果 */ @ApiOperation("删除") @PreAuthorize("@ss.hasPermi('iot:device:remove')") @Log(title = "删除", businessType = BusinessType.DELETE) @DeleteMapping("{ids}") public AjaxResult removeDevice(@PathVariable List ids) { return toAjax(dcDeviceService.removeDevice(ids)); } /** * 导出 * @param response 响应 * @param iotDevice 导入数据结果 */ @PreAuthorize("@ss.hasPermi('iot:device:export')") @Log(title = "导出设备", businessType = BusinessType.EXPORT) @PostMapping("export") public void exportDevice(HttpServletResponse response, DcDevice iotDevice) { List list = dcDeviceService.listDevice(iotDevice); ExcelUtil util = new ExcelUtil<>(DcDevice.class); util.exportExcel(response, list, "设备数据"); } //***********************************物联设备接口************************************** /** * 根据物联产品id获取设备数据 * * @param productId 物联产品id * @return 获取设备数据操作结果 */ @ApiOperation("根据物联产品id获取设备数据") @GetMapping("/devices/{productId}") public AjaxResult getDeviceByProductId(@PathVariable @Parameter(description = "产品ID") String productId) throws HttpException, IOException { if (!StringUtils.hasText(productId)) { return AjaxResult.error("产品ID不能为空"); } OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/cache/" + productId) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 根据物联设备id获取最新属性数据 * * @param deviceId 物联设备id * @return 获取属性数据操作结果 */ @ApiOperation("获取设备最新属性数据") @GetMapping("/properties/latest/{deviceId}") public AjaxResult getDeviceLatestProperties(@PathVariable @Parameter(description = "设备ID") String deviceId) throws HttpException, IOException { if (!StringUtils.hasText(deviceId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/latest/" + deviceId) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 查询当天设备指定属性列表 * * @param deviceId 设备id * @param propertyId 属性id * @return 属性列表 */ @ApiOperation("查询当天设备指定属性列表") @GetMapping("/properties/history/day/{deviceId}/{propertyId}") public AjaxResult queryDevicePropertiesOneDay(@PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "属性ID") String propertyId) throws HttpException, IOException { HashMap props = new HashMap<>(); // 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内 props.put("terms[0].column", "timestamp$BTW"); ArrayList dateList = new ArrayList<>(); // 添加当前日期的开始和结束时间到列表,用于设定时间范围 dateList.add(DateUtil.beginOfDay(new Date()).toString()); dateList.add(DateUtil.endOfDay(new Date()).toString()); // 将日期列表以逗号分隔并设置为查询条件的值 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); if (!ajaxResult.get("code").equals(200)) { return ajaxResult; } Object data = JSON.parseObject(queryDeviceProperties(deviceId, propertyId, props).get("data").toString()).get("data"); JSONArray dataArray = JSON.parseArray(data.toString()); List> list = new ArrayList<>(); dataArray.forEach(o -> { Map map = new HashMap<>(); JSONObject jsonObject = JSON.parseObject(o.toString()); JSONObject formatValue = JSON.parseObject(jsonObject.get("formatValue").toString()); map.put("1", formatValue.get("1")); map.put("3", formatValue.get("3")); map.put("timestamp", formatValue.get("equipmentReportingTime") == null ? "" : Long.valueOf(formatValue.get("equipmentReportingTime").toString())); list.add(map); }); List> newList = list.stream() .filter(map -> !map.get("timestamp").equals("")) .collect(Collectors.toList()); Collections.sort(newList, new Comparator>() { @Override public int compare(Map map1, Map map2) { return map1.get("timestamp").toString().compareTo(map2.get("timestamp").toString()); } }); return AjaxResult.success(newList); } /** * 查询设备指定属性列表 * * @param deviceId 设备id * @param propertyId 属性id * @param props 查询条件 * @return 属性列表 */ @ApiOperation("获取设备历史属性列表") @GetMapping("/properties/history/{deviceId}/{propertyId}") public AjaxResult queryDeviceProperties(@PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "属性ID") String propertyId, @Parameter(hidden = true) HashMap props) throws HttpException, IOException { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(propertyId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(props); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/history/" + deviceId + "/" + propertyId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 根据物联设备id获取最新属性数据 * * @param deviceId 物联设备id * @param propertyId 属性id * @return 获取属性数据操作结果 */ @ApiOperation("获取设备指定属性最新数据") @GetMapping("/properties/latest/{deviceId}/{propertyId}") public AjaxResult getDeviceLatestProperty(@PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "属性ID") String propertyId) throws HttpException, IOException { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(propertyId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/latest/" + deviceId + "/" + propertyId) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 获取物联设备实时属性值 * * @param deviceId 物联设备id * @param propertyId 属性 * @return 属性实时数据 */ @ApiOperation("获取设备指定属性实时数据") @GetMapping("/properties/realtime/{deviceId}/{propertyId}") public AjaxResult getDeviceRealtimeProperty( @PathVariable String deviceId, @PathVariable String propertyId, @RequestParam HashMap props) throws HttpException, IOException { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(propertyId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(props); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/realtime/" + deviceId + "/" + propertyId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 批量获取物联设备实时属性 * * @param deviceId 物联设备id * @param props 属性id集 * @return 属性实时数据 */ @ApiOperation("获取设备属性实时数据") @PostMapping("/properties/realtime/{deviceId}") public AjaxResult getDeviceRealtimeProperties( @PathVariable String deviceId, @RequestBody DeviceGetPropertiesOperateRequest props) throws HttpException, IOException { if (!StringUtils.hasText(deviceId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); String string = JSON.toJSONString(props); JSONObject jsonObject = JSON.parseObject(string); RequestParams requestParams = new RequestParams(jsonObject); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/realtime/" + deviceId) // 请求地址 .data(requestParams) .post(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 发送设置属性指令到设备 * * @param deviceId 物联设备id * @param props 参数 * @return 设备属性操作结果 */ @ApiOperation("设置设备属性值") @PostMapping("/properties/setting/{deviceId}") public AjaxResult setDeviceProperties( @PathVariable String deviceId, @RequestBody HashMap props ) throws HttpException, IOException { if (!StringUtils.hasText(deviceId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(props); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/properties/setting/" + deviceId) // 请求地址 .data(requestParams) .post(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 设备功能调用 * * @param deviceId 物联设备id * @param functionId 功能id * @param props 调用参数 * @return 调用结果 */ @ApiOperation("设备功能调用") @PostMapping("/functions/{deviceId}/{functionId}") @OperationLog(operUrl = "/business/device/functions/{deviceId}/{functionId}") public AjaxResult invokedFunction( @PathVariable String deviceId, @PathVariable String functionId, @RequestBody HashMap props) throws HttpException, IOException { //public AjaxResult invokedFunction(@PathVariable String deviceId,@PathVariable String functionId,@RequestBody HashMap props,int operType) throws HttpException, IOException { todo return getAjaxResult(deviceId, functionId, props); } private AjaxResult getAjaxResult(String deviceId, String functionId, HashMap props) { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(functionId)) { return AjaxResult.error("设备未接入"); } try { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(props); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/functions/" + deviceId + "/" + functionId) // 请求地址 .data(requestParams) .post(); // 请求方法 if (response.body() != null) { return JSON.parseObject(response.body().string(), AjaxResult.class); } return AjaxResult.error(); } catch (Exception e) { return AjaxResult.error("请求失败"); } } /** * 批量设备功能调用 * * @param props 调用参数列表 * @return 调用结果 */ @ApiOperation("批量设备功能调用") @PostMapping("/batchFunctions") @OperationLog(operUrl = "/business/device/batchFunctions") //public AjaxResult batchInvokedFunction(@RequestBody Map props,int operType) throws HttpException, IOException, InterruptedException { todo public AjaxResult batchInvokedFunction(@RequestBody Map props) throws HttpException, IOException, InterruptedException { ArrayList devices = (ArrayList) props.get("devices"); ArrayList functions = (ArrayList) props.get("functions"); JSONArray resultArray = new JSONArray(); for (Object dev : devices) { // 将Object转换为JSONObject JSONObject device = (JSONObject) JSON.toJSON(dev); //JSONObject device = (JSONObject) JSON.toJSON(dev.toString()); String iotDeviceId = device.getString("iotDeviceId"); for (Object function : functions) { JSONObject functionJSONObject = (JSONObject) JSON.toJSON(function); //JSONObject functionJSONObject = (JSONObject) JSON.toJSON(function.toString()) ; String functionId = functionJSONObject.getString("functionId"); JSONObject jsonObject = functionJSONObject.getJSONObject("params") != null ? functionJSONObject.getJSONObject("params") : new JSONObject(); JSONObject result = getResult(device, iotDeviceId, functionId, jsonObject); resultArray.add(result); AjaxResult ajaxResult = (AjaxResult) result.get("result"); if (!Objects.equals(String.valueOf(ajaxResult.get("code")), "200")) { if (Objects.equals(device.getString("deviceType"), "15")) { return AjaxResult.error(200, "500"); } else { return AjaxResult.error(); } } } } return AjaxResult.success(resultArray); } private JSONObject getResult(JSONObject device, String iotDeviceId, String functionId, JSONObject jsonObject) throws HttpException, IOException { HashMap params = jsonObject.toJavaObject(new TypeReference>() { }); JSONObject result = new JSONObject(); result.put("device", device.getString("id")); result.put("functionId", functionId); if (device.getInteger("deviceType").equals(DeviceTypeConstants.ROAD_SECTION_VOICE_BROADCASTING)) { JSONObject value = broadcastController.nearCamListDistance(jsonObject); AjaxResult ajaxResult = new AjaxResult(); value.keySet().forEach(item -> { ajaxResult.put(item, value.getString(item)); }); ajaxResult.put("code", 200); result.put("result", ajaxResult); } else { result.put("result", getAjaxResult(iotDeviceId, functionId, params)); } return result; } /** * 批量设备功能调用 * * @param props 调用参数列表 * @return 调用结果 */ @ApiOperation("批量激光疲劳设备功能调用") @PostMapping("/batchLaserFatigueInvokedFunction") public AjaxResult batchLaserFatigueInvokedFunction(@RequestBody Map props) throws HttpException, IOException, InterruptedException { String deviceId = (String) props.get("deviceId"); String functionId = (String) props.get("functionId"); ArrayList params = (ArrayList) props.get("params"); JSONArray resultArray = new JSONArray(); for (Object param : params) { resultArray.add(getAjaxResult(deviceId, functionId, (HashMap) param)); } HashMap hashMap = new HashMap<>(); hashMap.put("SET", "7"); getAjaxResult(deviceId, "SETMD", hashMap); return AjaxResult.success(resultArray); } /** * 查询物联设备指定事件数据 * * @param deviceId 物联设备id * @param eventId 事件id * @param queryParam 查询条件 * @return 查询事件结果 */ @ApiOperation("查询指定事件历史数据列表") @GetMapping("/events/history/{deviceId}/{eventId}") public AjaxResult queryPagerDeviceEvents( @PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "事件ID") String eventId, @Parameter(hidden = true) HashMap queryParam ) throws HttpException, IOException { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(eventId)) { return AjaxResult.error("设备未接入"); } OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(queryParam); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/events/history/" + deviceId + "/" + eventId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 查询物联设备指定事件数据 * * @param deviceId 物联设备id * @param type 类型 * @param queryParam 查询条件 * @return 查询事件结果 */ @ApiOperation("查询指定事件历史数据列表") @GetMapping("/events/history/{deviceId}/type/{type}") public AjaxResult listPagerDeviceEvents( @PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "事件ID") String type, @RequestParam @Parameter(hidden = true) HashMap queryParam ) throws HttpException, IOException { if (!StringUtils.hasText(deviceId) || !StringUtils.hasText(type)) { return AjaxResult.error("设备未接入"); } if (type.equals("all")){ OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(queryParam); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/logs/"+deviceId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); }else { return queryPagerDeviceEvents(deviceId, type, queryParam); } } /** * 获取物联设备物模型 * * @param id 物联设备id * @return 更新结果 */ @ApiOperation("获取设备物模型") @GetMapping(value = "/metadata/{id}") public AjaxResult getMetadata(@PathVariable String id) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/api/iot/device/metadata/" + id) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * @Description 查询上游10公里内的情报板 * * @author liuwenge * @date 2024/4/15 14:22 * @param stakeMark * @param direction * @return com.ruoyi.common.core.domain.AjaxResult */ @ApiOperation("查询上游10公里内的情报板") @PostMapping("/selectNearBoard") public AjaxResult selectNearBoard(@ApiParam(value="桩号", name="stakeMark", required=true) @RequestParam ("stakeMark") String stakeMark, @ApiParam(value="方向", name="direction", required=true) @RequestParam ("direction") String direction){ return dcDeviceService.selectNearBoard(stakeMark,direction); } @OperationLog(operUrl = "/business/device/batchFunctions") public AjaxResult batchInvokedFunction(Object object) throws HttpException, IOException, InterruptedException { //public AjaxResult batchInvokedFunction(Object object,int operType) throws HttpException, IOException, InterruptedException { todo Map 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 props = new HashMap<>(); // 设置查询条件的键为“timestamp$BTW”,表示时间戳在一定范围内 props.put("terms[0].column", "timestamp$BTW"); ArrayList 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 jsonObjectList = dataVale.toJavaList(JSONObject.class); // 定义一个时间格式转换的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); // 使用Stream API提取每个JSONObject中的timestamp和propertyName值 List> 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 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 jsonObjectList = dataValue.toJavaList(JSONObject.class); // 定义一个时间格式转换的工具 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); List> 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 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; } }