From 99337539e6d6449e70294dfb4114533665269572 Mon Sep 17 00:00:00 2001 From: Amalka Subasinghe Date: Mon, 24 Apr 2023 12:40:48 +0530 Subject: [PATCH] added changes to virtual fire alaram sample --- components/device-types/pom.xml | 59 ++++++ .../pom.xml | 3 +- .../mqtt/FireAlarmMQTTCommunicator.java | 21 +- .../pom.xml | 90 ++++++++- .../service/impl/VirtualFireAlarmService.java | 3 +- .../impl/VirtualFireAlarmServiceImpl.java | 67 +++--- .../constants/VirtualFireAlarmConstants.java | 2 +- .../service/impl/dao/DeviceEventsDAO.java | 27 +++ .../impl/dao/DeviceEventsDAOFactory.java | 120 +++++++++++ .../service/impl/dao/DeviceEventsDAOImpl.java | 77 +++++++ .../service/impl/dto/SensorRecord.java | 53 +---- .../service/impl/util/APIUtil.java | 89 -------- .../service/impl/util/ZipUtil.java | 4 +- .../webapp/META-INF/webapp-classloading.xml | 2 +- .../virtual-fire-alarm-plugin/pom.xml | 59 ++++++ features/device-types-feature/pom.xml | 39 ++++ .../pom.xml | 157 +++++++++++++++ .../resources/agent/deviceConfig.properties | 34 ++++ .../main/resources/agent/sketch.properties | 2 + .../src/main/resources/agent/start-device.bat | 7 + .../src/main/resources/agent/start-device.sh | 190 ++++++++++++++++++ .../resources/agent/virtual_firealarm.jks | Bin 0 -> 635 bytes .../src/main/resources/build.properties | 1 + .../devicetypes/virtual_firealarm.xml | 58 ++++++ .../src/main/resources/p2.inf | 16 ++ ..._firealam_rdbms_publisher-carbon.super.xml | 13 ++ .../receiver/virtualfirealarm_receiver.xml | 29 +++ ....device.stream.virtualfirealarm_1.0.0.json | 16 ++ .../virtual-fire-alarm-plugin-feature/pom.xml | 39 ++++ pom.xml | 12 ++ 30 files changed, 1101 insertions(+), 188 deletions(-) create mode 100644 components/device-types/pom.xml create mode 100644 components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAO.java create mode 100644 components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOFactory.java create mode 100644 components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOImpl.java create mode 100644 components/device-types/virtual-fire-alarm-plugin/pom.xml create mode 100644 features/device-types-feature/pom.xml create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/pom.xml create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/deviceConfig.properties create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/sketch.properties create mode 100755 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.bat create mode 100755 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.sh create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/virtual_firealarm.jks create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/build.properties create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/devicetypes/virtual_firealarm.xml create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/p2.inf create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/publisher/virtual_firealam_rdbms_publisher-carbon.super.xml create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/receiver/virtualfirealarm_receiver.xml create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/streams/iot.per.device.stream.virtualfirealarm_1.0.0.json create mode 100644 features/device-types-feature/virtual-fire-alarm-plugin-feature/pom.xml diff --git a/components/device-types/pom.xml b/components/device-types/pom.xml new file mode 100644 index 000000000..499c62ac6 --- /dev/null +++ b/components/device-types/pom.xml @@ -0,0 +1,59 @@ + + + + + + + org.wso2.carbon.devicemgt-plugins + carbon-device-mgt-plugins-parent + 6.0.16-SNAPSHOT + ../../pom.xml + + + 4.0.0 + device-types + pom + WSO2 Carbon - IoT Plugins + http://wso2.org + + + virtual-fire-alarm-plugin + + + + + + + org.apache.felix + maven-scr-plugin + 1.7.2 + + + generate-scr-scrdescriptor + + scr + + + + + + + + + diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/pom.xml b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/pom.xml index e9d51564c..c2160bfcc 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/pom.xml +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/pom.xml @@ -23,8 +23,7 @@ virtual-fire-alarm-plugin org.wso2.carbon.devicemgt-plugins - 4.1.21-SNAPSHOT - ../pom.xml + 6.0.16-SNAPSHOT org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java index 8f89750fd..777671038 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java @@ -69,8 +69,9 @@ public class FireAlarmMQTTCommunicator extends MQTTTransportHandler { Runnable connector = new Runnable() { public void run() { while (!isConnected()) { - try { - connectToQueue(agentManager.getAgentConfigs().getAuthToken(), DEFAULT_PASSWORD); + try { // uudi formay 8-4-4-4-12 + connectToQueue(agentManager.getAgentConfigs().getAuthToken().substring(0, 18), + agentManager.getAgentConfigs().getAuthToken().substring(19)); agentManager.updateAgentStatus("Connected to MQTT Queue"); } catch (TransportHandlerException e) { log.warn(AgentConstants.LOG_APPENDER + "Connection to MQTT Broker at: " + mqttBrokerEndPoint + @@ -138,6 +139,18 @@ public class FireAlarmMQTTCommunicator extends MQTTTransportHandler { String replyMessage; String securePayLoad; + if (message.toString().contains("BULB:ON")) { + boolean stateToSwitch = true; + agentManager.changeAlarmStatus(stateToSwitch); + log.info(AgentConstants.LOG_APPENDER + "Bulb was switched to state: 'ON'"); + return; + } else if (message.toString().contains("BULB:OFF")) { + boolean stateToSwitch = false; + agentManager.changeAlarmStatus(stateToSwitch); + log.info(AgentConstants.LOG_APPENDER + "Bulb was switched to state: 'OFF'"); + return; + } + try { receivedMessage = AgentUtilOperations.extractMessageFromPayload(message.toString()); log.info(AgentConstants.LOG_APPENDER + "Message [" + receivedMessage + "] was received"); @@ -210,8 +223,8 @@ public class FireAlarmMQTTCommunicator extends MQTTTransportHandler { int currentTemperature = agentManager.getTemperature(); String message = "{\"event\": {\"metaData\": {\"owner\": \"" + AgentManager .getInstance().getAgentConfigs().getDeviceOwner() + "\",\"deviceId\": \"" + AgentManager - .getInstance().getAgentConfigs().getDeviceId() + "\",\"time\": " + - "0},\"payloadData\": { \"temperature\": " + currentTemperature + "} }}"; + .getInstance().getAgentConfigs().getDeviceId() + "\",\"time\": \"" + + System.currentTimeMillis() + "\"},\"payloadData\": { \"temperature\": " + currentTemperature + "} }}"; try { String payLoad = AgentUtilOperations.prepareSecurePayLoad(message); diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/pom.xml b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/pom.xml index f84043397..5e0eafc03 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/pom.xml +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/pom.xml @@ -21,7 +21,7 @@ virtual-fire-alarm-plugin org.wso2.carbon.devicemgt-plugins - 4.1.21-SNAPSHOT + 6.0.16-SNAPSHOT ../pom.xml @@ -32,6 +32,10 @@ WSO2 Carbon - Virtual FireAlarm Service Management API Implementation http://wso2.org + + 2.7.18 + + @@ -208,11 +212,11 @@ org.wso2.carbon.apimgt.application.extension provided - - org.wso2.carbon.analytics - org.wso2.carbon.analytics.api - provided - + + + + + org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.extensions @@ -223,6 +227,80 @@ org.wso2.carbon.apimgt.annotations provided + + org.springframework + spring-web + provided + + + org.apache.cxf + cxf-bundle + 3.0.0-milestone2 + test + + + org.apache.cxf + cxf-bundle-jaxrs + provided + ${cxf-bundle-package.version} + + + + + io.swagger + swagger-annotations + + + io.swagger + swagger-core + + + com.fasterxml.jackson.module + jackson-module-jaxb-annotations + + + org.slf4j + slf4j-api + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-core + + + + + io.swagger + swagger-jaxrs + + + com.fasterxml.jackson.module + jackson-module-jaxb-annotations + + + org.slf4j + slf4j-api + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-core + + + + + + org.json.wso2 + json + + + javax.ws.rs + javax.ws.rs-api + provided + + + javax.ws.rs + jsr311-api + provided + diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java index 2726948d2..e9a080abe 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java @@ -54,7 +54,8 @@ import javax.ws.rs.core.Response; name = "Enroll device", description = "", key = "perm:firealarm:enroll", - permissions = {"/device-mgt/devices/enroll/firealarm"} + permissions = {"/device-mgt/devices/enroll/firealarm"}, + roles = {"Internal/devicemgt-user"} ) } ) diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java index 367cd248b..d8f298eaf 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java @@ -18,24 +18,24 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl; -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.SortByField; -import org.wso2.carbon.analytics.dataservice.commons.SortType; -import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; 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.common.*; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.constants.VirtualFireAlarmConstants; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dao.DeviceEventsDAO; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dao.DeviceEventsDAOImpl; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.APIUtil; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.ZipArchive; @@ -49,23 +49,11 @@ 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.Consumes; -import javax.ws.rs.FormParam; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.Response; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Properties; -import java.util.UUID; +import java.util.*; public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { @@ -135,26 +123,16 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { @Produces("application/json") public Response getVirtualFirealarmStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from, @QueryParam("to") long to) { - String fromDate = String.valueOf(from*1000); // converting time to ms - String toDate = String.valueOf(to*1000); // converting time to ms - String query = "meta_deviceId:" + deviceId + " AND meta_deviceType:" + - VirtualFireAlarmConstants.DEVICE_TYPE + " AND meta_time : [" + fromDate + " TO " + toDate + "]"; - String sensorTableName = VirtualFireAlarmConstants.TEMPERATURE_EVENT_TABLE; try { if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized( new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) { return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } - List sortByFields = new ArrayList<>(); - SortByField sortByField = new SortByField("meta_time", SortType.ASC); - 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(); + DeviceEventsDAO eventsDAO = new DeviceEventsDAOImpl(); + SensorRecord sensorRecord = eventsDAO.getStats(deviceId, from, to); + return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecord).build(); + } catch (DeviceAccessAuthorizationException e) { log.error(e.getErrorMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); @@ -247,6 +225,8 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantAdminDomainName); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(adminUsername); + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true, @@ -256,10 +236,21 @@ public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService { } } JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient(); - String scopes = " device_" + deviceId; + + String deviceType = sketchType.replace(" ", ""); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + StringBuilder scopes = new StringBuilder("device:" + deviceType + ":" + deviceId); + + // add scopes for event publishing + scopes.append(" perm:topic:pub:" + tenantDomain + ":" + deviceType + ":" + deviceId + ":temperature"); + + // add scopes for retrieve operation topic /tenantDomain/deviceType/deviceId/operation/# + scopes.append(" perm:topic:sub:" + tenantDomain + ":" + deviceType + ":" + deviceId + ":operation"); + AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(), - apiApplicationKey.getConsumerSecret(), owner, - scopes); + apiApplicationKey.getConsumerSecret(), owner, + scopes.toString()); + String accessToken = accessTokenInfo.getAccessToken(); String refreshToken = accessTokenInfo.getRefreshToken(); XmppAccount newXmppAccount = new XmppAccount(); diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java index 6c3186104..16a1bccbc 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/constants/VirtualFireAlarmConstants.java @@ -30,7 +30,7 @@ public class VirtualFireAlarmConstants { public static final String POLICY_CONTEXT = "POLICY"; //sensor events sumerized table name for temperature - public static final String TEMPERATURE_EVENT_TABLE = "IOT_PER_DEVICE_STREAM_VIRTUALFIREALARM_TEMPERATURE"; +// public static final String TEMPERATURE_EVENT_TABLE = "IOT_PER_DEVICE_STREAM_VIRTUALFIREALARM_TEMPERATURE"; public final static String DEVICE_TYPE_PROVIDER_DOMAIN = "carbon.super"; //mqtt tranport related constants diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAO.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAO.java new file mode 100644 index 000000000..84f8a29ab --- /dev/null +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAO.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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 org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dao; + +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord; + +public interface DeviceEventsDAO { + + SensorRecord getStats(String deviceId, long fromTime, long toTime); +} diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOFactory.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOFactory.java new file mode 100644 index 000000000..5d3820bd1 --- /dev/null +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOFactory.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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 org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dao; + +import org.wso2.carbon.device.mgt.common.exceptions.*; +import org.wso2.carbon.device.mgt.core.dao.util.*; + +public class DeviceEventsDAOFactory { + + private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(DeviceEventsDAOFactory.class); + private static javax.sql.DataSource dataSource; + private static String databaseEngine; + private static final ThreadLocal currentConnection = new ThreadLocal<>(); + + public static void init(String jndiName) { + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiName, null); + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (java.sql.SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } + } + + public static DeviceEventsDAO getDeviceEventDao() { + return new DeviceEventsDAOImpl(); + } + + public static void openDBConnection() throws DBConnectionException { + java.sql.Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + try { + conn = dataSource.getConnection(); + } catch (java.sql.SQLException e) { + throw new DBConnectionException("Failed to get a database connection.", e); + } + currentConnection.set(conn); + } + + public static void beginTransaction() throws DBConnectionException { + try { + java.sql.Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (java.sql.SQLException e) { + throw new DBConnectionException("Error occurred while retrieving datasource connection", e); + } + } + + public static java.sql.Connection getConnection() throws DBConnectionException { + if (currentConnection.get() == null) { + try { + currentConnection.set(dataSource.getConnection()); + } catch (java.sql.SQLException e) { + throw new DBConnectionException("Error occurred while retrieving data source connection", e); + } + } + return currentConnection.get(); + } + + public static void commitTransaction() throws DBConnectionException { + try { + java.sql.Connection conn = currentConnection.get(); + if (conn != null) { + conn.commit(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence commit " + + "has not been attempted"); + } + } + } catch (java.sql.SQLException e) { + throw new DBConnectionException("Error occurred while committing the transaction", e); + } + } + + public static void closeConnection() { + java.sql.Connection conn = currentConnection.get(); + try { + if (conn != null) { + conn.close(); + } + } catch (java.sql.SQLException e) { + log.error("Error occurred while close the connection"); + } + currentConnection.remove(); + } + + public static void rollbackTransaction() { + java.sql.Connection conn = currentConnection.get(); + if (conn == null) { + throw new org.wso2.carbon.device.mgt.common.exceptions.IllegalTransactionStateException("Database connection is not active. Hence, rollback is " + + "not attempted."); + } + try { + conn.rollback(); + } catch (java.sql.SQLException e) { + log.warn("Error occurred while roll-backing the transaction", e); + } + } + +} diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOImpl.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOImpl.java new file mode 100644 index 000000000..9bbd73613 --- /dev/null +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dao/DeviceEventsDAOImpl.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2018 - 2023 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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 org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord; +import org.wso2.carbon.device.mgt.common.exceptions.*; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedHashMap; +import java.util.Map; +/** + * Implements MobileDeviceDAO for Android Devices. + */ +public class DeviceEventsDAOImpl implements DeviceEventsDAO { + + private static final Log log = LogFactory.getLog(DeviceEventsDAOImpl.class); + + @Override + public SensorRecord getStats(String deviceId, long fromTime, long toTime) { + String sql = "SELECT * FROM TABLE_VIRTUALFIREALARM_CARBONSUPER_RDBMS_PUBLISHER WHERE " + + "META_DEVICEID = ? AND " + + "META_TIME >= ? AND " + + "META_TIME <= ? " + + "ORDER BY META_TIME ASC"; + Map stats = new LinkedHashMap<>(); + try { + DeviceEventsDAOFactory.init("jdbc/EVENT_DB"); + Connection conn = DeviceEventsDAOFactory.getConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, deviceId); + stmt.setLong(2, fromTime); + stmt.setLong(3, toTime); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + stats.put(rs.getLong("META_TIME"), rs.getFloat("TEMPERATURE")); + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving device details"; + log.error(msg); + + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to retrieve device details"; + log.error(msg); + + } +// stats.entrySet() +// .stream() +// .sorted(Map.Entry.comparingByKey()) +// .forEach(System.out::println); + return new SensorRecord(stats); + } + +} diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dto/SensorRecord.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dto/SensorRecord.java index e1f46d2d7..833851ea9 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dto/SensorRecord.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/dto/SensorRecord.java @@ -11,58 +11,23 @@ import java.util.Map; @XmlRootElement /** - * This stores sensor event data for android sense. + * This stores sensor event data for virtual fire alarm sense. */ @JsonIgnoreProperties(ignoreUnknown = true) public class SensorRecord { - @XmlElementWrapper(required = true, name = "values") - private Map values; + @XmlElementWrapper(required = true, name = "stats") + private Map stats; - /** The id. */ - @XmlElement(required = false, name = "id") - private String id; - - /** - * Gets the values. - * @return the values - */ - public Map getValues() { - return values; - } - - /** - * Sets the values. - * @param values the values - */ - public void setValues(Map values) { - this.values = values; + public Map getStats() { + return stats; } - /** - * Sets the id. - * @param id the new id - */ - public void setId(String id) { - this.id = id; + public void setStats(Map stats) { + this.stats = stats; } - /** - * Gets the id. - * @return the id - */ - public String getId() { - return id; + public SensorRecord(Map stats) { + this.stats = stats; } - - @Override - public String toString(){ - List valueList = new ArrayList(); - for (Map.Entry entry : values.entrySet()) { - valueList.add(entry.getKey() + ":" + entry.getValue()); - } - return valueList.toString(); - - } - } diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/APIUtil.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/APIUtil.java index bbcf1acef..cc31583e1 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/APIUtil.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/APIUtil.java @@ -2,27 +2,13 @@ package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.analytics.api.AnalyticsDataAPI; -import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; -import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; -import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; -import org.wso2.carbon.analytics.dataservice.commons.SortByField; -import org.wso2.carbon.analytics.datasource.commons.Record; -import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * This class provides utility functions used by REST-API. */ @@ -52,81 +38,6 @@ public class APIUtil { return deviceManagementProviderService; } - public static AnalyticsDataAPI getAnalyticsDataAPI() { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - AnalyticsDataAPI analyticsDataAPI = - (AnalyticsDataAPI) ctx.getOSGiService(AnalyticsDataAPI.class, null); - if (analyticsDataAPI == null) { - String msg = "Analytics api service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } - return analyticsDataAPI; - } - - public static List getAllEventsForDevice(String tableName, String query, - List sortByFields) throws AnalyticsException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - AnalyticsDataAPI analyticsDataAPI = getAnalyticsDataAPI(); - int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query); - if (eventCount == 0) { - return null; - } - List resultEntries = analyticsDataAPI.search(tenantId, tableName, query, 0, eventCount, - sortByFields); - List recordIds = getRecordIds(resultEntries); - AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds); - Map sensorDatas = createSensorData(AnalyticsDataAPIUtil.listRecords( - analyticsDataAPI, response)); - List sortedSensorData = getSortedSensorData(sensorDatas, resultEntries); - return sortedSensorData; - } - - private static List getRecordIds(List searchResults) { - List ids = new ArrayList<>(); - for (SearchResultEntry searchResult : searchResults) { - ids.add(searchResult.getId()); - } - return ids; - } - - public static List getSortedSensorData(Map sensorDatas, - List searchResults) { - List sortedRecords = new ArrayList<>(); - for (SearchResultEntry searchResultEntry : searchResults) { - sortedRecords.add(sensorDatas.get(searchResultEntry.getId())); - } - return sortedRecords; - } - - /** - * 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) { - SensorRecord sensorData = createSensorData(record); - sensorDatas.put(sensorData.getId(), sensorData); - } - 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()); - recordBean.setValues(record.getValues()); - return recordBean; - } - public static APIManagementProviderService getAPIManagementProviderService() { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); APIManagementProviderService apiManagementProviderService = diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java index 496d5eb5a..ed05d03c6 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/util/ZipUtil.java @@ -25,7 +25,7 @@ import org.apache.commons.logging.LogFactory; import org.json.JSONObject; import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants; import org.wso2.carbon.core.util.Utils; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; @@ -72,7 +72,7 @@ public class ZipUtil { String iotServerIP; try { - iotServerIP = getServerUrl(); + iotServerIP = "127.0.0.1"; //getServerUrl(); String httpsServerEP = Utils.replaceSystemProperty(HTTPS_PROTOCOL_URL); String httpServerEP = Utils.replaceSystemProperty(HTTP_PROTOCOL_URL); String mqttEndpoint = Utils.replaceSystemProperty(DEFAULT_MQTT_ENDPOINT); diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/webapp-classloading.xml index fa4461919..7fc571bef 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/webapp-classloading.xml +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/webapp-classloading.xml @@ -29,5 +29,5 @@ Tomcat environment is the default and every webapps gets it even if they didn't specify it. e.g. If a webapps requires CXF, they will get both Tomcat and CXF. --> - CXF,Carbon + CXF3,Carbon diff --git a/components/device-types/virtual-fire-alarm-plugin/pom.xml b/components/device-types/virtual-fire-alarm-plugin/pom.xml new file mode 100644 index 000000000..086689116 --- /dev/null +++ b/components/device-types/virtual-fire-alarm-plugin/pom.xml @@ -0,0 +1,59 @@ + + + + + + + org.wso2.carbon.devicemgt-plugins + device-types + 6.0.16-SNAPSHOT + ../pom.xml + + + 4.0.0 + virtual-fire-alarm-plugin + pom + WSO2 Carbon - Virtual Fire Alarm Plugin + http://wso2.org + + + org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl + org.wso2.carbon.device.mgt.iot.virtualfirealarm.api + + + + + + + org.apache.felix + maven-scr-plugin + 1.7.2 + + + generate-scr-scrdescriptor + + scr + + + + + + + + diff --git a/features/device-types-feature/pom.xml b/features/device-types-feature/pom.xml new file mode 100644 index 000000000..80dee946e --- /dev/null +++ b/features/device-types-feature/pom.xml @@ -0,0 +1,39 @@ + + + + + + + org.wso2.carbon.devicemgt-plugins + carbon-device-mgt-plugins-parent + 6.0.16-SNAPSHOT + ../../pom.xml + + + 4.0.0 + device-types-feature + pom + WSO2 Carbon - Device Management IoT Plugins Feature + http://wso2.org + + + virtual-fire-alarm-plugin-feature + + + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/pom.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/pom.xml new file mode 100644 index 000000000..12b505e63 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/pom.xml @@ -0,0 +1,157 @@ + + + + + + + + org.wso2.carbon.devicemgt-plugins + virtual-fire-alarm-plugin-feature + 6.0.16-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature + pom + WSO2 Carbon - IoT Server VirtualFireAlarm Backend Feature + http://wso2.org + This feature contains the VirtualFireAlarm Device type specific backend implementations for the IoT Server + + + + + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.virtualfirealarm.api + ${carbon.devicemgt.plugins.version} + war + + + org.wso2.orbit.com.h2database + h2 + ${orbit.h2.version} + + + + + + + maven-resources-plugin + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + copy-jaxrs-war + package + + copy + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.virtualfirealarm.api + + war + true + ${project.build.directory}/maven-shared-archive-resources/webapps/ + virtual_firealarm.war + + + + + + copy-agent-jar + package + + copy + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl + + true + ${project.build.directory}/maven-shared-archive-resources/agent/ + wso2-firealarm-virtual-agent.jar + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend + ../../../features/etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + org.eclipse.equinox.p2.type.group:true + + + + + + + + + + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/deviceConfig.properties b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/deviceConfig.properties new file mode 100644 index 000000000..80bee115a --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/deviceConfig.properties @@ -0,0 +1,34 @@ +# +# Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# +# Licensed 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. +# +# + +#[Device-Configurations] +tenantDomain=${TENANT_DOMAIN} +owner=${DEVICE_OWNER} +deviceId=${DEVICE_ID} +device-name=${DEVICE_NAME} +https-ep=${HTTPS_EP} +http-ep=${HTTP_EP} +apim-ep=${APIM_EP} +mqtt-ep=${MQTT_EP} +xmpp-ep=${XMPP_EP} +application-key=${API_APPLICATION_KEY} +auth-token=${DEVICE_TOKEN} +refresh-token=${DEVICE_REFRESH_TOKEN} +push-interval=15 +xmpp-server-name=${SERVER_NAME} +server-jid=${SERVER_JID} + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/sketch.properties b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/sketch.properties new file mode 100644 index 000000000..801bd88de --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/sketch.properties @@ -0,0 +1,2 @@ +templates=deviceConfig.properties +zipfilename=FireAlarmVirtualAgent.zip diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.bat b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.bat new file mode 100755 index 000000000..f19520f14 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.bat @@ -0,0 +1,7 @@ +@echo off +echo. +echo.WSO2 IOT Sample +echo.Virtual Fire Alarm +echo.initializing agent +echo. +java -jar wso2-firealarm-virtual-agent.jar \ No newline at end of file diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.sh b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.sh new file mode 100755 index 000000000..37853c9b1 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/agent/start-device.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +echo "----------------------------------------------------------------" +echo "| WSO2 IOT Sample " +echo "| Virtual RaspiAlarm " +echo "| ---------------- " +echo "| ....initializing startup-script " +echo "----------------------------------------------------------------" + +#while true; do +# read -p "What is the network-interface of your device that the Agent should use (find from ifconfig. ex: wlan0,en0,eth0..) > " interface +# +# echo "Setting the network-interface to " $interface +# sed s/^network-interface=.*/network-interface=$interface/ deviceConfig.properties > myTmp +# mv -f myTmp deviceConfig.properties +# break; +#done +# +#while true; do +# read -p "Whats the time-interval (in seconds) between successive Data-Pushes to the WSO2-IoT-Server (ex: '60' indicates 1 minute) > " interval +# +# if [ $interval -eq $interval 2>/dev/null ] +# then +# echo "Setting data-push interval to " $interval " seconds." +# sed s/^push-interval=.*/push-interval=$interval/ deviceConfig.properties > myTmp +# mv -f myTmp deviceConfig.properties +# break; +# else +# echo "Input needs to be an integer indicating the number seconds between successive data-pushes." +# fi +#done + + +java -jar wso2-firealarm-virtual-agent.jar + +#while true; do +# read -p "Do you wish to run 'apt-get update' and continue? [Yes/No] " yn +# case $yn in +# [Yy]* ) sudo apt-get update; +# break;; +# [Nn]* ) echo "Continuing without apt-get update..."; +# break;; +# * ) echo "Please answer yes or no."; +# esac +#done +# +#if [ $? -ne 0 ]; then +# echo "apt-get update failed.... Some dependencies may not get installed" +# echo "If an already installed version of the package exists, try running:" +# echo "----------------------------------------------------------------" +# echo "sudo -i" +# echo "cd /var/lib/dpkg/info" +# echo "rm -rf wso2-raspi-alarm*" +# echo "dpkg --remove --force-remove-reinstreq wso2-raspi-alarm" +# echo "exit" +# echo "----------------------------------------------------------------" +# echo "Retry Installation...." +# break; +#fi +# +#echo "Installing 'gdebi' package..." +#sudo apt-get install gdebi # installation of gdebi +# +# +#if [ $? -ne 0 ]; then +# echo "gdebi installation failed.... dependencies will not be installed without gdebi" +# read -p "Do you wish to continue without gdebi? [Yes/No] " yn +# case $yn in +# [Yy]* ) echo "Continueing without gdebi.....";; +# [Nn]* ) echo "Try to resolve errors and re-run the script."; +# exit;; +# * ) exit;; +# esac +#fi +# +# +#for f in ./wso2-raspi-alarm_1.0_armhf.deb; do +# ## Check if the glob gets expanded to existing files. +# ## If not, f here will be exactly the pattern above +# ## and the exists test will evaluate to false. +# # [ -e "$f" ] && echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing" || echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path"; exit; +# if [ -e "$f" ]; then +# echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing now...." +# else +# echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path. \nExiting installation..."; +# exit; +# fi +# ## This is all we needed to know, so we can break after the first iteration +# break +#done +# +#echo "Installing the 'wso2-raspi-alarm deb package'" +#sudo gdebi wso2-raspi-alarm_1.0_armhf.deb +# +#if [ $? -ne 0 ]; then +# echo "Installation Failed...." +# exit; +#fi + +#sudo killall -9 python +# +#for f in ./RaspberryAgent.zip; do +# ## Check if the glob gets expanded to existing files. +# ## If not, f here will be exactly the pattern above +# ## and the exists test will evaluate to false. +# # [ -e "$f" ] && echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing" || echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path"; exit; +# if [ -e "$f" ]; then +# echo "Agent files found......" +# sudo rm -rf /usr/local/src/RaspberryAgent +# sudo unzip RaspberryAgent.zip -d /usr/local/src/ +# else +# echo "'RaspberryAgent.zip' file does not exist in current path. \nInstalling without upgrading agent..."; +# fi +# ## This is all we needed to know, so we can break after the first iteration +# break +#done +# +#for f in /usr/local/src/RaspberryAgent/rc.local; do +# ## Check if the glob gets expanded to existing files. +# ## If not, f here will be exactly the pattern above +# ## and the exists test will evaluate to false. +# if [ -e "$f" ]; then +# echo "Copying boot script" +# sudo mv /usr/local/src/RaspberryAgent/rc.local /etc/rc.local +# sudo chmod +x /etc/rc.local +# else +# echo "Unable to set agent statup on boot"; +# fi +# ## This is all we needed to know, so we can break after the first iteration +# break +#done +# +#for f in ./deviceConfigs.cfg; do +# ## Check if the glob gets expanded to existing files. +# ## If not, f here will be exactly the pattern above +# ## and the exists test will evaluate to false. +# if [ -e "$f" ]; then +# echo "Configuration file found......" +# else +# echo "'deviceConfigs.cfg' file does not exist in current path. \nExiting installation..."; +# exit; +# fi +# ## This is all we needed to know, so we can break after the first iteration +# break +#done +# +#echo "Altering Configuration file" +#sed -i 's|[/,]||g' deviceConfigs.cfg +# +#echo "Copying configurations file to /usr/local/src/RaspberryAgent" +#sudo cp ./deviceConfigs.cfg /usr/local/src/RaspberryAgent/ +# +#if [ $? -ne 0 ]; then +# echo "Copying configuration file failed...." +# exit; +#fi +# +#while true; do +# read -p "Whats the time-interval (in seconds) between successive Data-Pushes to the WSO2-DC (ex: '60' indicates 1 minute) > " input +# +# if [ $input -eq $input 2>/dev/null ] +# then +# echo "Setting data-push interval to $input seconds." +# echo $input > /usr/local/src/RaspberryAgent/time-interval +# break; +# else +# echo "Input needs to be an integer indicating the number seconds between successive data-pushes." +# fi +#done +# +#cd /usr/local/src/RaspberryAgent/ +#sudo chmod +x RaspberryStats.py +#sudo nohup ./RaspberryStats.py -i $input ~|d-uF1FtA4GnHpFE6m?<$jZRn*vnwh*vZt`$S`I3FVSmX=Y0P1 z#8IPRua(}0Gv|!k*05(R-L`jvR?qY9X;a1Cn|?Uur#nge!)A4V)1^~pzt7+cWMI$C zahUzPpS5>G+$Cwbz{{0#bM)ubAC5j2Q;_}Sd6AXXuH7vg)?c0YcE^JilQ@e1u)J

