diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml index 61c65924..aef42d11 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -19,6 +19,7 @@ + @@ -87,6 +88,8 @@ **/*.sh **/*.hbs **/*.py + **/*-view.js + **/*-graph.js @@ -99,6 +102,29 @@ + + + + src/assembly + + **/*.xml + + + + src/main/resources/carbonapps + + **/*.* + + + + + + build.xml + + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/build.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/build.xml new file mode 100644 index 00000000..be799706 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/build.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/pom.xml new file mode 100644 index 00000000..c15ef204 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/pom.xml @@ -0,0 +1,86 @@ + + + + ${groupId} + ${rootArtifactId}-component + ${version} + ../pom.xml + + 4.0.0 + ${artifactId} + ${version} + pom + ${artifactId} + http://wso2.org + + + + maven-clean-plugin + ${maven-clean-plugin.version} + + + auto-clean + initialize + + clean + + + + + + maven-antrun-plugin + ${wso2.maven.compiler.source} + + + process-resources + + + + + + + run + + + + + + maven-assembly-plugin + ${maven-assembly-plugin.version} + + ${project.artifactId}-${version} + false + + src/assembly/src.xml + + + + + create-archive + package + + single + + + + + + + \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/assembly/src.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/assembly/src.xml new file mode 100644 index 00000000..a5a37501 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/assembly/src.xml @@ -0,0 +1,36 @@ + + + + src + + zip + + false + ${basedir}/src + + + ${basedir}/target/carbonapps + / + true + + + \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/EventPublisher___nameOfTheSensor__.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/EventPublisher___nameOfTheSensor__.xml new file mode 100644 index 00000000..bbfd2189 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/EventPublisher___nameOfTheSensor__.xml @@ -0,0 +1,25 @@ + + + + + + + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/artifact.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/artifact.xml new file mode 100644 index 00000000..bdbf22a2 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventpublisher___nameOfTheSensor___1.0.0/artifact.xml @@ -0,0 +1,22 @@ + + + + + EventPublisher_${nameOfTheSensor}.xml + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/EventReceiver_mqtt___nameOfTheSensor__.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/EventReceiver_mqtt___nameOfTheSensor__.xml new file mode 100644 index 00000000..761036b0 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/EventReceiver_mqtt___nameOfTheSensor__.xml @@ -0,0 +1,32 @@ + + + + + carbon.super/${deviceType}/+/publisher + admin + device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2 + org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTContentValidator + default + https://localhost:${carbon.https.port}/dynamic-client-web/register + tcp://${mqtt.broker.host}:${mqtt.broker.port} + true + + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/artifact.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/artifact.xml new file mode 100644 index 00000000..ca3e0301 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventreceiver_mqtt___nameOfTheSensor___1.0.0/artifact.xml @@ -0,0 +1,22 @@ + + + + + EventReceiver_mqtt_${nameOfTheSensor}.xml + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/artifact.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/artifact.xml new file mode 100644 index 00000000..0877f658 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/artifact.xml @@ -0,0 +1,22 @@ + + + + + org_wso2_iot_devices_${nameOfTheSensor}.xml + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/org_wso2_iot_devices___nameOfTheSensor__.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/org_wso2_iot_devices___nameOfTheSensor__.xml new file mode 100644 index 00000000..22764126 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstore___nameOfTheSensor___1.0.0/org_wso2_iot_devices___nameOfTheSensor__.xml @@ -0,0 +1,62 @@ + + + + + + org.wso2.iot.devices.${nameOfTheSensor}:1.0.0 + + EVENT_STORE + + + meta_owner + true + true + false + STRING + + + meta_deviceType + true + true + false + STRING + + + meta_deviceId + true + true + false + STRING + + + meta_time + true + true + false + LONG + + + ${nameOfTheSensor} + false + false + false + FLOAT + + + \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/artifact.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/artifact.xml new file mode 100644 index 00000000..5bc9ec1b --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/artifact.xml @@ -0,0 +1,23 @@ + + + + + org.wso2.iot.devices.${nameOfTheSensor}_1.0.0.json + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/org.wso2.iot.devices.__nameOfTheSensor___1.0.0.json b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/org.wso2.iot.devices.__nameOfTheSensor___1.0.0.json new file mode 100644 index 00000000..6d4e8a5c --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Eventstream___nameOfTheSensor___1.0.0/org.wso2.iot.devices.__nameOfTheSensor___1.0.0.json @@ -0,0 +1,20 @@ +{ + "name": "org.wso2.iot.devices.${nameOfTheSensor}", + "version": "1.0.0", + "nickName": "${nameOfTheSensor}", + "description": "${nameOfTheSensor} data received from the Device", + "metaData": [ + {"name":"owner","type":"STRING"}, + {"name":"deviceType","type":"STRING"}, + {"name":"deviceId","type":"STRING"}, + {"name":"time","type":"LONG"} + ], + "payloadData": [ + { + "name": "${nameOfTheSensor}","type": "FLOAT" + } + ] +} + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/__nameOfTheSensor___Script.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/__nameOfTheSensor___Script.xml new file mode 100644 index 00000000..b527e0cb --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/__nameOfTheSensor___Script.xml @@ -0,0 +1,28 @@ + + + + + IoTServer_Sensor_Script + + 0 * * * * ? + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/artifact.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/artifact.xml new file mode 100644 index 00000000..4304b82c --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/Sparkscripts_1.0.0/artifact.xml @@ -0,0 +1,22 @@ + + + + + ${nameOfTheSensor}_Script.xml + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/artifacts.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/artifacts.xml new file mode 100644 index 00000000..b8f962f5 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/analytics/src/main/resources/carbonapps/__nameOfTheSensor__/artifacts.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/pom.xml index fbe78494..72b48405 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/pom.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/pom.xml @@ -144,11 +144,6 @@ org.wso2.carbon.apimgt.annotations provided - - org.wso2.carbon.devicemgt - org.wso2.carbon.apimgt.webapp.publisher - provided - org.wso2.carbon.analytics org.wso2.carbon.analytics.api diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerServiceImpl.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerServiceImpl.java deleted file mode 100644 index 5e3aac6e..00000000 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerServiceImpl.java +++ /dev/null @@ -1,201 +0,0 @@ -/* -* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ - -package ${groupId}.${rootArtifactId}.api; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -import ${groupId}.${rootArtifactId}.api.util.APIUtil; -import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; -import ${groupId}.${rootArtifactId}.api.dto.DeviceJSON; -import ${groupId}.${rootArtifactId}.api.transport.MQTTConnector; -import ${groupId}.${rootArtifactId}.api.dto.SensorRecord; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.wso2.carbon.analytics.dataservice.commons.SortByField; -import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; -import org.wso2.carbon.analytics.dataservice.commons.SORT; -import org.wso2.carbon.apimgt.annotations.api.API; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; -import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; -import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener; -import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.FormParam; -import javax.ws.rs.QueryParam; -import javax.ws.rs.PathParam; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - - -/** - * This is the controller API which is used to control agent side functionality - */ -@SuppressWarnings("NonJaxWsWebServices") -@API(name = "${deviceType}", version = "1.0.0", context = "/${deviceType}", tags = "${deviceType}") -@DeviceType(value = "${deviceType}") -public class ControllerServiceImpl implements ControllerService{ - - private static Log log = LogFactory.getLog(ControllerService.class); - private MQTTConnector mqttConnector; - private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); - - private boolean waitForServerStartup() { - while (!IoTServerStartupListener.isServerReady()) { - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - return true; - } - } - return false; - } - - public MQTTConnector getMQTTConnector() { - return mqttConnector; - } - - public void setMQTTConnector(final MQTTConnector MQTTConnector) { - Runnable connector = new Runnable() { - public void run() { - if (waitForServerStartup()) { - return; - } - //The delay is added for the server to starts up. - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - ControllerServiceImpl.this.mqttConnector = MQTTConnector; - if (MqttConfig.getInstance().isEnabled()) { - mqttConnector.connect(); - } else { - log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, MQTTConnector" + - " not started."); - } - } - }; - Thread connectorThread = new Thread(connector); - connectorThread.setDaemon(true); - connectorThread.start(); - } - - /** - * @param agentInfo device owner,id and sensor value - * @return - */ - @Path("device/register") - @POST - @Consumes(MediaType.APPLICATION_JSON) - public Response registerDevice(final DeviceJSON agentInfo) { - String deviceId = agentInfo.deviceId; - if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) { - deviceToIpMap.put(deviceId, agentInfo); - return Response.status(Response.Status.OK).build(); - } - return Response.status(Response.Status.NOT_ACCEPTABLE).build(); - } - - /** - * @param deviceId unique identifier for given device type - * @param state change status of sensor: on/off - * @param response - */ - @Path("device/{deviceId}/change-status") - @POST - @Feature(code = "change-status", name = "Change status of sensor: on/off", type = "operation", - description = "Change status of sensor: on/off") - public Response changeStatus(@PathParam("deviceId") String deviceId, - @QueryParam("state") String state, - @Context HttpServletResponse response) { - try { - if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, - DeviceTypeConstants.DEVICE_TYPE))) { - return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); - } - String sensorState = state.toUpperCase(); - if (!sensorState.equals(DeviceTypeConstants.STATE_ON) && !sensorState.equals( - DeviceTypeConstants.STATE_OFF)) { - log.error("The requested state change should be either - 'ON' or 'OFF'"); - return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build(); - } - String mqttResource = DeviceTypeConstants.SENSOR_CONTEXT.replace("/", ""); - mqttConnector.publishDeviceData(deviceId, mqttResource, sensorState); - return Response.ok().build(); - } catch (TransportHandlerException e) { - log.error("Failed to send switch-bulb request to device [" + deviceId + "]"); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (DeviceAccessAuthorizationException e) { - log.error(e.getErrorMessage(), e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } - } - - /** - * Retrieve Sensor data for the ${deviceType} - */ - @Path("device/stats/{deviceId}") - @GET - @Consumes("application/json") - @Produces("application/json") - public Response getSensorStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, - @QueryParam("to") long to){ - String fromDate = String.valueOf(from); - String toDate = String.valueOf(to); - String query = "deviceId:" + deviceId + " AND deviceType:" + - DeviceTypeConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]"; - String sensorTableName = DeviceTypeConstants.TEMPERATURE_EVENT_TABLE; - try { - if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, - DeviceTypeConstants.DEVICE_TYPE))) { - return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); - } - if (sensorTableName != null) { - List sortByFields = new ArrayList<>(); - SortByField sortByField = new SortByField("time", SORT.ASC, false); - sortByFields.add(sortByField); - List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields); - return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build(); - } - } catch (AnalyticsException e) { - String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; - log.error(errorMsg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); - } catch (DeviceAccessAuthorizationException e) { - log.error(e.getErrorMessage(), e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } - return Response.status(Response.Status.BAD_REQUEST).build(); - } -} \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerService.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeService.java similarity index 55% rename from modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerService.java rename to modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeService.java index f6968246..8724a93c 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ControllerService.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeService.java @@ -18,29 +18,23 @@ package ${groupId}.${rootArtifactId}.api; -import java.util.concurrent.ConcurrentHashMap; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import ${groupId}.${rootArtifactId}.api.dto.DeviceJSON; -import ${groupId}.${rootArtifactId}.api.transport.MQTTConnector; import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener; import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Path; import javax.ws.rs.Consumes; -import javax.ws.rs.FormParam; import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; -import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.PUT; +import javax.ws.rs.DELETE; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -52,7 +46,7 @@ import javax.ws.rs.core.Response; @SuppressWarnings("NonJaxWsWebServices") @API(name = "${deviceType}", version = "1.0.0", context = "/${deviceType}", tags = "${deviceType}") @DeviceType(value = "${deviceType}") -public interface ControllerService { +public interface DeviceTypeService { /** * @param agentInfo device owner,id and sensor value @@ -61,6 +55,7 @@ public interface ControllerService { @Path("device/register") @POST @Consumes(MediaType.APPLICATION_JSON) + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/user/register"}) Response registerDevice(final DeviceJSON agentInfo); /** @@ -70,11 +65,12 @@ public interface ControllerService { */ @Path("device/{deviceId}/change-status") @POST - @Feature(code = "change-status", name = "Change status of sensor: on/off", type = "operation", + @Feature(code = "change-status", name = "Change status of sensor: on/off", description = "Change status of sensor: on/off") + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/change-status"}) Response changeStatus(@PathParam("deviceId") String deviceId, - @QueryParam("state") String state, - @Context HttpServletResponse response); + @QueryParam("state") String state, + @Context HttpServletResponse response); /** * Retrieve Sensor data for the ${deviceType} @@ -83,8 +79,37 @@ public interface ControllerService { @GET @Consumes("application/json") @Produces("application/json") + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/stats"}) Response getSensorStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, - @QueryParam("to") long to); + @QueryParam("to") long to); + + @Path("/device/{device_id}") + @DELETE + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/removeDevice"}) + Response removeDevice(@PathParam("device_id") String deviceId); + @Path("/device/{device_id}") + @PUT + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/updateDevice"}) + Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); + @Path("/device/{device_id}") + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/updateDevice"}) + Response getDevice(@PathParam("device_id") String deviceId); + + @Path("/devices") + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/devices"}) + Response getAllDevices(); + + @Path("/device/download") + @GET + @Produces("application/zip") + @Permission(scope = "${deviceType}_user", permissions = {"/permission/admin/device-mgt/download"}) + Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType); } \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerServiceImpl.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeServiceImpl.java similarity index 61% rename from modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerServiceImpl.java rename to modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeServiceImpl.java index 45fc7555..eebd47ee 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerServiceImpl.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/DeviceTypeServiceImpl.java @@ -1,29 +1,37 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ +* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ package ${groupId}.${rootArtifactId}.api; -import ${groupId}.${rootArtifactId}.api.util.ZipUtil; +import ${groupId}.${rootArtifactId}.api.dto.DeviceJSON; +import ${groupId}.${rootArtifactId}.api.dto.SensorRecord; import ${groupId}.${rootArtifactId}.api.util.APIUtil; +import ${groupId}.${rootArtifactId}.api.util.ZipUtil; import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; +import ${groupId}.${rootArtifactId}.api.DeviceTypeService; + import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.analytics.dataservice.commons.SORT; +import org.wso2.carbon.analytics.dataservice.commons.SortByField; +import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; +import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey; import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException; @@ -33,30 +41,52 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; -import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; +import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; +import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; import org.wso2.carbon.device.mgt.iot.util.ZipArchive; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.user.api.UserStoreException; -import javax.ws.rs.*; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Path; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Produces; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.PUT; +import javax.ws.rs.DELETE; +import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; + +import java.util.UUID; +import java.util.Map; +import java.util.List; +import java.util.HashMap; import java.util.ArrayList; import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; -@Path("enrollment") -public class ManagerServiceImpl implements ManagerService { + +/** + * This is the controller API which is used to control agent side functionality + */ +@SuppressWarnings("NonJaxWsWebServices") +@API(name = "${deviceType}", version = "1.0.0", context = "/${deviceType}", tags = "${deviceType}") +@DeviceType(value = "${deviceType}") +public class DeviceTypeServiceImpl implements DeviceTypeService { private static final String KEY_TYPE = "PRODUCTION"; + private static Log log = LogFactory.getLog(DeviceTypeService.class); private static ApiApplicationKey apiApplicationKey; - private static Log log = LogFactory.getLog(ManagerServiceImpl.class); + private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); private static String shortUUID() { UUID uuid = UUID.randomUUID(); @@ -64,7 +94,96 @@ public class ManagerServiceImpl implements ManagerService { return Long.toString(l, Character.MAX_RADIX); } - @Path("/devices/{device_id}") + /** + * @param agentInfo device owner,id and sensor value + * @return + */ + @Path("device/register") + @POST + @Consumes(MediaType.APPLICATION_JSON) + public Response registerDevice(final DeviceJSON agentInfo) { + String deviceId = agentInfo.deviceId; + if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) { + deviceToIpMap.put(deviceId, agentInfo); + return Response.status(Response.Status.OK).build(); + } + return Response.status(Response.Status.NOT_ACCEPTABLE).build(); + } + + /** + * @param deviceId unique identifier for given device type + * @param state change status of sensor: on/off + * @param response + */ + @Path("device/{deviceId}/change-status") + @POST + @Feature(code = "change-status", name = "Change status of sensor: on/off", + description = "Change status of sensor: on/off") + public Response changeStatus(@PathParam("deviceId") String deviceId, + @QueryParam("state") String state, + @Context HttpServletResponse response) { + try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + DeviceTypeConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } + String sensorState = state.toUpperCase(); + if (!sensorState.equals(DeviceTypeConstants.STATE_ON) && !sensorState.equals( + DeviceTypeConstants.STATE_OFF)) { + log.error("The requested state change should be either - 'ON' or 'OFF'"); + return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build(); + } + Map dynamicProperties = new HashMap<>(); + String publishTopic = APIUtil.getAuthenticatedUserTenantDomain() + + "/" + DeviceTypeConstants.DEVICE_TYPE + "/" + deviceId + "/command"; + dynamicProperties.put(DeviceTypeConstants.ADAPTER_TOPIC_PROPERTY, publishTopic); + APIUtil.getOutputEventAdapterService().publish(DeviceTypeConstants.MQTT_ADAPTER_NAME, + dynamicProperties, state); + return Response.ok().build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + } + + /** + * Retrieve Sensor data for the + */ + @Path("device/stats/{deviceId}") + @GET + @Consumes("application/json") + @Produces("application/json") + public Response getSensorStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, + @QueryParam("to") long to) { + String fromDate = String.valueOf(from); + String toDate = String.valueOf(to); + String query = "deviceId:" + deviceId + " AND deviceType:" + + DeviceTypeConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]"; + String sensorTableName = DeviceTypeConstants.TEMPERATURE_EVENT_TABLE; + try { + if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, + DeviceTypeConstants.DEVICE_TYPE))) { + return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); + } + if (sensorTableName != null) { + List sortByFields = new ArrayList<>(); + SortByField sortByField = new SortByField("time", SORT.ASC, false); + sortByFields.add(sortByField); + List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields); + return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build(); + } + } catch (AnalyticsException e) { + String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query; + log.error(errorMsg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build(); + } catch (DeviceAccessAuthorizationException e) { + log.error(e.getErrorMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + @Path("/device/{device_id}") @DELETE public Response removeDevice(@PathParam("device_id") String deviceId) { try { @@ -90,7 +209,7 @@ public class ManagerServiceImpl implements ManagerService { } } - @Path("/devices/{device_id}") + @Path("/device/{device_id}") @PUT public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) { try { @@ -120,7 +239,7 @@ public class ManagerServiceImpl implements ManagerService { } } - @Path("/devices/{device_id}") + @Path("/device/{device_id}") @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -166,7 +285,7 @@ public class ManagerServiceImpl implements ManagerService { } } - @Path("/devices/download") + @Path("/device/download") @GET @Produces("application/zip") public Response downloadSketch(@QueryParam("deviceName") String deviceName, @@ -191,9 +310,6 @@ public class ManagerServiceImpl implements ManagerService { } catch (APIManagerException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); - } catch (DeviceControllerException ex) { - log.error(ex.getMessage(), ex); - return Response.status(500).entity(ex.getMessage()).build(); } catch (IOException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); @@ -234,13 +350,13 @@ public class ManagerServiceImpl implements ManagerService { } private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType) - throws DeviceManagementException, APIManagerException, JWTClientException, DeviceControllerException, + throws DeviceManagementException, JWTClientException, APIManagerException, UserStoreException { //create new device id String deviceId = shortUUID(); if (apiApplicationKey == null) { - String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration() - .getAdminUserName(); + String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() + .getRealmConfiguration().getAdminUserName(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String[] tags = {DeviceTypeConstants.DEVICE_TYPE}; apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( @@ -250,20 +366,17 @@ public class ManagerServiceImpl implements ManagerService { String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_" + deviceId; AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), apiApplicationKey.getConsumerSecret(), owner, scopes); + //create token String accessToken = accessTokenInfo.getAccessToken(); String refreshToken = accessTokenInfo.getRefreshToken(); - boolean status; - status = register(deviceId, deviceName); + boolean status = register(deviceId, deviceName); if (!status) { String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner; throw new DeviceManagementException(msg); } ZipUtil ziputil = new ZipUtil(); - ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId, - deviceName, accessToken, refreshToken); - zipFile.setDeviceId(deviceId); + ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, + deviceId, deviceName, accessToken, refreshToken); return zipFile; } - -} - +} \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerService.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerService.java deleted file mode 100644 index 69d59414..00000000 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/ManagerService.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package ${groupId}.${rootArtifactId}.api; - -import org.wso2.carbon.apimgt.annotations.api.API; - -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -@Path("enrollment") -@API(name = "${deviceType}_mgt", version = "1.0.0", context = "/${deviceType}_mgt", tags = "${deviceType}") -public interface ManagerService { - - @Path("/devices/{device_id}") - @DELETE - Response removeDevice(@PathParam("device_id") String deviceId); - - @Path("/devices/{device_id}") - @PUT - Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name); - - @Path("/devices/{device_id}") - @GET - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - Response getDevice(@PathParam("device_id") String deviceId); - - @Path("/devices") - @GET - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - Response getAllDevices(); - - @Path("/devices/download") - @GET - @Produces("application/zip") - Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType); - -} \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/DeviceJSON.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/DeviceJSON.java index 54d2654a..be3a86c0 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/DeviceJSON.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/DeviceJSON.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package ${groupId}.${rootArtifactId}.api.dto; import org.codehaus.jackson.annotate.JsonIgnoreProperties; @@ -22,12 +23,15 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** - *These information are sent by agent in each request to server + * These information are sent by agent in each request to server */ @XmlRootElement @JsonIgnoreProperties(ignoreUnknown = true) public class DeviceJSON { - @XmlElement(required = true) public String owner; - @XmlElement(required = true) public String deviceId; - @XmlElement(required = true) public Float sensorValue; + @XmlElement(required = true) + public String owner; + @XmlElement(required = true) + public String deviceId; + @XmlElement(required = true) + public Float sensorValue; } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/SensorRecord.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/SensorRecord.java index dd097a89..8a197967 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/SensorRecord.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/dto/SensorRecord.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package ${groupId}.${rootArtifactId}.api.dto; import org.codehaus.jackson.annotate.JsonIgnoreProperties; @@ -9,17 +27,19 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -@XmlRootElement /** * This stores sensor event data for android sense. */ +@XmlRootElement @JsonIgnoreProperties(ignoreUnknown = true) public class SensorRecord { @XmlElementWrapper(required = true, name = "values") private Map values; - /** The id. */ + /** + * The id. + */ @XmlElement(required = false, name = "id") private String id; @@ -39,14 +59,6 @@ public class SensorRecord { this.values = values; } - /** - * Sets the id. - * @param id the new id - */ - public void setId(String id) { - this.id = id; - } - /** * Gets the id. * @return the id @@ -55,8 +67,16 @@ public class SensorRecord { return id; } + /** + * Sets the id. + * @param id the new id + */ + public void setId(String id) { + this.id = id; + } + @Override - public String toString(){ + public String toString() { List valueList = new ArrayList(); for (Map.Entry entry : values.entrySet()) { valueList.add(entry.getKey() + ":" + entry.getValue()); diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/exception/DeviceTypeException.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/exception/DeviceTypeException.java index e2e996dc..84fbf846 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/exception/DeviceTypeException.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/exception/DeviceTypeException.java @@ -24,14 +24,6 @@ public class DeviceTypeException extends Exception { private String errorMessage; - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - public DeviceTypeException(String msg, DeviceTypeException nestedEx) { super(msg, nestedEx); setErrorMessage(msg); @@ -54,4 +46,12 @@ public class DeviceTypeException extends Exception { public DeviceTypeException(Throwable cause) { super(cause); } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/transport/MQTTConnector.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/transport/MQTTConnector.java deleted file mode 100644 index 495f9dc5..00000000 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/transport/MQTTConnector.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package ${groupId}.${rootArtifactId}.api.transport; - -import java.nio.charset.StandardCharsets; - -import ${groupId}.${rootArtifactId}.api.util.APIUtil; -import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.eclipse.paho.client.mqttv3.MqttException; -import org.eclipse.paho.client.mqttv3.MqttMessage; -import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; -import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey; -import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException; -import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler; -import org.wso2.carbon.identity.jwt.client.extension.JWTClient; -import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; -import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; -import org.wso2.carbon.user.api.UserStoreException; - -import java.nio.charset.StandardCharsets; -import java.util.UUID; - - -import java.io.File; -import java.util.Calendar; -import java.util.UUID; - -/** - * MQTT is used as transport protocol. So this will provide basic functional requirement in order to communicate over - * MQTT - */ -@SuppressWarnings("no JAX-WS annotation") -public class MQTTConnector extends MQTTTransportHandler { - - private static final String publisherContext = "publisher"; - private static final String subscriberContext = "subscriber"; - private static final String subscribeTopic = "wso2/+/"+ DeviceTypeConstants.DEVICE_TYPE + "/+/" + publisherContext; - private static final String KEY_TYPE = "PRODUCTION"; - private static final String EMPTY_STRING = ""; - private static final String JSON_SERIAL_KEY = "SerialNumber"; - private static final String JSON_TENANT_KEY = "Tenant"; - private static Log log = LogFactory.getLog(MQTTConnector.class); - private static String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); - - private MQTTConnector() { - super(iotServerSubscriber, DeviceTypeConstants.DEVICE_TYPE, - MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic); - } - - /** - * {@inheritDoc} - * This method will initialize connection with message broker - */ - @Override - public void connect() { - Runnable connector = new Runnable() { - public void run() { - while (!isConnected()) { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( - DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); - try { - String applicationUsername = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm() - .getRealmConfiguration().getAdminUserName(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(applicationUsername); - APIManagementProviderService apiManagementProviderService = APIUtil - .getAPIManagementProviderService(); - String[] tags = {DeviceTypeConstants.DEVICE_TYPE}; - ApiApplicationKey apiApplicationKey = apiManagementProviderService - .generateAndRetrieveApplicationKeys(DeviceTypeConstants.DEVICE_TYPE, tags, KEY_TYPE, - applicationUsername, true); - JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient(); - String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_mqtt_connector"; - AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), - apiApplicationKey.getConsumerSecret(), applicationUsername, scopes); - //create token - String accessToken = accessTokenInfo.getAccessToken(); - setUsernameAndPassword(accessToken, EMPTY_STRING); - connectToQueue(); - } catch (TransportHandlerException e) { - log.error("Connection/Subscription to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e); - try { - Thread.sleep(timeoutInterval); - } catch (InterruptedException ex) { - log.error("MQTT-Connector: Thread Sleep Interrupt Exception.", ex); - } - } catch (JWTClientException e) { - log.error("Failed to retrieve token from JWT Client.", e); - return; - } catch (UserStoreException e) { - log.error("Failed to retrieve the user.", e); - return; - } catch (APIManagerException e) { - log.error("Failed to create an application and generate keys.", e); - return; - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - } - }; - - Thread connectorThread = new Thread(connector); - connectorThread.start(); - } - - /** - * This callback function will be called by message broker when some messages available to subscribed topic - * - * @param message mqtt message which is comming form agent side - * @param messageParams metadata of mqtt message - */ - @Override - public void processIncomingMessage(MqttMessage message, String... messageParams) throws TransportHandlerException { - - } - - /** - * connection with message broker can be terminated - */ - @Override - public void disconnect() { - Runnable stopConnection = new Runnable() { - public void run() { - while (isConnected()) { - try { - closeConnection(); - } catch (MqttException e) { - if (log.isDebugEnabled()) { - log.warn("Unable to 'STOP' MQTT connection at broker at: " + mqttBrokerEndPoint); - } - try { - Thread.sleep(timeoutInterval); - } catch (InterruptedException e1) { - log.error("MQTT-Terminator: Thread Sleep Interrupt Exception"); - } - } - } - } - }; - Thread terminatorThread = new Thread(stopConnection); - terminatorThread.setDaemon(true); - terminatorThread.start(); - } - - - @Override - public void publishDeviceData() throws TransportHandlerException { - - } - - @Override - public void publishDeviceData(MqttMessage publishData) throws TransportHandlerException { - - } - - @Override - public void publishDeviceData(String... publishData) throws TransportHandlerException { - if (publishData.length != 3) { - String errorMsg = "Incorrect number of arguments received to SEND-MQTT Message. " + - "Need to be [owner, deviceId, resource{SENSOR}, state{ON/OFF or null}]"; - log.error(errorMsg); - throw new TransportHandlerException(errorMsg); - } - - String deviceId = publishData[0]; - String resource = publishData[1]; - String state = publishData[2]; - - MqttMessage pushMessage = new MqttMessage(); - String publishTopic = "wso2/" + APIUtil.getTenantDomainOftheUser() + "/" + DeviceTypeConstants.DEVICE_TYPE - + "/" + deviceId; - String actualMessage = resource + ":" + state; - pushMessage.setPayload(actualMessage.getBytes(StandardCharsets.UTF_8)); - pushMessage.setQos(DEFAULT_MQTT_QUALITY_OF_SERVICE); - pushMessage.setRetained(false); - publishToQueue(publishTopic, pushMessage); - } - - @Override - public void processIncomingMessage() { - - } - - @Override - public void processIncomingMessage(MqttMessage message) throws TransportHandlerException { - - } - -} \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/APIUtil.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/APIUtil.java index 1ef00669..4d54030c 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/APIUtil.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/APIUtil.java @@ -1,6 +1,8 @@ package ${groupId}.${rootArtifactId}.api.util; import ${groupId}.${rootArtifactId}.api.dto.SensorRecord; + +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.dataservice.core.AnalyticsDataServiceUtils; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; @@ -172,12 +174,6 @@ public class APIUtil { return ids; } - /** - * Creates the SensorDatas from records. - * - * @param records the records - * @return the Map of SensorRecord - */ public static Map createSensorData(List records) { Map sensorDatas = new HashMap<>(); for (Record record : records) { @@ -187,12 +183,6 @@ public class APIUtil { return sensorDatas; } - /** - * Create a SensorRecord object out of a Record object - * - * @param record the record object - * @return SensorRecord object - */ public static SensorRecord createSensorData(Record record) { SensorRecord recordBean = new SensorRecord(); recordBean.setId(record.getId()); @@ -211,4 +201,21 @@ public class APIUtil { } return analyticsDataAPI; } + + public static String getAuthenticatedUserTenantDomain() { + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + return threadLocalCarbonContext.getTenantDomain(); + } + + public static OutputEventAdapterService getOutputEventAdapterService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + OutputEventAdapterService outputEventAdapterService = + (OutputEventAdapterService) ctx.getOSGiService(OutputEventAdapterService.class, null); + if (outputEventAdapterService == null) { + String msg = "Device Authorization service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return outputEventAdapterService; + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/Constants.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/Constants.java index ba838aae..86cc5bc0 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/Constants.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/Constants.java @@ -25,8 +25,8 @@ import org.wso2.carbon.user.core.Permission; */ public class Constants { - public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/${deviceType}/user"; - public static final String DEFAULT_ROLE_NAME = "${deviceType}_user"; - public static final Permission DEFAULT_PERMISSION[] = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE, - "ui.execute")}; + public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/${deviceType}/user"; + public static final String DEFAULT_ROLE_NAME = "${deviceType}_user"; + public static final Permission DEFAULT_PERMISSION[] + = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE, "ui.execute")}; } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ServiceUtils.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ServiceUtils.java index 485be466..df682a51 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ServiceUtils.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ServiceUtils.java @@ -19,8 +19,10 @@ package ${groupId}.${rootArtifactId}.api.util; import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -28,9 +30,11 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; public class ServiceUtils { private static final Log log = LogFactory.getLog(ServiceUtils.class); + /** * Sensor data are published to DAS - * @param deviceId unique identifier of the device + * + * @param deviceId unique identifier of the device * @param sensorValue current value of sensor which is set at agent side * @return */ @@ -42,8 +46,8 @@ public class ServiceUtils { Object metdaData[] = {owner, DeviceTypeConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()}; Object payloadData[] = {sensorValue}; try { - deviceAnalyticsService.publishEvent(DeviceTypeConstants.TEMPERATURE_STREAM_DEFINITION, - DeviceTypeConstants.TEMPERATURE_STREAM_DEFINITION_VERSION, metdaData, new Object[0], payloadData); + deviceAnalyticsService.publishEvent(DeviceTypeConstants.SENSOR_STREAM_DEFINITION, + DeviceTypeConstants.SENSOR_STREAM_DEFINITION_VERSION, metdaData, new Object[0], payloadData); } catch (DataPublisherConfigurationException e) { return false; } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ZipUtil.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ZipUtil.java index d28e3e84..ec72a87b 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ZipUtil.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/java/__groupId__/__rootArtifactId__/api/util/ZipUtil.java @@ -18,12 +18,10 @@ package ${groupId}.${rootArtifactId}.api.util; +import ${groupId}.${rootArtifactId}.plugin.mqtt.MqttConfig; + import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; -import org.wso2.carbon.device.mgt.iot.exception.IoTException; -import org.wso2.carbon.device.mgt.iot.util.IoTUtil; -import org.wso2.carbon.device.mgt.iot.util.IotDeviceManagementUtil; +import org.wso2.carbon.device.mgt.iot.util.Utils; import org.wso2.carbon.device.mgt.iot.util.ZipArchive; import org.wso2.carbon.utils.CarbonUtils; @@ -50,30 +48,24 @@ public class ZipUtil { String sketchFolder = "repository" + File.separator + "resources" + File.separator + "sketches"; String archivesPath = CarbonUtils.getCarbonHome() + File.separator + sketchFolder + File.separator + "archives" + - File.separator + deviceId; + File.separator + deviceId; String templateSketchPath = sketchFolder + File.separator + deviceType; String iotServerIP; try { - iotServerIP = IoTUtil.getHostName(); + iotServerIP = Utils.getServerUrl(); String httpsServerPort = System.getProperty(HTTPS_PORT_PROPERTY); String httpServerPort = System.getProperty(HTTP_PORT_PROPERTY); String httpsServerEP = HTTPS_PROTOCOL_APPENDER + iotServerIP + ":" + httpsServerPort; String httpServerEP = HTTP_PROTOCOL_APPENDER + iotServerIP + ":" + httpServerPort; String apimEndpoint = httpsServerEP; - String mqttEndpoint = MqttConfig.getInstance().getMqttQueueEndpoint(); + String mqttEndpoint = MqttConfig.getInstance().getBrokerEndpoint(); if (mqttEndpoint.contains(LOCALHOST)) { mqttEndpoint = mqttEndpoint.replace(LOCALHOST, iotServerIP); } - String xmppEndpoint = XmppConfig.getInstance().getXmppEndpoint(); - int indexOfChar = xmppEndpoint.lastIndexOf(":"); - if (indexOfChar != -1) { - xmppEndpoint = xmppEndpoint.substring(0, indexOfChar); - } - xmppEndpoint = xmppEndpoint + ":" + XmppConfig.getInstance().getSERVER_CONNECTION_PORT(); Map contextParams = new HashMap<>(); - contextParams.put("SERVER_NAME", "wso2/" + APIUtil.getTenantDomainOftheUser()); + contextParams.put("SERVER_NAME", APIUtil.getTenantDomainOftheUser()); contextParams.put("DEVICE_OWNER", owner); contextParams.put("DEVICE_ID", deviceId); contextParams.put("DEVICE_NAME", deviceName); @@ -81,15 +73,12 @@ public class ZipUtil { contextParams.put("HTTP_EP", httpServerEP); contextParams.put("APIM_EP", apimEndpoint); contextParams.put("MQTT_EP", mqttEndpoint); - contextParams.put("XMPP_EP", xmppEndpoint); contextParams.put("DEVICE_TOKEN", token); contextParams.put("DEVICE_REFRESH_TOKEN", refreshToken); ZipArchive zipFile; - zipFile = IotDeviceManagementUtil.getSketchArchive(archivesPath, templateSketchPath, contextParams); + zipFile = Utils.getSketchArchive(archivesPath, templateSketchPath, contextParams, deviceName); return zipFile; - } catch (IoTException e) { - throw new DeviceManagementException(e.getMessage()); } catch (IOException e) { throw new DeviceManagementException("Zip File Creation Failed", e); } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/META-INF/permissions.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/META-INF/permissions.xml index c45b9a19..87c3aa3d 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/META-INF/permissions.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/META-INF/permissions.xml @@ -26,40 +26,40 @@ it will result 403 error at the runtime. --> - - - - Get device - /device-mgt/${deviceType}/user - /enrollment/devices/* - GET - ${deviceType}_user - - - Remove device - /device-mgt/${deviceType}/user - /enrollment/devices/* - DELETE - ${deviceType}_user - - - Download device - /device-mgt/user - /enrollment/devices/download - GET - ${deviceType}_user - - - Update device - /device-mgt/${deviceType}/user - /enrollment/devices/* - POST - ${deviceType}_user - + + + + Get device + /device-mgt/${deviceType}/user + /device/* + GET + ${deviceType}_user + + + Remove device + /device-mgt/${deviceType}/user + /device/* + DELETE + ${deviceType}_user + + + Download device + /device-mgt/${deviceType}/user + /device/download + GET + ${deviceType}_user + + + Update device + /device-mgt/${deviceType}/user + /device/* + POST + ${deviceType}_user + Get Devices /device-mgt/${deviceType}/user - /enrollment/devices + /device GET ${deviceType}_user diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/cxf-servlet.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/cxf-servlet.xml index f6ed0e7a..a47a6891 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -25,19 +25,12 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - - - - + - - \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/web.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/web.xml index 2f18393c..e2641b15 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/web.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/api/src/main/webapp/WEB-INF/web.xml @@ -26,7 +26,7 @@ isSharedWithAllTenants - true + false providerTenantDomain diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml index eaebda7a..cf34e955 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/pom.xml @@ -70,7 +70,14 @@ org.wso2.carbon.device.mgt.common, org.wso2.carbon.device.mgt.iot.*, org.wso2.carbon.device.mgt.extensions.feature.mgt.*, - org.wso2.carbon.utils.* + org.wso2.carbon.utils.*, + org.wso2.carbon.event.output.adapter.core, + org.wso2.carbon.event.output.adapter.core.exception, + org.wso2.carbon.base, + org.wso2.carbon.core.util, + org.wso2.carbon.context, + org.wso2.carbon.core, + org.apache.commons.codec.binary !${project-base-package}.plugin.internal, @@ -82,6 +89,10 @@ + + commons-codec.wso2 + commons-codec + org.eclipse.osgi org.eclipse.osgi @@ -99,16 +110,24 @@ org.wso2.carbon.device.mgt.common - org.wso2.carbon - org.wso2.carbon.ndatasource.core + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core - org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot + org.wso2.carbon + org.wso2.carbon.ndatasource.core org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.extensions + + org.wso2.carbon + org.wso2.carbon.utils + + + org.wso2.carbon.analytics-common + org.wso2.carbon.event.output.adapter.core + \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java index b1680be8..7107829a 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/constants/DeviceTypeConstants.java @@ -18,19 +18,49 @@ package ${groupId}.${rootArtifactId}.plugin.constants; +import org.wso2.carbon.utils.CarbonUtils; +import java.io.File; + public class DeviceTypeConstants { public final static String DEVICE_TYPE = "${deviceType}"; public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME"; public final static String DEVICE_PLUGIN_DEVICE_ID = "${deviceType}_DEVICE_ID"; - public final static String SENSOR_READING = "sensorValue"; - public static final String DATA_SOURCE_NAME = "jdbc/${deviceType}DM_DB"; - public final static String DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN = "accessToken"; - public final static String DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN = "refreshToken"; - public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; public final static String STATE_ON = "ON"; public final static String STATE_OFF = "OFF"; - public final static String SENSOR_CONTEXT = "SENSOR"; - public final static String TEMPERATURE_EVENT_TABLE = "DEVICE_TEMPERATURE_SUMMARY"; - public final static String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; - public final static String TEMPERATURE_STREAM_DEFINITION_VERSION = "1.0.0"; + + //sensor events summerized table name + public static final String TEMPERATURE_EVENT_TABLE = "ORG_WSO2_IOT_DEVICES_${nameOfTheSensor}"; + public static final String DATA_SOURCE_NAME = "jdbc/${deviceType}DM_DB"; + public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; + + //mqtt tranport related constants + public static final String MQTT_ADAPTER_NAME = "${nameOfTheSensor}_mqtt"; + public static final String MQTT_ADAPTER_TYPE = "oauth-mqtt"; + public static final String ADAPTER_TOPIC_PROPERTY = "topic"; + public static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}"; + public static final String MQTT_BROKER_HOST = "\\$\\{mqtt.broker.host\\}"; + public static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset"; + public static final String DEFAULT_CARBON_LOCAL_IP_PROPERTY = "carbon.local.ip"; + public static final int CARBON_DEFAULT_PORT_OFFSET = 0; + public static final int DEFAULT_MQTT_PORT = 1883; + public static final String SUBSCRIBED_TOPIC = "carbon.super/${deviceType}/+/publisher"; + public static final String CONTENT_TRANSFORMATION = "contentTransformer"; + public static final String CONTENT_VALIDATION = "contentValidator"; + public static final String RESOURCE = "resource"; + + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String DCR_PROPERTY_KEY = "dcrUrl"; + public static final String BROKER_URL_PROPERTY_KEY = "url"; + public static final String SCOPES_PROPERTY_KEY = "scopes"; + public static final String QOS_PROPERTY_KEY = "qos"; + public static final String CLIENT_ID_PROPERTY_KEY = "qos"; + public static final String CLEAR_SESSION_PROPERTY_KEY = "clearSession"; + public static final String TOPIC = "topic"; + + public final static String SENSOR_STREAM_DEFINITION = "org.wso2.iot.devices.${nameOfTheSensor}"; + public final static String SENSOR_STREAM_DEFINITION_VERSION = "1.0.0"; + + public static final String MQTT_CONFIG_LOCATION = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + + "mqtt.properties"; } + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java index eca6621c..1038f636 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/DeviceTypeManagerService.java @@ -32,71 +32,37 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import java.util.List; -public class DeviceTypeManagerService implements DeviceManagementService{ - private DeviceManager deviceManager; +public class DeviceTypeManagerService implements DeviceManagementService { + private DeviceManager deviceManager; + + @Override + public String getType() { + return DeviceTypeConstants.DEVICE_TYPE; + } + + @Override + public void init() throws DeviceManagementException { + this.deviceManager = new DeviceTypeManager(); + } + + @Override + public DeviceManager getDeviceManager() { + return deviceManager; + } + + @Override + public ApplicationManager getApplicationManager() { + return null; + } + + @Override + public ProvisioningConfig getProvisioningConfig() { + return new ProvisioningConfig(DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, false); + } + + @Override + public PushNotificationConfig getPushNotificationConfig() { + return null; + } - @Override - public String getType() { - return DeviceTypeConstants.DEVICE_TYPE; - } - - @Override - public void init() throws DeviceManagementException { - this.deviceManager = new DeviceTypeManager(); - } - - @Override - public DeviceManager getDeviceManager() { - return deviceManager; - } - - @Override - public ApplicationManager getApplicationManager() { - return null; - } - - @Override - public ProvisioningConfig getProvisioningConfig() { - return new ProvisioningConfig(DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); - } - - @Override - public PushNotificationConfig getPushNotificationConfig() { - return null; - } - - /*@Override - public Application[] getApplications(String domain, int pageNumber, int size) - throws ApplicationManagementException { - return new Application[0]; - }*/ - - /*@Override - public void updateApplicationStatus(DeviceIdentifier deviceId, Application application, - String status) throws ApplicationManagementException { - - }*/ - - /*@Override - public String getApplicationStatus(DeviceIdentifier deviceId, Application application) - throws ApplicationManagementException { - return null; - } -*/ - /*@Override - public void installApplicationForDevices(Operation operation, List deviceIdentifiers) - throws ApplicationManagementException { - } - - @Override - public void installApplicationForUsers(Operation operation, List userNameList) - throws ApplicationManagementException { - - } - - @Override - public void installApplicationForUserRoles(Operation operation, List userRoleList) - throws ApplicationManagementException { - - }*/ } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java index a189b5ad..2092d615 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/DeviceTypeDAO.java @@ -20,11 +20,11 @@ package ${groupId}.${rootArtifactId}.plugin.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.impl.dao.impl.DeviceTypeDAOImpl; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; - import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -32,7 +32,7 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -public class DeviceTypeDAO{ +public class DeviceTypeDAO { private static final Log log = LogFactory.getLog(DeviceTypeDAO.class); static DataSource dataSource; // package local variable @@ -42,17 +42,13 @@ public class DeviceTypeDAO{ initDeviceTypeDAO(); } - public DeviceTypeDAOImpl getDeviceTypeDAO() { - return new DeviceTypeDAOImpl(); - } - - public static void initDeviceTypeDAO(){ + public static void initDeviceTypeDAO() { try { Context ctx = new InitialContext(); dataSource = (DataSource) ctx.lookup(DeviceTypeConstants.DATA_SOURCE_NAME); } catch (NamingException e) { log.error("Error while looking up the data source: " + - DeviceTypeConstants.DATA_SOURCE_NAME); + DeviceTypeConstants.DATA_SOURCE_NAME); } } @@ -98,14 +94,14 @@ public class DeviceTypeDAO{ public static void closeConnection() throws DeviceMgtPluginException { - Connection con = currentConnection.get(); - if(con != null){ - try { - con.close(); - } catch (SQLException e) { - log.error("Error occurred while close the connection"); - } - } + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } currentConnection.remove(); } @@ -126,4 +122,8 @@ public class DeviceTypeDAO{ closeConnection(); } } + + public DeviceTypeDAOImpl getDeviceTypeDAO() { + return new DeviceTypeDAOImpl(); + } } \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java index 64b0faba..db5e4031 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/dao/impl/DeviceTypeDAOImpl.java @@ -20,10 +20,12 @@ package ${groupId}.${rootArtifactId}.plugin.impl.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; import ${groupId}.${rootArtifactId}.plugin.impl.dao.DeviceTypeDAO; import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; + import org.wso2.carbon.device.mgt.common.Device; import java.sql.Connection; diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java new file mode 100644 index 00000000..be6c243c --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeStartupListener.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package ${groupId}.${rootArtifactId}.plugin.impl.util; + +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.core.ServerStartupObserver; + +import java.io.IOException; + +public class DeviceTypeStartupListener implements ServerStartupObserver { + private static final Log log = LogFactory.getLog(${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeStartupListener.class); + + @Override + public void completingServerStartup() { + } + + @Override + public void completedServerStartup() { + try { + DeviceTypeUtils.setupMqttOutputAdapter(); + } catch (IOException e) { + log.error("Failed to initialize the ${deviceType} output adapter", e); + } + } +} diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java index c9a4eabf..eb0252c9 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/impl/util/DeviceTypeUtils.java @@ -18,51 +18,40 @@ package ${groupId}.${rootArtifactId}.plugin.impl.util; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.sql.DataSource; - - - import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; +import ${groupId}.${rootArtifactId}.plugin.internal.DeviceTypeManagementDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.core.util.Utils; +import org.wso2.carbon.event.output.adapter.core.MessageType; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; +import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Properties; /** - * Contains utility methods used by ${rootArtifactId} plugin. + * Contains utility methods used by ${deviceType} plugin. */ public class DeviceTypeUtils { - private static Log log = LogFactory.getLog(DeviceTypeUtils.class); - - public static String getDeviceProperty(List deviceProperties, String propertyKey) { - String deviceProperty = ""; - for (Device.Property property : deviceProperties) { - if (propertyKey.equals(property.getName())) { - deviceProperty = property.getValue(); - } - } - return deviceProperty; - } - - public static Device.Property getProperty(String property, String value) { - if (property != null) { - Device.Property prop = new Device.Property(); - prop.setName(property); - prop.setValue(value); - return prop; - } - return null; - } + private static Log log = LogFactory.getLog(${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils.class); public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { if (rs != null) { @@ -111,4 +100,91 @@ public class DeviceTypeUtils { } } + public static String replaceMqttProperty(String urlWithPlaceholders) { + String MQTT_BROKER_HOST = null; + String MQTT_PORT = null; + if(!DeviceTypeConstants.MQTT_BROKER_HOST.startsWith("$")){ + MQTT_BROKER_HOST = "\\$".concat(DeviceTypeConstants.MQTT_BROKER_HOST); + } + if(!DeviceTypeConstants.MQTT_PORT.startsWith("$")){ + MQTT_PORT = "\\$".concat(DeviceTypeConstants.MQTT_PORT); + } + urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_PORT, "" + + (DeviceTypeConstants.DEFAULT_MQTT_PORT + getPortOffset())); + urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_BROKER_HOST, + System.getProperty(DeviceTypeConstants.DEFAULT_CARBON_LOCAL_IP_PROPERTY, "localhost")); + return urlWithPlaceholders; + } + + private static int getPortOffset() { + ServerConfiguration carbonConfig = ServerConfiguration.getInstance(); + String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty( + DeviceTypeConstants.CARBON_CONFIG_PORT_OFFSET)); + try { + if ((portOffset != null)) { + return Integer.parseInt(portOffset.trim()); + } else { + return DeviceTypeConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } catch (NumberFormatException e) { + return DeviceTypeConstants.CARBON_DEFAULT_PORT_OFFSET; + } + } + + public static void setupMqttOutputAdapter() throws IOException { + OutputEventAdapterConfiguration outputEventAdapterConfiguration = + createMqttOutputEventAdapterConfiguration(DeviceTypeConstants.MQTT_ADAPTER_NAME, + DeviceTypeConstants.MQTT_ADAPTER_TYPE, MessageType.TEXT); + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + DeviceTypeConstants.DEVICE_TYPE_PROVIDER_DOMAIN, true); + DeviceTypeManagementDataHolder.getInstance().getOutputEventAdapterService() + .create(outputEventAdapterConfiguration); + } catch (OutputEventAdapterException e) { + log.error("Unable to create Output Event Adapter : " + DeviceTypeConstants.MQTT_ADAPTER_NAME, e); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + /** + * Create Output Event Adapter Configuration for given configuration. + * + * @param name Output Event Adapter name + * @param type Output Event Adapter type + * @param msgFormat Output Event Adapter message format + * @return OutputEventAdapterConfiguration instance for given configuration + */ + private static OutputEventAdapterConfiguration createMqttOutputEventAdapterConfiguration(String name, String type, + String msgFormat) throws IOException { + OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration(); + outputEventAdapterConfiguration.setName(name); + outputEventAdapterConfiguration.setType(type); + outputEventAdapterConfiguration.setMessageFormat(msgFormat); + File configFile = new File(DeviceTypeConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + Map mqttAdapterProperties = new HashMap<>(); + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + mqttAdapterProperties.put(DeviceTypeConstants.USERNAME_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.USERNAME_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.DCR_PROPERTY_KEY, Utils.replaceSystemProperty( + properties.getProperty(DeviceTypeConstants.DCR_PROPERTY_KEY))); + mqttAdapterProperties.put(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY, replaceMqttProperty( + properties.getProperty(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY))); + mqttAdapterProperties.put(DeviceTypeConstants.SCOPES_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.SCOPES_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.CLEAR_SESSION_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.CLEAR_SESSION_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.QOS_PROPERTY_KEY, properties.getProperty( + DeviceTypeConstants.QOS_PROPERTY_KEY)); + mqttAdapterProperties.put(DeviceTypeConstants.CLIENT_ID_PROPERTY_KEY, ""); + mqttAdapterProperties.put(DeviceTypeConstants.RESOURCE, "output-event"); + outputEventAdapterConfiguration.setStaticProperties(mqttAdapterProperties); + } + return outputEventAdapterConfiguration; + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java new file mode 100644 index 00000000..4caab566 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/DeviceTypeManagementDataHolder.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package ${groupId}.${rootArtifactId}.plugin.internal; + +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; + +/** + * DataHolder class of fgfgt plugins component. + */ +public class DeviceTypeManagementDataHolder { + + private static DeviceTypeManagementDataHolder thisInstance = new DeviceTypeManagementDataHolder(); + private OutputEventAdapterService outputEventAdapterService; + + private DeviceTypeManagementDataHolder() { + } + + public static DeviceTypeManagementDataHolder getInstance() { + return thisInstance; + } + + public OutputEventAdapterService getOutputEventAdapterService() { + return outputEventAdapterService; + } + + public void setOutputEventAdapterService( + OutputEventAdapterService outputEventAdapterService) { + this.outputEventAdapterService = outputEventAdapterService; + } +} + diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java index a4f12aff..d675a0ed 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/internal/ServiceComponent.java @@ -18,34 +18,47 @@ package ${groupId}.${rootArtifactId}.plugin.internal; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeStartupListener; import ${groupId}.${rootArtifactId}.plugin.exception.DeviceMgtPluginException; import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; import ${groupId}.${rootArtifactId}.plugin.impl.DeviceTypeManagerService; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; /** * @scr.component name="${groupId}.${rootArtifactId}.plugin.internal.ServiceComponent" * immediate="true" + * @scr.reference name="event.output.adapter.service" + * interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService" + * cardinality="1..1" + * policy="dynamic" + * bind="setOutputEventAdapterService" + * unbind="unsetOutputEventAdapterService" */ public class ServiceComponent { - private ServiceRegistration serviceRegistration; private static final Log log = LogFactory.getLog(ServiceComponent.class); + private ServiceRegistration serviceRegistration; protected void activate(ComponentContext ctx) { if (log.isDebugEnabled()) { - log.debug("Activating ${rootArtifactId} Management Service Component"); + log.debug("Activating b Management Service Component"); } try { + DeviceTypeManagerService deviceTypeManagerService = new DeviceTypeManagerService(); BundleContext bundleContext = ctx.getBundleContext(); serviceRegistration = - bundleContext.registerService(DeviceManagementService.class.getName(), new - DeviceTypeManagerService(), null); + bundleContext.registerService(DeviceManagementService.class.getName(), + deviceTypeManagerService, null); + bundleContext.registerService(ServerStartupObserver.class.getName(), new DeviceTypeStartupListener(), + null); String setupOption = System.getProperty("setup"); if (setupOption != null) { if (log.isDebugEnabled()) { @@ -59,7 +72,7 @@ public class ServiceComponent { } } if (log.isDebugEnabled()) { - log.debug("${rootArtifactId} Management Service Component has been successfully activated"); + log.debug("b Management Service Component has been successfully activated"); } } catch (Throwable e) { log.error("Error occurred while activating Current Sensor Management Service Component", e); @@ -68,7 +81,7 @@ public class ServiceComponent { protected void deactivate(ComponentContext ctx) { if (log.isDebugEnabled()) { - log.debug("De-activating ${rootArtifactId} Management Service Component"); + log.debug("De-activating b Management Service Component"); } try { if (serviceRegistration != null) { @@ -81,4 +94,20 @@ public class ServiceComponent { log.error("Error occurred while de-activating Iot Device Management bundle", e); } } + + /** + * Initialize the Output EventAdapter Service dependency + * + * @param outputEventAdapterService Output EventAdapter Service reference + */ + protected void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DeviceTypeManagementDataHolder.getInstance().setOutputEventAdapterService(outputEventAdapterService); + } + + /** + * De-reference the Output EventAdapter Service dependency. + */ + protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + DeviceTypeManagementDataHolder.getInstance().setOutputEventAdapterService(null); + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java new file mode 100644 index 00000000..f5dbdb09 --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/plugin/src/main/java/__groupId__/__rootArtifactId__/plugin/mqtt/MqttConfig.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package ${groupId}.${rootArtifactId}.plugin.mqtt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import ${groupId}.${rootArtifactId}.plugin.constants.DeviceTypeConstants; +import ${groupId}.${rootArtifactId}.plugin.impl.util.DeviceTypeUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class MqttConfig { + + private static String brokerEndpoint; + + private static MqttConfig mqttConfig = new MqttConfig(); + private static final Log log = LogFactory.getLog(MqttConfig.class); + + private MqttConfig() { + File configFile = new File(DeviceTypeConstants.MQTT_CONFIG_LOCATION); + if (configFile.exists()) { + try { + InputStream propertyStream = configFile.toURI().toURL().openStream(); + Properties properties = new Properties(); + properties.load(propertyStream); + brokerEndpoint = DeviceTypeUtils.replaceMqttProperty( + properties.getProperty(DeviceTypeConstants.BROKER_URL_PROPERTY_KEY)); + } catch (IOException e) { + log.error("Failed to read the mqtt.properties file" + e); + } + } + } + + public static MqttConfig getInstance() { + return mqttConfig; + } + + public String getBrokerEndpoint() { + return brokerEndpoint; + } +} diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.hbs b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.hbs index 6a28c72c..03cf0d59 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.hbs +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.hbs @@ -5,7 +5,7 @@ data-appcontext="{{@app.context}}">
- Temperature + ${nameOfTheSensor}
diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.js b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.js index 55e39e6f..5336bc2c 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.js +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/analytics-view.js @@ -17,14 +17,23 @@ */ function onRequest(context) { + var devices = context.unit.params.devices; var deviceType = context.uriParams.deviceType; var deviceId = request.getParameter("deviceId"); - if (deviceType != null && deviceType != undefined && deviceId != null && deviceId != undefined) { + if (devices) { + return { + "devices": stringify(devices), + "backendApiUri": devicemgtProps["httpsURL"] + "/"+deviceType+"/device/stats/" + }; + } else if (deviceType != null && deviceType != undefined && deviceId != null && deviceId != undefined) { var deviceModule = require("/app/modules/device.js").deviceModule; var device = deviceModule.viewDevice(deviceType, deviceId); if (device && device.status != "error") { - return {"device": device, "backendApiUri" : devicemgtProps["httpsURL"] + "/"+deviceType+"/device/stats/" + deviceId}; + return { + "device": device, + "backendApiUri": devicemgtProps["httpsURL"] + "/"+deviceType+"/device/stats/" + deviceId + }; } else { response.sendError(404, "Device Id " + deviceId + " of type " + deviceType + " cannot be found!"); exit(); diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/public/js/devicetype-graph.js b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/public/js/devicetype-graph.js index 70bf616c..8babb6fb 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/public/js/devicetype-graph.js +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.analytics-view/public/js/devicetype-graph.js @@ -18,27 +18,20 @@ var palette = new Rickshaw.Color.Palette({scheme: "classic9"}); -function drawGraph(from, to) { - var backendApiUrl = $("#chart").data("backend-api-url") + "?from=" + from + "&to=" + to; - - var successCallback = function (data) { - if (data) { - drawLineGraph(JSON.parse(data)); - } - }; - invokerUtil.get(backendApiUrl, successCallback, function (message) { - console.log(message); - }); -} +function drawGraph_${deviceType}(from, to) { + $("#y_axis").html(""); + $("#smoother").html(""); + $("#legend").html(""); + $("#chart").html(""); + $("#x_axis").html(""); + $("#slider").html(""); + + var devices = $("#details").data("devices"); + + var tzOffset = new Date().getTimezoneOffset() * 60; -function drawLineGraph(data) { - var chartWrapperElmId = "#div-chart"; - var graphWidth = $(chartWrapperElmId).width() - 50; - if (data.length == 0 || data.length == undefined) { - $("#chart").html("
No data available..."); - return; - } - $("#chart").empty(); + var chartWrapperElmId = "#div-chart"; + var graphWidth = $(chartWrapperElmId).width() - 50; var graphConfig = { element: document.getElementById("chart"), width: graphWidth, @@ -53,47 +46,28 @@ function drawLineGraph(data) { series: [] }; - var tzOffset = new Date().getTimezoneOffset() * 60; - - var min = Number.MAX_VALUE; - var max = Number.MIN_VALUE; - var range_min = 99999, range_max = 0; - var max_val = parseInt(data[0].values.temperature); - var min_val = max_val; - var chartData = []; - for (var i = 0; i < data.length; i++) { - var y_val = parseInt(data[i].values.temperature); - if (y_val > max_val) { - max_val = y_val; - } else if (y_val < min_val) { - min_val = y_val; - } - chartData.push( + if (devices) { + for (var i = 0; i < devices.length; i++) { + graphConfig['series'].push( { - x: parseInt(data[i].values.time) - tzOffset, - y: y_val - } - ); - } - if (range_max < max_val) { - range_max = max_val; - } - if (range_min > min_val) { - range_min = min_val; - } - graphConfig['series'].push( + 'color': palette.color(), + 'data': [{ + x: parseInt(new Date().getTime() / 1000), + y: 0 + }], + 'name': devices[i].name + }); + } + } else { + graphConfig['series'].push( { 'color': palette.color(), - 'data': chartData, - 'name': $("#details").data("devicename"), - 'scale': d3.scale.linear().domain([Math.min(min, min_val), Math.max(max, max_val)]) - .nice() - } - ); - - if (graphConfig['series'].length == 0) { - $(chartWrapperElmId).html("No data available..."); - return; + 'data': [{ + x: parseInt(new Date().getTime() / 1000), + y: 0 + }], + 'name': $("#details").data("devicename") + }); } var graph = new Rickshaw.Graph(graphConfig); @@ -106,13 +80,12 @@ function drawLineGraph(data) { xAxis.render(); - var yAxis = new Rickshaw.Graph.Axis.Y.Scaled({ + var yAxis = new Rickshaw.Graph.Axis.Y({ graph: graph, orientation: 'left', element: document.getElementById("y_axis"), width: 40, - height: 410, - 'scale': d3.scale.linear().domain([Math.min(min, range_min), Math.max(max, range_max)]).nice() + height: 410 }); yAxis.render(); @@ -131,9 +104,9 @@ function drawLineGraph(data) { graph: graph, formatter: function (series, x, y) { var date = '' + - moment((x + tzOffset) * 1000).format('Do MMM YYYY h:mm:ss a') + ''; + moment.unix((x + tzOffset) * 1000).format('Do MMM YYYY h:mm:ss a') + ''; var swatch = ''; + series.color + '">'; return swatch + series.name + ": " + parseInt(y) + '
' + date; } }); @@ -152,4 +125,59 @@ function drawLineGraph(data) { graph: graph, legend: legend }); + + var deviceIndex = 0; + + if (devices) { + getData(); + } else { + var backendApiUrl = $("#chart").data("backend-api-url") + "?from=" + from + "&to=" + to; + var successCallback = function (data) { + if (data) { + drawLineGraph(JSON.parse(data)); + } + }; + invokerUtil.get(backendApiUrl, successCallback, function (message) { + console.log(message); + }); + } + + function getData() { + if (deviceIndex >= devices.length) { + return; + } + var backendApiUrl = $("#chart").data("backend-api-url") + devices[deviceIndex].deviceIdentifier + + "?from=" + from + "&to=" + to; + var successCallback = function (data) { + if (data) { + drawLineGraph(JSON.parse(data)); + } + deviceIndex++; + getData(); + }; + invokerUtil.get(backendApiUrl, successCallback, function (message) { + console.log(message); + deviceIndex++; + getData(); + }); + } + + function drawLineGraph(data) { + if (data.length === 0 || data.length === undefined) { + return; + } + + var chartData = []; + for (var i = 0; i < data.length; i++) { + chartData.push( + { + x: parseInt(data[i].values.time) - tzOffset, + y: parseInt(data[i].values.${nameOfTheSensor}) + } + ); + } + + graphConfig.series[deviceIndex].data = chartData; + graph.update(); + } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.hbs b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.hbs index 9d2f07c7..ad79ed4b 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.hbs +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.hbs @@ -3,7 +3,7 @@ {{/zone}}
-
Temperature
+
${nameOfTheSensor}
diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.js b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.js index 5b4bf524..78f6ab41 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.js +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/analytics-view.js @@ -27,7 +27,7 @@ function onRequest(context) { if (tokenPair) { token = tokenPair.accessToken; } - websocketEndpoint = websocketEndpoint + "/secured-outputui/org.wso2.iot.devices.temperature/1.0.0?" + + websocketEndpoint = websocketEndpoint + "/secured-outputui/org.wso2.iot.devices.${nameOfTheSensor}/1.0.0?" + "token="+ token +"&deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type; return {"device": device, "websocketEndpoint" : websocketEndpoint}; } \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/public/js/device-stats.js b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/public/js/device-stats.js index d24b1918..e3ea82f8 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/public/js/device-stats.js +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.realtime.analytics-view/public/js/device-stats.js @@ -63,7 +63,7 @@ $(window).load(function () { new Rickshaw.Graph.HoverDetail({ graph: graph, formatter: function (series, x, y) { - var date = '' + moment(x * 1000).format('Do MMM YYYY h:mm:ss a') + ''; + var date = '' + moment.unix(x*1000).format('Do MMM YYYY h:mm:ss a') + ''; var swatch = ''; return swatch + series.name + ": " + parseInt(y) + '
' + date; } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.type-view/private/config.json b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.type-view/private/config.json new file mode 100644 index 00000000..97bb71cf --- /dev/null +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/component/ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.__deviceType__.type-view/private/config.json @@ -0,0 +1,8 @@ + +{ + "deviceType": { + "label": "${deviceType}", + "category": "virtual", + "downloadAgentUri": "${deviceType}/device/download" + } +} \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/pom.xml index 92e01aa3..b3bcd521 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/pom.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/pom.xml @@ -84,6 +84,17 @@ + + ${groupId} + ${project-base-package}.analytics + ${version} + zip + true + + ${project.build.directory}/maven-shared-archive-resources/carbonapps + + **/* + ${groupId} ${project-base-package}.ui @@ -173,7 +184,7 @@ org.wso2.carbon.p2.category.type:server - org.eclipse.equinox.p2.type.group:false + org.eclipse.equinox.p2.type.group:true diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/iotUtils.py b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/iotUtils.py index 51d7007f..4291489a 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/iotUtils.py +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/iotUtils.py @@ -44,7 +44,7 @@ CONTROLLER_CONTEXT = configParser.get('Device-Configurations', 'controller-conte DEVICE_INFO = '{"owner":"' + DEVICE_OWNER + '","deviceId":"' + DEVICE_ID + '",' HTTPS_EP = configParser.get('Device-Configurations', 'https-ep') DEVICE_DATA = '"sensorValue":"{sensorValue}"' -SENSOR_STATS = '{{"event":{{"metaData":{{"owner":"' + DEVICE_OWNER + '","type":"'+ DEVICE_TYPE +'","deviceId":"' + DEVICE_ID + '","time":{:.2f}}},"payloadData":{{"temperature":{:.2f}}}}}}}' +SENSOR_STATS = '{{"event":{{"metaData":{{"owner":"' + DEVICE_OWNER + '","deviceType":"'+ DEVICE_TYPE +'","deviceId":"' + DEVICE_ID + '","time":{}}},"payloadData":{{"${nameOfTheSensor}":{:.2f}}}}}}}' ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/mqttHandler.py b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/mqttHandler.py index 44980ed9..4b74b8a5 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/mqttHandler.py +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/agent/src/mqttHandler.py @@ -71,9 +71,9 @@ def main(): DEV_TYPE =iotUtils.DEVICE_TYPE TANENT_DOMAIN = iotUtils.SERVER_NAME global TOPIC_TO_SUBSCRIBE - TOPIC_TO_SUBSCRIBE = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + TOPIC_TO_SUBSCRIBE = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/command" global TOPIC_TO_PUBLISH - TOPIC_TO_PUBLISH = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/temperature" + TOPIC_TO_PUBLISH = TANENT_DOMAIN + "/" + DEV_TYPE + "/" + DEV_ID + "/publisher" print ("MQTT_LISTENER: MQTT_ENDPOINT is " + str(MQTT_ENDPOINT)) print ("MQTT_LISTENER: MQTT_TOPIC is " + TOPIC_TO_SUBSCRIBE) diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/configs/__deviceType__.json b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/configs/__deviceType__.json index 703b0d88..63d2a0bd 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/configs/__deviceType__.json +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/configs/__deviceType__.json @@ -7,12 +7,12 @@ "analyticStreams": [ { "name": "Temperature", - "table": "DEVICE_TEMPERATURE_SUMMARY", + "table": "DEVICE_${deviceType}_${nameOfTheSensor}_SUMMARY", "ui_unit": { "name": "cdmf.unit.analytics.line-chart", "data":[ {"column": {"name":"TIME", "label":"time", "ui-mapping":"x-axis"}}, - {"column": {"name":"TEMPERATURE", "label":"Temperature", "ui-mapping":"y-axis"}} + {"column": {"name":"${nameOfTheSensor}", "label":"${nameOfTheSensor}", "ui-mapping":"y-axis"}} ] } } diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/p2.inf b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/p2.inf index 989a3510..3b50a17c 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/p2.inf +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/feature/feature/src/main/resources/p2.inf @@ -12,3 +12,17 @@ org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../depl org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/${groupId}.${rootArtifactId}_${feature.version}/jaggeryapps/,target:${installFolder}/../../deployment/server/jaggeryapps/,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../database/);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/${groupId}.${rootArtifactId}_${feature.version}/database/,target:${installFolder}/../../database/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../deployment/server/carbonapps/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/${groupId}.${rootArtifactId}_${feature.version}/carbonapps/,target:${installFolder}/../../deployment/server/carbonapps/,overwrite:true);\ + +instructions.unconfigure = \ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../conf/device-types/${deviceType}.xml);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/webapps/${deviceType}.war);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/webapps/${deviceType});\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../../dbscripts/cdm/plugins/${deviceType});\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../conf/datasources/${deviceType}-datasources.xml);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../database/${deviceType}DM_DB.h2.db);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.${deviceType}.device-view);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.${deviceType}.type-view);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.${deviceType}.realtime.analytics-view);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../deployment/server/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.${deviceType}.analytics-view);\ \ No newline at end of file diff --git a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/pom.xml b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/pom.xml index 9c3765e2..4037da42 100644 --- a/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/pom.xml +++ b/modules/tools/cdmf-devicetype-archetype/src/main/resources/archetype-resources/pom.xml @@ -78,19 +78,16 @@ org.apache.cxf cxf-rt-frontend-jaxws ${cxf.version} - org.apache.cxf cxf-rt-frontend-jaxrs ${cxf.version} - org.apache.cxf cxf-rt-transports-http ${cxf.version} - org.eclipse.paho @@ -118,7 +115,6 @@ javax.ws.rs jsr311-api ${javax.ws.rs.version} - org.apache.httpcomponents @@ -132,11 +128,6 @@ ${version} provided - - org.wso2.carbon.devicemgt - org.wso2.carbon.apimgt.webapp.publisher - ${carbon.device.mgt.version} - org.wso2.carbon org.wso2.carbon.ndatasource.core @@ -300,6 +291,21 @@ h2-database-engine 1.2.140.wso2v3 + + org.wso2.carbon.analytics-common + org.wso2.carbon.event.output.adapter.core + ${carbon.analytics.common.version} + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.extensions + ${carbon.devicemgt.version} + + + commons-codec.wso2 + commons-codec + ${commons-codec.wso2.version} + @@ -409,6 +415,8 @@ 1.2.140.wso2v3 + (3.0.0, 4.0.0] + 1.4.0.wso2v1 3.0.4.wso2v1 3.0.4.wso2v1