committed by
GitHub
18 changed files with 209 additions and 265 deletions
@ -0,0 +1,54 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.coap.callback; |
|||
|
|||
import org.eclipse.californium.core.coap.Response; |
|||
import org.eclipse.californium.core.server.resources.CoapExchange; |
|||
import org.thingsboard.server.common.transport.TransportServiceCallback; |
|||
|
|||
public class CoapResponseCallback implements TransportServiceCallback<Void> { |
|||
|
|||
protected final CoapExchange exchange; |
|||
protected final Response onSuccessResponse; |
|||
protected final Response onFailureResponse; |
|||
|
|||
public CoapResponseCallback(CoapExchange exchange, Response onSuccessResponse, Response onFailureResponse) { |
|||
this.exchange = exchange; |
|||
this.onSuccessResponse = onSuccessResponse; |
|||
this.onFailureResponse = onFailureResponse; |
|||
} |
|||
|
|||
/** |
|||
* @param msg |
|||
*/ |
|||
@Override |
|||
public void onSuccess(Void msg) { |
|||
this.onSuccessResponse.setConfirmable(isConRequest()); |
|||
exchange.respond(this.onSuccessResponse); |
|||
} |
|||
|
|||
/** |
|||
* @param e |
|||
*/ |
|||
@Override |
|||
public void onError(Throwable e) { |
|||
exchange.respond(onFailureResponse); |
|||
} |
|||
|
|||
protected boolean isConRequest() { |
|||
return exchange.advanced().getRequest().isConfirmable(); |
|||
} |
|||
} |
|||
@ -0,0 +1,81 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.msa; |
|||
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import org.eclipse.californium.core.CoapClient; |
|||
import org.eclipse.californium.core.coap.MediaTypeRegistry; |
|||
import org.eclipse.californium.core.config.CoapConfig; |
|||
import org.eclipse.californium.elements.config.Configuration; |
|||
import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; |
|||
import org.eclipse.californium.elements.config.IntegerDefinition; |
|||
import org.eclipse.californium.elements.config.TcpConfig; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.msg.session.FeatureType; |
|||
|
|||
public abstract class AbstractCoapClientTest extends AbstractContainerTest{ |
|||
|
|||
private static final String COAP_BASE_URL = "coap://localhost:5683/api/v1/"; |
|||
private static final long CLIENT_REQUEST_TIMEOUT = 60000L; |
|||
|
|||
|
|||
private static final String COAP_CLIENT_TEST = "COAP_CLIENT_TEST."; |
|||
private static final IntegerDefinition COAP_PORT_DEF = CoapConfig.COAP_PORT; |
|||
|
|||
private static final ModuleDefinitionsProvider MODULE_DEFINITIONS_PROVIDER = new ModuleDefinitionsProvider() { |
|||
|
|||
@Override |
|||
public String getModule() { |
|||
return COAP_CLIENT_TEST; |
|||
} |
|||
|
|||
@Override |
|||
public void applyDefinitions(Configuration config) { |
|||
TcpConfig.register(); |
|||
config.set(COAP_PORT_DEF, 5683); |
|||
} |
|||
}; |
|||
|
|||
protected CoapClient client; |
|||
|
|||
protected byte[] createCoapClientAndPublish(String deviceName) throws Exception { |
|||
String provisionRequestMsg = createTestProvisionMessage(deviceName); |
|||
Configuration.addDefaultModule(MODULE_DEFINITIONS_PROVIDER); |
|||
String featureTokenUrl = COAP_BASE_URL + FeatureType.PROVISION.name().toLowerCase(); |
|||
client = new CoapClient(featureTokenUrl); |
|||
return client.setTimeout(CLIENT_REQUEST_TIMEOUT) |
|||
.post(provisionRequestMsg.getBytes(), MediaTypeRegistry.APPLICATION_JSON) |
|||
.getPayload(); |
|||
} |
|||
|
|||
protected void disconnect() { |
|||
if (client != null) { |
|||
client.shutdown(); |
|||
} |
|||
} |
|||
|
|||
private String createTestProvisionMessage(String deviceName) { |
|||
ObjectNode provisionRequest = JacksonUtil.newObjectNode(); |
|||
provisionRequest.put("provisionDeviceKey", TEST_PROVISION_DEVICE_KEY); |
|||
provisionRequest.put("provisionDeviceSecret", TEST_PROVISION_DEVICE_SECRET); |
|||
if (deviceName != null) { |
|||
provisionRequest.put("deviceName", deviceName); |
|||
} |
|||
return provisionRequest.toString(); |
|||
} |
|||
} |
|||
|
|||
|
|||
@ -1,122 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.msa; |
|||
|
|||
import org.eclipse.californium.core.CoapClient; |
|||
import org.eclipse.californium.core.CoapHandler; |
|||
import org.eclipse.californium.core.CoapObserveRelation; |
|||
import org.eclipse.californium.core.CoapResponse; |
|||
import org.eclipse.californium.core.coap.CoAP; |
|||
import org.eclipse.californium.core.coap.MediaTypeRegistry; |
|||
import org.eclipse.californium.core.coap.Request; |
|||
import org.eclipse.californium.elements.exception.ConnectorException; |
|||
import org.thingsboard.server.common.msg.session.FeatureType; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
public class TestCoapClient { |
|||
|
|||
private static final String COAP_BASE_URL = "coap://localhost:5683/api/v1/"; |
|||
private static final long CLIENT_REQUEST_TIMEOUT = 60000L; |
|||
|
|||
private final CoapClient client; |
|||
|
|||
public TestCoapClient(){ |
|||
this.client = createClient(); |
|||
} |
|||
|
|||
public TestCoapClient(String accessToken, FeatureType featureType) { |
|||
this.client = createClient(getFeatureTokenUrl(accessToken, featureType)); |
|||
} |
|||
|
|||
public TestCoapClient(String featureTokenUrl) { |
|||
this.client = createClient(featureTokenUrl); |
|||
} |
|||
|
|||
public void connectToCoap(String accessToken) { |
|||
setURI(accessToken, null); |
|||
} |
|||
|
|||
public void connectToCoap(String accessToken, FeatureType featureType) { |
|||
setURI(accessToken, featureType); |
|||
} |
|||
|
|||
public void disconnect() { |
|||
if (client != null) { |
|||
client.shutdown(); |
|||
} |
|||
} |
|||
|
|||
public CoapResponse postMethod(String requestBody) throws ConnectorException, IOException { |
|||
return this.postMethod(requestBody.getBytes()); |
|||
} |
|||
|
|||
public CoapResponse postMethod(byte[] requestBodyBytes) throws ConnectorException, IOException { |
|||
return client.setTimeout(CLIENT_REQUEST_TIMEOUT).post(requestBodyBytes, MediaTypeRegistry.APPLICATION_JSON); |
|||
} |
|||
|
|||
public void postMethod(CoapHandler handler, String payload, int format) { |
|||
client.post(handler, payload, format); |
|||
} |
|||
|
|||
public void postMethod(CoapHandler handler, byte[] payload, int format) { |
|||
client.post(handler, payload, format); |
|||
} |
|||
|
|||
public CoapResponse getMethod() throws ConnectorException, IOException { |
|||
return client.setTimeout(CLIENT_REQUEST_TIMEOUT).get(); |
|||
} |
|||
|
|||
public CoapObserveRelation getObserveRelation(TestCoapClientCallback callback){ |
|||
Request request = Request.newGet().setObserve(); |
|||
request.setType(CoAP.Type.CON); |
|||
return client.observe(request, callback); |
|||
} |
|||
|
|||
public void setURI(String featureTokenUrl) { |
|||
if (client == null) { |
|||
throw new RuntimeException("Failed to connect! CoapClient is not initialized!"); |
|||
} |
|||
client.setURI(featureTokenUrl); |
|||
} |
|||
|
|||
public void setURI(String accessToken, FeatureType featureType) { |
|||
if (featureType == null){ |
|||
featureType = FeatureType.ATTRIBUTES; |
|||
} |
|||
setURI(getFeatureTokenUrl(accessToken, featureType)); |
|||
} |
|||
|
|||
private CoapClient createClient() { |
|||
return new CoapClient(); |
|||
} |
|||
|
|||
private CoapClient createClient(String featureTokenUrl) { |
|||
return new CoapClient(featureTokenUrl); |
|||
} |
|||
|
|||
public static String getFeatureTokenUrl(FeatureType featureType) { |
|||
return COAP_BASE_URL + featureType.name().toLowerCase(); |
|||
} |
|||
|
|||
public static String getFeatureTokenUrl(String token, FeatureType featureType) { |
|||
return COAP_BASE_URL + token + "/" + featureType.name().toLowerCase(); |
|||
} |
|||
|
|||
public static String getFeatureTokenUrl(String token, FeatureType featureType, int requestId) { |
|||
return COAP_BASE_URL + token + "/" + featureType.name().toLowerCase() + "/" + requestId; |
|||
} |
|||
} |
|||
@ -1,68 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2024 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.msa; |
|||
|
|||
import lombok.Data; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.eclipse.californium.core.CoapHandler; |
|||
import org.eclipse.californium.core.CoapResponse; |
|||
import org.eclipse.californium.core.coap.CoAP; |
|||
|
|||
import java.util.concurrent.CountDownLatch; |
|||
|
|||
@Slf4j |
|||
@Data |
|||
public class TestCoapClientCallback implements CoapHandler { |
|||
|
|||
protected final CountDownLatch latch; |
|||
protected Integer observe; |
|||
protected byte[] payloadBytes; |
|||
protected CoAP.ResponseCode responseCode; |
|||
|
|||
public TestCoapClientCallback() { |
|||
this.latch = new CountDownLatch(1); |
|||
} |
|||
|
|||
public TestCoapClientCallback(int subscribeCount) { |
|||
this.latch = new CountDownLatch(subscribeCount); |
|||
} |
|||
|
|||
public Integer getObserve() { |
|||
return observe; |
|||
} |
|||
|
|||
public byte[] getPayloadBytes() { |
|||
return payloadBytes; |
|||
} |
|||
|
|||
public CoAP.ResponseCode getResponseCode() { |
|||
return responseCode; |
|||
} |
|||
|
|||
@Override |
|||
public void onLoad(CoapResponse response) { |
|||
observe = response.getOptions().getObserve(); |
|||
payloadBytes = response.getPayload(); |
|||
responseCode = response.getCode(); |
|||
latch.countDown(); |
|||
} |
|||
|
|||
@Override |
|||
public void onError() { |
|||
log.warn("Command Response Ack Error, No connect"); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue