|
|
@ -18,6 +18,7 @@ package org.thingsboard.server.transport.coap.client; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.eclipse.californium.core.coap.CoAP; |
|
|
import org.eclipse.californium.core.coap.CoAP; |
|
|
|
|
|
import org.eclipse.californium.core.coap.MediaTypeRegistry; |
|
|
import org.eclipse.californium.core.coap.Response; |
|
|
import org.eclipse.californium.core.coap.Response; |
|
|
import org.eclipse.californium.core.observe.ObserveRelation; |
|
|
import org.eclipse.californium.core.observe.ObserveRelation; |
|
|
import org.eclipse.californium.core.server.resources.CoapExchange; |
|
|
import org.eclipse.californium.core.server.resources.CoapExchange; |
|
|
@ -328,8 +329,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
state.lock(); |
|
|
state.lock(); |
|
|
try { |
|
|
try { |
|
|
if (state.getConfiguration() == null || state.getAdaptor() == null) { |
|
|
if (state.getConfiguration() == null || state.getAdaptor() == null) { |
|
|
state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); |
|
|
initStateAdaptor(deviceProfile, state); |
|
|
state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); |
|
|
|
|
|
} |
|
|
} |
|
|
if (state.getCredentials() == null) { |
|
|
if (state.getCredentials() == null) { |
|
|
state.init(deviceCredentials); |
|
|
state.init(deviceCredentials); |
|
|
@ -393,6 +393,12 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initStateAdaptor(DeviceProfile deviceProfile, TbCoapClientState state) throws AdaptorException { |
|
|
|
|
|
state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); |
|
|
|
|
|
state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); |
|
|
|
|
|
state.setContentFormat(state.getAdaptor().getContentFormat()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private CoapTransportAdaptor getCoapTransportAdaptor(boolean jsonPayloadType) { |
|
|
private CoapTransportAdaptor getCoapTransportAdaptor(boolean jsonPayloadType) { |
|
|
return jsonPayloadType ? transportContext.getJsonCoapAdaptor() : transportContext.getProtoCoapAdaptor(); |
|
|
return jsonPayloadType ? transportContext.getJsonCoapAdaptor() : transportContext.getProtoCoapAdaptor(); |
|
|
} |
|
|
} |
|
|
@ -409,7 +415,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
try { |
|
|
try { |
|
|
boolean conRequest = AbstractSyncSessionCallback.isConRequest(state.getAttrs()); |
|
|
boolean conRequest = AbstractSyncSessionCallback.isConRequest(state.getAttrs()); |
|
|
Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
|
|
Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
|
|
attrs.getExchange().respond(response); |
|
|
respond(attrs.getExchange(), response, state.getContentFormat()); |
|
|
} catch (AdaptorException e) { |
|
|
} catch (AdaptorException e) { |
|
|
log.trace("Failed to reply due to error", e); |
|
|
log.trace("Failed to reply due to error", e); |
|
|
cancelObserveRelation(attrs); |
|
|
cancelObserveRelation(attrs); |
|
|
@ -440,10 +446,10 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
int requestId = getNextMsgId(); |
|
|
int requestId = getNextMsgId(); |
|
|
Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
|
|
Response response = state.getAdaptor().convertToPublish(conRequest, msg); |
|
|
response.setMID(requestId); |
|
|
response.setMID(requestId); |
|
|
attrs.getExchange().respond(response); |
|
|
|
|
|
if (conRequest) { |
|
|
if (conRequest) { |
|
|
response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
|
|
response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
|
|
} |
|
|
} |
|
|
|
|
|
respond(attrs.getExchange(), response, state.getContentFormat()); |
|
|
} catch (AdaptorException e) { |
|
|
} catch (AdaptorException e) { |
|
|
log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); |
|
|
log.trace("[{}] Failed to reply due to error", state.getDeviceId(), e); |
|
|
cancelObserveRelation(attrs); |
|
|
cancelObserveRelation(attrs); |
|
|
@ -454,8 +460,24 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onDeviceProfileUpdate(TransportProtos.SessionInfoProto newSessionInfo, DeviceProfile deviceProfile) { |
|
|
|
|
|
try { |
|
|
|
|
|
initStateAdaptor(deviceProfile, state); |
|
|
|
|
|
} catch (AdaptorException e) { |
|
|
|
|
|
log.warn("[{}] Failed to update device profile: ", deviceProfile.getId(), e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { |
|
|
public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) { |
|
|
|
|
|
if (deviceProfileOpt.isPresent()) { |
|
|
|
|
|
try { |
|
|
|
|
|
initStateAdaptor(deviceProfileOpt.get(), state); |
|
|
|
|
|
} catch (AdaptorException e) { |
|
|
|
|
|
log.warn("[{}] Failed to update device: ", device.getId(), e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
state.onDeviceUpdate(device); |
|
|
state.onDeviceUpdate(device); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -503,7 +525,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
if (conRequest) { |
|
|
if (conRequest) { |
|
|
response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
|
|
response.addMessageObserver(new TbCoapMessageObserver(requestId, id -> awake(state), id -> asleep(state))); |
|
|
} |
|
|
} |
|
|
state.getRpc().getExchange().respond(response); |
|
|
respond(state.getRpc().getExchange(), response, state.getContentFormat()); |
|
|
sent = true; |
|
|
sent = true; |
|
|
} catch (AdaptorException e) { |
|
|
} catch (AdaptorException e) { |
|
|
log.trace("Failed to reply due to error", e); |
|
|
log.trace("Failed to reply due to error", e); |
|
|
@ -705,4 +727,9 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
state.setAdaptor(null); |
|
|
state.setAdaptor(null); |
|
|
//TODO: add optimistic lock check that the client was already deleted and cleanup "clients" map.
|
|
|
//TODO: add optimistic lock check that the client was already deleted and cleanup "clients" map.
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void respond(CoapExchange exchange, Response response, int defContentFormat) { |
|
|
|
|
|
response.getOptions().setContentFormat(TbCoapContentFormatUtil.getContentFormat(exchange.getRequestOptions().getContentFormat(), defContentFormat)); |
|
|
|
|
|
exchange.respond(response); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|