use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService in project kapua by eclipse.
the class GwtDeviceManagementServiceImpl method findDeviceConfigurations.
//
// Configurations
//
@Override
public List<GwtConfigComponent> findDeviceConfigurations(GwtDevice device) throws GwtKapuaException {
List<GwtConfigComponent> gwtConfigs = new ArrayList<GwtConfigComponent>();
try {
// get the configuration
KapuaLocator locator = KapuaLocator.getInstance();
DeviceConfigurationManagementService deviceConfiguratiomManagementService = locator.getService(DeviceConfigurationManagementService.class);
KapuaId scopeId = KapuaEid.parseShortId(device.getScopeId());
KapuaId deviceId = KapuaEid.parseShortId(device.getId());
DeviceConfiguration deviceConfigurations = deviceConfiguratiomManagementService.get(scopeId, deviceId, null, null, null);
if (deviceConfigurations != null) {
// sort the list alphabetically by service name
List<DeviceComponentConfiguration> configs = deviceConfigurations.getComponentConfigurations();
Collections.sort(configs, new Comparator<DeviceComponentConfiguration>() {
@Override
public int compare(DeviceComponentConfiguration arg0, DeviceComponentConfiguration arg1) {
String name0 = arg0.getId();
String name1 = arg1.getId();
if (name0.contains(".")) {
name0 = name0.substring(name0.lastIndexOf('.'));
}
if (name1.contains(".")) {
name1 = name1.substring(name1.lastIndexOf('.'));
}
return name0.compareTo(name1);
}
});
// prepare results
ConsoleSetting consoleConfig = ConsoleSetting.getInstance();
List<String> serviceIgnore = consoleConfig.getList(String.class, ConsoleSettingKeys.DEVICE_CONFIGURATION_SERVICE_IGNORE);
for (DeviceComponentConfiguration config : deviceConfigurations.getComponentConfigurations()) {
// ignore items we want to hide
if (serviceIgnore != null && serviceIgnore.contains(config.getId())) {
continue;
}
KapuaTocd ocd = config.getDefinition();
if (ocd != null) {
GwtConfigComponent gwtConfig = new GwtConfigComponent();
gwtConfig.setId(config.getId());
gwtConfig.setName(ocd.getName());
gwtConfig.setDescription(ocd.getDescription());
if (ocd.getIcon() != null && ocd.getIcon().size() > 0) {
KapuaTicon icon = ocd.getIcon().get(0);
checkIconResource(icon);
gwtConfig.setComponentIcon(icon.getResource());
}
List<GwtConfigParameter> gwtParams = new ArrayList<GwtConfigParameter>();
gwtConfig.setParameters(gwtParams);
for (KapuaTad ad : ocd.getAD()) {
if (ad != null) {
GwtConfigParameter gwtParam = new GwtConfigParameter();
gwtParam.setId(ad.getId());
gwtParam.setName(ad.getName());
gwtParam.setDescription(ad.getDescription());
gwtParam.setType(GwtConfigParameterType.fromString(ad.getType().value()));
gwtParam.setRequired(ad.isRequired());
gwtParam.setCardinality(ad.getCardinality());
if (ad.getOption() != null && ad.getOption().size() > 0) {
Map<String, String> options = new HashMap<String, String>();
for (KapuaToption option : ad.getOption()) {
options.put(option.getLabel(), option.getValue());
}
gwtParam.setOptions(options);
}
gwtParam.setMin(ad.getMin());
gwtParam.setMax(ad.getMax());
if (config.getProperties() != null) {
// handle the value based on the cardinality of the attribute
int cardinality = ad.getCardinality();
Object value = config.getProperties().get(ad.getId());
if (value != null) {
if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
gwtParam.setValue(value.toString());
} else {
// this could be an array value
if (value instanceof Object[]) {
Object[] objValues = (Object[]) value;
List<String> strValues = new ArrayList<String>();
for (Object v : objValues) {
if (v != null) {
strValues.add(v.toString());
}
}
gwtParam.setValues(strValues.toArray(new String[] {}));
}
}
}
gwtParams.add(gwtParam);
}
}
}
gwtConfigs.add(gwtConfig);
}
}
}
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtConfigs;
}
use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService 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);
}
}
use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService in project kapua by eclipse.
the class DeviceSnapshotsServlet method doGet.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
try {
// parameter extraction
String account = request.getParameter("scopeId");
String clientId = request.getParameter("deviceId");
String snapshotId = request.getParameter("snapshotId");
//
// get the devices and append them to the exporter
KapuaLocator locator = KapuaLocator.getInstance();
DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
DeviceConfiguration conf = deviceConfigurationManagementService.get(KapuaEid.parseShortId(account), KapuaEid.parseShortId(clientId), snapshotId, null, null);
String contentDispositionFormat = "attachment; filename*=UTF-8''snapshot_%s_%s_%s.xml; ";
response.setContentType("application/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-transform, max-age=0");
response.setHeader("Content-Disposition", String.format(contentDispositionFormat, URLEncoder.encode(account, "UTF-8"), URLEncoder.encode(clientId, "UTF-8"), snapshotId));
XmlUtil.marshal(conf, writer);
} catch (Exception e) {
s_logger.error("Error creating Excel export", e);
throw new ServletException(e);
} finally {
if (writer != null)
writer.close();
}
}
use of org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService in project kapua by eclipse.
the class UploadRequest method doPostConfigurationSnapshot.
private void doPostConfigurationSnapshot(KapuaFormFields kapuaFormFields, HttpServletResponse resp) throws ServletException, IOException {
try {
List<FileItem> fileItems = kapuaFormFields.getFileItems();
String scopeIdString = kapuaFormFields.get("scopeIdString");
String deviceIdString = kapuaFormFields.get("deviceIdString");
if (scopeIdString == null || scopeIdString.isEmpty()) {
throw new IllegalArgumentException("scopeIdString");
}
if (deviceIdString == null || deviceIdString.isEmpty()) {
throw new IllegalArgumentException("deviceIdString");
}
if (fileItems == null || fileItems.size() != 1) {
throw new IllegalArgumentException("configuration");
}
KapuaLocator locator = KapuaLocator.getInstance();
DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
FileItem fileItem = fileItems.get(0);
byte[] data = fileItem.get();
String xmlConfigurationString = new String(data, "UTF-8");
deviceConfigurationManagementService.put(KapuaEid.parseShortId(scopeIdString), KapuaEid.parseShortId(deviceIdString), xmlConfigurationString, null);
} catch (IllegalArgumentException iae) {
resp.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
return;
} catch (KapuaEntityNotFoundException eenfe) {
resp.sendError(400, eenfe.getMessage());
return;
} catch (KapuaUnauthenticatedException eiae) {
resp.sendError(401, eiae.getMessage());
return;
} catch (KapuaIllegalAccessException eiae) {
resp.sendError(403, eiae.getMessage());
return;
} catch (Exception e) {
s_logger.error("Error posting configuration", e);
throw new ServletException(e);
}
}
Aggregations