smart-switch-java/smart-switch-service/src/main/java/com/ruoyi/iot/service/IotReceiveServiceImpl.java

159 lines
5.8 KiB
Java
Raw Normal View History

2024-05-06 18:06:58 +08:00
package com.ruoyi.iot.service;
2024-05-20 16:04:35 +08:00
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StringUtils;
2024-09-06 17:36:41 +08:00
import com.ruoyi.common.utils.collection.CollectionUtils;
2024-05-06 18:06:58 +08:00
import com.ruoyi.iot.constants.ReceiveConstants;
2024-06-04 15:13:00 +08:00
import com.ruoyi.iot.domain.IotDeviceDetail;
2024-05-06 18:06:58 +08:00
import com.ruoyi.iot.domain.ReceiveMsg;
2024-09-06 17:36:41 +08:00
import com.ruoyi.iot.enums.ReceiveStatus;
2024-05-06 18:06:58 +08:00
import com.ruoyi.iot.enums.ReceiveType;
import com.ruoyi.ss.device.domain.Device;
2024-05-20 16:04:35 +08:00
import com.ruoyi.ss.device.domain.enums.DeviceOutageWay;
2024-09-06 17:36:41 +08:00
import com.ruoyi.ss.device.domain.vo.DeviceVO;
2024-07-15 15:26:48 +08:00
import com.ruoyi.ss.device.service.DeviceService;
2024-09-06 17:36:41 +08:00
import com.ruoyi.ss.suit.domain.enums.SuitFeeType;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.service.TransactionAssembler;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
2024-05-06 18:06:58 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
2024-05-20 16:04:35 +08:00
import java.math.BigDecimal;
import java.math.RoundingMode;
2024-09-06 17:36:41 +08:00
import java.util.List;
2024-05-20 16:04:35 +08:00
2024-05-06 18:06:58 +08:00
/**
* @author wjh
* 2024/5/6
*/
@Service
@Slf4j
public class IotReceiveServiceImpl implements IotReceiveService{
@Autowired
2024-07-15 15:26:48 +08:00
private DeviceService deviceService;
2024-05-06 18:06:58 +08:00
2024-09-06 17:36:41 +08:00
@Autowired
private TransactionBillService transactionBillService;
@Autowired
private TransactionAssembler transactionAssembler;
@Autowired
private IotService iotService;
2024-05-06 18:06:58 +08:00
@Override
public void handleReceive(ReceiveMsg msg) {
2024-09-06 17:36:41 +08:00
if (msg == null) {
return;
}
2024-06-04 15:13:00 +08:00
// 数据点推送
if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) {
2024-09-11 17:53:38 +08:00
// 若推送数据点:CSQ,则恢复余额
if (ReceiveConstants.DS_CSQ.equals(msg.getDsId())) {
// this.recoverBalance(msg);
}
2024-05-06 18:06:58 +08:00
}
2024-06-04 15:13:00 +08:00
// 生命周期
else if (ReceiveType.DEVICE_STATUS.getType().equals(msg.getType())) {
2024-09-06 17:36:41 +08:00
}
}
// 恢复设备的余额
private void recoverBalance(ReceiveMsg msg) {
// 查询设备
DeviceVO device = deviceService.selectByMac(msg.getDevName());
if (device == null) {
return;
}
// 查询未结束的订单
TransactionBillQuery query = new TransactionBillQuery();
query.setDeviceId(device.getDeviceId());
query.setIsFinished(false);
query.setStatus(TransactionBillStatus.SUCCESS.getStatus());
query.setSuitFeeTypes(SuitFeeType.singleList());
List<TransactionBillVO> billList = transactionBillService.selectSmTransactionBillList(query);
if (CollectionUtils.isNotEmptyElement(billList)) {
// 拼接剩余时长(秒)/电量(度)
transactionAssembler.assembleSuitSurplus(billList);
long seconds = 0; // 待恢复的时长(秒)
BigDecimal ele = BigDecimal.ZERO; // 待恢复的电量(度)
for (TransactionBillVO bill : billList) {
if (bill.getSuitSurplus().compareTo(BigDecimal.ZERO) > 0) {
if (SuitFeeType.rechargeTimeList().contains(bill.getSuitFeeType())) {
seconds += bill.getSuitSurplus().longValue();
} else if (SuitFeeType.rechargeCountList().contains(bill.getSuitFeeType())) {
ele = ele.add(bill.getSuitSurplus());
}
}
}
if (seconds > 0) {
iotService.setTime(device, seconds);
2024-09-06 17:36:41 +08:00
}
if (ele.compareTo(BigDecimal.ZERO) > 0) {
iotService.setEle(device.getMac(), ele, device.getModelProductId());
}
2024-06-04 15:13:00 +08:00
}
}
// 生命周期转设备信息
private Device parseToDeviceByLife(ReceiveMsg msg) {
Device device = new Device();
2024-06-04 15:13:00 +08:00
device.setMac(msg.getDevName());
device.setOnlineStatus(IotDeviceDetail.Status.toDeviceOnlineStatus(msg.getStatus()).getStatus());
return device;
2024-05-06 18:06:58 +08:00
}
2024-06-04 15:13:00 +08:00
// 数据点数据转设备信息
private Device parseToDevice(ReceiveMsg msg) {
Device device = new Device();
2024-05-06 18:06:58 +08:00
device.setMac(msg.getDevName());
2024-05-20 16:04:35 +08:00
if (StringUtils.hasText(msg.getDsId())) {
String value = msg.getValue().toString();
switch(msg.getDsId()) {
case ReceiveConstants.DS_SSID:
device.setWifi(value);
break;
case ReceiveConstants.DS_V:
device.setVoltage(NumberUtils.nonNullDecimal(value));
break;
case ReceiveConstants.DS_P:
device.setRealTimePower(NumberUtils.nonNullDecimal(value));
break;
case ReceiveConstants.DS_A:
device.setElectricity(NumberUtils.nonNullDecimal(value));
break;
case ReceiveConstants.DS_W:
device.setTotalElectriQuantity(NumberUtils.nonNullDecimal(value).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP));
break;
case ReceiveConstants.DS_S:
device.setPowerStatus(value);
break;
case ReceiveConstants.DS_SET:
DeviceOutageWay deviceOutageWay = DeviceOutageWay.parse(value);
device.setOutageWay(deviceOutageWay.getValue());
break;
case ReceiveConstants.DS_TIME:
device.setRemainTime(NumberUtils.nonNullDecimal(value));
break;
default:
break;
}
}
2024-05-06 18:06:58 +08:00
return device;
}
}