electripper-v2/ruoyi-web/src/main/java/com/ruoyi/web/app/AppDeviceIotController.java

102 lines
4.0 KiB
Java
Raw Normal View History

2025-04-11 18:08:08 +08:00
package com.ruoyi.web.app;
2025-04-22 14:27:55 +08:00
import java.math.BigDecimal;
2025-05-17 12:41:41 +08:00
import java.util.function.Supplier;
2025-04-22 14:27:55 +08:00
2025-04-11 18:08:08 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2025-05-13 11:13:58 +08:00
import org.springframework.validation.annotation.Validated;
2025-04-11 18:08:08 +08:00
import org.springframework.web.bind.annotation.PutMapping;
2025-05-13 11:13:58 +08:00
import org.springframework.web.bind.annotation.RequestBody;
2025-04-11 18:08:08 +08:00
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.bst.device.domain.DeviceVO;
2025-05-13 11:13:58 +08:00
import com.ruoyi.bst.device.domain.dto.DeviceBltUploadDTO;
2025-04-11 18:08:08 +08:00
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
2025-04-22 12:00:46 +08:00
import com.ruoyi.common.annotation.Log;
2025-04-11 18:08:08 +08:00
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
2025-05-17 12:41:41 +08:00
import com.ruoyi.common.core.redis.RedisLock;
import com.ruoyi.common.core.redis.enums.RedisLockKey;
2025-04-22 12:00:46 +08:00
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
2025-04-22 14:27:55 +08:00
import com.ruoyi.common.utils.MathUtils;
2025-04-11 18:08:08 +08:00
import com.ruoyi.common.utils.ServiceUtil;
2025-04-22 14:27:55 +08:00
import com.ruoyi.common.utils.map.GeoUtils;
2025-04-11 18:08:08 +08:00
import com.ruoyi.iot.constants.IotConstants;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/app/device/iot")
public class AppDeviceIotController extends BaseController {
@Autowired
private DeviceIotService deviceIotService;
@Autowired
private DeviceService deviceService;
2025-05-17 12:41:41 +08:00
@Autowired
private RedisLock redisLock;
/**
* 执行带设备锁的操作
* @param device 设备信息
* @param operation 具体操作
* @return 操作结果
*/
private AjaxResult executeWithLock(Long deviceId, Supplier<AjaxResult> operation) {
ServiceUtil.assertion(deviceId == null, "设备ID不能为空");
// 获取设备锁
boolean lock = redisLock.lock(RedisLockKey.DEVICE_IOT_OPERATION, deviceId);
if (!lock) {
return AjaxResult.error("设备操作过于频繁,请稍后再试");
}
try {
return operation.get();
} finally {
redisLock.unlock(RedisLockKey.DEVICE_IOT_OPERATION, deviceId);
}
}
private DeviceVO getDevice(Long id, String sn) {
if (id != null) {
return deviceService.selectDeviceById(id);
} else {
return deviceService.selectDeviceBySn(sn);
}
}
2025-04-11 18:08:08 +08:00
@ApiOperation("用户响铃寻车")
@PutMapping("/ring")
2025-04-22 12:00:46 +08:00
@Log(title = "用户响铃寻车", businessType = BusinessType.OTHER, bizIdName = "arg0", bizType = LogBizType.DEVICE)
2025-05-13 11:13:58 +08:00
public AjaxResult ring(@RequestParam(required = false) Long id,
@RequestParam(required = false) String sn,
@RequestParam(required = false) BigDecimal lon,
2025-04-22 14:27:55 +08:00
@RequestParam(required = false) BigDecimal lat) {
2025-05-17 12:41:41 +08:00
DeviceVO device = getDevice(id, sn);
2025-04-15 18:16:16 +08:00
ServiceUtil.assertion(device == null, "当前车辆不存在,无法响铃寻车");
2025-04-22 14:27:55 +08:00
if (device.getAreaRequiredRingRadius() != null && device.getAreaRequiredRingRadius()) {
2025-04-30 11:47:57 +08:00
ServiceUtil.assertion(lon == null || lat == null, "请开启定位后重试");
2025-04-22 14:27:55 +08:00
BigDecimal distance = GeoUtils.calculateDistance(device.getLatitude(), device.getLongitude(), lat, lon);
2025-05-13 11:13:58 +08:00
ServiceUtil.assertion(MathUtils.biggerThan(distance, device.getAreaRingRadius()),
2025-04-22 14:27:55 +08:00
"您当前距离车辆%s米无法响铃寻车。请距离车辆%s米以内再试", distance, device.getAreaRingRadius());
}
2025-05-17 12:41:41 +08:00
return executeWithLock(device.getId(), () -> {
return toAjax(deviceIotService.play(device, IotConstants.PLAY_WARNING, "用户响铃寻车", true));
});
2025-04-11 18:08:08 +08:00
}
2025-05-13 11:13:58 +08:00
@ApiOperation("蓝牙上传设备信息")
@PutMapping("/bltUpload")
@Log(title = "蓝牙上传设备信息", businessType = BusinessType.OTHER, bizType = LogBizType.DEVICE)
public AjaxResult bltUpload(@RequestBody @Validated DeviceBltUploadDTO dto) {
return toAjax(deviceIotService.bltUpload(dto));
}
2025-04-11 18:08:08 +08:00
}