package com.ruoyi.iot.service; import com.ruoyi.common.utils.NumberUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.collection.CollectionUtils; import com.ruoyi.iot.constants.ReceiveConstants; import com.ruoyi.iot.domain.IotDeviceDetail; import com.ruoyi.iot.domain.ReceiveMsg; import com.ruoyi.iot.enums.ReceiveStatus; import com.ruoyi.iot.enums.ReceiveType; import com.ruoyi.ss.device.domain.Device; import com.ruoyi.ss.device.domain.enums.DeviceOutageWay; import com.ruoyi.ss.device.domain.vo.DeviceVO; import com.ruoyi.ss.device.service.DeviceService; 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; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; /** * @author wjh * 2024/5/6 */ @Service @Slf4j public class IotReceiveServiceImpl implements IotReceiveService{ @Autowired private DeviceService deviceService; @Autowired private TransactionBillService transactionBillService; @Autowired private TransactionAssembler transactionAssembler; @Autowired private IotService iotService; @Override public void handleReceive(ReceiveMsg msg) { if (msg == null) { return; } // 数据点推送 if (ReceiveType.DATA_POINT.getType().equals(msg.getType())) { // 若推送数据点:CSQ,则恢复余额 if (ReceiveConstants.DS_CSQ.equals(msg.getDsId())) { // this.recoverBalance(msg); } } // 生命周期 else if (ReceiveType.DEVICE_STATUS.getType().equals(msg.getType())) { } } // 恢复设备的余额 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 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); } if (ele.compareTo(BigDecimal.ZERO) > 0) { iotService.setEle(device.getMac(), ele, device.getModelProductId()); } } } // 生命周期转设备信息 private Device parseToDeviceByLife(ReceiveMsg msg) { Device device = new Device(); device.setMac(msg.getDevName()); device.setOnlineStatus(IotDeviceDetail.Status.toDeviceOnlineStatus(msg.getStatus()).getStatus()); return device; } // 数据点数据转设备信息 private Device parseToDevice(ReceiveMsg msg) { Device device = new Device(); device.setMac(msg.getDevName()); 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; } } return device; } }