Skip to content

fix: entity attribute #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -124,7 +125,7 @@ public ResponseBody<List<GatewayDeviceListItem>> getGatewayDevices(@PathVariable
item.setName(device.getName());
item.setCreatedAt(device.getCreatedAt());
return item;
}).toList());
}).sorted(Comparator.comparingLong(GatewayDeviceListItem::getCreatedAt).reversed()).toList());
}

@GetMapping("/gateways/{gatewayEUI}/sync-devices")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MsGwIntegrationEntities extends ExchangePayload {
@EqualsAndHashCode(callSuper = true)
@Entities
public static class AddDevice extends ExchangePayload implements AddDeviceAware {
@Entity(name = "DevEUI", attributes = @Attribute(maxLength = 16, minLength = 16))
@Entity(name = "DevEUI", attributes = @Attribute(lengthRange = "16", format = "HEX"))
private String eui;

@Entity(name = "Model", identifier = ADD_DEVICE_GATEWAY_DEVICE_MODEL_IDENTIFIER, attributes = @Attribute(enumClass = EmptyEnum.class))
Expand All @@ -76,13 +76,13 @@ public static class AddDevice extends ExchangePayload implements AddDeviceAware
@Entity(name = "Gateway", identifier = ADD_DEVICE_GATEWAY_EUI_IDENTIFIER, attributes = @Attribute(enumClass = EmptyEnum.class))
private String gatewayEUI;

@Entity(name = "fPort", identifier = "fport", attributes = @Attribute(max = 223, min = 1))
@Entity(name = "fPort", identifier = "fport", attributes = @Attribute(max = 223, min = 1, defaultValue = "1"))
private Long fPort;

@Entity(name = "Frame-counter Validation", identifier = "frame-counter-validation")
private Boolean frameCounterValidation;

@Entity(name = "Application Key", identifier = "app-key", attributes = @Attribute(optional = true, minLength = 32, maxLength = 32))
@Entity(name = "Application Key", identifier = "app-key", attributes = @Attribute(optional = true, lengthRange = "32", format = "HEX"))
private String appKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public void validateGatewayInfo(String eui) {
}
}

public ConnectionValidateResponse validateGatewayConnection(String eui, String credentialId) {
public ConnectionValidateResponse validateGatewayConnection(String inputEui, String credentialId) {
String eui = GatewayString.standardizeEUI(inputEui);
validateGatewayInfo(eui);
ConnectionValidateResponse result = new ConnectionValidateResponse();
MqttResponse<DeviceListResponse> response = gatewayRequester.requestDeviceList(eui, 0, 1, null);
Expand Down Expand Up @@ -178,6 +179,7 @@ private void putAddDeviceGatewayEui(List<Device> gateways) {
Entity gatewayEuiEntity = getAddDeviceGatewayEntity();
Map<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
gateways.forEach(gateway -> attrEnum.put(getGatewayEui(gateway), gateway.getName()));
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, getGatewayEui(gateways.get(0)));

gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, attrEnum);
entityServiceProvider.save(gatewayEuiEntity);
Expand All @@ -189,6 +191,11 @@ private void removeAddDeviceGatewayEui(List<String> gatewayEuiList) {
Map<String, String> attrEnum = json.convertValue(gatewayEuiEntity.getAttributes().get(AttributeBuilder.ATTRIBUTE_ENUM), new TypeReference<>() {});
gatewayEuiList.forEach(eui -> attrEnum.remove(GatewayString.standardizeEUI(eui)));
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, attrEnum);
if (!gatewayEuiList.isEmpty()) {
gatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, gatewayEuiList.get(0));
} else {
gatewayEuiEntity.getAttributes().remove(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE);
}
entityServiceProvider.save(gatewayEuiEntity);
}

Expand All @@ -197,11 +204,11 @@ public GatewayData addGateway(AddGatewayRequest request) {
GatewayData newGatewayData = new GatewayData();

// validate connection again
newGatewayData.setEui(request.getEui());
newGatewayData.setEui(GatewayString.standardizeEUI(request.getEui()));
newGatewayData.setCredentialId(request.getCredentialId());

// check application
ConnectionValidateResponse validateResult = validateGatewayConnection(request.getEui(), request.getCredentialId());
ConnectionValidateResponse validateResult = validateGatewayConnection(newGatewayData.getEui(), request.getCredentialId());
if (validateResult.getAppResult().stream().noneMatch(appItem -> appItem.getApplicationID().equals(request.getApplicationId()))) {
throw ServiceException.with(ErrorCode.PARAMETER_VALIDATION_FAILED.getErrorCode(), "Unknown application: " + request.getApplicationId()).build();
}
Expand Down Expand Up @@ -350,6 +357,9 @@ public void syncGatewayListToAddDeviceGatewayEuiList() {
Device::getName
));
addDeviceGatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_ENUM, euiToNameMap);
if (!gatwayList.isEmpty()) {
addDeviceGatewayEuiEntity.getAttributes().put(AttributeBuilder.ATTRIBUTE_DEFAULT_VALUE, getGatewayEui(gatwayList.get(0)));
}
entityServiceProvider.save(addDeviceGatewayEuiEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MscServiceEntities extends ExchangePayload {
@Entities
public static class AddDevice extends ExchangePayload implements AddDeviceAware {

@Entity(attributes = {@Attribute(minLength = 12, maxLength = 16)})
@Entity(attributes = {@Attribute(lengthRange = "12,16")})
private String sn;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void onAddDevice(Event<MscServiceEntities.AddDevice> event) {
.with(ErrorCode.SERVER_ERROR.getErrorCode(), "Integration has not been initialized yet.")
.build();
}
val identifier = event.getPayload().getSn();
val identifier = event.getPayload().getSn().toUpperCase();
val mscClient = mscClientProvider.getMscClient();
val addDeviceResponse = mscClient.device().attach(DeviceSaveOrUpdateRequest.builder()
.name(deviceName)
Expand Down