2
0

fix: 带宽限速调整

This commit is contained in:
caiyuchao
2025-01-03 17:46:39 +08:00
parent bb726cc05f
commit 1fbc95e907
8 changed files with 333 additions and 155 deletions

View File

@@ -6,4 +6,5 @@ package org.wfc.user.api;
* @since: 2024-12-30
*/
public abstract class AbstractWifiApi implements IWifiApi {
}

View File

@@ -15,5 +15,6 @@ public interface IWifiApi {
boolean reconnectClient(String siteId, String clientMac);
boolean updateClientRateLimitSetting(String siteId, String clientMac, ClientRateLimitSettingDto clientRateLimitSettingDto);
boolean updateClientRateLimitSetting(String siteId, String clientMac, ClientRateLimitSettingDto settingDto);
}

View File

@@ -1,19 +1,33 @@
package org.wfc.user.api.omada;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.wfc.omada.api.client.OmadaClientApi;
import org.wfc.omada.api.client.model.ClientRateLimitSetting;
import org.wfc.omada.api.client.model.CustomRateLimitEntity;
import org.wfc.omada.api.client.model.OperationResponseWithoutResult;
import org.wfc.omada.api.hotspot.OmadaAuthorizedClientApi;
import org.wfc.user.api.omada.domain.convert.OmadaConvert;
import org.wfc.omada.api.sitesetting.OmadaProfilesApi;
import org.wfc.omada.api.sitesetting.model.CreateRateLimitProfileOpenApiVo;
import org.wfc.omada.api.sitesetting.model.OperationResponseListRateLimitProfileOpenApiVo;
import org.wfc.omada.api.sitesetting.model.RateLimitProfileOpenApiVo;
import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @description: Omada API
* @author: cyc
* @since: 2024-12-30
*/
@Slf4j
public class OmadaWifiApi extends AbstractOmadaWifiApi {
@Autowired
@@ -22,6 +36,12 @@ public class OmadaWifiApi extends AbstractOmadaWifiApi {
@Autowired
private OmadaClientApi omadaClientApi;
@Autowired
private OmadaProfilesApi omadaProfilesApi;
private static final int KBPS_LIMIT_MAX = 1048576;
private static final int MBPS_LIMIT_MAX = 1024;
@Override
public boolean authClient(String siteId, String clientMac) {
omadaAuthorizedClientApi.authClient(siteId, clientMac);
@@ -41,9 +61,111 @@ public class OmadaWifiApi extends AbstractOmadaWifiApi {
}
@Override
public boolean updateClientRateLimitSetting(String siteId, String clientMac, ClientRateLimitSettingDto clientRateLimitSettingDto) {
ClientRateLimitSetting clientRateLimitSetting = OmadaConvert.INSTANCE.toClientRateLimitSettingDto(clientRateLimitSettingDto);
public boolean updateClientRateLimitSetting(String siteId, String clientMac, ClientRateLimitSettingDto settingDto) {
initRateLimitSettingDto(settingDto);
ClientRateLimitSetting clientRateLimitSetting = new ClientRateLimitSetting();
if (settingDto.getRateLimitEnable() && ((settingDto.getUpLimitEnable() && settingDto.getUpLimit() > KBPS_LIMIT_MAX)
|| (settingDto.getDownLimitEnable() && settingDto.getDownLimit() > KBPS_LIMIT_MAX))) {
// 大于1024Mbps走Profile限速
Optional<RateLimitProfileOpenApiVo> rateLimitOptional = getRateLimitByProfile(siteId, settingDto);
String profileId = null;
if (rateLimitOptional.isPresent()) {
profileId = rateLimitOptional.get().getProfileId();
} else {
CreateRateLimitProfileOpenApiVo newRateLimit = new CreateRateLimitProfileOpenApiVo();
newRateLimit.setName(UUID.randomUUID().toString());
newRateLimit.setDownLimitEnable(settingDto.getDownLimitEnable());
newRateLimit.setDownLimit(settingDto.getDownLimit());
newRateLimit.setUpLimitEnable(settingDto.getUpLimitEnable());
newRateLimit.setUpLimit(settingDto.getUpLimit());
omadaProfilesApi.createRateLimitProfile(siteId, newRateLimit);
Optional<RateLimitProfileOpenApiVo> newRateLimitOptional = getRateLimitByProfile(siteId, settingDto);
if (newRateLimitOptional.isPresent()) {
profileId = newRateLimitOptional.get().getProfileId();
}
}
if (profileId == null) {
return false;
}
clientRateLimitSetting.setMode(1);
clientRateLimitSetting.setRateLimitProfileId(profileId);
} else {
// 否则走Custom限速
clientRateLimitSetting.setMode(0);
CustomRateLimitEntity custom = new CustomRateLimitEntity();
if (!settingDto.getRateLimitEnable()) {
custom.setDownEnable(false);
custom.setUpEnable(false);
custom.setDownUnit(1);
custom.setDownLimit(1);
custom.setUpUnit(1);
custom.setUpLimit(1);
} else {
custom.setDownEnable(settingDto.getDownLimitEnable());
if (settingDto.getDownLimitEnable() && settingDto.getDownLimit() > MBPS_LIMIT_MAX) {
// Down Mbps
custom.setDownUnit(2);
double downLimitDouble = NumberUtil.div(settingDto.getDownLimit().intValue(), MBPS_LIMIT_MAX, 0, RoundingMode.HALF_UP);
custom.setDownLimit((int) downLimitDouble);
} else if (settingDto.getDownLimitEnable()) {
// Down Kbps
custom.setDownUnit(1);
custom.setDownLimit(settingDto.getDownLimit().intValue());
}
custom.setUpEnable(settingDto.getUpLimitEnable());
if (settingDto.getUpLimitEnable() && settingDto.getUpLimit() > MBPS_LIMIT_MAX) {
// Up Kbps
custom.setUpUnit(2);
double upLimitDouble = NumberUtil.div(settingDto.getUpLimit().intValue(), MBPS_LIMIT_MAX, 0, RoundingMode.HALF_UP);
custom.setUpLimit((int) upLimitDouble);
} else if (settingDto.getUpLimitEnable()) {
// Up Mbps
custom.setUpUnit(1);
custom.setUpLimit(settingDto.getUpLimit().intValue());
}
}
if (!settingDto.getDownLimitEnable()) {
custom.setDownUnit(1);
custom.setDownLimit(1);
}
if (!settingDto.getUpLimitEnable()) {
custom.setUpUnit(1);
custom.setUpLimit(1);
}
clientRateLimitSetting.setCustomRateLimit(custom);
}
ResponseEntity<OperationResponseWithoutResult> response = omadaClientApi.updateClientRateLimitSetting(siteId, clientMac, clientRateLimitSetting);
OperationResponseWithoutResult body = response.getBody();
if (ObjectUtil.isNotNull(body) && body.getErrorCode() != 0) {
log.error("omada api rate limit errorCode {}, {}", body.getErrorCode(), body.getMsg());
}
return true;
}
private void initRateLimitSettingDto(ClientRateLimitSettingDto settingDto) {
if (ObjectUtil.isNull(settingDto.getRateLimitEnable())) {
settingDto.setRateLimitEnable(false);
}
if (ObjectUtil.isNull(settingDto.getUpLimitEnable())) {
settingDto.setUpLimitEnable(false);
}
if (ObjectUtil.isNull(settingDto.getDownLimitEnable())) {
settingDto.setDownLimitEnable(false);
}
}
private Optional<RateLimitProfileOpenApiVo> getRateLimitByProfile(String siteId, ClientRateLimitSettingDto settingDto) {
ResponseEntity<OperationResponseListRateLimitProfileOpenApiVo> rateLimitProfileList = omadaProfilesApi.getRateLimitProfileList(siteId);
if (ObjectUtil.isNull(rateLimitProfileList.getBody())) {
return Optional.empty();
}
List<RateLimitProfileOpenApiVo> rateLimitProfiles = rateLimitProfileList.getBody().getResult();
return rateLimitProfiles.stream().filter(rateLimit -> Objects.equals(settingDto.getDownLimitEnable(), rateLimit.getDownLimitEnable())
&& Objects.equals(settingDto.getUpLimitEnable(), rateLimit.getUpLimitEnable())
&& Objects.equals(settingDto.getDownLimit(), rateLimit.getDownLimit())
&& Objects.equals(settingDto.getUpLimit(), rateLimit.getUpLimit())).findFirst();
}
}

View File

@@ -2,8 +2,8 @@ package org.wfc.user.api.omada.domain.convert;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.wfc.omada.api.client.model.ClientRateLimitSetting;
import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto;
import org.wfc.user.domain.UAccount;
/**
* @description: OmadaMapping
@@ -15,5 +15,6 @@ public interface OmadaConvert {
OmadaConvert INSTANCE = Mappers.getMapper(OmadaConvert.class);
ClientRateLimitSetting toClientRateLimitSettingDto(ClientRateLimitSettingDto dto);
ClientRateLimitSettingDto toClientRateLimitSettingDto(UAccount account);
}

View File

@@ -1,130 +1,27 @@
package org.wfc.user.api.omada.domain.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* ClientRateLimitSetting
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-12-04T10:16:05.926+08:00[Asia/Shanghai]")
@Data
public class ClientRateLimitSettingDto {
@JsonProperty("mode")
private Integer mode;
@JsonProperty("rateLimitProfileId")
private String rateLimitProfileId;
@Schema(description = "带宽是否限制")
private Boolean rateLimitEnable;
@JsonProperty("customRateLimit")
private CustomRateLimitEntityDto customRateLimit;
@Schema(description = "下行限速")
private Long downLimit;
public ClientRateLimitSettingDto mode(Integer mode) {
this.mode = mode;
return this;
}
@Schema(description = "下行限速启用")
private Boolean downLimitEnable;
/**
* Rate limit mode should be a value as follows: <br/>0: Custom mode. Apply the given rate limit value to the client; <br/>1: Rate limit profile mode. Find the corresponding rate limit file with rate limit ID and apply it to the client.
* @return mode
*/
@ApiModelProperty(required = true, value = "Rate limit mode should be a value as follows: <br/>0: Custom mode. Apply the given rate limit value to the client; <br/>1: Rate limit profile mode. Find the corresponding rate limit file with rate limit ID and apply it to the client.")
@NotNull
@Schema(description = "上行限速")
private Long upLimit;
public Integer getMode() {
return mode;
}
public void setMode(Integer mode) {
this.mode = mode;
}
public ClientRateLimitSettingDto rateLimitProfileId(String rateLimitProfileId) {
this.rateLimitProfileId = rateLimitProfileId;
return this;
}
/**
* Rate limit profile ID. Required when ratelimit mode is 1
* @return rateLimitProfileId
*/
@ApiModelProperty(value = "Rate limit profile ID. Required when ratelimit mode is 1")
public String getRateLimitProfileId() {
return rateLimitProfileId;
}
public void setRateLimitProfileId(String rateLimitProfileId) {
this.rateLimitProfileId = rateLimitProfileId;
}
public ClientRateLimitSettingDto customRateLimit(CustomRateLimitEntityDto customRateLimit) {
this.customRateLimit = customRateLimit;
return this;
}
/**
* Get customRateLimit
* @return customRateLimit
*/
@ApiModelProperty(value = "")
@Valid
public CustomRateLimitEntityDto getCustomRateLimit() {
return customRateLimit;
}
public void setCustomRateLimit(CustomRateLimitEntityDto customRateLimit) {
this.customRateLimit = customRateLimit;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClientRateLimitSettingDto clientRateLimitSetting = (ClientRateLimitSettingDto) o;
return Objects.equals(this.mode, clientRateLimitSetting.mode) &&
Objects.equals(this.rateLimitProfileId, clientRateLimitSetting.rateLimitProfileId) &&
Objects.equals(this.customRateLimit, clientRateLimitSetting.customRateLimit);
}
@Override
public int hashCode() {
return Objects.hash(mode, rateLimitProfileId, customRateLimit);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ClientRateLimitSetting {\n");
sb.append(" mode: ").append(toIndentedString(mode)).append("\n");
sb.append(" rateLimitProfileId: ").append(toIndentedString(rateLimitProfileId)).append("\n");
sb.append(" customRateLimit: ").append(toIndentedString(customRateLimit)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
@Schema(description = "上行限速启用")
private Boolean upLimitEnable;
}

View File

@@ -0,0 +1,181 @@
package org.wfc.user.api.omada.domain.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Objects;
/**
* CreateRateLimitProfileOpenApiVo
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-12-04T11:32:19.054+08:00[Asia/Shanghai]")
public class CreateRateLimitProfileOpenApiDto {
@JsonProperty("name")
private String name;
@JsonProperty("downLimitEnable")
private Boolean downLimitEnable;
@JsonProperty("downLimit")
private Long downLimit;
@JsonProperty("upLimitEnable")
private Boolean upLimitEnable;
@JsonProperty("upLimit")
private Long upLimit;
public CreateRateLimitProfileOpenApiDto name(String name) {
this.name = name;
return this;
}
/**
* Rate limit profile name should contain 1 to 64 characters.
* @return name
*/
@ApiModelProperty(required = true, value = "Rate limit profile name should contain 1 to 64 characters.")
@NotNull
@Pattern(regexp="^[^ ]$|^[^ ].{0,62}[^ ]$")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CreateRateLimitProfileOpenApiDto downLimitEnable(Boolean downLimitEnable) {
this.downLimitEnable = downLimitEnable;
return this;
}
/**
* Whether to enable download limit
* @return downLimitEnable
*/
@ApiModelProperty(required = true, value = "Whether to enable download limit")
@NotNull
public Boolean getDownLimitEnable() {
return downLimitEnable;
}
public void setDownLimitEnable(Boolean downLimitEnable) {
this.downLimitEnable = downLimitEnable;
}
public CreateRateLimitProfileOpenApiDto downLimit(Long downLimit) {
this.downLimit = downLimit;
return this;
}
/**
* Download limit(Unit: Kbps), this field is required when parameter [downLimitEnable] is true; It should be within the range of 110485760.
* @return downLimit
*/
@ApiModelProperty(value = "Download limit(Unit: Kbps), this field is required when parameter [downLimitEnable] is true; It should be within the range of 110485760.")
public Long getDownLimit() {
return downLimit;
}
public void setDownLimit(Long downLimit) {
this.downLimit = downLimit;
}
public CreateRateLimitProfileOpenApiDto upLimitEnable(Boolean upLimitEnable) {
this.upLimitEnable = upLimitEnable;
return this;
}
/**
* Whether to enable upload limit
* @return upLimitEnable
*/
@ApiModelProperty(required = true, value = "Whether to enable upload limit")
@NotNull
public Boolean getUpLimitEnable() {
return upLimitEnable;
}
public void setUpLimitEnable(Boolean upLimitEnable) {
this.upLimitEnable = upLimitEnable;
}
public CreateRateLimitProfileOpenApiDto upLimit(Long upLimit) {
this.upLimit = upLimit;
return this;
}
/**
* Upload limit(Unit: Kbps), this field is required when parameter [upLimitEnable] is true; It should be within the range of 110485760.
* @return upLimit
*/
@ApiModelProperty(value = "Upload limit(Unit: Kbps), this field is required when parameter [upLimitEnable] is true; It should be within the range of 110485760.")
public Long getUpLimit() {
return upLimit;
}
public void setUpLimit(Long upLimit) {
this.upLimit = upLimit;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CreateRateLimitProfileOpenApiDto createRateLimitProfileOpenApiVo = (CreateRateLimitProfileOpenApiDto) o;
return Objects.equals(this.name, createRateLimitProfileOpenApiVo.name) &&
Objects.equals(this.downLimitEnable, createRateLimitProfileOpenApiVo.downLimitEnable) &&
Objects.equals(this.downLimit, createRateLimitProfileOpenApiVo.downLimit) &&
Objects.equals(this.upLimitEnable, createRateLimitProfileOpenApiVo.upLimitEnable) &&
Objects.equals(this.upLimit, createRateLimitProfileOpenApiVo.upLimit);
}
@Override
public int hashCode() {
return Objects.hash(name, downLimitEnable, downLimit, upLimitEnable, upLimit);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class CreateRateLimitProfileOpenApiVo {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" downLimitEnable: ").append(toIndentedString(downLimitEnable)).append("\n");
sb.append(" downLimit: ").append(toIndentedString(downLimit)).append("\n");
sb.append(" upLimitEnable: ").append(toIndentedString(upLimitEnable)).append("\n");
sb.append(" upLimit: ").append(toIndentedString(upLimit)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}

View File

@@ -16,8 +16,8 @@ import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.omada.api.client.OmadaClientApi;
import org.wfc.user.api.IWifiApi;
import org.wfc.user.api.domain.bo.UClientBo;
import org.wfc.user.api.omada.domain.convert.OmadaConvert;
import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto;
import org.wfc.user.api.omada.domain.dto.CustomRateLimitEntityDto;
import org.wfc.user.domain.UAccount;
import org.wfc.user.domain.UClient;
import org.wfc.user.domain.vo.UAccountDashboardVo;
@@ -95,11 +95,7 @@ public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> i
// 取消带宽限速
ClientRateLimitSettingDto clientRateLimitSetting = new ClientRateLimitSettingDto();
clientRateLimitSetting.setMode(0);
CustomRateLimitEntityDto customRateLimitEntityDto = new CustomRateLimitEntityDto();
customRateLimitEntityDto.setDownEnable(false);
customRateLimitEntityDto.setUpEnable(false);
clientRateLimitSetting.setCustomRateLimit(customRateLimitEntityDto);
clientRateLimitSetting.setRateLimitEnable(false);
wifiApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
} catch (Exception e) {
log.info("unAuth error: {}", e.getMessage());
@@ -119,19 +115,8 @@ public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> i
wifiApi.authClient(client.getSiteId(), client.getClientMac());
// 带宽限速
if (account.getRateLimitEnable()) {
ClientRateLimitSettingDto clientRateLimitSetting = new ClientRateLimitSettingDto();
clientRateLimitSetting.setMode(0);
CustomRateLimitEntityDto customRateLimitEntityDto = new CustomRateLimitEntityDto();
customRateLimitEntityDto.setDownEnable(account.getDownLimitEnable());
customRateLimitEntityDto.setDownLimit(account.getDownLimit() == null ? 0 : account.getDownLimit().intValue());
customRateLimitEntityDto.setDownUnit(1);
customRateLimitEntityDto.setUpEnable(account.getUpLimitEnable());
customRateLimitEntityDto.setUpLimit(account.getUpLimit() == null ? 0 : account.getUpLimit().intValue());
customRateLimitEntityDto.setUpUnit(1);
clientRateLimitSetting.setCustomRateLimit(customRateLimitEntityDto);
wifiApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
}
ClientRateLimitSettingDto clientRateLimitSetting = OmadaConvert.INSTANCE.toClientRateLimitSettingDto(account);
wifiApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
}
}

View File

@@ -12,8 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.api.IWifiApi;
import org.wfc.user.api.omada.domain.convert.OmadaConvert;
import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto;
import org.wfc.user.api.omada.domain.dto.CustomRateLimitEntityDto;
import org.wfc.user.domain.UAccount;
import org.wfc.user.domain.UClient;
import org.wfc.user.domain.UOrder;
@@ -105,19 +105,8 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
}
wifiApi.authClient(client.getSiteId(), client.getClientMac());
// 带宽限速
if (account.getRateLimitEnable()) {
ClientRateLimitSettingDto clientRateLimitSetting = new ClientRateLimitSettingDto();
clientRateLimitSetting.setMode(0);
CustomRateLimitEntityDto customRateLimitEntityDto = new CustomRateLimitEntityDto();
customRateLimitEntityDto.setDownEnable(account.getDownLimitEnable());
customRateLimitEntityDto.setDownLimit(account.getDownLimit() == null ? 0 : account.getDownLimit().intValue());
customRateLimitEntityDto.setDownUnit(1);
customRateLimitEntityDto.setUpEnable(account.getUpLimitEnable());
customRateLimitEntityDto.setUpLimit(account.getUpLimit() == null ? 0 : account.getUpLimit().intValue());
customRateLimitEntityDto.setUpUnit(1);
clientRateLimitSetting.setCustomRateLimit(customRateLimitEntityDto);
wifiApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
}
ClientRateLimitSettingDto clientRateLimitSetting = OmadaConvert.INSTANCE.toClientRateLimitSettingDto(account);
wifiApi.updateClientRateLimitSetting(client.getSiteId(), client.getClientMac(), clientRateLimitSetting);
}
}
@@ -170,6 +159,7 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
public boolean saveOrder(UOrder order) {
order.setUserId(SecurityUtils.getUserId());
order.setStatus(OrderStatusEnum.UNPAID.getCode());
order.setOrderNo(System.currentTimeMillis() + StrUtil.EMPTY);
this.save(order);
// 支付成功回调
paySuccess(order.getId());