Search in sources :

Example 1 with DeviceConfigurationFactory

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory in project kapua by eclipse.

the class DeviceConfigurationManagementServiceImpl method put.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void put(KapuaId scopeId, KapuaId deviceId, DeviceComponentConfiguration deviceComponentConfiguration, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notNull(deviceComponentConfiguration, "componentConfiguration");
    ArgumentValidator.notEmptyOrNull(deviceComponentConfiguration.getId(), "componentConfiguration.componentId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.write, scopeId));
    // 
    // Prepare the request
    ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
    configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    configurationRequestChannel.setMethod(KapuaMethod.WRITE);
    configurationRequestChannel.setComponentId(deviceComponentConfiguration.getId());
    ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
    try {
        DeviceConfigurationFactory deviceConfigurationFactory = locator.getFactory(DeviceConfigurationFactory.class);
        DeviceConfiguration deviceConfiguration = deviceConfigurationFactory.newConfigurationInstance();
        deviceConfiguration.getComponentConfigurations().add(deviceComponentConfiguration);
        DeviceManagementSetting deviceManagementConfig = DeviceManagementSetting.getInstance();
        String charEncoding = deviceManagementConfig.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        StringWriter sw = new StringWriter();
        XmlUtil.marshal(deviceConfiguration, sw);
        byte[] requestBody = sw.toString().getBytes(charEncoding);
        configurationRequestPayload.setBody(requestBody);
        new String(configurationRequestPayload.getBody());
    } catch (Exception e) {
        throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_EXCEPTION, e, deviceComponentConfiguration);
    }
    ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
    configurationRequestMessage.setScopeId(scopeId);
    configurationRequestMessage.setDeviceId(deviceId);
    configurationRequestMessage.setCapturedOn(new Date());
    configurationRequestMessage.setPayload(configurationRequestPayload);
    configurationRequestMessage.setChannel(configurationRequestChannel);
    // 
    // Do put
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
    ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceConfigurationAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.WRITE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) SAXException(org.xml.sax.SAXException) KapuaException(org.eclipse.kapua.KapuaException) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) ConfigurationRequestMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage) ConfigurationRequestChannel(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel) StringWriter(java.io.StringWriter) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) ConfigurationRequestPayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration) ConfigurationResponseMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage) DeviceConfigurationFactory(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory)

Example 2 with DeviceConfigurationFactory

use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method updateComponentConfiguration.

@Override
public void updateComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice gwtDevice, GwtConfigComponent gwtCompConfig) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
    DeviceConfigurationFactory deviceConfigurationManagementFactory = locator.getFactory(DeviceConfigurationFactory.class);
    // set name and properties
    DeviceComponentConfiguration compConfig = deviceConfigurationManagementFactory.newComponentConfigurationInstance(gwtCompConfig.getUnescapedComponentId());
    compConfig.setName(gwtCompConfig.getUnescapedComponentName());
    Map<String, Object> compProps = new HashMap<String, Object>();
    for (GwtConfigParameter gwtConfigParam : gwtCompConfig.getParameters()) {
        Object objValue;
        int cardinality = gwtConfigParam.getCardinality();
        if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
            String strValue = gwtConfigParam.getValue();
            objValue = getObjectValue(gwtConfigParam, strValue);
        } else {
            String[] strValues = gwtConfigParam.getValues();
            objValue = getObjectValue(gwtConfigParam, strValues);
        }
        compProps.put(gwtConfigParam.getName(), objValue);
    }
    compConfig.setProperties(compProps);
    // execute the update
    try {
        KapuaId scopeId = KapuaEid.parseShortId(gwtDevice.getScopeId());
        KapuaId deviceId = KapuaEid.parseShortId(gwtDevice.getId());
        deviceConfigurationManagementService.put(scopeId, deviceId, compConfig, null);
        // 
        // Add an additional delay after the configuration update
        // to give the time to the device to apply the received
        // configuration
        Thread.sleep(1000);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) HashMap(java.util.HashMap) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceComponentConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration) DeviceConfigurationFactory(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 DeviceConfigurationFactory (org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory)2 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JAXBException (javax.xml.bind.JAXBException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)1 GwtConfigParameter (org.eclipse.kapua.app.console.shared.model.GwtConfigParameter)1 KapuaId (org.eclipse.kapua.model.id.KapuaId)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)1 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)1 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)1 DeviceComponentConfiguration (org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration)1 DeviceConfiguration (org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)1 DeviceConfigurationManagementService (org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)1 ConfigurationRequestChannel (org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel)1