feat: 新功能开发,监测历史记录。微信和物联网平台,模块搭建。

This commit is contained in:
tianyongbao
2025-11-05 00:35:23 +08:00
parent 1d2e2e2513
commit 05fb822744
38 changed files with 2099 additions and 49 deletions

View File

@@ -0,0 +1,212 @@
package com.intc.iot.controller;
import com.intc.common.core.domain.R;
import com.intc.common.web.core.BaseController;
import com.intc.iot.service.DeviceDataService;
import com.intc.iot.service.IotDeviceService;
import com.intc.iot.service.MqttService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 阿里云生活物联网平台(飞燕平台)控制器
*
* @author intc
*/
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot")
@Tag(name = "生活物联网平台管理", description = "阿里云飞燕平台对接接口")
public class IotController extends BaseController {
@Autowired(required = false)
private IotDeviceService iotDeviceService;
@Autowired(required = false)
private MqttService mqttService;
@Autowired(required = false)
private DeviceDataService deviceDataService;
@Operation(summary = "测试接口")
@GetMapping("/test")
public R<String> test() {
return R.ok("飞燕平台模块测试成功!");
}
@Operation(summary = "查询设备列表")
@GetMapping("/device/list")
public R<Map<String, Object>> queryDeviceList(
@Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNo,
@Parameter(description = "每页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.queryDeviceList(pageNo, pageSize);
return R.ok(response);
} catch (Exception e) {
log.error("查询设备列表失败", e);
return R.fail("查询设备列表失败: " + e.getMessage());
}
}
@Operation(summary = "查询设备详情")
@GetMapping("/device/info")
public R<Map<String, Object>> queryDeviceInfo(
@Parameter(description = "设备ID") @RequestParam String iotId) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.queryDeviceInfo(iotId);
return R.ok(response);
} catch (Exception e) {
log.error("查询设备详情失败", e);
return R.fail("查询设备详情失败: " + e.getMessage());
}
}
@Operation(summary = "查询设备属性")
@GetMapping("/device/properties")
public R<Map<String, Object>> queryDeviceProperties(
@Parameter(description = "设备ID") @RequestParam String iotId) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.queryDeviceProperties(iotId);
return R.ok(response);
} catch (Exception e) {
log.error("查询设备属性失败", e);
return R.fail("查询设备属性失败: " + e.getMessage());
}
}
@Operation(summary = "设置设备属性")
@PostMapping("/device/property/set")
public R<Map<String, Object>> setDeviceProperty(
@Parameter(description = "设备ID") @RequestParam String iotId,
@Parameter(description = "属性JSON") @RequestParam String properties) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.setDeviceProperty(iotId, properties);
return R.ok(response);
} catch (Exception e) {
log.error("设置设备属性失败", e);
return R.fail("设置设备属性失败: " + e.getMessage());
}
}
@Operation(summary = "调用设备服务")
@PostMapping("/device/invoke")
public R<Map<String, Object>> invokeService(
@Parameter(description = "设备ID") @RequestParam String iotId,
@Parameter(description = "服务标识符") @RequestParam String identifier,
@Parameter(description = "参数JSON") @RequestParam String args) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.invokeService(iotId, identifier, args);
return R.ok(response);
} catch (Exception e) {
log.error("调用设备服务失败", e);
return R.fail("调用设备服务失败: " + e.getMessage());
}
}
@Operation(summary = "解绑设备")
@DeleteMapping("/device/unbind")
public R<Map<String, Object>> unbindDevice(
@Parameter(description = "设备ID") @RequestParam String iotId) {
try {
if (iotDeviceService == null) {
return R.fail("飞燕平台配置未启用");
}
Map<String, Object> response = iotDeviceService.unbindDevice(iotId);
return R.ok(response);
} catch (Exception e) {
log.error("解绑设备失败", e);
return R.fail("解绑设备失败: " + e.getMessage());
}
}
@Operation(summary = "发布MQTT消息")
@PostMapping("/mqtt/publish")
public R<String> publishMqtt(
@Parameter(description = "主题") @RequestParam String topic,
@Parameter(description = "消息内容") @RequestParam String payload,
@Parameter(description = "QoS等级") @RequestParam(defaultValue = "1") int qos) {
try {
if (mqttService == null) {
return R.fail("MQTT配置未启用");
}
mqttService.publish(topic, payload, qos);
return R.ok("消息发布成功");
} catch (Exception e) {
log.error("发布MQTT消息失败", e);
return R.fail("发布MQTT消息失败: " + e.getMessage());
}
}
@Operation(summary = "订阅MQTT主题")
@PostMapping("/mqtt/subscribe")
public R<String> subscribeMqtt(
@Parameter(description = "主题") @RequestParam String topic,
@Parameter(description = "QoS等级") @RequestParam(defaultValue = "1") int qos) {
try {
if (mqttService == null) {
return R.fail("MQTT配置未启用");
}
mqttService.subscribe(topic, qos);
return R.ok("订阅成功");
} catch (Exception e) {
log.error("订阅MQTT主题失败", e);
return R.fail("订阅MQTT主题失败: " + e.getMessage());
}
}
@Operation(summary = "取消订阅MQTT主题")
@PostMapping("/mqtt/unsubscribe")
public R<String> unsubscribeMqtt(
@Parameter(description = "主题") @RequestParam String topic) {
try {
if (mqttService == null) {
return R.fail("MQTT配置未启用");
}
mqttService.unsubscribe(topic);
return R.ok("取消订阅成功");
} catch (Exception e) {
log.error("取消订阅MQTT主题失败", e);
return R.fail("取消订阅MQTT主题失败: " + e.getMessage());
}
}
@Operation(summary = "订阅设备实时数据(按产品)")
@PostMapping("/device/data/subscribe")
public R<String> subscribeDeviceData(
@Parameter(description = "产品Key") @RequestParam String productKey) {
try {
if (deviceDataService == null) {
return R.fail("设备数据服务未启用");
}
deviceDataService.subscribeAllDevices(productKey);
return R.ok("订阅成功,设备数据将实时推送");
} catch (Exception e) {
log.error("订阅设备数据失败", e);
return R.fail("订阅设备数据失败: " + e.getMessage());
}
}
}