fix(设备中心):增加设备执行任务接口幂等性配置
This commit is contained in:
+50
-7
@@ -5,7 +5,6 @@ import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
|||||||
import com.alibaba.nacos.shaded.com.google.gson.JsonElement;
|
import com.alibaba.nacos.shaded.com.google.gson.JsonElement;
|
||||||
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
|
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
import com.central.common.lock.Lock;
|
|
||||||
import com.central.device.feign.OrderCenterService;
|
import com.central.device.feign.OrderCenterService;
|
||||||
import com.central.device.message.ControlEnum;
|
import com.central.device.message.ControlEnum;
|
||||||
import com.central.device.message.ControlMessageConstant;
|
import com.central.device.message.ControlMessageConstant;
|
||||||
@@ -21,6 +20,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -49,6 +49,9 @@ public class ApiDeviceController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RequestUtils requestUtils;
|
private RequestUtils requestUtils;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
private ControlMessageConstant controlMessageConstant = new ControlMessageConstant();
|
private ControlMessageConstant controlMessageConstant = new ControlMessageConstant();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -78,8 +81,6 @@ public class ApiDeviceController {
|
|||||||
*/
|
*/
|
||||||
@Operation(summary = "设备属性设置")
|
@Operation(summary = "设备属性设置")
|
||||||
@PostMapping("/setting/{deviceId}/property")
|
@PostMapping("/setting/{deviceId}/property")
|
||||||
@Lock(key = "device:writeProperties:#deviceId:#param['order_no']:#param['control']",
|
|
||||||
waitTime = 3, leaseTime = 10, unit = TimeUnit.SECONDS)
|
|
||||||
public Result writeProperties(@RequestBody Map<String, Object> param, @PathVariable("deviceId") String deviceId) {
|
public Result writeProperties(@RequestBody Map<String, Object> param, @PathVariable("deviceId") String deviceId) {
|
||||||
log.info("开始设置设备属性: deviceId={}, param={}", deviceId, param);
|
log.info("开始设置设备属性: deviceId={}, param={}", deviceId, param);
|
||||||
try {
|
try {
|
||||||
@@ -90,23 +91,33 @@ public class ApiDeviceController {
|
|||||||
return Result.failed("参数错误");
|
return Result.failed("参数错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 检查订单状态
|
// 2. 幂等性检查
|
||||||
|
String idempotencyKey = generateIdempotencyKey(deviceId, orderNo, control);
|
||||||
|
if (isRequestProcessed(idempotencyKey)) {
|
||||||
|
log.info("检测到重复请求,返回幂等性响应: deviceId={}, orderNo={}, control={}", deviceId, orderNo, control);
|
||||||
|
return Result.succeed("请求已处理,请勿重复操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 检查订单状态
|
||||||
String orderStatus = orderCenterService.getOrderStatusByOrderNo(orderNo);
|
String orderStatus = orderCenterService.getOrderStatusByOrderNo(orderNo);
|
||||||
if (!isValidOrderStatus(orderStatus)) {
|
if (!isValidOrderStatus(orderStatus)) {
|
||||||
return Result.failed("设备已处理,请关注订单详情");
|
return Result.failed("设备已处理,请关注订单详情");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 构建控制消息
|
// 4. 构建控制消息
|
||||||
JsonObject controlMessage = buildControlMessage(control, orderNo, deviceId);
|
JsonObject controlMessage = buildControlMessage(control, orderNo, deviceId);
|
||||||
if (controlMessage == null) {
|
if (controlMessage == null) {
|
||||||
return Result.failed("暂未开放控制权限");
|
return Result.failed("暂未开放控制权限");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 发送设备控制请求
|
// 5. 发送设备控制请求
|
||||||
String url = baseUrl + "/device/instance/" + deviceId + "/property";
|
String url = baseUrl + "/device/instance/" + deviceId + "/property";
|
||||||
Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
Map<String, String> result = requestUtils.getPostRequestResultMap(url, controlMessage.toString());
|
||||||
|
|
||||||
// 5. 处理响应结果
|
// 6. 标记请求已处理(在成功发送请求后)
|
||||||
|
markRequestAsProcessed(idempotencyKey);
|
||||||
|
|
||||||
|
// 7. 处理响应结果
|
||||||
return handleDeviceResponse(result, orderNo);
|
return handleDeviceResponse(result, orderNo);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -309,4 +320,36 @@ public class ApiDeviceController {
|
|||||||
|
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成幂等性key
|
||||||
|
*/
|
||||||
|
private String generateIdempotencyKey(String deviceId, String orderNo, String control) {
|
||||||
|
return String.format("device:writeProperties:%s:%s:%s", deviceId, orderNo, control);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查请求是否已处理
|
||||||
|
*/
|
||||||
|
private boolean isRequestProcessed(String idempotencyKey) {
|
||||||
|
try {
|
||||||
|
return redisTemplate.hasKey(idempotencyKey);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("检查幂等性key异常: key={}, error={}", idempotencyKey, e.getMessage(), e);
|
||||||
|
return false; // Redis异常时不阻止请求
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记请求已处理
|
||||||
|
*/
|
||||||
|
private void markRequestAsProcessed(String idempotencyKey) {
|
||||||
|
try {
|
||||||
|
// 设置5分钟过期时间,防止Redis内存占用过多
|
||||||
|
redisTemplate.opsForValue().set(idempotencyKey, "processed", 5, TimeUnit.MINUTES);
|
||||||
|
log.debug("标记请求已处理: key={}", idempotencyKey);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("标记幂等性key异常: key={}, error={}", idempotencyKey, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user