From aa38126140720e49fd373ad67084aada3f158f7d Mon Sep 17 00:00:00 2001 From: zhouwentao <1577701412@qq.com> Date: Sun, 10 Sep 2023 12:10:49 +0800 Subject: [PATCH] updates --- .../zxweb/controller/IotController.java | 131 +++++++++++++++++- .../com/example/zxweb/utils/IotUtils.java | 4 + 2 files changed, 129 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/zxweb/controller/IotController.java b/src/main/java/com/example/zxweb/controller/IotController.java index 26d691c..3050a13 100644 --- a/src/main/java/com/example/zxweb/controller/IotController.java +++ b/src/main/java/com/example/zxweb/controller/IotController.java @@ -8,10 +8,8 @@ import com.example.zxweb.utils.IotUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.*; import java.util.LinkedHashMap; import java.util.Map; @@ -26,7 +24,6 @@ import java.util.Map; @Slf4j @Api(tags = "设备管理") public class IotController { - @ApiOperation(value = "获取单个设备信息") @GetMapping(value = "/devices") public Result devices(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId){ @@ -36,6 +33,128 @@ public class IotController { requestBody.put("serial",serial); requestBody.put("productId",productId); //请求数据 - return Result.OK(IotUtils.getApi(IotApiEnum.getPathByText("获取单个设备信息"),requestBody)); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("获取单个设备信息"), requestBody)); + } + + @ApiOperation(value = "获取设备列表") + @GetMapping("/devices/list") + public Result devicesList(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId, + @RequestParam(value = "modelCode",defaultValue = "")String modelCode,@RequestParam(value = "authority",defaultValue = "")String authority, + @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize, + @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime, + @RequestParam(value = "sort",defaultValue = "")String sort){ + AssertUtils.notEmpty(serial,"请输入-[serial]"); + JSONObject requestBody=new JSONObject(); + requestBody.put("serial",serial); + requestBody.put("productId",productId); + requestBody.put("modelCode",modelCode); + requestBody.put("authority",authority); + requestBody.put("pageNo",pageNo); + requestBody.put("pageSize",pageSize); + requestBody.put("startTime",startTime); + requestBody.put("endTime",endTime); + requestBody.put("sort",sort); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("获取设备列表"),requestBody)); + } + + @ApiOperation(value = "查询设备历史数据") + @GetMapping("/devices/datastreams") + public Result devicesDatastreams(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId, + @RequestParam(value = "dataStreamName",defaultValue = "")String dataStreamName, + @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize, + @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime, + @RequestParam(value = "sort",defaultValue = "")String sort){ + AssertUtils.notEmpty(serial,"请输入-[serial]"); + AssertUtils.notEmpty(dataStreamName,"请输入-[dataStreamName]"); + JSONObject requestBody=new JSONObject(); + requestBody.put("serial",serial); + requestBody.put("productId",productId); + requestBody.put("dataStreamName",dataStreamName); + requestBody.put("pageNo",pageNo); + requestBody.put("pageSize",pageSize); + requestBody.put("startTime",startTime); + requestBody.put("endTime",endTime); + requestBody.put("sort",sort); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询设备历史数据"),requestBody)); + } + + @ApiOperation(value = "查询消息是否下达到设备") + @GetMapping("/devices/datastreams/state") + public Result devicesDatastreamsState(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId, + @RequestParam(value = "streamCinId",defaultValue = "")String streamCinId){ + AssertUtils.notEmpty(serial,"请输入-[serial]"); + AssertUtils.notEmpty(productId,"请输入-[productId]"); + AssertUtils.notEmpty(streamCinId,"请输入-[streamCinId]"); + JSONObject requestBody=new JSONObject(); + requestBody.put("serial",serial); + requestBody.put("productId",productId); + requestBody.put("streamCinId",streamCinId); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询消息是否下达到设备"),requestBody)); + } + + @ApiOperation(value = "查询事件历史数据") + @GetMapping("/devices/events") + public Result devicesEvents(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId, + @RequestParam(value = "dataStreamName",defaultValue = "")String dataStreamName, + @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize, + @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime, + @RequestParam(value = "sort",defaultValue = "")String sort){ + AssertUtils.notEmpty(serial,"请输入-[serial]"); + AssertUtils.notEmpty(dataStreamName,"请输入-[dataStreamName]"); + JSONObject requestBody=new JSONObject(); + requestBody.put("serial",serial); + requestBody.put("productId",productId); + requestBody.put("dataStreamName",dataStreamName); + requestBody.put("pageNo",pageNo); + requestBody.put("pageSize",pageSize); + requestBody.put("startTime",startTime); + requestBody.put("endTime",endTime); + requestBody.put("sort",sort); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询事件历史数据"),requestBody)); + } + + + + @ApiOperation(value = "向设备发送消息") + @PostMapping("/devices/datastreams") + public Result devicesDatastreams(@RequestBody JSONObject requestBody){ + return jsonObjectToResult(IotUtils.postApi(IotApiEnum.getPathByText("向设备发送消息"),requestBody)); + } + + @ApiOperation(value = "向设备发送方法") + @PostMapping("/devices/methods") + public Result devicesMethods(@RequestBody JSONObject requestBody){ + return jsonObjectToResult(IotUtils.postApi(IotApiEnum.getPathByText("向设备发送方法"),requestBody)); + } + + @ApiOperation(value = "查询方法历史数据") + @GetMapping("/devices/methods") + public Result devicesMethods(@RequestParam(value = "serial",defaultValue = "")String serial,@RequestParam(value = "productId",defaultValue = "")String productId, + @RequestParam(value = "methodName",defaultValue = "")String methodName, + @RequestParam(value = "pageNo",defaultValue = "1")String pageNo,@RequestParam(value = "pageSize",defaultValue = "10")String pageSize, + @RequestParam(value = "startTime",defaultValue = "")String startTime,@RequestParam(value = "endTime",defaultValue = "")String endTime, + @RequestParam(value = "sort",defaultValue = "")String sort){ + AssertUtils.notEmpty(serial,"请输入设备序列号-[serial]"); + AssertUtils.notEmpty(methodName,"请输入方法名称-[methodName]"); + JSONObject requestBody=new JSONObject(); + requestBody.put("serial",serial); + requestBody.put("productId",productId); + requestBody.put("dataStreamName",methodName); + requestBody.put("pageNo",pageNo); + requestBody.put("pageSize",pageSize); + requestBody.put("startTime",startTime); + requestBody.put("endTime",endTime); + requestBody.put("sort",sort); + return jsonObjectToResult(IotUtils.getApi(IotApiEnum.getPathByText("查询方法历史数据"),requestBody)); + } + + public static Result jsonObjectToResult(JSONObject jsonObject) { + Integer code = jsonObject.getInteger("code"); + String message = jsonObject.getString("message"); + String data = jsonObject.getString("data"); + if (code!=0) { + return Result.error(code, StringUtils.isEmpty(message)?data:message); + } + return Result.OK(StringUtils.isEmpty(message)?data:message); } } diff --git a/src/main/java/com/example/zxweb/utils/IotUtils.java b/src/main/java/com/example/zxweb/utils/IotUtils.java index 8d21a32..737c807 100644 --- a/src/main/java/com/example/zxweb/utils/IotUtils.java +++ b/src/main/java/com/example/zxweb/utils/IotUtils.java @@ -32,4 +32,8 @@ public class IotUtils { JSONObject responseBody = RestUtil.get(API_PREFIX+apiUrl+sb,null,null,headers); return responseBody; } + + public static JSONObject postApi(String apiUrl,JSONObject requestBody){ + return RestUtil.post(API_PREFIX+apiUrl,requestBody,headers); + } }