Rd_Z+cf2pnXKsRr`tWWWK{TXH#K@FtlY7J=Tv9%<8+6* F4*;FQ(GLIs literal 0 HcmV?d00001 diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/build.properties b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/build.properties new file mode 100644 index 000000000..9c86577d7 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom = true diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/devicetypes/virtual_firealarm.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/devicetypes/virtual_firealarm.xml new file mode 100644 index 000000000..55196f13f --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/devicetypes/virtual_firealarm.xml @@ -0,0 +1,58 @@ + + + + + + + Control buzzer + Control buzzer on Virtual Firealarm + + + + + + + true + + + + true + + + + + + + + + + + + + + + + + + en_US + 1.0.0 + This is license text + + + \ No newline at end of file diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/p2.inf b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/p2.inf new file mode 100644 index 000000000..1656f09da --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/p2.inf @@ -0,0 +1,16 @@ +instructions.configure = \ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../../repository/deployment/server/webapps/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/webapps/,target:${installFolder}/../../../repository/deployment/server/webapps/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../../repository/deployment/server/devicetypes/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/devicetypes/,target:${installFolder}/../../../repository/deployment/server/devicetypes/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/receiver/,target:${installFolder}/../../../repository/deployment/server/eventreceivers/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/streams/,target:${installFolder}/../../../repository/deployment/server/eventstreams/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/publisher/,target:${installFolder}/../../../repository/deployment/server/eventpublishers/,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../../repository/resources/sketches/);\ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../../repository/resources/sketches/virtual_firealarm/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend_${feature.version}/agent/,target:${installFolder}/../../../repository/resources/sketches/virtual_firealarm/,overwrite:true);\ + +instructions.unconfigure = \ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../../repository/deployment/server/webapps/virtual_firealarm.war);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../../repository/deployment/server/webapps/virtual_firealarm);\ +org.eclipse.equinox.p2.touchpoint.natives.remove(path:${installFolder}/../../../repository/deployment/server/devicetypes/virtual_firealarm.xml);\ diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/publisher/virtual_firealam_rdbms_publisher-carbon.super.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/publisher/virtual_firealam_rdbms_publisher-carbon.super.xml new file mode 100644 index 000000000..39cd6d4cf --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/publisher/virtual_firealam_rdbms_publisher-carbon.super.xml @@ -0,0 +1,13 @@ + + + + + + EVENT_DB + table_virtualfirealarm_carbonsuper_rdbms_publisher + insert + + + + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/receiver/virtualfirealarm_receiver.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/receiver/virtualfirealarm_receiver.xml new file mode 100644 index 000000000..1deea34dd --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/receiver/virtualfirealarm_receiver.xml @@ -0,0 +1,29 @@ + + + + + + carbon.super/virtual_firealarm/+/temperature + default + true + + + + + diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/streams/iot.per.device.stream.virtualfirealarm_1.0.0.json b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/streams/iot.per.device.stream.virtualfirealarm_1.0.0.json new file mode 100644 index 000000000..751c13824 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature/src/main/resources/streams/iot.per.device.stream.virtualfirealarm_1.0.0.json @@ -0,0 +1,16 @@ +{ + "name": "iot.per.device.stream.virtualfirealarm", + "version": "1.0.0", + "nickName": "virtual_firealarm", + "description": "Temperature data received from the virtual_firealarm", + "metaData": [ + {"name":"owner","type":"STRING"}, + {"name":"deviceId","type":"STRING"}, + {"name":"time","type":"TIMESTAMP"} + ], + "payloadData": [ + { + "name": "temperature","type": "FLOAT" + } + ] +} \ No newline at end of file diff --git a/features/device-types-feature/virtual-fire-alarm-plugin-feature/pom.xml b/features/device-types-feature/virtual-fire-alarm-plugin-feature/pom.xml new file mode 100644 index 000000000..81b17d129 --- /dev/null +++ b/features/device-types-feature/virtual-fire-alarm-plugin-feature/pom.xml @@ -0,0 +1,39 @@ + + + + + + + org.wso2.carbon.devicemgt-plugins + device-types-feature + 6.0.16-SNAPSHOT + ../pom.xml + + + 4.0.0 + virtual-fire-alarm-plugin-feature + pom + WSO2 Carbon - IoT Server VirtualFireAlarm Device Feature + http://wso2.org + + + org.wso2.carbon.device.mgt.iot.virtualfirealarm.backend.feature + + + diff --git a/pom.xml b/pom.xml index 196acd4f5..aabd1f6c1 100644 --- a/pom.xml +++ b/pom.xml @@ -37,10 +37,12 @@ components/extensions components/mobile-plugins + components/device-types features/analytics-feature features/mobile-plugins-feature features/extensions-feature + features/device-types-feature @@ -491,6 +493,16 @@ org.wso2.carbon.device.mgt.mobile.android.api ${carbon.devicemgt.plugins.version} + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.virtualfirealarm.api + ${carbon.devicemgt.plugins.version} + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl + ${carbon.devicemgt.plugins.version} +