25 changed files with 438 additions and 101 deletions
@ -0,0 +1,66 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.config; |
|||
|
|||
import lombok.Getter; |
|||
import org.eclipse.leshan.core.LwM2m.Version; |
|||
import org.eclipse.leshan.core.request.ContentFormat; |
|||
|
|||
public enum LwM2mVersion { |
|||
VERSION_1_0(0, Version.V1_0, ContentFormat.TLV), |
|||
VERSION_1_1(1, Version.V1_1, ContentFormat.TEXT); |
|||
|
|||
@Getter |
|||
private final int code; |
|||
@Getter |
|||
private final Version version; |
|||
@Getter |
|||
private final ContentFormat contentFormat; |
|||
|
|||
LwM2mVersion(int code, Version version, ContentFormat contentFormat) { |
|||
this.code = code; |
|||
this.version = version; |
|||
this.contentFormat = contentFormat; |
|||
} |
|||
|
|||
public static LwM2mVersion fromType(Version version) { |
|||
for (LwM2mVersion to : LwM2mVersion.values()) { |
|||
if (to.version.equals(version)) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported typeLwM2mVersion type : %d", version)); |
|||
} |
|||
|
|||
public static ContentFormat fromContentFormat(String versionStr) { |
|||
for (LwM2mVersion to : LwM2mVersion.values()) { |
|||
if (to.version.toString().equals(versionStr)) { |
|||
return to.contentFormat; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported contentFormatLwM2mVersion version : %d", versionStr)); |
|||
} |
|||
|
|||
public static LwM2mVersion fromCode(int code) { |
|||
for (LwM2mVersion to : LwM2mVersion.values()) { |
|||
if (to.code == code) { |
|||
return to; |
|||
} |
|||
} |
|||
throw new IllegalArgumentException(String.format("Unsupported codeLwM2mVersion code : %d", code)); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.downlink; |
|||
|
|||
import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil; |
|||
|
|||
import java.util.Set; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
|
|||
public interface HasVersionedIds { |
|||
|
|||
String[] getVersionedIds(); |
|||
|
|||
default String[] getObjectIds() { |
|||
Set objectIds = ConcurrentHashMap.newKeySet(); |
|||
for (String versionedId : getVersionedIds()) { |
|||
objectIds.add(LwM2mTransportUtil.fromVersionedIdToObjectId(versionedId)); |
|||
} |
|||
return (String[]) objectIds.toArray(String[]::new); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.downlink.composite; |
|||
|
|||
import lombok.Getter; |
|||
import org.thingsboard.server.transport.lwm2m.server.downlink.HasVersionedIds; |
|||
import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MDownlinkRequest; |
|||
|
|||
public abstract class AbstractTbLwM2MTargetedDownlinkCompositeRequest<T> implements TbLwM2MDownlinkRequest<T>, HasVersionedIds { |
|||
|
|||
@Getter |
|||
private final String [] versionedIds; |
|||
@Getter |
|||
private final long timeout; |
|||
|
|||
public AbstractTbLwM2MTargetedDownlinkCompositeRequest(String [] versionedIds, long timeout) { |
|||
this.versionedIds = versionedIds; |
|||
this.timeout = timeout; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.rpc.composite; |
|||
|
|||
import org.eclipse.leshan.core.request.ReadCompositeRequest; |
|||
import org.eclipse.leshan.core.response.ReadCompositeResponse; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.transport.TransportService; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
|||
import org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback; |
|||
import org.thingsboard.server.transport.lwm2m.server.rpc.LwM2MRpcResponseBody; |
|||
import org.thingsboard.server.transport.lwm2m.server.rpc.RpcDownlinkRequestCallbackProxy; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
public abstract class RpcLwM2MDownlinkCompositeCallback<R extends ReadCompositeRequest, T extends ReadCompositeResponse> extends RpcDownlinkRequestCallbackProxy<R, T> { |
|||
|
|||
public RpcLwM2MDownlinkCompositeCallback(TransportService transportService, LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg, DownlinkRequestCallback<R, T> callback) { |
|||
super(transportService, client, requestMsg, callback); |
|||
} |
|||
|
|||
@Override |
|||
protected void sendRpcReplyOnSuccess(T response) { |
|||
LwM2MRpcResponseBody.LwM2MRpcResponseBodyBuilder builder = LwM2MRpcResponseBody.builder().result(response.getCode().getName()); |
|||
if (response.isSuccess()) { |
|||
Optional<String> responseValue = serializeSuccessfulResponse(response); |
|||
if (responseValue.isPresent() && StringUtils.isNotEmpty(responseValue.get())) { |
|||
builder.value(responseValue.get()); |
|||
} |
|||
} else { |
|||
if (StringUtils.isNotEmpty(response.getErrorMessage())) { |
|||
builder.error(response.getErrorMessage()); |
|||
} |
|||
} |
|||
reply(builder.build()); |
|||
} |
|||
|
|||
protected abstract Optional<String> serializeSuccessfulResponse(T response); |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.rpc.composite; |
|||
|
|||
import org.eclipse.leshan.core.request.ReadCompositeRequest; |
|||
import org.eclipse.leshan.core.response.ReadCompositeResponse; |
|||
import org.thingsboard.server.common.transport.TransportService; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
|||
import org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
public class RpcReadResponseCompositeCallback<R extends ReadCompositeRequest, T extends ReadCompositeResponse> extends RpcLwM2MDownlinkCompositeCallback<R, T> { |
|||
|
|||
public RpcReadResponseCompositeCallback(TransportService transportService, LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg, DownlinkRequestCallback<R, T> callback) { |
|||
super(transportService, client, requestMsg, callback); |
|||
} |
|||
|
|||
@Override |
|||
protected Optional<String> serializeSuccessfulResponse(T response) { |
|||
return Optional.of(String.format("%s", response.getContent().toString())); |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2021 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.transport.lwm2m.server.rpc.composite; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
import lombok.Data; |
|||
|
|||
import java.util.Map; |
|||
|
|||
@Data |
|||
@JsonIgnoreProperties(ignoreUnknown = true) |
|||
public class RpcWriteCompositeRequest { |
|||
|
|||
private Map<String, Object> nodes; |
|||
|
|||
} |
|||
Loading…
Reference in new issue