package com.zc.business.controller; import com.alibaba.fastjson.JSON; 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.zc.business.domain.DcDevice; import com.zc.business.request.DeviceGetPropertiesOperateRequest; import com.zc.business.request.DeviceOperateRequest; 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.v3.oas.annotations.Parameter; import okhttp3.Response; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import javax.annotation.Resource; import javax.validation.Valid; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 设备Controller * * @author zhaoxianglong */ @Api("设备") @RestController @RequestMapping("/business/device") public class DcDeviceController extends BaseController { @Resource private IDcDeviceService dcDeviceService; @Value("${iot.address}") private String iotAddress; //*********************************设备增删改查****************************************** /** * 分页查询设备列表 * * @param dcDevice 请求参数 * @return 分页查询结果 */ @ApiOperation("分页查询设备列表") @PreAuthorize("@ss.hasPermi('iot:device:list')") @GetMapping("list") public TableDataInfo listDevice(DcDevice dcDevice) { return getDataTable(dcDeviceService.pageDevice(dcDevice)); } /** * 无分页查询设备列表 * * @param dcDevice 请求参数 * @return 查询结果 */ @ApiOperation("无分页查询设备列表") @PreAuthorize("@ss.hasPermi('iot:device:query')") @GetMapping("query") public AjaxResult queryDevice(DcDevice dcDevice) { return AjaxResult.success(dcDeviceService.listDevice(dcDevice)); } /** * 根据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)); } /** * 新增 * * @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)); } //***********************************物联设备接口************************************** /** * 根据物联设备id获取最新属性数据 * @param deviceId 物联设备id * @return 获取属性数据操作结果 */ @GetMapping("/properties/latest/{deviceId}") public AjaxResult getDeviceLatestProperties(@PathVariable @Parameter(description = "设备ID") String deviceId) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/latest/" + deviceId) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 查询设备指定属性列表 * @param deviceId 设备id * @param propertyId 属性id * @param queryParam 查询条件 * @return 属性列表 */ @GetMapping("/properties/history/{deviceId}/{propertyId}") public AjaxResult queryDeviceProperties(@PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "属性ID") String propertyId, @Parameter(hidden = true) Map queryParam) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("queryParam", queryParam); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/history/" + deviceId + "/" + propertyId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 根据物联设备id获取最新属性数据 * @param deviceId 物联设备id * @param propertyId 属性id * @return 获取属性数据操作结果 */ @GetMapping("/properties/latest/{deviceId}/{propertyId}") public AjaxResult getDeviceLatestProperty(@PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "属性ID") String propertyId) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/latest/" + deviceId + "/" + propertyId) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 获取物联设备实时属性值 * @param deviceId 物联设备id * @param propertyId 属性 * @return 属性实时数据 */ @GetMapping("/properties/realtime/{deviceId}/{propertyId}") public AjaxResult getDeviceRealtimeProperty( @PathVariable String deviceId, @PathVariable String propertyId, @RequestParam Map headers) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("headers", headers); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/realtime/" + deviceId + "/" + propertyId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 批量获取物联设备实时属性 * @param deviceId 物联设备id * @param props 属性id集 * @return 属性实时数据 */ @GetMapping("/properties/realtime/{deviceId}") public AjaxResult getDeviceRealtimeProperties( @PathVariable String deviceId, @ModelAttribute DeviceGetPropertiesOperateRequest props) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("props", props); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/realtime/" + deviceId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 发送设置属性指令到设备 * @param deviceId 物联设备id * @param props 参数 * @return 设备属性操作结果 */ @PostMapping("/properties/setting/{deviceId}") public AjaxResult setDeviceProperties( @PathVariable String deviceId, @RequestBody DeviceOperateRequest> props ) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("props", props); Response response // 请求响应 = okHttp .url(iotAddress + "/properties/setting/" + deviceId) // 请求地址 .data(requestParams) .post(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 设备功能调用 * @param deviceId 物联设备id * @param functionId 功能id * @param props 调用参数 * @return 调用结果 */ @PostMapping("/functions/{deviceId}/{functionId}") public AjaxResult invokedFunction( @PathVariable String deviceId, @PathVariable String functionId, @RequestBody Map props) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("props", props); Response response // 请求响应 = okHttp .url(iotAddress + "/functions/" + deviceId + "/" + functionId) // 请求地址 .data(requestParams) .post(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 查询物联设备事件数据 * @param deviceId 物联设备id * @param eventId 事件id * @param queryParam 查询条件 * @return 查询事件结果 */ @GetMapping("/events/history/{deviceId}/{eventId}") public AjaxResult queryPagerDeviceEvents( @PathVariable @Parameter(description = "设备ID") String deviceId, @PathVariable @Parameter(description = "事件ID") String eventId, @Parameter(hidden = true) Map queryParam ) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); RequestParams requestParams = new RequestParams(); requestParams.put("queryParam", queryParam); Response response // 请求响应 = okHttp .url(iotAddress + "/events/history/" + deviceId + "/" + eventId) // 请求地址 .data(requestParams) .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } /** * 获取物联设备物模型 * @param id 物联设备id * @return 更新结果 */ @GetMapping(value = "/metadata/{id}") public AjaxResult getMetadata(@PathVariable String id) throws HttpException, IOException { OkHttp okHttp = new OkHttp(); Response response // 请求响应 = okHttp .url(iotAddress + "/metadata/" + id) // 请求地址 .get(); // 请求方法 return JSON.parseObject(response.body().string(), AjaxResult.class); } }