From 56faa75861e9c38dce45b103ffbcd3fc48982423 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 25 May 2017 03:07:19 +0530 Subject: [PATCH] fixed analytics issues --- .../DeviceEventManagementServiceImpl.java | 57 ++++++++++----- .../impl/DeviceManagementServiceImpl.java | 10 +++ .../impl/DeviceTypeManagementServiceImpl.java | 16 +++-- .../DeviceManagementPluginRepository.java | 5 +- .../core/dto/DeviceTypeServiceIdentifier.java | 4 +- .../jaggeryapps/devicemgt/api/device-api.jag | 14 ++++ .../modules/business-controllers/device.js | 70 ++++++++++++++++++- .../pages/cdmf.page.devicetype.edit/edit.hbs | 4 +- .../pages/cdmf.page.devicetype.edit/edit.js | 2 +- .../device-view.hbs | 5 +- .../device-view.js | 2 - .../analytics-view.hbs | 7 +- .../analytics-view.js | 37 +++++++++- .../public/js/device-stats.js | 2 + .../public/js/type-view.js | 24 ++++++- .../type-view.hbs | 16 ++++- .../type-view.js | 6 +- .../units/cdmf.unit.policy.create/create.js | 11 +-- 18 files changed, 240 insertions(+), 52 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java index fa4847ef91..306a427252 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceEventManagementServiceImpl.java @@ -111,6 +111,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe private static final String OAUTH_MQTT_ADAPTER_TYPE = "oauth-mqtt"; private static final String OAUTH_HTTP_ADAPTER_TYPE = "oauth-http"; private static final String DEFAULT_DEVICE_ID_ATTRIBUTE = "deviceId"; + private static final String DEFAULT_META_DEVICE_ID_ATTRIBUTE = "meta_deviceId"; private static KeyStore keyStore; private static KeyStore trustStore; @@ -163,9 +164,6 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe EventAttributeList eventAttributeList = new EventAttributeList(); List attributes = new ArrayList<>(); for (EventStreamAttributeDto eventStreamAttributeDto : eventStreamAttributeDtos) { - if (DEFAULT_DEVICE_ID_ATTRIBUTE.equals(eventStreamAttributeDto.getAttributeName())) { - continue; - } attributes.add(new Attribute(eventStreamAttributeDto.getAttributeName() , AttributeType.valueOf(eventStreamAttributeDto.getAttributeType().toUpperCase()))); } @@ -176,10 +174,12 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe eventReceiverAdminServiceStub = getEventReceiverAdminServiceStub(); EventReceiverConfigurationDto eventReceiverConfigurationDto = eventReceiverAdminServiceStub .getActiveEventReceiverConfiguration(getReceiverName(deviceType, tenantDomain)); - String eventAdapterType = eventReceiverConfigurationDto.getFromAdapterConfigurationDto() - .getEventAdapterType(); - if (OAUTH_MQTT_ADAPTER_TYPE.equals(eventAdapterType)) { - deviceTypeEvent.setTransportType(TransportType.MQTT); + if (eventReceiverConfigurationDto != null) { + String eventAdapterType = eventReceiverConfigurationDto.getFromAdapterConfigurationDto() + .getEventAdapterType(); + if (OAUTH_MQTT_ADAPTER_TYPE.equals(eventAdapterType)) { + deviceTypeEvent.setTransportType(TransportType.MQTT); + } } return Response.ok().entity(deviceTypeEvent).build(); } catch (AxisFault e) { @@ -285,7 +285,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); } String eventReceiverName = getReceiverName(deviceType, tenantDomain); - String eventPublisherName = deviceType.trim().toLowerCase() + "_websocket_publisher"; + String eventPublisherName = deviceType.trim().replace(" ", "_") + "_websocket_publisher"; String streamName = getStreamDefinition(deviceType, tenantDomain); eventStreamAdminServiceStub = getEventStreamAdminServiceStub(); if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamName + ":" + DEFAULT_STREAM_VERSION) == null) { @@ -360,7 +360,8 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } String fromDate = String.valueOf(from); String toDate = String.valueOf(to); - String query = "deviceId:" + deviceId + " AND _timestamp : [" + fromDate + " TO " + toDate + "]"; + String query = DEFAULT_META_DEVICE_ID_ATTRIBUTE + ":" + deviceId + + " AND _timestamp : [" + fromDate + " TO " + toDate + "]"; String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String sensorTableName = getTableName(getStreamDefinition(deviceType, tenantDomain)); try { @@ -397,7 +398,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe @Path("/last-known/{type}/{deviceId}") @Override public Response getLastKnownData(@PathParam("deviceId") String deviceId, @PathParam("type") String deviceType) { - String query = "deviceId:" + deviceId; + String query = DEFAULT_META_DEVICE_ID_ATTRIBUTE + ":" + deviceId; String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String sensorTableName = getTableName(getStreamDefinition(deviceType, tenantDomain)); try { @@ -436,6 +437,25 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe throws RemoteException, UserStoreException, JWTClientException { EventReceiverAdminServiceStub receiverAdminServiceStub = getEventReceiverAdminServiceStub(); try { + EventReceiverConfigurationDto eventReceiverConfigurationDto = receiverAdminServiceStub + .getActiveEventReceiverConfiguration(getReceiverName(deviceType, requestedTenantDomain)); + if (eventReceiverConfigurationDto != null) { + String eventAdapterType = eventReceiverConfigurationDto.getFromAdapterConfigurationDto() + .getEventAdapterType(); + if (OAUTH_MQTT_ADAPTER_TYPE.equals(eventAdapterType)) { + if (transportType == TransportType.MQTT) { + return; + } + + } else if (OAUTH_HTTP_ADAPTER_TYPE.equals(eventAdapterType)) { + if (transportType == TransportType.HTTP) { + return; + } + } + // remove mqtt event reciever before publishing + receiverAdminServiceStub.undeployActiveEventReceiverConfiguration(eventRecieverName); + } + String adapterType = OAUTH_MQTT_ADAPTER_TYPE; BasicInputAdapterPropertyDto basicInputAdapterPropertyDtos[]; if (transportType == TransportType.MQTT) { @@ -448,7 +468,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } else { adapterType = OAUTH_HTTP_ADAPTER_TYPE; basicInputAdapterPropertyDtos = new BasicInputAdapterPropertyDto[1]; - basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("contentValidator", "iot-mqtt"); + basicInputAdapterPropertyDtos[0] = getBasicInputAdapterPropertyDto("contentValidator", "iot-http"); } if (receiverAdminServiceStub.getActiveEventReceiverConfiguration(eventRecieverName) == null) { receiverAdminServiceStub.deployJsonEventReceiverConfiguration(eventRecieverName, streamNameWithVersion @@ -468,7 +488,9 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe eventStreamDefinitionDto.setName(streamName); eventStreamDefinitionDto.setVersion(version); EventStreamAttributeDto eventStreamAttributeDtos[] = - new EventStreamAttributeDto[eventAttributes.getList().size() + 1]; + new EventStreamAttributeDto[eventAttributes.getList().size()]; + EventStreamAttributeDto metaStreamAttributeDtos[] = + new EventStreamAttributeDto[1]; int i = 0; for (Attribute attribute : eventAttributes.getList()) { EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); @@ -481,8 +503,9 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); eventStreamAttributeDto.setAttributeName(DEFAULT_DEVICE_ID_ATTRIBUTE); eventStreamAttributeDto.setAttributeType(AttributeType.STRING.toString()); - eventStreamAttributeDtos[i] = eventStreamAttributeDto; + metaStreamAttributeDtos[0] = eventStreamAttributeDto; eventStreamDefinitionDto.setPayloadData(eventStreamAttributeDtos); + eventStreamDefinitionDto.setMetaData(metaStreamAttributeDtos); String streamId = streamName + ":" + version; if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamId) != null) { eventStreamAdminServiceStub.editEventStreamDefinitionAsDto(eventStreamDefinitionDto, streamId); @@ -521,7 +544,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe i++; } AnalyticsTableRecord analyticsTableRecord = new AnalyticsTableRecord(); - analyticsTableRecord.setColumnName(DEFAULT_DEVICE_ID_ATTRIBUTE); + analyticsTableRecord.setColumnName(DEFAULT_META_DEVICE_ID_ATTRIBUTE); analyticsTableRecord.setColumnType(AttributeType.STRING.toString().toUpperCase()); analyticsTableRecord.setFacet(false); analyticsTableRecord.setIndexed(true); @@ -540,7 +563,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe throws RemoteException, UserStoreException, JWTClientException { EventPublisherAdminServiceStub eventPublisherAdminServiceStub = getEventPublisherAdminServiceStub(); try { - String eventPublisherName = deviceType.trim().toLowerCase() + "_websocket_publisher"; + String eventPublisherName = deviceType.trim().replace(" ", "_") + "_websocket_publisher"; if (eventPublisherAdminServiceStub.getActiveEventPublisherConfiguration(eventPublisherName) == null) { eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(eventPublisherName , streamNameWithVersion, DEFAULT_WEBSOCKET_PUBLISHER_ADAPTER_TYPE, null, null @@ -747,7 +770,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } private String getStreamDefinition(String deviceType, String tenantDomain) { - return tenantDomain.toLowerCase() + "." + deviceType.toLowerCase(); + return "iot.per.device.stream." + tenantDomain + "." + deviceType.replace(" ", "."); } private String getTableName(String streamName) { @@ -755,7 +778,7 @@ public class DeviceEventManagementServiceImpl implements DeviceEventManagementSe } private String getReceiverName(String deviceType, String tenantDomain) { - return deviceType.trim().toLowerCase() + "-" + tenantDomain.toLowerCase() + "-receiver"; + return deviceType.replace(" ", "_").trim() + "-" + tenantDomain + "-receiver"; } public static AnalyticsDataAPI getAnalyticsDataAPI() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 16e75f541c..0ae988be45 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -98,6 +98,16 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } try { DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + if (device.getType() == null || device.getDeviceIdentifier() == null) { + String errorMessage = "The payload of the device enrollment is incorrect."; + return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + } + Device existingDevice = dms.getDevice(new DeviceIdentifier(device.getType(), device.getType())); + if (existingDevice != null && existingDevice.getEnrolmentInfo() != null && existingDevice + .getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) { + String errorMessage = "An active enrolment exists"; + return Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build(); + } device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser()); device.getEnrolmentInfo().setDateOfEnrolment(System.currentTimeMillis()); device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java index 06ca5811df..5fdc06a501 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java @@ -93,7 +93,7 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ @Override @GET - @Path("/all") + @Path("/config") public Response getDeviceTypes() { try { List deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceTypes(); @@ -111,7 +111,7 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ @Override @GET - @Path("/all/{type}") + @Path("/config/{type}") public Response getDeviceTypeByName(@PathParam("type") String type) { if (type != null && type.length() > 0) { try { @@ -138,12 +138,14 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ */ private DeviceType clearMetaEntryInfo(DeviceType deviceType) { DeviceTypeMetaDefinition metaDefinition = deviceType.getDeviceTypeMetaDefinition(); - metaDefinition.setInitialOperationConfig(null); - if (metaDefinition.getPushNotificationConfig() != null) { - metaDefinition.setPushNotificationConfig(new PushNotificationConfig(metaDefinition. - getPushNotificationConfig().getType(), false, null)); + if (metaDefinition != null) { + metaDefinition.setInitialOperationConfig(null); + if (metaDefinition.getPushNotificationConfig() != null) { + metaDefinition.setPushNotificationConfig(new PushNotificationConfig(metaDefinition. + getPushNotificationConfig().getType(), false, null)); + } + deviceType.setDeviceTypeMetaDefinition(metaDefinition); } - deviceType.setDeviceTypeMetaDefinition(metaDefinition); return deviceType; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 496d7033c6..cc8083009b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -304,6 +304,9 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis public OperationManager getOperationManager(String deviceType, int tenantId) { //Priority need to be given to the tenant before public. DeviceTypeServiceIdentifier deviceTypeIdentifier = new DeviceTypeServiceIdentifier(deviceType, tenantId); + if (getDeviceManagementService(deviceType, tenantId) == null) { + return null; + } OperationManager operationManager = operationManagerRepository.getOperationManager(deviceTypeIdentifier); if (operationManager == null) { deviceTypeIdentifier = new DeviceTypeServiceIdentifier(deviceType); @@ -330,7 +333,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis DeviceTypeServiceIdentifier deviceTypeIdentifier = new DeviceTypeServiceIdentifier( provider.getType(), tenantId); DeviceManagementServiceHolder existingProvider = providers.get(deviceTypeIdentifier); - if (existingProvider == null) { + if (existingProvider != null) { removeDeviceManagementProvider(provider); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeServiceIdentifier.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeServiceIdentifier.java index 70f6ce84be..d6d48a8497 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeServiceIdentifier.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeServiceIdentifier.java @@ -29,12 +29,12 @@ public class DeviceTypeServiceIdentifier implements Serializable { private static final int DEFAULT_SHARE_WITH_ALL_TENANTS_ID = -1; public DeviceTypeServiceIdentifier(String deviceType, int tenantId) { - this.deviceType = deviceType.toLowerCase(); + this.deviceType = deviceType; this.tenantId = tenantId; } public DeviceTypeServiceIdentifier(String deviceType) { - this.deviceType = deviceType.toLowerCase(); + this.deviceType = deviceType; this.tenantId = DEFAULT_SHARE_WITH_ALL_TENANTS_ID; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index 3e4c75a337..d59af12fe8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -167,6 +167,20 @@ if (!user) { }else { response.sendError(403); } + } else if (uriMatcher.match("/{context}/api/devices/agent/{type}/{deviceId}/config")) { + log.error("matching"); + elements = uriMatcher.elements(); + deviceId = elements.deviceId; + type = elements.type; + operation = elements.operation; + if (userModule.isAuthorized("/permission/admin/device-mgt/devices/owning-device")) { + result = deviceModule.getDeviceAgentConfig(type, deviceId); + if (!result) { + response.sendError(500); + } + } else { + response.sendError(403); + } } else if (uriMatcher.match("{context}/api/devices/{type}/{deviceId}/{operation}")) { elements = uriMatcher.elements(); deviceId = elements.deviceId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js index 838444d8a8..7a792e2041 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js @@ -25,7 +25,8 @@ deviceModule = function () { var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"]; var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var batchProvider = require("/app/modules/batch-provider-api.js")["batchProviders"]; - + var process = require("process"); + var carbon = require("carbon"); var publicMethods = {}; var privateMethods = {}; @@ -305,6 +306,15 @@ deviceModule = function () { return response; }; + publicMethods.getDeviceTypesConfig = function () { + var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/device-types/config"; + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + if (response.status == "success") { + response.content = parse(response.content); + } + return response; + }; + /* @Updated */ @@ -344,5 +354,63 @@ deviceModule = function () { } ); }; + + publicMethods.getDeviceAgentConfig = function (type, deviceId) { + var carbonUser = session.get(constants["USER_SESSION_KEY"]); + if (!carbonUser) { + log.error("User object was not found in the session"); + throw constants["ERRORS"]["USER_NOT_FOUND"]; + } + var userName = carbonUser.username + "@" + carbonUser.domain; + var config = {}; + config.type = type; + config.deviceId = deviceId; + // register a tenant based app at API Manager + var applicationName = type.replace(" ", "") + "_" + carbonUser.domain; + var requestURL = (devicemgtProps["oauthProvider"]["appRegistration"] + ["apiManagerClientAppRegistrationServiceURL"]).replace("/tenants",""); + var payload = {applicationName:applicationName, tags:["device_management"], + isAllowedToAllDomains:false, validityPeriod: 3600}; + + serviceInvokers.XMLHttp.post( + requestURL, payload, function (responsePayload) { + var app = JSON.parse(responsePayload.responseText); + + config.clientId = app["client_id"]; + config.clientSecret = app["client_secret"]; + if (config.clientId && config.clientSecret) { + var JWTClientManagerServicePackagePath = + "org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService"; + //noinspection JSUnresolvedFunction, JSUnresolvedVariable + var JWTClientManagerService = carbon.server.osgiService(JWTClientManagerServicePackagePath); + //noinspection JSUnresolvedFunction + var jwtClient = JWTClientManagerService.getJWTClient(); + // returning access token by JWT grant type + var deviceScope = "device_" + type.replace(" ", "") + "_" + deviceId; + var tokenInfo = jwtClient.getAccessToken(config.clientId, config.clientSecret, + userName, deviceScope); + config.accessToken = tokenInfo.getAccessToken(); + config.refreshToken = tokenInfo.getRefreshToken(); + if (config.accessToken == null) { + return null; + } + config.mqttGateway = "tcp://" + process.getProperty("mqtt.broker.host") + ":" + process.getProperty("mqtt.broker.port"); + config.httpsGateway = "https://" + process.getProperty("iot.gateway.host") + ":" + process.getProperty("iot.gateway.https.port"); + config.httpGateway = "http://" + process.getProperty("iot.gateway.host") + ":" + process.getProperty("iot.gateway.http.port"); + return config; + } else { + return null; + } + return config; + }, + function (responsePayload) { + log.error(responsePayload); + return null; + } + ); + return config; + + }; + return publicMethods; }(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.hbs index 326dd582bf..88731579ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.hbs @@ -103,9 +103,7 @@
+ placeholder="description" data-error-msg="invalid feature description" class="form-control" rows="1" cols="30">{{this.description}}
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.js index 279f4a6552..94284ac016 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devicetype.edit/edit.js @@ -35,7 +35,7 @@ function onRequest(context) { var deviceType = request.getParameter("type"); var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; var restAPIEndpoint = deviceMgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] - + "/device-types/all/" + deviceType; + + "/device-types/config/" + deviceType; displayData.name = deviceType; serviceInvokers.XMLHttp.get( restAPIEndpoint, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.hbs index 90b23e1531..f482826acf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.hbs @@ -25,8 +25,7 @@ {{#zone "overview-section"}}
- Device Overview - {{label}}
- {{device.type}}sdf + Device Overview - {{device.type}} {{unit "cdmf.unit.default.device.overview-section" device=device}} {{/zone}} @@ -63,7 +62,7 @@ {{#if attributes}}
-
Realtime Statistics
+
Device Event
{{unit "cdmf.unit.default.device.type.realtime.analytics-view" device=device attributes=attributes}}
{{/if}} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.js index c491947a74..62e248c783 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.device-view/device-view.js @@ -24,7 +24,6 @@ function onRequest(context) { var deviceId = request.getParameter("id"); var attributes = []; var featureList = []; - log.error(featureList); var restAPIEndpoint = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/events/" + deviceType; serviceInvokers.XMLHttp.get( @@ -50,7 +49,6 @@ function onRequest(context) { + "/device-types/" + deviceType + "/features"; serviceInvokers.XMLHttp.get(featureEndpoint, function (responsePayload) { var features = JSON.parse(responsePayload.responseText); - new Log().error(responsePayload.responseText); var feature; for (var i = 0; i < features.length; i++) { feature = {}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.hbs index aa128d1630..485c91dcf7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.hbs @@ -18,12 +18,13 @@ {{unit "cdmf.unit.lib.rickshaw-graph"}}
+ {{#if timestamp}}Last Known:{{timestamp}}{{/if}} - {{#each attributes}} + {{#each events}} - - + + {{/each}} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.js index cec05e10b3..e11d700c79 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/analytics-view.js @@ -15,12 +15,15 @@ * specific language governing permissions and limitations * under the License. */ +var serviceInvokers = require("/app/modules/oauth/token-protected-service-invokers.js")["invokers"]; +var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"]; function onRequest(context) { var log = new Log("stats.js"); var carbonServer = require("carbon").server; var device = context.unit.params.device; var attributes = context.unit.params.attributes; + var events = []; var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"]; var userModule = require("/app/modules/business-controllers/user.js")["userModule"]; var constants = require("/app/modules/constants.js"); @@ -40,15 +43,43 @@ function onRequest(context) { token = tokenPair.accessToken; } if (tenantDomain == "carbon.super") { - websocketEndpoint = websocketEndpoint + "/secured-websocket/" + tenantDomain + "." + device.type + "/1.0.0?" + websocketEndpoint = websocketEndpoint + "/secured-websocket/iot.per.device.stream." + tenantDomain + "." + device.type + "/1.0.0?" + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; } else { - websocketEndpoint = websocketEndpoint + "/t/" + tenantDomain + "/secured-websocket/" + tenantDomain + websocketEndpoint = websocketEndpoint + "/t/" + tenantDomain + "/secured-websocket/iot.per.device.stream." + tenantDomain + "." + device.type + "/1.0.0?" + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; } } + var events = []; + var viewModel = {}; + viewModel.device = device; + viewModel.websocketEndpoint = websocketEndpoint; - return {"device": device, "websocketEndpoint": websocketEndpoint, "attributes": attributes}; + var restAPIEndpoint = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + + "/events/last-known/" + device.type + "/" + device.deviceIdentifier; + serviceInvokers.XMLHttp.get( + restAPIEndpoint, + function (restAPIResponse) { + if (restAPIResponse["status"] == 200 && restAPIResponse["responseText"]) { + var responsePayload = parse(restAPIResponse["responseText"]); + var records = responsePayload["records"]; + if (records && records[0] && records[0].values) { + var record = records[0].values; + viewModel.timestamp = new Date(records[0].timestamp); + for (var eventAttribute in attributes){ + var event = {}; + event.key = attributes[eventAttribute]; + event.value = record["" + attributes[eventAttribute]]; + events.push(event); + } + } + + } + } + ); + viewModel.attributes = attributes; + viewModel.events = events; + return viewModel; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/public/js/device-stats.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/public/js/device-stats.js index 6eddd09ff6..48bc419600 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/public/js/device-stats.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.realtime.analytics-view/public/js/device-stats.js @@ -41,10 +41,12 @@ function connect(target) { if (ws) { ws.onmessage = function (webSocketData) { var data = JSON.parse(webSocketData.data); + console.log(data); var payloadData = data["event"]["payloadData"]; for (var i = 0; i < attributes.length; i++){ $("#" + attributes[i] +"-value").text(payloadData[attributes[i]]); } + $("#time-mode").text("Real Time Mode"); }; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/public/js/type-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/public/js/type-view.js index d236553341..f5b93adcdc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/public/js/type-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/public/js/type-view.js @@ -434,8 +434,22 @@ $(document).ready(function () { device, function (data, textStatus, jqXHR) { if (jqXHR.status == 200) { - $(successMsg).text("Device added."); - $(successMsgWrapper).removeClass("hidden"); + $.ajax({ + type: "GET", + url: "/devicemgt/api/devices/agent/" + deviceType + "/" + deviceId + "/config", + success: function(data, status, xhr) { + var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data)); + var dlAnchorElem = document.getElementById('downloadAnchorElem'); + dlAnchorElem.setAttribute("href", dataStr ); + dlAnchorElem.setAttribute("download", deviceId + ".json"); + dlAnchorElem.click(); + $("#modalDevice").modal('show'); + }, + error: function(xhr, status, error) { + $(errorMsg).text("Device Created, But failed to download the agent configuration."); + $(errorMsgWrapper).removeClass("hidden"); + } + }); } }, function (jqXHR) { @@ -452,3 +466,9 @@ $(document).ready(function () { ); }); }); + +function redirectPage(url) { + var deviceType = $("#deviceTypeName").val(); + var deviceId = $("#deviceId").val(); + location.href= url + '/' + deviceType + "?id=" + deviceId; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.hbs index 1127befc8b..b26f7c72f7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.hbs @@ -172,7 +172,7 @@
- + @@ -188,6 +188,20 @@ + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.js index 5e8e1d143a..81b39fa204 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.default.device.type.type-view/type-view.js @@ -31,7 +31,7 @@ function onRequest(context) { return opts.inverse(this); }); var restAPIEndpoint = deviceMgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] - + "/device-types/all/" + deviceType; + + "/device-types/config/" + deviceType; displayData.deviceType = deviceType; displayData.tenantDomain = tenantDomain; serviceInvokers.XMLHttp.get( @@ -59,7 +59,6 @@ function onRequest(context) { var eventExample = {}; for (var i = 0; i < typeData.eventAttributes.attributes.length; i++) { var attribute = typeData.eventAttributes.attributes[i]; - new Log().error(attribute.type); switch (attribute.type) { case "STRING": eventExample[attribute.name] = "string"; @@ -83,7 +82,10 @@ function onRequest(context) { } } + var metaEventExample = {}; + metaEventExample.deviceId = "deviceIdentifier"; sample.event.payloadData = eventExample; + sample.event.metaData = metaEventExample; displayData.eventSample = JSON.stringify(sample); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.js index 26f20a2fe9..fc49fe5d4c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.js @@ -32,11 +32,11 @@ function onRequest(context) { types.isAuthorizedViewGroups = userModule.isAuthorized("/permission/admin/device-mgt/groups/view"); types["types"] = []; - var typesListResponse = deviceModule.getDeviceTypes(); + var typesListResponse = deviceModule.getDeviceTypesConfig(); if (typesListResponse["status"] == "success") { - for (var type in typesListResponse["content"]["deviceTypes"]) { + for (var type in typesListResponse["content"]) { var content = {}; - var deviceType = typesListResponse["content"]["deviceTypes"][type]; + var deviceType = typesListResponse["content"][type].name; content["name"] = deviceType; var configs = utility.getDeviceTypeConfig(deviceType); var deviceTypeLabel = deviceType; @@ -64,7 +64,10 @@ function onRequest(context) { } types["types"].push(content); } else { - policyWizardSrc = "cdmf.unit.policy.create" + if (!typesListResponse["content"][type].deviceTypeMetaDefinition) { + continue; + } + policyWizardSrc = "cdmf.unit.policy.create"; var policyOperationsTemplateSrc = policyWizardSrc + "/public/templates/" + deviceType + "-policy-operations.hbs"; if (new File(policyOperationsTemplateSrc).isExists()) { content["template"] = "/public/cdmf.unit.device.type." + deviceType +
{{this}}-{{this.key}}{{#if this.value}}{{this.value}}{{else}}-{{/if}}