|
|
|
@ -22,6 +22,7 @@ import org.eclipse.californium.core.coap.MediaTypeRegistry; |
|
|
|
import org.eclipse.californium.core.coap.Response; |
|
|
|
import org.eclipse.californium.core.observe.ObserveRelation; |
|
|
|
import org.eclipse.californium.core.server.resources.CoapExchange; |
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.server.coapserver.CoapServerContext; |
|
|
|
import org.thingsboard.server.coapserver.TbCoapServerComponent; |
|
|
|
@ -73,7 +74,7 @@ import static org.eclipse.californium.core.coap.Message.NONE; |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
@RequiredArgsConstructor |
|
|
|
@TbCoapServerComponent |
|
|
|
@ConditionalOnExpression("'${service.type:null}'=='tb-transport' || ('${service.type:null}'=='monolith' && '${transport.api_enabled:true}'=='true' && '${transport.coap.enabled}'=='true')") |
|
|
|
public class DefaultCoapClientContext implements CoapClientContext { |
|
|
|
|
|
|
|
private final CoapServerContext config; |
|
|
|
@ -329,9 +330,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
|
state.lock(); |
|
|
|
try { |
|
|
|
if (state.getConfiguration() == null || state.getAdaptor() == null) { |
|
|
|
state.setConfiguration(getTransportConfigurationContainer(deviceProfile)); |
|
|
|
state.setAdaptor(getCoapTransportAdaptor(state.getConfiguration().isJsonPayload())); |
|
|
|
state.setContentFormat(state.getAdaptor().getContentFormat()); |
|
|
|
initStateAdaptor(deviceProfile, state); |
|
|
|
} |
|
|
|
if (state.getCredentials() == null) { |
|
|
|
state.init(deviceCredentials); |
|
|
|
@ -395,6 +394,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) { |
|
|
|
return jsonPayloadType ? transportContext.getJsonCoapAdaptor() : transportContext.getProtoCoapAdaptor(); |
|
|
|
} |
|
|
|
@ -456,8 +461,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 |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
@ -709,9 +730,7 @@ public class DefaultCoapClientContext implements CoapClientContext { |
|
|
|
} |
|
|
|
|
|
|
|
private void respond(CoapExchange exchange, Response response, int defContentFormat) { |
|
|
|
int contentFormat = exchange.getRequestOptions().getContentFormat(); |
|
|
|
contentFormat = contentFormat != MediaTypeRegistry.UNDEFINED ? contentFormat : defContentFormat; |
|
|
|
response.getOptions().setContentFormat(contentFormat); |
|
|
|
response.getOptions().setContentFormat(TbCoapContentFormatUtil.getContentFormat(exchange.getRequestOptions().getContentFormat(), defContentFormat)); |
|
|
|
exchange.respond(response); |
|
|
|
} |
|
|
|
} |
|
|
|
|