use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel in project kapua by eclipse.
the class DeviceBundleManagementServiceImpl method get.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DeviceBundles get(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
//
// 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.read, scopeId));
//
// Prepare the request
BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
bundleRequestChannel.setMethod(KapuaMethod.READ);
BundleRequestPayload bundleRequestPayload = new BundleRequestPayload();
BundleRequestMessage bundleRequestMessage = new BundleRequestMessage();
bundleRequestMessage.setScopeId(scopeId);
bundleRequestMessage.setDeviceId(deviceId);
bundleRequestMessage.setCapturedOn(new Date());
bundleRequestMessage.setPayload(bundleRequestPayload);
bundleRequestMessage.setChannel(bundleRequestChannel);
//
// Do get
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(bundleRequestMessage, timeout);
BundleResponseMessage responseMessage = (BundleResponseMessage) deviceApplicationCall.send();
//
// Parse the response
BundleResponsePayload responsePayload = responseMessage.getPayload();
DeviceManagementSetting config = DeviceManagementSetting.getInstance();
String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
String body = null;
try {
body = new String(responsePayload.getBody(), charEncoding);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
}
DeviceBundles deviceBundleList = null;
try {
deviceBundleList = XmlUtil.unmarshal(body, DeviceBundlesImpl.class);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
}
//
// Create event
DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceBundleAppProperties.APP_NAME.getValue());
deviceEventCreator.setPosition(responseMessage.getPosition());
deviceEventCreator.setSentOn(responseMessage.getSentOn());
deviceEventCreator.setAction(KapuaMethod.READ);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
return deviceBundleList;
}
use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel in project kapua by eclipse.
the class DeviceBundleManagementServiceImpl method stop.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void stop(KapuaId scopeId, KapuaId deviceId, String bundleId, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notEmptyOrNull(bundleId, "bundleID");
//
// 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.execute, scopeId));
//
// Prepare the request
BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
bundleRequestChannel.setMethod(KapuaMethod.EXECUTE);
bundleRequestChannel.setStart(false);
bundleRequestChannel.setBundleId(bundleId);
BundleRequestPayload bundleRequestPayload = new BundleRequestPayload();
BundleRequestMessage bundleRequestMessage = new BundleRequestMessage();
bundleRequestMessage.setScopeId(scopeId);
bundleRequestMessage.setDeviceId(deviceId);
bundleRequestMessage.setCapturedOn(new Date());
bundleRequestMessage.setPayload(bundleRequestPayload);
bundleRequestMessage.setChannel(bundleRequestChannel);
//
// Do get
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(bundleRequestMessage, timeout);
BundleResponseMessage responseMessage = (BundleResponseMessage) deviceApplicationCall.send();
//
// Create event
DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceBundleAppProperties.APP_NAME.getValue());
deviceEventCreator.setPosition(responseMessage.getPosition());
deviceEventCreator.setSentOn(responseMessage.getSentOn());
deviceEventCreator.setAction(KapuaMethod.EXECUTE);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
}
use of org.eclipse.kapua.service.device.management.bundle.message.internal.BundleRequestChannel in project kapua by eclipse.
the class DeviceBundleManagementServiceImpl method start.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void start(KapuaId scopeId, KapuaId deviceId, String bundleId, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
ArgumentValidator.notEmptyOrNull(bundleId, "bundleId");
//
// 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.execute, scopeId));
//
// Prepare the request
BundleRequestChannel bundleRequestChannel = new BundleRequestChannel();
bundleRequestChannel.setAppName(DeviceBundleAppProperties.APP_NAME);
bundleRequestChannel.setVersion(DeviceBundleAppProperties.APP_VERSION);
bundleRequestChannel.setMethod(KapuaMethod.EXECUTE);
bundleRequestChannel.setStart(true);
bundleRequestChannel.setBundleId(bundleId);
BundleRequestPayload bundleRequestPayload = new BundleRequestPayload();
BundleRequestMessage bundleRequestMessage = new BundleRequestMessage();
bundleRequestMessage.setScopeId(scopeId);
bundleRequestMessage.setDeviceId(deviceId);
bundleRequestMessage.setCapturedOn(new Date());
bundleRequestMessage.setPayload(bundleRequestPayload);
bundleRequestMessage.setChannel(bundleRequestChannel);
//
// Do get
DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(bundleRequestMessage, timeout);
BundleResponseMessage responseMessage = (BundleResponseMessage) deviceApplicationCall.send();
//
// Create event
DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceBundleAppProperties.APP_NAME.getValue());
deviceEventCreator.setPosition(responseMessage.getPosition());
deviceEventCreator.setSentOn(responseMessage.getSentOn());
deviceEventCreator.setAction(KapuaMethod.EXECUTE);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
}
Aggregations