fix: 开关定时任务,后台定时扫描执行。
This commit is contained in:
@@ -41,6 +41,8 @@ import com.intc.iot.service.IotDeviceService;
|
||||
import com.intc.iot.service.MqttService;
|
||||
import com.intc.iot.service.VmsMnsConsumerService;
|
||||
import com.intc.iot.service.VmsNoticeService;
|
||||
import com.intc.iot.service.SwitchTurnResult;
|
||||
import com.intc.iot.service.TimingCtrlExecuteService;
|
||||
import com.intc.iot.handler.DeviceDataHandler;
|
||||
import com.intc.iot.service.WarnCallNoticeService;
|
||||
import com.intc.iot.utils.AliyunAmqpSignUtil;
|
||||
@@ -140,6 +142,9 @@ public class IotController extends BaseController {
|
||||
@Autowired(required = false)
|
||||
private IotCloudService iotCloudService;
|
||||
|
||||
@Autowired(required = false)
|
||||
private TimingCtrlExecuteService timingCtrlExecuteService;
|
||||
|
||||
@Operation(summary = "测试接口")
|
||||
@GetMapping("/test")
|
||||
public R<String> test() {
|
||||
@@ -2258,84 +2263,12 @@ public class IotController extends BaseController {
|
||||
@PutMapping("/switch/turn_switch")
|
||||
public R<Void> turnSwitch(@RequestBody ReqTurnOpen request) {
|
||||
try {
|
||||
if (deviceSwitchMapper == null || deviceMapper == null) {
|
||||
if (timingCtrlExecuteService == null) {
|
||||
return R.fail("系统配置未完成");
|
||||
}
|
||||
|
||||
Long userId = LoginHelper.getUserId();
|
||||
Long switchId = request.getId();
|
||||
Integer targetStatus = request.getIsOpen();
|
||||
|
||||
// 查询开关信息
|
||||
DeviceSwitch deviceSwitch = deviceSwitchMapper.selectById(switchId);
|
||||
if (deviceSwitch == null) {
|
||||
return R.fail("开关不存在");
|
||||
}
|
||||
|
||||
// 查询关联的设备信息
|
||||
Device device = deviceMapper.selectOne(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Device>()
|
||||
.eq(Device::getId, deviceSwitch.getDeviceId())
|
||||
.select(Device::getId, Device::getUserId, Device::getIotId, Device::getSerialNum,
|
||||
Device::getDeviceName, Device::getWarnCode, Device::getDeadTime)
|
||||
);
|
||||
|
||||
if (device == null) {
|
||||
return R.fail("开关不存在或无权限访问");
|
||||
}
|
||||
|
||||
// 检查设备是否到期
|
||||
if (device.getDeadTime() != null && new Date().after(device.getDeadTime())) {
|
||||
return R.fail("设备服务已过期");
|
||||
}
|
||||
|
||||
// 防抖处理:检查距离上次操作是否小于3秒
|
||||
if (deviceSwitch.getLastTurnTime() != null) {
|
||||
long timeDiff = System.currentTimeMillis() - deviceSwitch.getLastTurnTime().getTime();
|
||||
if (timeDiff < 3000) {
|
||||
return R.fail("操作过于频繁,请稍后再试");
|
||||
}
|
||||
}
|
||||
|
||||
// 如果开关状态已经是目标状态,无需操作
|
||||
if (deviceSwitch.getIsOpen() != null && deviceSwitch.getIsOpen().equals(targetStatus)) {
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
// 检查设备是否离线(warnCode为99表示离线)
|
||||
if (device.getWarnCode() == null || device.getWarnCode() == 99) {
|
||||
return R.fail("设备离线或已关机");
|
||||
}
|
||||
|
||||
// 检查IoT云服务是否可用
|
||||
if (iotCloudService == null) {
|
||||
return R.fail("物联网服务未启用");
|
||||
}
|
||||
|
||||
// 构造IoT属性
|
||||
Map<String, Object> properties = new java.util.HashMap<>();
|
||||
properties.put(IOTPropertyName.SWITCH_INDEX + deviceSwitch.getIndex(), targetStatus);
|
||||
|
||||
// 调用物联网服务设置属性
|
||||
boolean success = iotCloudService.setProperty(device.getIotId(), properties, true, 2);
|
||||
if (!success) {
|
||||
return R.fail("设置开关失败");
|
||||
}
|
||||
|
||||
// 更新数据库中的开关状态和最后操作时间
|
||||
deviceSwitchMapper.update(null,
|
||||
new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<DeviceSwitch>()
|
||||
.eq(DeviceSwitch::getId, switchId)
|
||||
.set(DeviceSwitch::getIsOpen, targetStatus)
|
||||
.set(DeviceSwitch::getLastTurnTime, new Date())
|
||||
);
|
||||
|
||||
// 记录操作日志
|
||||
String operation = targetStatus == 1 ? "开启" : "关闭";
|
||||
log.info("开关操作:{}({})的开关{}:{}",
|
||||
device.getDeviceName(), device.getSerialNum(), deviceSwitch.getSwitchName(), operation);
|
||||
|
||||
return R.ok();
|
||||
SwitchTurnResult result = timingCtrlExecuteService.executeSwitch(
|
||||
request.getId(), request.getIsOpen(), "开关操作", null, false);
|
||||
return result.isSuccess() ? R.ok() : R.fail(result.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("开关控制失败: {}", e.getMessage(), e);
|
||||
return R.fail("开关控制失败: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user