This commit is contained in:
墨大叔 2024-05-23 09:31:16 +08:00
parent 765a795e36
commit 5162eb39c2
3 changed files with 21 additions and 13 deletions

View File

@ -71,11 +71,12 @@ public interface IotService {
IotDeviceDetail getDeviceDetail(String deviceName);
/**
* 设备电量充值
* 设置剩余时长
*
* @param deviceName 设备名称
* @param num 充值度数
* @param seconds 时长
*/
boolean recharge(String deviceName, BigDecimal num);
boolean setTime(String deviceName, BigDecimal seconds);
/**
* 更新设备信息

View File

@ -168,21 +168,22 @@ public class IotServiceImpl implements IotService {
}
/**
* 充值设备
* 设置剩余时长
*
* @param deviceName 设备名称
* @param num 充值度数
* @param seconds 时长
* @return 是否成功
*/
@Override
@Transactional
public boolean recharge(String deviceName, BigDecimal num) {
if (num == null || BigDecimal.ZERO.compareTo(num) > 0) {
log.error("充值设备参数错误读数不允许为空或者小于0");
public boolean setTime(String deviceName, BigDecimal seconds) {
if (seconds == null || BigDecimal.ZERO.compareTo(seconds) > 0) {
log.error("设置剩余时长参数错误读数不允许为空或者小于0");
return false;
}
CommandResponse response = sendCommand(deviceName, IotConstants.COMMAND_RECHARGE + num.multiply(new BigDecimal(60)) + IotConstants.COMMAND_SEPARATOR);
CommandResponse response = sendCommand(deviceName, IotConstants.COMMAND_RECHARGE + seconds + IotConstants.COMMAND_SEPARATOR);
if (!Objects.equals(HttpStatus.IOT_SUCCESS, response.getCode())) {
log.error("充值设备发生异常:" + response.getMsg());
log.error("设置剩余时长发生异常:" + response.getMsg());
return false;
}
return true;

View File

@ -280,8 +280,12 @@ public class SmDeviceServiceImpl implements ISmDeviceService
changeStatus(deviceId, DeviceStatus.USING);
// 物联网设备增加时长
boolean rechargeResult = iotService.recharge(device.getMac(), num);
ServiceUtil.assertion(!rechargeResult, "设备充值失败");
SmDeviceVo newDevice = selectSmDeviceByDeviceId(deviceId);
long seconds = Duration.between(LocalDateTime.now(), newDevice.getExpireTime()).getSeconds();
if (seconds > 0) {
boolean rechargeResult = iotService.setTime(device.getMac(), new BigDecimal(seconds));
ServiceUtil.assertion(!rechargeResult, "设备充值失败");
}
return Boolean.TRUE;
});
@ -539,7 +543,9 @@ public class SmDeviceServiceImpl implements ISmDeviceService
form.setStatus(DeviceStatus.NORMAL.getStatus());
smDeviceMapper.updateSmDevice(form);
// TODO 物联网设备归零
// 物联网设备归零
boolean b = iotService.setTime(device.getMac(), BigDecimal.ONE);
ServiceUtil.assertion(!b, "操作设备发生异常");
// 归零记录
scheduledExecutorService.schedule(() -> {