临时提交

This commit is contained in:
墨大叔 2024-10-14 13:29:54 +08:00
parent 70bfea97e7
commit 4cc8ab112c
6 changed files with 141 additions and 49 deletions

View File

@ -175,4 +175,21 @@ public interface IotService {
* 获取历史数据
*/
HistoryDeviceData getHistoryDataPoint(String deviceName, String productId);
/**
* 设置设备时长和电量
* @param device 设备
* @param seconds 时长
* @param ele 电量
*/
int setTimeAndEle(IotDevice device, long seconds, BigDecimal ele);
/**
* 设置设备时长和电量
* @param mac mac
* @param productId 产品ID
* @param seconds
* @param ele 电量
*/
int setTimeAndEle(String mac, String productId, long seconds, BigDecimal ele);
}

View File

@ -184,6 +184,31 @@ public class IotServiceImpl implements IotService {
return response.getData();
}
@Override
public int setTimeAndEle(IotDevice device, long seconds, BigDecimal ele) {
try {
int result1 = this.setTimeAndEle(device.iotMac1(), device.getProductId(), seconds, ele);
ServiceUtil.assertion(result1 != 1, "mac1设置时间与电量失败");
return result1;
} catch (Exception e) {
int result2 = this.setTimeAndEle(device.iotMac2(), device.getProductId(), seconds, ele);
ServiceUtil.assertion(result2 != 1, "设置时间与电量失败" );
return result2;
}
}
@Override
public int setTimeAndEle(String mac, String productId, long seconds, BigDecimal ele) {
if (StringUtils.isAnyBlank(mac, productId) || ele == null) {
return 0;
}
StringBuilder sb = new StringBuilder();
sb.append(IotConstants.COMMAND_RECHARGE).append(seconds).append(IotConstants.COMMAND_SEPARATOR)
.append(IotConstants.COMMAND_SET_ELE).append(ele.multiply(BigDecimal.valueOf(1000))).append(IotConstants.COMMAND_SEPARATOR);
CommandResponse res = sendCommand(mac, sb.toString(), productId);
return res.isSuccess() ? 1 : 0;
}
// 获取当前设备数据点信息
public List<CurrentDeviceData> getCurrentDataPoint(List<String> deviceNames, String productId) {
String param = "device_name=" + String.join(",", deviceNames) + "&product_id=" + productId;

View File

@ -147,8 +147,8 @@ public interface DeviceService
*
* @param deviceId 设备id
*/
default int resetTimeWithBill(Long deviceId) {
return resetTimeWithBill(deviceId, true);
default int resetWithBill(Long deviceId) {
return resetWithBill(deviceId, true);
}
/**
@ -157,7 +157,7 @@ public interface DeviceService
* @param deviceId 设备id
* @param requiredIot 是否操作设备
*/
int resetTimeWithBill(Long deviceId, boolean requiredIot);
int resetWithBill(Long deviceId, boolean requiredIot);
/**
* 设备是否已经被绑定

View File

@ -35,6 +35,7 @@ import com.ruoyi.ss.deviceSuit.service.DeviceSuitService;
import com.ruoyi.ss.model.domain.SmModelVO;
import com.ruoyi.ss.model.domain.enums.ModelTag;
import com.ruoyi.ss.model.service.ModelService;
import com.ruoyi.ss.record.time.domain.RecordTime;
import com.ruoyi.ss.record.time.domain.enums.RecordTimeType;
import com.ruoyi.ss.record.time.service.IRecordTimeService;
import com.ruoyi.ss.record.time.service.RecordTimeConverter;
@ -440,14 +441,9 @@ public class DeviceServiceImpl implements DeviceService
@Override
public int resetTime(DeviceVO device, boolean required) {
LocalDateTime now = LocalDateTime.now();
Integer result = transactionTemplate.execute(status -> {
// 归零时间将过期时间设置为现在
Device form = new Device();
form.setDeviceId(device.getDeviceId());
form.setExpireTime(now);
form.setStatus(DeviceStatus.NORMAL.getStatus());
int update = deviceMapper.updateSmDevice(form);
int update = this.resetUpdateDbTime(device.getDeviceId());
ServiceUtil.assertion(update != 1, "修改设备时间失败");
// 物联网设备归零
@ -469,19 +465,21 @@ public class DeviceServiceImpl implements DeviceService
});
// 归零记录
LoginUser loginUser = SecurityUtils.getLoginUser();
if (result != null && result == 1 ) {
scheduledExecutorService.schedule(() -> {
// 设备剩余时长
Duration duration = Duration.between(now, device.getExpireTime());
long seconds = duration.getSeconds() > 0 ? duration.getSeconds() : 0;
recordTimeService.insertRecordTime(recordTimeConverter.toRecordTime(device, -seconds, "设备归零", loginUser, RecordTimeType.TIME.getType()));
}, 0, TimeUnit.SECONDS);
this.resetRecord(device, RecordTimeType.TIME, "设备归零");
}
return result == null ? 0 : result;
}
private int resetUpdateDbTime(Long deviceId) {
Device form = new Device();
form.setDeviceId(deviceId);
form.setExpireTime(LocalDateTime.now());
form.setStatus(DeviceStatus.NORMAL.getStatus());
return deviceMapper.updateSmDevice(form);
}
@Override
public int resetEle(DeviceVO device, boolean required) {
if (device == null) {
@ -490,15 +488,12 @@ public class DeviceServiceImpl implements DeviceService
Integer result = transactionTemplate.execute(status -> {
// 更新剩余电量
Device data = new Device();
data.setDeviceId(device.getDeviceId());
data.setSurplusEle(BigDecimal.ZERO);
int update = deviceMapper.updateSmDevice(data);
int update = this.resetUpdateDbEle(device.getDeviceId());
ServiceUtil.assertion(update != 1, "更新剩余电量失败");
// 直接设置电量为0
try {
CommandResponse res = iotService.setEle(device, BigDecimal.valueOf(0));
CommandResponse res = iotService.setEle(device, BigDecimal.ZERO);
ServiceUtil.assertion(res == null, "归零电量失败,返回值为空");
ServiceUtil.assertion(!res.isSuccess(), "归零电量失败,请检查设备是否在线或联系管理员");
} catch (Exception e) {
@ -511,16 +506,48 @@ public class DeviceServiceImpl implements DeviceService
});
// 归零记录
LoginUser loginUser = SecurityUtils.getLoginUser();
if (result != null && result == 1 ) {
scheduledExecutorService.schedule(() -> {
recordTimeService.insertRecordTime(recordTimeConverter.toRecordTime(device, device.getSurplusEle().negate().intValue(), "设备归零", loginUser, RecordTimeType.ELE.getType()));
}, 0, TimeUnit.SECONDS);
this.resetRecord(device, RecordTimeType.ELE, "设备归零");
}
return result == null ? 0 : result;
}
private void resetRecord(DeviceVO device, RecordTimeType type, String reason) {
LoginUser loginUser = SecurityUtils.getLoginUser();
scheduledExecutorService.schedule(() -> {
RecordTime record = null;
// 电量
if (RecordTimeType.ELE.equals(type)) {
int amount = device.getSurplusEle().negate().intValue();
record = recordTimeConverter.toRecordTime(
device,
amount,
reason,
loginUser,
RecordTimeType.ELE.getType()
);
}
// 时长
else if (RecordTimeType.TIME.equals(type)) {
Duration duration = Duration.between(LocalDateTime.now(), device.getExpireTime());
long seconds = duration.getSeconds() > 0 ? duration.getSeconds() : 0;
record = recordTimeConverter.toRecordTime(device, -seconds, "设备归零", loginUser, RecordTimeType.TIME.getType());
}
if (record != null) {
recordTimeService.insertRecordTime(record);
}
}, 0, TimeUnit.SECONDS);
}
private int resetUpdateDbEle(Long deviceId) {
Device data = new Device();
data.setDeviceId(deviceId);
data.setSurplusEle(BigDecimal.ZERO);
return deviceMapper.updateSmDevice(data);
}
@Override
public int resetEle(Long deviceId, boolean required) {
// 获取设备信息
@ -567,10 +594,20 @@ public class DeviceServiceImpl implements DeviceService
return false;
}
boolean result = false;
if (withIot) {
CommandResponse res = iotService.addEle(device, ele);
return res.isSuccess();
result = res.isSuccess();
}
// 记录下操作日志
if (result) {
LoginUser loginUser = SecurityUtils.getLoginUser();
scheduledExecutorService.schedule(() -> {
recordTimeService.insertRecordTime(recordTimeConverter.toRecordTime(device, ele.longValue(), reason, loginUser, RecordTimeType.ELE.getType()));
}, 0, TimeUnit.SECONDS);
}
return true;
}
@ -1051,7 +1088,7 @@ public class DeviceServiceImpl implements DeviceService
}
@Override
public int resetTimeWithBill(Long deviceId, boolean requiredIot) {
public int resetWithBill(Long deviceId, boolean requiredIot) {
ServiceUtil.assertion(deviceId == null, "设备id不能为空");
// 拉取最新设备信息
@ -1062,12 +1099,12 @@ public class DeviceServiceImpl implements DeviceService
ServiceUtil.assertion(device == null || device.getDeleted(), "设备不存在");
ServiceUtil.assertion(!UserUtil.hasFrontUser(device.getUserId()), "您不是该设备的商户,无法进行该操作");
Integer result = transactionTemplate.execute(status -> {
// 尝试关闭该设备未结束的所有订单
TransactionBillQuery query = new TransactionBillQuery();
query.setStatusList(TransactionBillStatus.canClose());
query.setDeviceId(device.getDeviceId());
query.setIsFinished(false); // 未结束的订单都会被关闭
query.setSuitFeeTypes(SuitFeeType.rechargeTimeList());
List<TransactionBillVO> billList = transactionBillService.selectSmTransactionBillList(query);
try {
int closeCount = transactionBillService.batchCloseBillByDevice(billList, false, device);
@ -1076,11 +1113,25 @@ public class DeviceServiceImpl implements DeviceService
log.error("关闭订单失败: {}", e.getMessage());
}
// 修改设备信息
int timeUpdate = this.resetUpdateDbTime(deviceId);
ServiceUtil.assertion(timeUpdate != 1, "修改设备时间失败");
int eleUpdate = this.resetUpdateDbEle(deviceId);
ServiceUtil.assertion(eleUpdate != 1, "修改设备电量失败");
// 发送命令
int reset = this.resetTime(device, requiredIot);
int reset = iotService.setTimeAndEle(device, 0L, BigDecimal.ZERO);
ServiceUtil.assertion(reset != 1, "归零失败");
return reset;
});
if (result != null && result == 1) {
this.resetRecord(device, RecordTimeType.TIME, "设备归零");
this.resetRecord(device, RecordTimeType.ELE, "设备归零");
}
return result == null ? 0 : result;
}
/**

View File

@ -139,10 +139,9 @@ public class AppDeviceController extends BaseController {
@Log(title = "设备归零", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
@ApiOperation("设备归零")
@PutMapping("{deviceId}/reset")
public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId, @RequestParam(required = false, defaultValue = "true") Boolean requiredIot) {
int r1 = smDeviceService.resetTimeWithBill(deviceId, requiredIot);
int r2 = smDeviceService.resetEleWithBill(deviceId, requiredIot);
return toAjax(r1 >= 0 && r2 >= 0);
public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId,
@RequestParam(required = false, defaultValue = "true") Boolean requiredIot) {
return success(smDeviceService.resetWithBill(deviceId, requiredIot));
}
@ApiOperation("获取设备用电量分析")

View File

@ -176,7 +176,7 @@ public class SmDeviceController extends BaseController
@Log(title = "设备时长归零", businessType = BusinessType.OTHER)
@PutMapping("/{deviceId}/reset")
public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId) {
return toAjax(deviceService.resetTimeWithBill(deviceId));
return toAjax(deviceService.resetWithBill(deviceId));
}
@PreAuthorize("@ss.hasPermi('system:device:resetEle')")