Search in sources :

Example 1 with Translator

use of org.eclipse.kapua.translator.Translator in project kapua by eclipse.

the class DeviceCallExecutor method send.

@SuppressWarnings({ "unchecked" })
public RS send() throws KapuaException {
    // 
    // Get the correct device call
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceCallFactory kapuaDeviceCallFactory = locator.getFactory(DeviceCallFactory.class);
    DeviceCall<DeviceRequestMessage, DeviceResponseMessage> deviceCall = kapuaDeviceCallFactory.newDeviceCall();
    Translator tKapuaToClient = Translator.getTranslatorFor(requestMessage.getRequestClass(), deviceCall.getBaseMessageClass());
    DeviceResponseMessage responseMessage;
    timeout = timeout == null ? DeviceManagementSetting.getInstance().getLong(DeviceManagementSettingKey.REQUEST_TIMEOUT) : timeout;
    DeviceRequestMessage deviceRequestMessage = (DeviceRequestMessage) tKapuaToClient.translate(requestMessage);
    switch(requestMessage.getChannel().getMethod()) {
        case CREATE:
            {
                responseMessage = deviceCall.create(deviceRequestMessage, timeout);
            }
            break;
        case READ:
            {
                responseMessage = deviceCall.read(deviceRequestMessage, timeout);
            }
            break;
        case OPTIONS:
            {
                responseMessage = deviceCall.options(deviceRequestMessage, timeout);
            }
            break;
        case DELETE:
            {
                responseMessage = deviceCall.delete(deviceRequestMessage, timeout);
            }
            break;
        case EXECUTE:
            {
                responseMessage = deviceCall.execute(deviceRequestMessage, timeout);
            }
            break;
        case WRITE:
            {
                responseMessage = deviceCall.write(deviceRequestMessage, timeout);
            }
            break;
        default:
            throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_BAD_METHOD, null, requestMessage.getChannel().getMethod());
    }
    Translator tClientToKapua = Translator.getTranslatorFor(deviceCall.getBaseMessageClass(), requestMessage.getResponseClass());
    return (RS) tClientToKapua.translate(responseMessage);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) DeviceResponseMessage(org.eclipse.kapua.service.device.call.message.app.response.DeviceResponseMessage) Translator(org.eclipse.kapua.translator.Translator) DeviceRequestMessage(org.eclipse.kapua.service.device.call.message.app.request.DeviceRequestMessage) DeviceCallFactory(org.eclipse.kapua.service.device.call.DeviceCallFactory)

Example 2 with Translator

use of org.eclipse.kapua.translator.Translator in project kapua by eclipse.

the class KuraDeviceCallImpl method send.

@SuppressWarnings({ "unchecked" })
private KuraResponseMessage send(KuraRequestMessage requestMessage, Long timeout) throws KuraMqttDeviceCallException {
    KuraResponseMessage response = null;
    TransportFacade transportFacade = null;
    try {
        // 
        // Borrow a KapuaClient
        transportFacade = borrowClient();
        // 
        // Get Kura to transport translator for the request and vice versa
        Translator translatorKuraTransport = getTranslator(KuraRequestMessage.class, transportFacade.getMessageClass());
        Translator translatorTransportKura = getTranslator(transportFacade.getMessageClass(), KuraResponseMessage.class);
        // 
        // Make the request
        // Add requestId and requesterClientId to both payload and channel if response is expected
        // Note: Adding to both payload and channel to let the translator choose what to do base on the transport used.
        KuraRequestChannel requestChannel = requestMessage.getChannel();
        KuraRequestPayload requestPayload = requestMessage.getPayload();
        if (timeout != null) {
            // FIXME: create an utilty class to use the same synchronized random instance to avoid duplicates
            Random r = new Random();
            String requestId = String.valueOf(r.nextLong());
            requestChannel.setRequestId(requestId);
            requestChannel.setRequesterClientId(transportFacade.getClientId());
            requestPayload.setRequestId(requestId);
            requestPayload.setRequesterClientId(transportFacade.getClientId());
        }
        // Do send
        try {
            // Set current timestamp
            requestMessage.setTimestamp(new Date());
            // Send
            TransportMessage transportResponseMessage = transportFacade.sendSync((TransportMessage) translatorKuraTransport.translate(requestMessage), timeout);
            // Translate response
            response = (KuraResponseMessage) translatorTransportKura.translate(transportResponseMessage);
        } catch (KapuaException e) {
            throw new KuraMqttDeviceCallException(KuraMqttDeviceCallErrorCodes.CLIENT_SEND_ERROR, e, (Object[]) null);
        }
    } catch (KapuaException ke) {
        throw new KuraMqttDeviceCallException(KuraMqttDeviceCallErrorCodes.CALL_ERROR, ke, (Object[]) null);
    } finally {
        if (transportFacade != null) {
            transportFacade.clean();
        }
    }
    return response;
}
Also used : KuraResponseMessage(org.eclipse.kapua.service.device.call.message.app.response.kura.KuraResponseMessage) Random(java.util.Random) Translator(org.eclipse.kapua.translator.Translator) KuraRequestChannel(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel) KapuaException(org.eclipse.kapua.KapuaException) TransportFacade(org.eclipse.kapua.transport.TransportFacade) Date(java.util.Date) KuraRequestPayload(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload) TransportMessage(org.eclipse.kapua.transport.message.TransportMessage) KuraMqttDeviceCallException(org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException)

Aggregations

Translator (org.eclipse.kapua.translator.Translator)2 Date (java.util.Date)1 Random (java.util.Random)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 DeviceCallFactory (org.eclipse.kapua.service.device.call.DeviceCallFactory)1 KuraMqttDeviceCallException (org.eclipse.kapua.service.device.call.kura.exception.KuraMqttDeviceCallException)1 DeviceRequestMessage (org.eclipse.kapua.service.device.call.message.app.request.DeviceRequestMessage)1 KuraRequestChannel (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel)1 KuraRequestPayload (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload)1 DeviceResponseMessage (org.eclipse.kapua.service.device.call.message.app.response.DeviceResponseMessage)1 KuraResponseMessage (org.eclipse.kapua.service.device.call.message.app.response.kura.KuraResponseMessage)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 TransportFacade (org.eclipse.kapua.transport.TransportFacade)1 TransportMessage (org.eclipse.kapua.transport.message.TransportMessage)1