mirror of
https://repository.entgra.net/community/product-iots.git
synced 2025-09-16 23:32:19 +00:00
added samples source
This commit is contained in:
parent
5384c8ef0a
commit
e444d60d1c
@ -178,177 +178,177 @@
|
|||||||
</tasks>
|
</tasks>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-firealarm-schema</id>
|
<!--<id>create-firealarm-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/firealarm/database"/>
|
<!--<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/firealarm/database"/>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/samples/firealarm/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/samples/firealarm/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/FireAlarmDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/FireAlarmDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_firealarm.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_firealarm.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-sensebot-schema</id>
|
<!--<id>create-sensebot-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/sensebot/database"/>
|
<!--<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/sensebot/database"/>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/samples/sensebot/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/samples/sensebot/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/SensebotDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/SensebotDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_sensebot.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_sensebot.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-arduino-schema</id>
|
<!--<id>create-arduino-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/arduino/database"/>
|
<!--<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/arduino/database"/>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/samples/arduino/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/samples/arduino/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/ArduinoDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/ArduinoDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_arduino.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_arduino.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-android-sense-schema</id>
|
<!--<id>create-android-sense-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/repository/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/repository/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/AndroidSenseDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/AndroidSenseDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_android_sense.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_android_sense.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-raspberrypi-schema</id>
|
<!--<id>create-raspberrypi-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/repository/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/repository/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/RaspberrypiDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/RaspberrypiDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_raspberrypi.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_raspberrypi.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
<execution>
|
<!--<execution>-->
|
||||||
<!-- Creating IOT Management schema -->
|
<!--<!– Creating IOT Management schema –>-->
|
||||||
<id>create-digitaldisply-schema</id>
|
<!--<id>create-digitaldisply-schema</id>-->
|
||||||
<phase>package</phase>
|
<!--<phase>package</phase>-->
|
||||||
<goals>
|
<!--<goals>-->
|
||||||
<goal>run</goal>
|
<!--<goal>run</goal>-->
|
||||||
</goals>
|
<!--</goals>-->
|
||||||
<configuration>
|
<!--<configuration>-->
|
||||||
<tasks>
|
<!--<tasks>-->
|
||||||
<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/digital_display/database"/>
|
<!--<mkdir dir="target/wso2carbon-core-${carbon.kernel.version}/samples/digital_display/database"/>-->
|
||||||
<property name="db.dir"
|
<!--<property name="db.dir"-->
|
||||||
value="target/wso2carbon-core-${carbon.kernel.version}/samples/digital_display/database"/>
|
<!--value="target/wso2carbon-core-${carbon.kernel.version}/samples/digital_display/database"/>-->
|
||||||
<property name="userid" value="wso2carbon"/>
|
<!--<property name="userid" value="wso2carbon"/>-->
|
||||||
<property name="password" value="wso2carbon"/>
|
<!--<property name="password" value="wso2carbon"/>-->
|
||||||
<property name="dbURL"
|
<!--<property name="dbURL"-->
|
||||||
value="jdbc:h2:file:${basedir}/${db.dir}/DigitalDisplayDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>
|
<!--value="jdbc:h2:file:${basedir}/${db.dir}/DigitalDisplayDM_DB;DB_CLOSE_ON_EXIT=FALSE"/>-->
|
||||||
|
|
||||||
<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"
|
<!--<sql driver="org.h2.Driver" url="${dbURL}" userid="${userid}" password="${password}"-->
|
||||||
autocommit="true" onerror="continue">
|
<!--autocommit="true" onerror="continue">-->
|
||||||
<classpath refid="maven.dependency.classpath"/>
|
<!--<classpath refid="maven.dependency.classpath"/>-->
|
||||||
<classpath refid="maven.compile.classpath"/>
|
<!--<classpath refid="maven.compile.classpath"/>-->
|
||||||
<classpath refid="maven.runtime.classpath"/>
|
<!--<classpath refid="maven.runtime.classpath"/>-->
|
||||||
<fileset
|
<!--<fileset-->
|
||||||
file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_digital_display.sql"/>
|
<!--file="${basedir}/../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/dbscripts/cdm/plugins/h2_digital_display.sql"/>-->
|
||||||
</sql>
|
<!--</sql>-->
|
||||||
</tasks>
|
<!--</tasks>-->
|
||||||
</configuration>
|
<!--</configuration>-->
|
||||||
</execution>
|
<!--</execution>-->
|
||||||
|
|
||||||
|
|
||||||
<execution>
|
<execution>
|
||||||
|
|||||||
@ -278,12 +278,6 @@
|
|||||||
<outputDirectory>${pom.artifactId}-${pom.version}/repository/deployment/server/webapps</outputDirectory>
|
<outputDirectory>${pom.artifactId}-${pom.version}/repository/deployment/server/webapps</outputDirectory>
|
||||||
<includes>
|
<includes>
|
||||||
<include>devicecloud.war</include>
|
<include>devicecloud.war</include>
|
||||||
<include>firealarm.war</include>
|
|
||||||
<include>arduino.war</include>
|
|
||||||
<include>android_sense.war</include>
|
|
||||||
<include>sensebot.war</include>
|
|
||||||
<include>raspberrypi.war</include>
|
|
||||||
<include>digitaldisplay.war</include>
|
|
||||||
</includes>
|
</includes>
|
||||||
</fileSet>
|
</fileSet>
|
||||||
|
|
||||||
@ -618,7 +612,7 @@
|
|||||||
../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/mobile-config.xml
|
../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/mobile-config.xml
|
||||||
</source>
|
</source>
|
||||||
<outputDirectory>
|
<outputDirectory>
|
||||||
${pom.artifactId}-${pom.version}/repository/conf/etc/device-mgt-plugin-configs/mobile
|
${pom.artifactId}-${pom.version}/repository/conf/etc/device-mgt-plugin-org.wso2.carbon.device.mgt.iot.common.config.server.configs/mobile
|
||||||
</outputDirectory>
|
</outputDirectory>
|
||||||
<filtered>true</filtered>
|
<filtered>true</filtered>
|
||||||
<fileMode>644</fileMode>
|
<fileMode>644</fileMode>
|
||||||
|
|||||||
@ -12,7 +12,7 @@ var getScope = function (unit,configs) {
|
|||||||
var cbResult;
|
var cbResult;
|
||||||
if (jsFile.isExists()) {
|
if (jsFile.isExists()) {
|
||||||
script = require(jsFile.getPath());
|
script = require(jsFile.getPath());
|
||||||
//Eagerly make the viewModel the template configs
|
//Eagerly make the viewModel the template org.wso2.carbon.device.mgt.iot.common.config.server.configs
|
||||||
viewModel = templateConfigs;
|
viewModel = templateConfigs;
|
||||||
//Check if the unit author has specified an onRequest
|
//Check if the unit author has specified an onRequest
|
||||||
//callback
|
//callback
|
||||||
@ -24,7 +24,7 @@ var getScope = function (unit,configs) {
|
|||||||
};
|
};
|
||||||
onRequestCb = script.onRequest;
|
onRequestCb = script.onRequest;
|
||||||
cbResult = onRequestCb(templateConfigs);
|
cbResult = onRequestCb(templateConfigs);
|
||||||
log.debug("passing configs to unit "+unit+" configs: "+stringify(templateConfigs));
|
log.debug("passing org.wso2.carbon.device.mgt.iot.common.config.server.configs to unit "+unit+" org.wso2.carbon.device.mgt.iot.common.config.server.configs: "+stringify(templateConfigs));
|
||||||
//If the execution does not yield an object we will print
|
//If the execution does not yield an object we will print
|
||||||
//a warning as the unit author may have forgotten to return a data object
|
//a warning as the unit author may have forgotten to return a data object
|
||||||
if(cbResult===undefined){
|
if(cbResult===undefined){
|
||||||
@ -157,7 +157,7 @@ Handlebars.registerHelper('unit', function (unitName,options) {
|
|||||||
//TODO warn when unspecified decencies are included.
|
//TODO warn when unspecified decencies are included.
|
||||||
fuseState.currentZone.push('main');
|
fuseState.currentZone.push('main');
|
||||||
var template = fuse.getFile(baseUnit, '', '.hbs');
|
var template = fuse.getFile(baseUnit, '', '.hbs');
|
||||||
//log.debug('[' + requestId + '] including "' + baseUnit + '"'+" with configs "+stringify(templateConfigs));
|
//log.debug('[' + requestId + '] including "' + baseUnit + '"'+" with org.wso2.carbon.device.mgt.iot.common.config.server.configs "+stringify(templateConfigs));
|
||||||
var result = new Handlebars.SafeString(Handlebars.compileFile(template)(getScope(baseUnit,templateConfigs)));
|
var result = new Handlebars.SafeString(Handlebars.compileFile(template)(getScope(baseUnit,templateConfigs)));
|
||||||
fuseState.currentZone.pop();
|
fuseState.currentZone.pop();
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `ANDROID_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `ANDROID_SENSE_DEVICE` (
|
||||||
|
`ANDROID_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`ANDROID_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `ANDROID_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `ANDROID_SENSE_DEVICE` (
|
||||||
|
`ANDROID_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`ANDROID_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,141 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.android.sense.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Android Sense Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - Android Sense Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.android.sense.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.android.sense.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.android.sense.*
|
||||||
|
</Export-Package>
|
||||||
|
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants;
|
||||||
|
|
||||||
|
public class AndroidSenseConstants {
|
||||||
|
|
||||||
|
public final static String DEVICE_TYPE = "android_sense";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "ANDROID_DEVICE_ID";
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,262 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.dao.AndroidSenseDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the Android implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class AndroidSenseManager implements DeviceManager {
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new AndroidSenseDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(AndroidSenseManager.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
AndroidSenseDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
AndroidSenseDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
AndroidSenseDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the Android device enrollment data");
|
||||||
|
}
|
||||||
|
AndroidSenseDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
AndroidSenseDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
AndroidSenseDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the Android device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling Android device : " + deviceId);
|
||||||
|
}
|
||||||
|
AndroidSenseDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
AndroidSenseDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
AndroidSenseDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the Android device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of Android device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of Android device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of Android device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the Android device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of Android device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
AndroidSenseDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
AndroidSenseDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
AndroidSenseDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the Android device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all Android devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all Android devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants.AndroidSenseConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AndroidSenseManagerService implements DeviceManagementService {
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return AndroidSenseConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
deviceManager=new AndroidSenseManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants.AndroidSenseConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.dao.impl.AndroidSenseDAOImpl;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class AndroidSenseDAO extends IotDeviceManagementDAOFactory implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AndroidSenseDAO.class);
|
||||||
|
static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public AndroidSenseDAO() {
|
||||||
|
initAndroidDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initAndroidDAO() {
|
||||||
|
dataSource = getDataSourceMap().get(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new AndroidSenseDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback "
|
||||||
|
+ "has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,236 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants.AndroidSenseConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.dao.AndroidSenseDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for android Devices.
|
||||||
|
*/
|
||||||
|
public class AndroidSenseDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AndroidSenseDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = AndroidSenseDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT ANDROID_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM ANDROID_SENSE_DEVICE WHERE ANDROID_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
AndroidSenseConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Android device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Android database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Android device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
AndroidSenseDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = AndroidSenseDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO ANDROID_SENSE_DEVICE(ANDROID_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Android device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Android database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Android device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Android db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = AndroidSenseDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE ANDROID_SENSE_DEVICE SET DEVICE_NAME = ? WHERE ANDROID_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Android device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Android device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = AndroidSenseDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM ANDROID_SENSE_DEVICE WHERE ANDROID_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Android device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Android database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Android device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = AndroidSenseDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT ANDROID_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM ANDROID_SENSE_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(AndroidSenseConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(AndroidSenseConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Android device details have fetched from Android database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Android device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
AndroidSenseDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by Android plugin.
|
||||||
|
*/
|
||||||
|
public class AndroidSenseUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(AndroidSenseUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.impl.AndroidSenseManagerService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.android.internal.AndroidSenseManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
public class AndroidSenseManagementServiceComponent {
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceRegistration androidServiceRegRef;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AndroidSenseManagementServiceComponent.class);
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Android Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
|
||||||
|
|
||||||
|
androidServiceRegRef =
|
||||||
|
bundleContext.registerService(DeviceManagementService.class.getName(), new
|
||||||
|
AndroidSenseManagerService(), null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Android Device Management Service Component has been successfully activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while activating Android Device Management Service Component", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating Android Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (androidServiceRegRef != null) {
|
||||||
|
androidServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Android Device Management Service Component has been successfully de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Android Device Management bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,105 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-api</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.android.sense.api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Cloud API - Android SenseAPI</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- CDM -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--MQTT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>mqtt-client</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--IOT -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.android.sense.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<warName>android_sense</warName>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.util.DeviceJSON;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
public class AndroidSenseControllerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(AndroidSenseControllerService.class);
|
||||||
|
|
||||||
|
|
||||||
|
/* Service to push all the sensor data collected by the Android
|
||||||
|
Called by the Android device */
|
||||||
|
@Path("/sensordata")
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public void pushSensorData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
String temperature = dataMsg.value; //TEMP
|
||||||
|
log.info("Recieved Sensor Data Values: " + temperature);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Recieved Temperature Data Value: " + temperature + " degrees C");
|
||||||
|
}
|
||||||
|
//try {
|
||||||
|
boolean result = false;
|
||||||
|
// result=DeviceController.pushBamData(dataMsg.owner, AndroidSenseConstants.DEVICE_TYPE,
|
||||||
|
// dataMsg.deviceId,
|
||||||
|
// System.currentTimeMillis(), "DeviceData",
|
||||||
|
// temperature, "TEMPERATURE");
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
//return result;
|
||||||
|
// } catch (UnauthorizedException e) {
|
||||||
|
// response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,178 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
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.iot.sample.android.sense.plugin.constants.AndroidSenseConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceManagement;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
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.core.Context;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class AndroidSenseManagerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(AndroidSenseManagerService.class);
|
||||||
|
|
||||||
|
@Path("/device")
|
||||||
|
@PUT
|
||||||
|
public boolean register(@FormParam("deviceId") String deviceId,
|
||||||
|
@FormParam("owner") String owner) {
|
||||||
|
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||||
|
device.setEnrolmentInfo(enrolmentInfo);
|
||||||
|
|
||||||
|
String name = "android_sense" + deviceId;
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
||||||
|
if (added) {
|
||||||
|
Response.status(HttpStatus.SC_OK).build();
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return added;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@DELETE
|
||||||
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
||||||
|
if (removed) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@POST
|
||||||
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@FormParam("name") String name,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
|
||||||
|
// device.setDeviceTypeId(deviceTypeId);
|
||||||
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
|
||||||
|
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return deviceManagement.getDeviceManagementService().getDevice(
|
||||||
|
deviceIdentifier);
|
||||||
|
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.util;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class DeviceJSON {
|
||||||
|
@XmlElement(required = true) public String owner;
|
||||||
|
@XmlElement(required = true) public String deviceId;
|
||||||
|
@XmlElement(required = true) public String reply;
|
||||||
|
@XmlElement public Long time;
|
||||||
|
@XmlElement public String key;
|
||||||
|
@XmlElement public String value;
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2005-2013 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="AndroidSenseController" address="/controller">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="AndroidSenseControllerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.AndroidSenseControllerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="AndroidSenseManager" address="/manager">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="AndroidSenseManagerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.AndroidSenseManagerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>Android Sense</display-name>
|
||||||
|
<description>Android Sense</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `ARDUINO_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `ARDUINO_DEVICE` (
|
||||||
|
`ARDUINO_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`ARDUINO_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `ARDUINO_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `ARDUINO_DEVICE` (
|
||||||
|
`ARDUINO_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`ARDUINO_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,141 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.arduino.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Arduino Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - Arduino Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.arduino.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.arduino.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.arduino.*
|
||||||
|
</Export-Package>
|
||||||
|
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants;
|
||||||
|
|
||||||
|
public class ArduinoConstants {
|
||||||
|
|
||||||
|
public final static String DEVICE_TYPE = "arduino";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "ARDUINO_DEVICE_ID";
|
||||||
|
public final static String STATE_ON = "ON";
|
||||||
|
public final static String STATE_OFF = "OFF";
|
||||||
|
}
|
||||||
@ -0,0 +1,262 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.dao.ArduinoDAO;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the Arduino implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class ArduinoManager implements DeviceManager {
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new ArduinoDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(ArduinoManager.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new Arduino device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
ArduinoDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
ArduinoDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
ArduinoDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the Arduino device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the Arduino device enrollment data");
|
||||||
|
}
|
||||||
|
ArduinoDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
ArduinoDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
ArduinoDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the Arduino device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling Arduino device : " + deviceId);
|
||||||
|
}
|
||||||
|
ArduinoDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
ArduinoDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
ArduinoDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the Arduino device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of Arduino device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of Arduino device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of Arduino device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the Arduino device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of Arduino device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
ArduinoDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
ArduinoDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
ArduinoDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the Arduino device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all Arduino devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all Arduino devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ArduinoManagerService implements DeviceManagementService {
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return ArduinoConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
deviceManager=new ArduinoManager();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.dao.impl.ArduinoDeviceDAOImpl;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class ArduinoDAO extends IotDeviceManagementDAOFactory implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ArduinoDAO.class);
|
||||||
|
static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public ArduinoDAO() {
|
||||||
|
initArduinoDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initArduinoDAO() {
|
||||||
|
dataSource = getDataSourceMap().get(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new ArduinoDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback "
|
||||||
|
+ "has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,236 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.dao.ArduinoDAO;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for arduino Devices.
|
||||||
|
*/
|
||||||
|
public class ArduinoDeviceDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ArduinoDeviceDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = ArduinoDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT ARDUINO_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM ARDUINO_DEVICE WHERE ARDUINO_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
ArduinoConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Arduino device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Arduino database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Arduino device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
ArduinoDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = ArduinoDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO ARDUINO_DEVICE(ARDUINO_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Arduino device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Arduino database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Arduino device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Arduino db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = ArduinoDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE ARDUINO_DEVICE SET DEVICE_NAME = ? WHERE ARDUINO_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Arduino device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Arduino device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = ArduinoDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM ARDUINO_DEVICE WHERE ARDUINO_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Arduino device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Arduino database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Arduino device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = ArduinoDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT ARDUINO_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM ARDUINO_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(ArduinoConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(ArduinoConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Arduino device details have fetched from Arduino database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Arduino device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
ArduinoDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by Arduino plugin.
|
||||||
|
*/
|
||||||
|
public class ArduinoUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(ArduinoUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.impl.ArduinoManagerService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.arduino.internal.ArduinoManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
public class ArduinoManagementServiceComponent {
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceRegistration arduinoServiceRegRef;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ArduinoManagementServiceComponent.class);
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Arduino Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
|
||||||
|
|
||||||
|
arduinoServiceRegRef =
|
||||||
|
bundleContext.registerService(DeviceManagementService.class.getName(), new
|
||||||
|
ArduinoManagerService(), null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Arduino Device Management Service Component has been successfully activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while activating Arduino Device Management Service Component", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating Arduino Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (arduinoServiceRegRef != null) {
|
||||||
|
arduinoServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Arduino Device Management Service Component has been successfully de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Arduino Device Management bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-api</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.arduino.api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Cloud API - Arduiono API</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- CDM -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--MQTT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>mqtt-client</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--IOT -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.arduino.impl</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<warName>arduino</warName>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.util.DeviceJSON;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.util.MqttArduinoSubscriber;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.UnauthorizedException;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class ArduinoControllerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(ArduinoControllerService.class);
|
||||||
|
|
||||||
|
private static Map<String, LinkedList<String>> replyMsgQueue = new HashMap<>();
|
||||||
|
private static Map<String, LinkedList<String>> internalControlsQueue = new HashMap<>();
|
||||||
|
private static MqttArduinoSubscriber mqttArduinoSubscriber;
|
||||||
|
|
||||||
|
|
||||||
|
public void setMqttArduinoSubscriber(MqttArduinoSubscriber mqttArduinoSubscriber) {
|
||||||
|
ArduinoControllerService.mqttArduinoSubscriber = mqttArduinoSubscriber;
|
||||||
|
try {
|
||||||
|
mqttArduinoSubscriber.subscribe();
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public MqttArduinoSubscriber getMqttArduinoSubscriber() {
|
||||||
|
return mqttArduinoSubscriber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, LinkedList<String>> getReplyMsgQueue() {
|
||||||
|
return replyMsgQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, LinkedList<String>> getInternalControlsQueue() {
|
||||||
|
return internalControlsQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Service to switch arduino bulb (pin 13) between "ON" and "OFF"
|
||||||
|
Called by an external client intended to control the Arduino */
|
||||||
|
@Path("/bulb/{deviceId}/{state}")
|
||||||
|
@POST
|
||||||
|
public void switchBulb(@QueryParam("owner") String owner,
|
||||||
|
@PathParam("deviceId") String deviceId,
|
||||||
|
@PathParam("state") String state,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
String switchToState = state.toUpperCase();
|
||||||
|
|
||||||
|
if (!switchToState.equals(ArduinoConstants.STATE_ON) && !switchToState.equals(
|
||||||
|
ArduinoConstants.STATE_OFF)) {
|
||||||
|
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||||
|
response.setStatus(HttpStatus.SC_BAD_REQUEST);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
boolean result = deviceController.publishMqttControl(owner,
|
||||||
|
ArduinoConstants.DEVICE_TYPE,
|
||||||
|
deviceId, "BULB", switchToState);
|
||||||
|
if (result) {
|
||||||
|
response.setStatus(HttpStatus.SC_ACCEPTED);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (DeviceControllerException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Service to poll the control-queue for the controls sent to the Arduino
|
||||||
|
Called by the Arduino device */
|
||||||
|
@Path("/readcontrols/{deviceId}")
|
||||||
|
@GET
|
||||||
|
public String readControls(@QueryParam("owner") String owner,
|
||||||
|
@PathParam("deviceId") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
String result;
|
||||||
|
LinkedList<String> deviceControlList = internalControlsQueue.get(deviceId);
|
||||||
|
|
||||||
|
if (deviceControlList == null) {
|
||||||
|
result = "No controls have been set for device " + deviceId + " of owner " + owner;
|
||||||
|
response.setStatus(HttpStatus.SC_NO_CONTENT);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
result = deviceControlList.remove(); //returns the head value
|
||||||
|
response.setStatus(HttpStatus.SC_ACCEPTED);
|
||||||
|
|
||||||
|
} catch (NoSuchElementException ex) {
|
||||||
|
result = "There are no more controls for device " + deviceId + " of owner " +
|
||||||
|
owner;
|
||||||
|
response.setStatus(HttpStatus.SC_NO_CONTENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Service to push all the sensor data collected by the Arduino
|
||||||
|
Called by the Arduino device */
|
||||||
|
@Path("/pushdata")
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public void pushData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
String temperature = dataMsg.value; //TEMP
|
||||||
|
log.info("Recieved Sensor Data Values: " + temperature);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Recieved Temperature Data Value: " + temperature + " degrees C");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
boolean result = deviceController.pushBamData(dataMsg.owner,
|
||||||
|
ArduinoConstants.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
temperature,
|
||||||
|
DataStreamDefinitions.StreamTypeLabel
|
||||||
|
.TEMPERATURE);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (UnauthorizedException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,251 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
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.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceManagement;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipUtil;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class ArduinoManagerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(ArduinoManagerService.class);
|
||||||
|
|
||||||
|
@Path("/device/register")
|
||||||
|
@PUT
|
||||||
|
public boolean register(@QueryParam("deviceId") String deviceId,
|
||||||
|
@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||||
|
Response.status(HttpStatus.SC_CONFLICT).build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
EnrolmentInfo enrolmentInfo=new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
enrolmentInfo.setOwner(owner);
|
||||||
|
device.setEnrolmentInfo(enrolmentInfo);
|
||||||
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
||||||
|
if (added) {
|
||||||
|
Response.status(HttpStatus.SC_OK).build();
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return added;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/remove/{device_id}")
|
||||||
|
@DELETE
|
||||||
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
||||||
|
if (removed) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/update/{device_id}")
|
||||||
|
@POST
|
||||||
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@QueryParam("name") String name,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
|
||||||
|
// device.setDeviceTypeId(deviceTypeId);
|
||||||
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
|
||||||
|
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
return device;
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/download")
|
||||||
|
@GET
|
||||||
|
@Produces("application/octet-stream")
|
||||||
|
public Response downloadSketch(@QueryParam("owner") String owner, @PathParam("sketch_type") String
|
||||||
|
sketchType) {
|
||||||
|
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = createDownloadFile(owner, sketchType);
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||||
|
rb.header("Content-Disposition",
|
||||||
|
"attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||||
|
return rb.build();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/generate_link")
|
||||||
|
@GET
|
||||||
|
public Response generateSketchLink(@QueryParam("owner") String owner, @PathParam("sketch_type") String
|
||||||
|
sketchType) {
|
||||||
|
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = createDownloadFile(owner, sketchType);
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||||
|
return rb.build();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ZipArchive createDownloadFile(String owner, String sketchType) throws DeviceManagementException{
|
||||||
|
if (owner == null) {
|
||||||
|
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//create new device id
|
||||||
|
String deviceId = shortUUID();
|
||||||
|
|
||||||
|
//create token
|
||||||
|
String token = UUID.randomUUID().toString();
|
||||||
|
String refreshToken = UUID.randomUUID().toString();
|
||||||
|
//adding registering data
|
||||||
|
|
||||||
|
boolean status = register(deviceId, owner + "s_" + sketchType + "_" + deviceId.substring(0,
|
||||||
|
3),
|
||||||
|
owner);
|
||||||
|
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 = null;
|
||||||
|
|
||||||
|
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, token, refreshToken);
|
||||||
|
zipFile.setDeviceId(deviceId);
|
||||||
|
return zipFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String shortUUID() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
||||||
|
return Long.toString(l, Character.MAX_RADIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.util;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class DeviceJSON {
|
||||||
|
@XmlElement(required = true) public String owner;
|
||||||
|
@XmlElement(required = true) public String deviceId;
|
||||||
|
@XmlElement(required = true) public String reply;
|
||||||
|
@XmlElement public Long time;
|
||||||
|
@XmlElement public String key;
|
||||||
|
@XmlElement public String value;
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.ArduinoControllerService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.arduino.plugin.constants.ArduinoConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttSubscriber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class MqttArduinoSubscriber extends MqttSubscriber {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(MqttArduinoSubscriber.class);
|
||||||
|
private static final String subscribetopic =
|
||||||
|
"wso2" + File.separator + "iot" + File.separator + "+" + File.separator +
|
||||||
|
ArduinoConstants.DEVICE_TYPE + File.separator + "#";
|
||||||
|
|
||||||
|
|
||||||
|
//make it singleton
|
||||||
|
private MqttArduinoSubscriber() {
|
||||||
|
|
||||||
|
super("Subscriber", ArduinoConstants.DEVICE_TYPE, MqttConfig.getInstance().getMqttQueueEndpoint(),
|
||||||
|
subscribetopic);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override protected void postMessageArrived(final String topic, final MqttMessage message) {
|
||||||
|
int lastIndex = topic.lastIndexOf("/");
|
||||||
|
String deviceId = topic.substring(lastIndex + 1);
|
||||||
|
|
||||||
|
lastIndex = message.toString().lastIndexOf(":");
|
||||||
|
String msgContext = message.toString().substring(lastIndex + 1);
|
||||||
|
|
||||||
|
LinkedList<String> deviceControlList = null;
|
||||||
|
LinkedList<String> replyMessageList = null;
|
||||||
|
|
||||||
|
if (msgContext.equals("IN") || msgContext.equals(ArduinoConstants.STATE_ON) || msgContext
|
||||||
|
.equals(ArduinoConstants.STATE_OFF)) {
|
||||||
|
log.info("Recieved a control message: ");
|
||||||
|
log.info("Control message topic: " + topic);
|
||||||
|
log.info("Control message: " + message.toString());
|
||||||
|
synchronized (ArduinoControllerService.getInternalControlsQueue()) {
|
||||||
|
deviceControlList = ArduinoControllerService.getInternalControlsQueue().get(deviceId);
|
||||||
|
if (deviceControlList == null) {
|
||||||
|
ArduinoControllerService.getInternalControlsQueue()
|
||||||
|
.put(deviceId, deviceControlList = new LinkedList<String>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deviceControlList.add(message.toString());
|
||||||
|
} else if (msgContext.equals("OUT")) {
|
||||||
|
log.info("Recieved reply from a device: ");
|
||||||
|
log.info("Reply message topic: " + topic);
|
||||||
|
log.info("Reply message: " + message.toString().substring(0, lastIndex));
|
||||||
|
synchronized (ArduinoControllerService.getReplyMsgQueue()) {
|
||||||
|
replyMessageList = ArduinoControllerService.getReplyMsgQueue().get(deviceId);
|
||||||
|
if (replyMessageList == null) {
|
||||||
|
ArduinoControllerService.getReplyMsgQueue()
|
||||||
|
.put(deviceId, replyMessageList = new LinkedList<String>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
replyMessageList.add(message.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2005-2013 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="ArduinoController" address="/controller">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="ArduinoControllerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.ArduinoControllerService">
|
||||||
|
<property name="mqttArduinoSubscriber" ref="mqttSubscriber"/>
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="ArduinoManager" address="/manager">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="ArduinoManagerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.ArduinoManagerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
<bean id="mqttSubscriber" class="org.wso2.carbon.device.mgt.iot.sample.arduino.service.impl.util.MqttArduinoSubscriber" >
|
||||||
|
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>DigitalDisplay </display-name>
|
||||||
|
<description>DigitalDisplay</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `DIGITALDISPLAY_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `DIGITAL_DISPLAY_DEVICE` (
|
||||||
|
`DIGITAL_DISPLAY_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`DIGITAL_DISPLAY_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `DIGITALDISPLAY_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `DIGITAL_DISPLAY_DEVICE` (
|
||||||
|
`DIGITAL_DISPLAY_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`DIGITAL_DISPLAY_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,141 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.digitaldisplay.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Digital Display Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - digitaldisplay Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.digitaldisplay.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.digitaldisplay.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.digitaldisplay.*
|
||||||
|
</Export-Package>
|
||||||
|
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.constants;
|
||||||
|
|
||||||
|
public class DigitalDisplayConstants {
|
||||||
|
|
||||||
|
public final static String DEVICE_TYPE = "digital_display";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "DIGITAL_DISPLAY_DEVICE_ID";
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,266 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.dao.DigitalDisplayDAO;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the DigitalDisplay implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class DigitalDisplayManager implements DeviceManager
|
||||||
|
{
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new DigitalDisplayDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(DigitalDisplayManager.class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new DigitalDisplay device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
DigitalDisplayDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
DigitalDisplayDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
DigitalDisplayDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the DigitalDisplay device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the DigitalDisplay device enrollment data");
|
||||||
|
}
|
||||||
|
DigitalDisplayDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
DigitalDisplayDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
DigitalDisplayDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the DigitalDisplay device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling DigitalDisplay device : " + deviceId);
|
||||||
|
}
|
||||||
|
DigitalDisplayDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
DigitalDisplayDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
DigitalDisplayDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the DigitalDisplay device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of DigitalDisplay device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of DigitalDisplay device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of DigitalDisplay device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the DigitalDisplay device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of DigitalDisplay device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
DigitalDisplayDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
DigitalDisplayDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
DigitalDisplayDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the DigitalDisplay device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all DigitalDisplay devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all DigitalDisplay devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.constants.DigitalDisplayConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DigitalDisplayManagerService implements DeviceManagementService{
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return DigitalDisplayConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
deviceManager= new DigitalDisplayManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.constants.DigitalDisplayConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.dao.impl.DigitalDisplayDeviceDAOImpl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class DigitalDisplayDAO extends IotDeviceManagementDAOFactory
|
||||||
|
implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DigitalDisplayDAO.class);
|
||||||
|
static DataSource dataSource; // package local variable
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public DigitalDisplayDAO() {
|
||||||
|
initDigitalDisplayDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new DigitalDisplayDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initDigitalDisplayDAO(){
|
||||||
|
dataSource = getDataSourceMap().get(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if(con != null){
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
|
||||||
|
"has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,238 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.constants.DigitalDisplayConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.dao.DigitalDisplayDAO;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for digital display Devices.
|
||||||
|
*/
|
||||||
|
public class DigitalDisplayDeviceDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DigitalDisplayDeviceDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = DigitalDisplayDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT DIGITAL_DISPLAY_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM DIGITAL_DISPLAY_DEVICE WHERE DIGITAL_DISPLAY_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
DigitalDisplayConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Digital Display device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Digital Display database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Digital Display device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
DigitalDisplayDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = DigitalDisplayDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO DIGITAL_DISPLAY_DEVICE(DIGITAL_DISPLAY_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Digital Display device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Digital Display database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Digital Display device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Digital Display db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = DigitalDisplayDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE DIGITAL_DISPLAY_DEVICE SET DEVICE_NAME = ? WHERE DIGITAL_DISPLAY_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Digital Display device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Digital Display device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = DigitalDisplayDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM DIGITAL_DISPLAY_DEVICE WHERE DIGITAL_DISPLAY_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Digital Display device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Digital Display database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Digital Display device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = DigitalDisplayDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT DIGITAL_DISPLAY_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM DIGITAL_DISPLAY_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(DigitalDisplayConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(DigitalDisplayConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Digital Display device details have fetched from Digital Display database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Digital Display device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
DigitalDisplayDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by Digital Display plugin.
|
||||||
|
*/
|
||||||
|
public class DigitalDisplayUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(DigitalDisplayUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.impl.DigitalDisplayManagerService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.digitaldisplay.internal.DigitalDisplayManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DigitalDisplayManagementServiceComponent {
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceRegistration digitalDisplayServiceRegRef;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DigitalDisplayManagementServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Digital Display Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
|
||||||
|
|
||||||
|
digitalDisplayServiceRegRef =
|
||||||
|
bundleContext.registerService(DeviceManagementService.class.getName(), new
|
||||||
|
DigitalDisplayManagerService(),
|
||||||
|
null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Digital Display Management Service Component has been successfully activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while activating Digital Display Management Service Component", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating DigitalDisplay Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (digitalDisplayServiceRegRef != null) {
|
||||||
|
digitalDisplayServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"DigitalDisplay Management Service Component has been successfully de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Iot Device Management bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,100 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-api</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.digitaldisplay.api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Cloud API - Digital Display API</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- CDM -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!--IOT -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.digitaldisplay.impl</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<warName>digitaldisplay</warName><!--devices.war-->
|
||||||
|
<!--<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>-->
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,235 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
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.iot.common.DeviceManagement;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.plugin.constants.DigitalDisplayConstants;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
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.core.Context;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class DigitalDisplayManagerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(DigitalDisplayManagerService.class);
|
||||||
|
|
||||||
|
@Path("/device/register")
|
||||||
|
@PUT
|
||||||
|
public boolean register(@QueryParam("deviceId") String deviceId,
|
||||||
|
@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||||
|
Response.status(HttpStatus.SC_CONFLICT).build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||||
|
device.setEnrolmentInfo(enrolmentInfo);
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
enrolmentInfo.setOwner(owner);
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||||
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
||||||
|
if (added) {
|
||||||
|
Response.status(HttpStatus.SC_OK).build();
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return added;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/remove/{device_id}")
|
||||||
|
@DELETE
|
||||||
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
|
||||||
|
deviceIdentifier);
|
||||||
|
if (removed) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/update/{device_id}")
|
||||||
|
@POST
|
||||||
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@QueryParam("name") String name,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
||||||
|
deviceIdentifier);
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
|
||||||
|
// device.setDeviceTypeId(deviceTypeId);
|
||||||
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
|
||||||
|
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
return device;
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/download")
|
||||||
|
@GET
|
||||||
|
@Produces("application/octet-stream")
|
||||||
|
public Response downloadSketch(@QueryParam("owner") String owner,
|
||||||
|
@PathParam("sketch_type") String
|
||||||
|
sketchType) {
|
||||||
|
|
||||||
|
if (owner == null) {
|
||||||
|
return Response.status(400).build();//bad request
|
||||||
|
}
|
||||||
|
|
||||||
|
//create new device id
|
||||||
|
String deviceId = shortUUID();
|
||||||
|
|
||||||
|
//create token
|
||||||
|
String token = UUID.randomUUID().toString();
|
||||||
|
String refreshToken = UUID.randomUUID().toString();
|
||||||
|
//adding registering data
|
||||||
|
|
||||||
|
boolean status = register(deviceId,
|
||||||
|
owner + "s_" + sketchType + "_" + deviceId.substring(0, 3),
|
||||||
|
owner);
|
||||||
|
if (!status) {
|
||||||
|
return Response.status(500).entity(
|
||||||
|
"Error occurred while registering the device with " + "id: " + deviceId
|
||||||
|
+ " owner:" + owner).build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ZipUtil ziputil = new ZipUtil();
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId,
|
||||||
|
token, refreshToken);
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity("Error occurred while creating zip file").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||||
|
rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||||
|
return rb.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String shortUUID() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
||||||
|
return Long.toString(l, Character.MAX_RADIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2005-2013 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2011-2012 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!--<jaxrs:server id="DigitalDisplayController" address="controller">-->
|
||||||
|
<!--<jaxrs:serviceBeans>-->
|
||||||
|
<!--<bean id="SenseBotControllerService"-->
|
||||||
|
<!--class="org.wso2.carbon.device.mgt.iot.digitaldisplay.api.DigitalDisplayControllerService">-->
|
||||||
|
<!--</bean>-->
|
||||||
|
<!--</jaxrs:serviceBeans>-->
|
||||||
|
<!--<jaxrs:providers>-->
|
||||||
|
<!--<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />-->
|
||||||
|
<!--</jaxrs:providers>-->
|
||||||
|
<!--</jaxrs:server>-->
|
||||||
|
|
||||||
|
<jaxrs:server id="DigitalDisplayManager" address="/manager">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="DigitalDisplayManagerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.digitaldisplay.service.impl.DigitalDisplayManagerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>DigitalDisplay </display-name>
|
||||||
|
<description>DigitalDisplay</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `FIREALARM_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `FIREALARM_DEVICE` (
|
||||||
|
`FIREALARM_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`FIREALARM_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `FIREALARM_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `FIREALARM_DEVICE` (
|
||||||
|
`FIREALARM_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`FIREALARM_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,152 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.firealarm.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Firealarm Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - Firealarm Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.firealarm.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*,
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.firealarm.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.firealarm.*
|
||||||
|
</Export-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
|
<!--<repositories>-->
|
||||||
|
<!--<repository>-->
|
||||||
|
<!--<id>Eclipse Paho Repo</id>-->
|
||||||
|
<!--<url>http://repo.spring.io/plugins-release/</url>-->
|
||||||
|
<!--</repository>-->
|
||||||
|
<!--</repositories>-->
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.eclipse.paho</groupId>-->
|
||||||
|
<!--<artifactId>mqtt-client</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants;
|
||||||
|
|
||||||
|
public class FireAlarmConstants {
|
||||||
|
public final static String DEVICE_TYPE = "firealarm";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "FIREALARM_DEVICE_ID";
|
||||||
|
public final static String STATE_ON = "ON";
|
||||||
|
public final static String STATE_OFF = "OFF";
|
||||||
|
}
|
||||||
@ -0,0 +1,261 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.*;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.dao.FireAlarmDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the FireAlarm implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class FireAlarmManager implements DeviceManager {
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new FireAlarmDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(FireAlarmManager.class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new Firealarm device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
FireAlarmDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
FireAlarmDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
FireAlarmDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the Firealarm device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the Firealarm device enrollment data");
|
||||||
|
}
|
||||||
|
FireAlarmDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
FireAlarmDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
FireAlarmDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the Firealarm device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling Firealarm device : " + deviceId);
|
||||||
|
}
|
||||||
|
FireAlarmDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
FireAlarmDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
FireAlarmDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the Firealarm device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of Firealarm device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of Firealarm device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of Firealarm device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the Firealarm device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of Firealarm device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
FireAlarmDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
FireAlarmDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
FireAlarmDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the Firealarm device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all Firealarm devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all Firealarm devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FireAlarmManagerService implements DeviceManagementService{
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return FireAlarmConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
this.deviceManager=new FireAlarmManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.dao.impl.FireAlarmDeviceDAOImpl;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class FireAlarmDAO extends IotDeviceManagementDAOFactory implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(FireAlarmDAO.class);
|
||||||
|
static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public FireAlarmDAO() {
|
||||||
|
initFireAlarmDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initFireAlarmDAO() {
|
||||||
|
dataSource = getDataSourceMap().get(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new FireAlarmDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback "
|
||||||
|
+ "has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,238 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.dao.FireAlarmDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for firealarm Devices.
|
||||||
|
*/
|
||||||
|
public class FireAlarmDeviceDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(FireAlarmDeviceDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = FireAlarmDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT FIREALARM_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM FIREALARM_DEVICE WHERE FIREALARM_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
FireAlarmConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Firealarm device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Firealarm database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Firealarm device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
FireAlarmDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = FireAlarmDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO FIREALARM_DEVICE(FIREALARM_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Firealarm device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Firealarm database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Firealarm device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Firealarm db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = FireAlarmDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE FIREALARM_DEVICE SET DEVICE_NAME = ? WHERE FIREALARM_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Firealarm device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Firealarm device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = FireAlarmDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM FIREALARM_DEVICE WHERE FIREALARM_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Firealarm device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Firealarm database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Firealarm device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = FireAlarmDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT FIREALARM_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM FIREALARM_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(FireAlarmConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(FireAlarmConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Firealarm device details have fetched from Firealarm database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Firealarm device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
FireAlarmDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by FireAlarm plugin.
|
||||||
|
*/
|
||||||
|
public class FireAlarmUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(FireAlarmUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl.FireAlarmManagerService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.firealarm.internal.FirealarmManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
public class FirealarmManagementServiceComponent {
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceRegistration firealarmServiceRegRef;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(FirealarmManagementServiceComponent.class);
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Firealarm Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
|
||||||
|
|
||||||
|
firealarmServiceRegRef =
|
||||||
|
bundleContext.registerService(DeviceManagementService.class.getName(),
|
||||||
|
new FireAlarmManagerService(),
|
||||||
|
null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Firealarm Device Management Service Component has been successfully activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while activating Firealarm Device Management Service Component", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating Firealarm Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (firealarmServiceRegRef != null) {
|
||||||
|
firealarmServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Firealarm Device Management Service Component has been successfully de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Firealarm Device Management bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,107 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-api</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.firealarm.api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Cloud API - Firealarm API</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- CDM -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--MQTT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.paho</groupId>
|
||||||
|
<artifactId>mqtt-client</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--IOT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpasyncclient</artifactId>
|
||||||
|
<version>4.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.firealarm.impl</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<warName>firealarm</warName>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
# Root logger option
|
||||||
|
log4j.rootLogger=INFO, stdout
|
||||||
|
|
||||||
|
# Direct log messages to stdout
|
||||||
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
|
log4j.appender.stdout.Target=System.out
|
||||||
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %c{1}:%L - %m%n
|
||||||
@ -0,0 +1,676 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.concurrent.FutureCallback;
|
||||||
|
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
||||||
|
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceValidator;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.UnauthorizedException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl.util.DeviceJSON;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
|
||||||
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.HeaderParam;
|
||||||
|
import javax.ws.rs.HttpMethod;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.ProtocolException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
public class FireAlarmControllerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(FireAlarmControllerService.class);
|
||||||
|
|
||||||
|
private static final String URL_PREFIX = "http://";
|
||||||
|
private static final String BULB_CONTEXT = "/BULB/";
|
||||||
|
private static final String FAN_CONTEXT = "/FAN/";
|
||||||
|
private static final String TEMPERATURE_CONTEXT = "/TEMP/";
|
||||||
|
|
||||||
|
public static final String XMPP_PROTOCOL = "XMPP";
|
||||||
|
public static final String HTTP_PROTOCOL = "HTTP";
|
||||||
|
public static final String MQTT_PROTOCOL = "MQTT";
|
||||||
|
|
||||||
|
|
||||||
|
private static ConcurrentHashMap<String, String> deviceToIpMap =
|
||||||
|
new ConcurrentHashMap<String, String>();
|
||||||
|
|
||||||
|
@Path("/register/{owner}/{deviceId}/{ip}")
|
||||||
|
@POST
|
||||||
|
public String registerDeviceIP(@PathParam("owner") String owner,
|
||||||
|
@PathParam("deviceId") String deviceId,
|
||||||
|
@PathParam("ip") String deviceIP,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
String result;
|
||||||
|
|
||||||
|
log.info("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId +
|
||||||
|
" of owner: " + owner);
|
||||||
|
|
||||||
|
deviceToIpMap.put(deviceId, deviceIP);
|
||||||
|
|
||||||
|
result = "Device-IP Registered";
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Service to switch "ON" and "OFF" the FireAlarm bulb
|
||||||
|
Called by an external client intended to control the FireAlarm bulb */
|
||||||
|
@Path("/bulb/{state}")
|
||||||
|
@POST
|
||||||
|
public void switchBulb(@HeaderParam("owner") String owner,
|
||||||
|
@HeaderParam("deviceId") String deviceId,
|
||||||
|
@HeaderParam("protocol") String protocol,
|
||||||
|
@PathParam("state") String state,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceValidator deviceValidator = new DeviceValidator();
|
||||||
|
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId,
|
||||||
|
FireAlarmConstants
|
||||||
|
.DEVICE_TYPE))) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner);
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String switchToState = state.toUpperCase();
|
||||||
|
|
||||||
|
if (!switchToState.equals(FireAlarmConstants.STATE_ON) && !switchToState.equals(
|
||||||
|
FireAlarmConstants.STATE_OFF)) {
|
||||||
|
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||||
|
response.setStatus(HttpStatus.SC_BAD_REQUEST);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String deviceIP = deviceToIpMap.get(deviceId);
|
||||||
|
if (deviceIP == null) {
|
||||||
|
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String protocolString = protocol.toUpperCase();
|
||||||
|
String callUrlPattern = BULB_CONTEXT + switchToState;
|
||||||
|
|
||||||
|
log.info("Sending command: '" + callUrlPattern + "' to firealarm at: " + deviceIP + " " +
|
||||||
|
"via" + " " + protocol);
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (protocolString) {
|
||||||
|
case HTTP_PROTOCOL:
|
||||||
|
sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
|
||||||
|
break;
|
||||||
|
case MQTT_PROTOCOL:
|
||||||
|
callUrlPattern = BULB_CONTEXT.replace("/", "");
|
||||||
|
sendCommandViaMQTT(owner, deviceId, callUrlPattern, switchToState);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (protocolString == null) {
|
||||||
|
sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" +
|
||||||
|
" " + protocol);
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Path("/fan/{state}")
|
||||||
|
@POST
|
||||||
|
public void switchFan(@HeaderParam("owner") String owner,
|
||||||
|
@HeaderParam("deviceId") String deviceId,
|
||||||
|
@HeaderParam("protocol") String protocol,
|
||||||
|
@PathParam("state") String state,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceValidator deviceValidator = new DeviceValidator();
|
||||||
|
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId,
|
||||||
|
FireAlarmConstants
|
||||||
|
.DEVICE_TYPE))) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner);
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String switchToState = state.toUpperCase();
|
||||||
|
|
||||||
|
if (!switchToState.equals(FireAlarmConstants.STATE_ON) && !switchToState.equals(
|
||||||
|
FireAlarmConstants.STATE_OFF)) {
|
||||||
|
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||||
|
response.setStatus(HttpStatus.SC_BAD_REQUEST);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String deviceIP = deviceToIpMap.get(deviceId);
|
||||||
|
|
||||||
|
if (deviceIP == null) {
|
||||||
|
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String protocolString = protocol.toUpperCase();
|
||||||
|
String callUrlPattern = FAN_CONTEXT + switchToState;
|
||||||
|
|
||||||
|
log.info("Sending command: '" + callUrlPattern + "' to firealarm at: " + deviceIP + " " +
|
||||||
|
"via" + " " + protocol);
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (protocolString) {
|
||||||
|
case HTTP_PROTOCOL:
|
||||||
|
sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
|
||||||
|
break;
|
||||||
|
case MQTT_PROTOCOL:
|
||||||
|
callUrlPattern = FAN_CONTEXT.replace("/", "");
|
||||||
|
sendCommandViaMQTT(owner, deviceId, callUrlPattern, switchToState);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (protocolString == null) {
|
||||||
|
sendCommandViaHTTP(deviceIP, 80, callUrlPattern, true);
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("Failed to send command '" + callUrlPattern + "' to: " + deviceIP + " via" +
|
||||||
|
" " + protocol);
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Path("/readtemperature")
|
||||||
|
@GET
|
||||||
|
public String requestTemperature(@HeaderParam("owner") String owner,
|
||||||
|
@HeaderParam("deviceId") String deviceId,
|
||||||
|
@HeaderParam("protocol") String protocol,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
String replyMsg = "";
|
||||||
|
|
||||||
|
DeviceValidator deviceValidator = new DeviceValidator();
|
||||||
|
try {
|
||||||
|
if (!deviceValidator.isExist(owner, new DeviceIdentifier(deviceId,
|
||||||
|
FireAlarmConstants
|
||||||
|
.DEVICE_TYPE))) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
return "Unauthorized Access";
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
replyMsg = e.getErrorMessage();
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
String deviceIp = deviceToIpMap.get(deviceId);
|
||||||
|
|
||||||
|
if (deviceIp == null) {
|
||||||
|
replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
|
||||||
|
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (protocol) {
|
||||||
|
case HTTP_PROTOCOL:
|
||||||
|
log.info("Sending request to read firealarm-temperature at : " + deviceIp +
|
||||||
|
" via " + HTTP_PROTOCOL);
|
||||||
|
|
||||||
|
replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case XMPP_PROTOCOL:
|
||||||
|
log.info("Sending request to read firealarm-temperature at : " + deviceIp +
|
||||||
|
" via " +
|
||||||
|
XMPP_PROTOCOL);
|
||||||
|
replyMsg = requestTemperatureViaXMPP(deviceIp, response);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (protocol == null) {
|
||||||
|
log.info("Sending request to read firealarm-temperature at : " + deviceIp +
|
||||||
|
" via " + HTTP_PROTOCOL);
|
||||||
|
|
||||||
|
replyMsg = sendCommandViaHTTP(deviceIp, 80, TEMPERATURE_CONTEXT, false);
|
||||||
|
} else {
|
||||||
|
replyMsg = "Requested protocol '" + protocol + "' is not supported";
|
||||||
|
response.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
replyMsg = e.getErrorMessage();
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
replyMsg = "The current temperature of the device is " + replyMsg;
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String requestTemperatureViaXMPP(String deviceIp,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
String replyMsg = "";
|
||||||
|
|
||||||
|
String sep = File.separator;
|
||||||
|
String scriptsFolder = "repository" + sep + "resources" + sep + "scripts";
|
||||||
|
String scriptPath = CarbonUtils.getCarbonHome() + sep + scriptsFolder + sep
|
||||||
|
+ "xmpp_client.py";
|
||||||
|
String command = "python " + scriptPath;
|
||||||
|
|
||||||
|
replyMsg = executeCommand(command);
|
||||||
|
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
return replyMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String executeCommand(String command) {
|
||||||
|
StringBuffer output = new StringBuffer();
|
||||||
|
|
||||||
|
Process p;
|
||||||
|
try {
|
||||||
|
p = Runtime.getRuntime().exec(command);
|
||||||
|
p.waitFor();
|
||||||
|
BufferedReader reader =
|
||||||
|
new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||||
|
|
||||||
|
String line = "";
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
output.append(line + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Path("/push_temperature")
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public void pushTemperatureData(
|
||||||
|
final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
||||||
|
boolean result;
|
||||||
|
String deviceId = dataMsg.deviceId;
|
||||||
|
String deviceIp = dataMsg.reply;
|
||||||
|
String temperature = dataMsg.value;
|
||||||
|
|
||||||
|
String registeredIp = deviceToIpMap.get(deviceId);
|
||||||
|
|
||||||
|
if (registeredIp == null) {
|
||||||
|
log.warn(
|
||||||
|
"Unregistered IP: Temperature Data Received from an un-registered IP " +
|
||||||
|
deviceIp +
|
||||||
|
" for device ID - " + deviceId);
|
||||||
|
response.setStatus(HttpStatus.SC_PRECONDITION_FAILED);
|
||||||
|
return;
|
||||||
|
} else if (!registeredIp.equals(deviceIp)) {
|
||||||
|
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " +
|
||||||
|
deviceId +
|
||||||
|
" is already registered under some other IP. Re-registration " +
|
||||||
|
"required");
|
||||||
|
response.setStatus(HttpStatus.SC_CONFLICT);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
temperature,
|
||||||
|
DataStreamDefinitions.StreamTypeLabel
|
||||||
|
.TEMPERATURE);
|
||||||
|
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
} catch (UnauthorizedException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Data Push Attempt Failed for BAM Publisher: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
result = deviceController.pushCepData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
temperature,
|
||||||
|
DataStreamDefinitions.StreamTypeLabel
|
||||||
|
.TEMPERATURE);
|
||||||
|
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
} catch (UnauthorizedException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Data Push Attempt Failed for CEP Publisher: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Service to push all the sensor data collected by the FireAlarm
|
||||||
|
Called by the FireAlarm device */
|
||||||
|
|
||||||
|
@Path("/pushalarmdata")
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public void pushAlarmData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
||||||
|
boolean result;
|
||||||
|
String sensorValues = dataMsg.value;
|
||||||
|
log.info("Recieved Sensor Data Values: " + sensorValues);
|
||||||
|
|
||||||
|
String sensors[] = sensorValues.split(":");
|
||||||
|
try {
|
||||||
|
if (sensors.length == 3) {
|
||||||
|
String temperature = sensors[0];
|
||||||
|
String bulb = sensors[1];
|
||||||
|
String fan = sensors[2];
|
||||||
|
|
||||||
|
sensorValues = "Temperature:" + temperature + "C\tBulb Status:" + bulb +
|
||||||
|
"\t\tFan Status:" + fan;
|
||||||
|
log.info(sensorValues);
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
temperature, "TEMPERATURE");
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Error whilst pushing temperature: " + sensorValues);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
bulb,
|
||||||
|
"BULB");
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Error whilst pushing Bulb data: " + sensorValues);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
fan,
|
||||||
|
"FAN");
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Error whilst pushing Fan data: " + sensorValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
||||||
|
.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
dataMsg.value, dataMsg.reply);
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Error whilst pushing sensor data: " + sensorValues);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (UnauthorizedException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
log.error("Data Push Attempt Failed at Publisher: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean sendCommandViaMQTT(String deviceOwner, String deviceId, String resource,
|
||||||
|
String state) throws DeviceManagementException {
|
||||||
|
|
||||||
|
boolean result = false;
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = deviceController.publishMqttControl(deviceOwner,
|
||||||
|
FireAlarmConstants.DEVICE_TYPE,
|
||||||
|
deviceId, resource, state);
|
||||||
|
} catch (DeviceControllerException e) {
|
||||||
|
String errorMsg = "Error whilst trying to publish to MQTT Queue";
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String sendCommandViaHTTP(final String deviceIp, int deviceServerPort,
|
||||||
|
String callUrlPattern,
|
||||||
|
boolean fireAndForgot)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
|
||||||
|
if (deviceServerPort == 0) {
|
||||||
|
deviceServerPort = 80;
|
||||||
|
}
|
||||||
|
|
||||||
|
String responseMsg = "";
|
||||||
|
String urlString = URL_PREFIX + deviceIp + ":" + deviceServerPort + callUrlPattern;
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(urlString);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fireAndForgot) {
|
||||||
|
HttpURLConnection httpConnection = getHttpConnection(urlString);
|
||||||
|
|
||||||
|
try {
|
||||||
|
httpConnection.setRequestMethod(HttpMethod.GET);
|
||||||
|
} catch (ProtocolException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"Protocol specific error occurred when trying to set method to GET" +
|
||||||
|
" for:" + urlString;
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
responseMsg = readResponseFromGetRequest(httpConnection);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
CloseableHttpAsyncClient httpclient = null;
|
||||||
|
try {
|
||||||
|
|
||||||
|
httpclient = HttpAsyncClients.createDefault();
|
||||||
|
httpclient.start();
|
||||||
|
HttpGet request = new HttpGet(urlString);
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
Future<HttpResponse> future = httpclient.execute(
|
||||||
|
request, new FutureCallback<HttpResponse>() {
|
||||||
|
@Override
|
||||||
|
public void completed(HttpResponse httpResponse) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Exception e) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelled() {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
latch.await();
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sync Interrupted");
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (httpclient != null) {
|
||||||
|
httpclient.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Failed on close");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Utility methods relevant to creating and sending http requests */
|
||||||
|
|
||||||
|
/* This methods creates and returns a http connection object */
|
||||||
|
|
||||||
|
private HttpURLConnection getHttpConnection(String urlString) throws
|
||||||
|
DeviceManagementException {
|
||||||
|
|
||||||
|
URL connectionUrl = null;
|
||||||
|
HttpURLConnection httpConnection = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
connectionUrl = new URL(urlString);
|
||||||
|
httpConnection = (HttpURLConnection) connectionUrl.openConnection();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"Error occured whilst trying to form HTTP-URL from string: " + urlString;
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
String errorMsg = "Error occured whilst trying to open a connection to: " +
|
||||||
|
connectionUrl.toString();
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This methods reads and returns the response from the connection */
|
||||||
|
|
||||||
|
private String readResponseFromGetRequest(HttpURLConnection httpConnection)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try {
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(
|
||||||
|
httpConnection.getInputStream()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"There is an issue with connecting the reader to the input stream at: " +
|
||||||
|
httpConnection.getURL();
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
String responseLine;
|
||||||
|
StringBuffer completeResponse = new StringBuffer();
|
||||||
|
|
||||||
|
try {
|
||||||
|
while ((responseLine = bufferedReader.readLine()) != null) {
|
||||||
|
completeResponse.append(responseLine);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"Error occured whilst trying read from the connection stream at: " +
|
||||||
|
httpConnection.getURL();
|
||||||
|
log.error(errorMsg);
|
||||||
|
throw new DeviceManagementException(errorMsg, e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
bufferedReader.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(
|
||||||
|
"Could not succesfully close the bufferedReader to the connection at: " +
|
||||||
|
httpConnection.getURL());
|
||||||
|
}
|
||||||
|
|
||||||
|
return completeResponse.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,346 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
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.iot.common.DeviceManagement;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.apimgt.AccessTokenInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.apimgt.TokenClient;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppAccount;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppServerClient;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.AccessTokenException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
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.core.Context;
|
||||||
|
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.UUID;
|
||||||
|
|
||||||
|
//@Path("/FireAlarmDeviceManager")
|
||||||
|
public class FireAlarmManagerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(FireAlarmManagerService.class);
|
||||||
|
|
||||||
|
@Path("/device/register")
|
||||||
|
@PUT
|
||||||
|
public boolean register(@QueryParam("deviceId") String deviceId,
|
||||||
|
@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||||
|
Response.status(HttpStatus.SC_CONFLICT).build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
|
||||||
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
enrolmentInfo.setOwner(owner);
|
||||||
|
device.setEnrolmentInfo(enrolmentInfo);
|
||||||
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
||||||
|
|
||||||
|
if (added) {
|
||||||
|
Response.status(HttpStatus.SC_OK).build();
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return added;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/remove/{device_id}")
|
||||||
|
@DELETE
|
||||||
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
|
||||||
|
deviceIdentifier);
|
||||||
|
if (removed) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/update/{device_id}")
|
||||||
|
@POST
|
||||||
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@QueryParam("name") String name,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
||||||
|
deviceIdentifier);
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
|
||||||
|
// device.setDeviceTypeId(deviceTypeId);
|
||||||
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(
|
||||||
|
device);
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/devices/{username}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device[] getFirealarmDevices(@PathParam("username") String username) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<Device> userDevices =
|
||||||
|
deviceManagement.getDeviceManagementService().getDevicesOfUser(
|
||||||
|
username);
|
||||||
|
ArrayList<Device> userDevicesforFirealarm = new ArrayList<Device>();
|
||||||
|
for (Device device : userDevices) {
|
||||||
|
|
||||||
|
if (device.getType().equals(FireAlarmConstants.DEVICE_TYPE) &&
|
||||||
|
device.getEnrolmentInfo().getStatus().equals(
|
||||||
|
EnrolmentInfo.Status.ACTIVE)) {
|
||||||
|
userDevicesforFirealarm.add(device);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userDevicesforFirealarm.toArray(new Device[]{});
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving devices for " + username);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/download")
|
||||||
|
@GET
|
||||||
|
@Produces("application/octet-stream")
|
||||||
|
public Response downloadSketch(@QueryParam("owner") String owner,
|
||||||
|
@PathParam("sketch_type") String sketchType) {
|
||||||
|
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = createDownloadFile(owner, sketchType);
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||||
|
rb.header("Content-Disposition",
|
||||||
|
"attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||||
|
return rb.build();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
} catch (AccessTokenException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
} catch (DeviceControllerException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/generate_link")
|
||||||
|
@GET
|
||||||
|
public Response generateSketchLink(@QueryParam("owner") String owner,
|
||||||
|
@PathParam("sketch_type") String sketchType) {
|
||||||
|
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = createDownloadFile(owner, sketchType);
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||||
|
return rb.build();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
} catch (AccessTokenException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
} catch (DeviceControllerException ex) {
|
||||||
|
return Response.status(500).entity(ex.getMessage()).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ZipArchive createDownloadFile(String owner, String sketchType)
|
||||||
|
throws DeviceManagementException, AccessTokenException, DeviceControllerException {
|
||||||
|
if (owner == null) {
|
||||||
|
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
||||||
|
}
|
||||||
|
|
||||||
|
//create new device id
|
||||||
|
String deviceId = shortUUID();
|
||||||
|
|
||||||
|
TokenClient accessTokenClient = new TokenClient(FireAlarmConstants.DEVICE_TYPE);
|
||||||
|
AccessTokenInfo accessTokenInfo = null;
|
||||||
|
|
||||||
|
accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
||||||
|
|
||||||
|
//create token
|
||||||
|
String accessToken = accessTokenInfo.getAccess_token();
|
||||||
|
String refreshToken = accessTokenInfo.getRefresh_token();
|
||||||
|
//adding registering data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
XmppAccount newXmppAccount = new XmppAccount();
|
||||||
|
newXmppAccount.setAccountName(owner + "_" + deviceId);
|
||||||
|
newXmppAccount.setUsername(deviceId);
|
||||||
|
newXmppAccount.setPassword(accessToken);
|
||||||
|
|
||||||
|
String xmppEndPoint = XmppConfig.getInstance().getXmppControlQueue().getServerURL();
|
||||||
|
|
||||||
|
int indexOfChar = xmppEndPoint.lastIndexOf('/');
|
||||||
|
|
||||||
|
if (indexOfChar != -1) {
|
||||||
|
xmppEndPoint = xmppEndPoint.substring((indexOfChar + 1), xmppEndPoint.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
newXmppAccount.setEmail(deviceId + "@wso2.com");
|
||||||
|
|
||||||
|
XmppServerClient xmppServerClient = new XmppServerClient();
|
||||||
|
xmppServerClient.initControlQueue();
|
||||||
|
boolean status;
|
||||||
|
if(XmppConfig.getInstance().isEnabled()) {
|
||||||
|
status = xmppServerClient.createXMPPAccount(newXmppAccount);
|
||||||
|
|
||||||
|
if (!status) {
|
||||||
|
String msg =
|
||||||
|
"XMPP Account was not created for device - " + deviceId + " of owner - " +
|
||||||
|
owner +
|
||||||
|
". XMPP might have been disabled in org.wso2.carbon.device.mgt.iot.common.config.server.configs";
|
||||||
|
log.warn(msg);
|
||||||
|
throw new DeviceManagementException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status = register(deviceId, owner + "s_" + sketchType + "_" + deviceId.substring(0,
|
||||||
|
3),
|
||||||
|
owner);
|
||||||
|
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 = null;
|
||||||
|
|
||||||
|
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId, accessToken, refreshToken);
|
||||||
|
zipFile.setDeviceId(deviceId);
|
||||||
|
return zipFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String shortUUID() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
||||||
|
return Long.toString(l, Character.MAX_RADIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl.util;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class DeviceJSON {
|
||||||
|
@XmlElement(required = true) public String owner;
|
||||||
|
@XmlElement(required = true) public String deviceId;
|
||||||
|
@XmlElement(required = true) public String reply;
|
||||||
|
@XmlElement public Long time;
|
||||||
|
@XmlElement public String key;
|
||||||
|
@XmlElement public String value;
|
||||||
|
}
|
||||||
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*//*
|
||||||
|
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.firealarm.api.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttSubscriber;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.firealarm.api.FireAlarmControllerService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.firealarm.constants.FireAlarmConstants;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class MQTTFirealarmSubscriber extends MqttSubscriber {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(MQTTFirealarmSubscriber.class);
|
||||||
|
private static final String subscribetopic =
|
||||||
|
"wso2" + File.separator + "iot" + File.separator + "+" + File.separator +
|
||||||
|
FireAlarmConstants.DEVICE_TYPE + File.separator + "#";
|
||||||
|
|
||||||
|
private MQTTFirealarmSubscriber() {
|
||||||
|
super("Subscriber", FireAlarmConstants.DEVICE_TYPE, FireAlarmControllerService.CONTROL_QUEUE_ENDPOINT,
|
||||||
|
subscribetopic);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override protected void postMessageArrived(final String topic, final MqttMessage message) {
|
||||||
|
|
||||||
|
int lastIndex = topic.lastIndexOf("/");
|
||||||
|
String deviceId = topic.substring(lastIndex + 1);
|
||||||
|
|
||||||
|
lastIndex = message.toString().lastIndexOf(":");
|
||||||
|
String msgContext = message.toString().substring(lastIndex + 1);
|
||||||
|
|
||||||
|
LinkedList<String> deviceControlList = null;
|
||||||
|
LinkedList<String> replyMessageList = null;
|
||||||
|
|
||||||
|
if (msgContext.equals("IN") || msgContext.equals(FireAlarmConstants.STATE_ON) || msgContext
|
||||||
|
.equals(FireAlarmConstants.STATE_OFF)) {
|
||||||
|
log.info("Recieved a control message: ");
|
||||||
|
log.info("Control message topic: " + topic);
|
||||||
|
log.info("Control message: " + message.toString());
|
||||||
|
// synchronized (FireAlarmControllerService.internalControlsQueue) {
|
||||||
|
// deviceControlList = FireAlarmControllerService.internalControlsQueue.get(deviceId);
|
||||||
|
synchronized (FireAlarmControllerService.getInternalControlsQueue()) {
|
||||||
|
deviceControlList = FireAlarmControllerService.getInternalControlsQueue().get(deviceId);
|
||||||
|
if (deviceControlList == null) {
|
||||||
|
// FireAlarmControllerService.internalControlsQueue
|
||||||
|
FireAlarmControllerService.getInternalControlsQueue()
|
||||||
|
.put(deviceId, deviceControlList = new LinkedList<String>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deviceControlList.add(message.toString());
|
||||||
|
} else if (msgContext.equals("OUT")) {
|
||||||
|
log.info("Recieved reply from a device: ");
|
||||||
|
log.info("Reply message topic: " + topic);
|
||||||
|
log.info("Reply message: " + message.toString().substring(0, lastIndex));
|
||||||
|
// synchronized (FireAlarmControllerService.replyMsgQueue) {
|
||||||
|
// replyMessageList = FireAlarmControllerService.replyMsgQueue.get(deviceId);
|
||||||
|
synchronized (FireAlarmControllerService.getReplyMsgQueue()) {
|
||||||
|
replyMessageList = FireAlarmControllerService.getReplyMsgQueue().get(deviceId);
|
||||||
|
if (replyMessageList == null) {
|
||||||
|
// FireAlarmControllerService.replyMsgQueue
|
||||||
|
FireAlarmControllerService.getReplyMsgQueue()
|
||||||
|
.put(deviceId, replyMessageList = new LinkedList<String>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
replyMessageList.add(message.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2005-2013 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2011-2012 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="FireAlarmController" address="/controller">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="FireAlarmControllerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl.FireAlarmControllerService">
|
||||||
|
<!--<property name="mqttFireAlarmSubscriber" ref="mqttSubscriber"/>-->
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
<jaxrs:server id="FireAlarmManager" address="/manager">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="FireAlarmManagerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl.FireAlarmManagerService"/>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
<!--<bean id="mqttSubscriber" class="org.wso2.carbon.device.mgt.iot.firealarm.api.util.MQTTFirealarmSubscriber" >-->
|
||||||
|
<!-- -->
|
||||||
|
<!--</bean>-->
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>WSO2 Device Cloud</display-name>
|
||||||
|
<description>WSO2 Device Cloud</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `RASPBERRYPI_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `RASPBERRYPI_DEVICE` (
|
||||||
|
`RASPBERRYPI_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`RASPBERRYPI_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `RASPBERRYPI_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `RASPBERRYPI_DEVICE` (
|
||||||
|
`RASPBERRYPI_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`RASPBERRYPI_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,140 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.raspberrypi.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - RaspberryPi Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - Raspberrypi Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.raspberrypi.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*,
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.raspberrypi.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.raspberrypi.*
|
||||||
|
</Export-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants;
|
||||||
|
|
||||||
|
public class RaspberrypiConstants {
|
||||||
|
|
||||||
|
public final static String DEVICE_TYPE = "raspberrypi";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "RASPBERRYPI_DEVICE_ID";
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,264 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.dao.RaspberrypiDAO;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the Raspberrypi implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class RaspberrypiManager implements DeviceManager {
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new RaspberrypiDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(RaspberrypiManager.class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new Raspberrypi device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
RaspberrypiDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
RaspberrypiDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
RaspberrypiDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the Raspberrypi device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the Raspberrypi device enrollment data");
|
||||||
|
}
|
||||||
|
RaspberrypiDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
RaspberrypiDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
RaspberrypiDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the Raspberrypi device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling Raspberrypi device : " + deviceId);
|
||||||
|
}
|
||||||
|
RaspberrypiDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
RaspberrypiDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
RaspberrypiDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the Raspberrypi device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of Raspberrypi device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of Raspberrypi device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of Raspberrypi device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the Raspberrypi device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of Raspberrypi device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
RaspberrypiDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
RaspberrypiDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
RaspberrypiDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the Raspberrypi device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all Raspberrypi devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all Raspberrypi devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,63 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class RaspberrypiManagerService implements DeviceManagementService {
|
||||||
|
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return RaspberrypiConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
deviceManager = new RaspberrypiManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation
|
||||||
|
operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.dao.impl.RaspberrypiDeviceDAOImpl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class RaspberrypiDAO extends IotDeviceManagementDAOFactory implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(RaspberrypiDAO.class);
|
||||||
|
static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public RaspberrypiDAO() {
|
||||||
|
initRaspberrypiDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new RaspberrypiDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initRaspberrypiDAO() {
|
||||||
|
dataSource = getDataSourceMap().get(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback "
|
||||||
|
+ "has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,238 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.dao.RaspberrypiDAO;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for Raspberrypi Devices.
|
||||||
|
*/
|
||||||
|
public class RaspberrypiDeviceDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(RaspberrypiDeviceDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = RaspberrypiDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT RASPBERRYPI_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM RASPBERRYPI_DEVICE WHERE RASPBERRYPI_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
RaspberrypiConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Raspberrypi device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Raspberrypi database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Raspberrypi device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
RaspberrypiDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = RaspberrypiDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO RASPBERRYPI_DEVICE(RASPBERRYPI_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Raspberrypi device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Raspberrypi database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Raspberrypi device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Raspberrypi db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = RaspberrypiDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE RASPBERRYPI_DEVICE SET DEVICE_NAME = ? WHERE RASPBERRYPI_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Raspberrypi device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Raspberrypi device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = RaspberrypiDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM RASPBERRYPI_DEVICE WHERE RASPBERRYPI_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Raspberrypi device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Raspberrypi database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Raspberrypi device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = RaspberrypiDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT RASPBERRYPI_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM RASPBERRYPI_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(RaspberrypiConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(RaspberrypiConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Raspberrypi device details have fetched from Raspberrypi database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Raspberrypi device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
RaspberrypiDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by Raspberrypi plugin.
|
||||||
|
*/
|
||||||
|
public class RaspberrypiUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(RaspberrypiUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.impl.RaspberrypiManagerService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.raspberrypi.internal.RaspberrypiManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
public class RaspberrypiManagementServiceComponent {
|
||||||
|
|
||||||
|
|
||||||
|
private ServiceRegistration raspberrypiServiceRegRef;
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(RaspberrypiManagementServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Raspberrypi Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
raspberrypiServiceRegRef =
|
||||||
|
bundleContext.registerService(DeviceManagementService.class.getName(),
|
||||||
|
new RaspberrypiManagerService(), null);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Raspberrypi Device Management Service Component has been successfully " +
|
||||||
|
"activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(
|
||||||
|
"Error occurred while activating Raspberrypi Device Management Service " +
|
||||||
|
"Component",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating Raspberrypi Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (raspberrypiServiceRegRef != null) {
|
||||||
|
raspberrypiServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Raspberrypi Device Management Service Component has been successfully " +
|
||||||
|
"de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Raspberrypi Device Management bundle",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-api</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.raspberrypi.api</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Cloud API - Raspberrypi API</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- CDM -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--IOT -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.raspberrypi.impl</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<warName>raspberrypi</warName><!--devices.war-->
|
||||||
|
<!--<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>-->
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.datastore.impl.DataStreamDefinitions;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl.util.DeviceJSON;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.exception.UnauthorizedException;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
|
public class RaspberrypiControllerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(RaspberrypiControllerService.class);
|
||||||
|
|
||||||
|
/* Service to push all the sensor data collected by the Arduino
|
||||||
|
Called by the Arduino device */
|
||||||
|
@Path("/pushdata")
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public void pushData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
String temperature = dataMsg.value; //TEMP
|
||||||
|
log.info("Recieved Sensor Data Values: " + temperature);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Recieved Temperature Data Value: " + temperature + " degrees C");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
|
||||||
|
DeviceController deviceController = new DeviceController();
|
||||||
|
boolean result = deviceController.pushBamData(dataMsg.owner,
|
||||||
|
RaspberrypiConstants.DEVICE_TYPE,
|
||||||
|
dataMsg.deviceId,
|
||||||
|
System.currentTimeMillis(), "DeviceData",
|
||||||
|
temperature,
|
||||||
|
DataStreamDefinitions.StreamTypeLabel.TEMPERATURE);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (UnauthorizedException e) {
|
||||||
|
response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,231 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
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.iot.common.DeviceManagement;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.ZipUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
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.core.Context;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class RaspberrypiManagerService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(RaspberrypiManagerService.class);
|
||||||
|
|
||||||
|
@Path("/device/register")
|
||||||
|
@PUT
|
||||||
|
public boolean register(@QueryParam("deviceId") String deviceId,
|
||||||
|
@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||||
|
Response.status(HttpStatus.SC_CONFLICT).build();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
enrolmentInfo.setOwner(owner);
|
||||||
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
||||||
|
if (added) {
|
||||||
|
Response.status(HttpStatus.SC_OK).build();
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Response.status(HttpStatus.SC_EXPECTATION_FAILED).build();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return added;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/remove/{device_id}")
|
||||||
|
@DELETE
|
||||||
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
||||||
|
if (removed) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/update/{device_id}")
|
||||||
|
@POST
|
||||||
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
||||||
|
@QueryParam("name") String name,
|
||||||
|
@Context HttpServletResponse response) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
device.setDeviceIdentifier(deviceId);
|
||||||
|
|
||||||
|
// device.setDeviceTypeId(deviceTypeId);
|
||||||
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||||
|
|
||||||
|
device.setName(name);
|
||||||
|
device.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
|
||||||
|
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
response.setStatus(HttpStatus.SC_OK);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpStatus.SC_EXPECTATION_FAILED);
|
||||||
|
|
||||||
|
}
|
||||||
|
return updated;
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{device_id}")
|
||||||
|
@GET
|
||||||
|
@Consumes("application/json")
|
||||||
|
@Produces("application/json")
|
||||||
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
||||||
|
|
||||||
|
DeviceManagement deviceManagement = new DeviceManagement();
|
||||||
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||||
|
deviceIdentifier.setId(deviceId);
|
||||||
|
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
return device;
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("/device/{sketch_type}/download")
|
||||||
|
@GET
|
||||||
|
@Produces("application/octet-stream")
|
||||||
|
public Response downloadSketch(@QueryParam("owner") String owner, @PathParam("sketch_type") String
|
||||||
|
sketchType) {
|
||||||
|
|
||||||
|
if (owner == null) {
|
||||||
|
return Response.status(400).build();//bad request
|
||||||
|
}
|
||||||
|
|
||||||
|
//create new device id
|
||||||
|
String deviceId = shortUUID();
|
||||||
|
|
||||||
|
//create token
|
||||||
|
String token = UUID.randomUUID().toString();
|
||||||
|
String refreshToken = UUID.randomUUID().toString();
|
||||||
|
//adding registering data
|
||||||
|
|
||||||
|
boolean status = register(deviceId,
|
||||||
|
owner + "s_" + sketchType + "_" + deviceId.substring(0, 3),
|
||||||
|
owner);
|
||||||
|
if (!status) {
|
||||||
|
return Response.status(500).entity(
|
||||||
|
"Error occurred while registering the device with " + "id: " + deviceId
|
||||||
|
+ " owner:" + owner).build();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ZipUtil ziputil = new ZipUtil();
|
||||||
|
ZipArchive zipFile = null;
|
||||||
|
try {
|
||||||
|
zipFile = ziputil.downloadSketch(owner, sketchType, deviceId,
|
||||||
|
token,refreshToken);
|
||||||
|
} catch (DeviceManagementException ex) {
|
||||||
|
return Response.status(500).entity("Error occurred while creating zip file").build();
|
||||||
|
}
|
||||||
|
|
||||||
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||||
|
rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||||
|
return rb.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String shortUUID() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
||||||
|
return Long.toString(l, Character.MAX_RADIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl.util;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class DeviceJSON {
|
||||||
|
@XmlElement(required = true) public String owner;
|
||||||
|
@XmlElement(required = true) public String deviceId;
|
||||||
|
@XmlElement(required = true) public String reply;
|
||||||
|
@XmlElement public Long time;
|
||||||
|
@XmlElement public String key;
|
||||||
|
@XmlElement public String value;
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2005-2013 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Copyright 2011-2012 WSO2, Inc. (http://wso2.com)
|
||||||
|
~
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<jaxrs:server id="RaspberryPiController" address="controller">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="RaspberryPiControllerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl.RaspberrypiControllerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
<jaxrs:server id="RaspberrypiManager" address="/manager">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="RaspberrypiManagerService"
|
||||||
|
class="org.wso2.carbon.device.mgt.iot.sample.raspberrypi.service.impl.RaspberrypiManagerService">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>DigitalDisplay </display-name>
|
||||||
|
<description>DigitalDisplay</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `SENSEBOT_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `SENSEBOT_DEVICE` (
|
||||||
|
`SENSEBOT_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`SENSEBOT_DEVICE_ID`) );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
-- -----------------------------------------------------
|
||||||
|
-- Table `SENSEBOT_DEVICE`
|
||||||
|
-- -----------------------------------------------------
|
||||||
|
CREATE TABLE IF NOT EXISTS `SENSEBOT_DEVICE` (
|
||||||
|
`SENSEBOT_DEVICE_ID` VARCHAR(45) NOT NULL ,
|
||||||
|
`DEVICE_NAME` VARCHAR(100) NULL DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`SENSEBOT_DEVICE_ID`) )
|
||||||
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,140 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!-- ~ 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. -->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>devicecloud-mgt</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.sensebot.impl</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Sensebot Management Impl</name>
|
||||||
|
<description>WSO2 Carbon - Sensebot Management and Control Implementation</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.iot.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Device Cloud Impl Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.iot.sensebot.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.xml.bind.*,
|
||||||
|
javax.naming,
|
||||||
|
javax.sql,
|
||||||
|
javax.xml.bind.annotation.*,
|
||||||
|
javax.xml.parsers,
|
||||||
|
javax.net,
|
||||||
|
javax.net.ssl,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.device.mgt.common,
|
||||||
|
org.wso2.carbon.context.*,
|
||||||
|
org.wso2.carbon.ndatasource.core,
|
||||||
|
org.wso2.carbon.device.mgt.iot.common.*,
|
||||||
|
</Import-Package>
|
||||||
|
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.iot.sensebot.internal,
|
||||||
|
org.wso2.carbon.device.mgt.iot.sensebot.*
|
||||||
|
</Export-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!--<plugin>-->
|
||||||
|
<!--<groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!--<artifactId>maven-surefire-plugin</artifactId>-->
|
||||||
|
<!--<version>2.18</version>-->
|
||||||
|
<!--<configuration>-->
|
||||||
|
<!--<systemPropertyVariables>-->
|
||||||
|
<!--<log4j.configuration>file:src/test/resources/log4j.properties-->
|
||||||
|
<!--</log4j.configuration>-->
|
||||||
|
<!--</systemPropertyVariables>-->
|
||||||
|
<!--<suiteXmlFiles>-->
|
||||||
|
<!--<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
|
||||||
|
<!--</suiteXmlFiles>-->
|
||||||
|
<!--</configuration>-->
|
||||||
|
<!--</plugin>-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>org.testng</groupId>-->
|
||||||
|
<!--<artifactId>testng</artifactId>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
|
||||||
|
<!--<dependency>-->
|
||||||
|
<!--<groupId>com.h2database.wso2</groupId>-->
|
||||||
|
<!--<artifactId>h2-database-engine</artifactId>-->
|
||||||
|
<!--<scope>test</scope>-->
|
||||||
|
<!--</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.device.mgt.iot</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.constants;
|
||||||
|
|
||||||
|
public class SensebotConstants {
|
||||||
|
|
||||||
|
public final static String DEVICE_TYPE = "sensebot";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
|
||||||
|
public final static String DEVICE_PLUGIN_DEVICE_ID = "SENSEBOT_DEVICE_ID";
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,262 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.util.IotDeviceManagementUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.dao.SensebotDAO;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This represents the Sensebot implementation of DeviceManagerService.
|
||||||
|
*/
|
||||||
|
public class SensebotManager implements DeviceManager {
|
||||||
|
|
||||||
|
private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new SensebotDAO();
|
||||||
|
private static final Log log = LogFactory.getLog(SensebotManager.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FeatureManager getFeatureManager() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TenantConfiguration getConfiguration() throws DeviceManagementException {
|
||||||
|
//TODO implement this
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enrollDevice(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Enrolling a new Sensebot device : " + device.getDeviceIdentifier());
|
||||||
|
}
|
||||||
|
SensebotDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice(
|
||||||
|
iotDevice);
|
||||||
|
SensebotDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
SensebotDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while enrolling the Sensebot device : " + device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Modifying the Sensebot device enrollment data");
|
||||||
|
}
|
||||||
|
SensebotDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
SensebotDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
SensebotDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while updating the enrollment of the Sensebot device : " +
|
||||||
|
device.getDeviceIdentifier();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Dis-enrolling Sensebot device : " + deviceId);
|
||||||
|
}
|
||||||
|
SensebotDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.deleteIotDevice(deviceId.getId());
|
||||||
|
SensebotDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
SensebotDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg = "Error while removing the Sensebot device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
boolean isEnrolled = false;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Checking the enrollment of Sensebot device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice(
|
||||||
|
deviceId.getId());
|
||||||
|
if (iotDevice != null) {
|
||||||
|
isEnrolled = true;
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while checking the enrollment status of Sensebot device : " +
|
||||||
|
deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return isEnrolled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setActive(DeviceIdentifier deviceId, boolean status)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
|
||||||
|
Device device;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Getting the details of Sensebot device : " + deviceId.getId());
|
||||||
|
}
|
||||||
|
IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO().
|
||||||
|
getIotDevice(deviceId.getId());
|
||||||
|
device = IotDeviceManagementUtil.convertToDevice(iotDevice);
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching the Sensebot device : " + deviceId.getId();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
|
||||||
|
throws DeviceManagementException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
|
||||||
|
EnrolmentInfo.Status status) throws DeviceManagementException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
|
||||||
|
boolean status;
|
||||||
|
IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device);
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"updating the details of Sensebot device : " + deviceIdentifier);
|
||||||
|
}
|
||||||
|
SensebotDAO.beginTransaction();
|
||||||
|
status = iotDeviceManagementDAOFactory.getIotDeviceDAO()
|
||||||
|
.updateIotDevice(iotDevice);
|
||||||
|
SensebotDAO.commitTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
try {
|
||||||
|
SensebotDAO.rollbackTransaction();
|
||||||
|
} catch (IotDeviceManagementDAOException iotDAOEx) {
|
||||||
|
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||||
|
log.warn(msg, iotDAOEx);
|
||||||
|
}
|
||||||
|
String msg =
|
||||||
|
"Error while updating the Sensebot device : " + deviceIdentifier;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getAllDevices() throws DeviceManagementException {
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Fetching the details of all Sensebot devices");
|
||||||
|
}
|
||||||
|
List<IotDevice> iotDevices =
|
||||||
|
iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices();
|
||||||
|
if (iotDevices != null) {
|
||||||
|
devices = new ArrayList<Device>();
|
||||||
|
for (IotDevice iotDevice : iotDevices) {
|
||||||
|
devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IotDeviceManagementDAOException e) {
|
||||||
|
String msg = "Error while fetching all Sensebot devices.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new DeviceManagementException(msg, e);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.constants.SensebotConstants;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SensebotManagerService implements DeviceManagementService {
|
||||||
|
private DeviceManager deviceManager;
|
||||||
|
@Override
|
||||||
|
public String getType() {
|
||||||
|
return SensebotConstants.DEVICE_TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() throws DeviceManagementException {
|
||||||
|
deviceManager=new SensebotManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceManager getDeviceManager() {
|
||||||
|
return deviceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationManager getApplicationManager() {
|
||||||
|
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 installApplication(Operation operation, List<DeviceIdentifier> deviceIdentifiers)
|
||||||
|
throws ApplicationManagementException {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.constants.SensebotConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.dao.impl.SensebotDeviceDAOImpl;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class SensebotDAO extends IotDeviceManagementDAOFactory implements IotDeviceManagementDAOFactoryInterface {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(SensebotDAO.class);
|
||||||
|
static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
public SensebotDAO() {
|
||||||
|
initSensebotDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public IotDeviceDAO getIotDeviceDAO() {
|
||||||
|
return new SensebotDeviceDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initSensebotDAO() {
|
||||||
|
dataSource = getDataSourceMap().get(SensebotConstants.DEVICE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void beginTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection getConnection() throws IotDeviceManagementDAOException {
|
||||||
|
if (currentConnection.get() == null) {
|
||||||
|
try {
|
||||||
|
currentConnection.set(dataSource.getConnection());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentConnection.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void commitTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
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 (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeConnection() throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection con = currentConnection.get();
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void rollbackTransaction() throws IotDeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Datasource connection associated with the current thread is null, hence rollback "
|
||||||
|
+ "has not been attempted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,238 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.IotDeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dao.util.IotDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.util.iotdevice.dto.IotDevice;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.constants.SensebotConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.dao.SensebotDAO;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements IotDeviceDAO for sensebot Devices.
|
||||||
|
*/
|
||||||
|
public class SensebotDeviceDAOImpl implements IotDeviceDAO{
|
||||||
|
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(SensebotDeviceDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IotDevice getIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
IotDevice iotDevice = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
conn = SensebotDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT SENSEBOT_DEVICE_ID, DEVICE_NAME" +
|
||||||
|
" FROM SENSEBOT_DEVICE WHERE SENSEBOT_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(
|
||||||
|
SensebotConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sensebot device " + iotDeviceId + " data has been fetched from " +
|
||||||
|
"Sensebot database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching Sensebot device : '" + iotDeviceId + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
SensebotDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iotDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = SensebotDAO.getConnection();
|
||||||
|
String createDBQuery =
|
||||||
|
"INSERT INTO SENSEBOT_DEVICE(SENSEBOT_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(createDBQuery);
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceId());
|
||||||
|
stmt.setString(2,iotDevice.getIotDeviceName());
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sensebot device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" added to the Sensebot database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while adding the Sensebot device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' to the Sensebot db.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateIotDevice(IotDevice iotDevice)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = SensebotDAO.getConnection();
|
||||||
|
String updateDBQuery =
|
||||||
|
"UPDATE SENSEBOT_DEVICE SET DEVICE_NAME = ? WHERE SENSEBOT_DEVICE_ID = ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(updateDBQuery);
|
||||||
|
|
||||||
|
if (iotDevice.getDeviceProperties() == null) {
|
||||||
|
iotDevice.setDeviceProperties(new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
stmt.setString(1, iotDevice.getIotDeviceName());
|
||||||
|
|
||||||
|
stmt.setString(2, iotDevice.getIotDeviceId());
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sensebot device " + iotDevice.getIotDeviceId() + " data has been" +
|
||||||
|
" modified.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while modifying the Sensebot device '" +
|
||||||
|
iotDevice.getIotDeviceId() + "' data.";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteIotDevice(String iotDeviceId)
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
boolean status = false;
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = SensebotDAO.getConnection();
|
||||||
|
String deleteDBQuery =
|
||||||
|
"DELETE FROM SENSEBOT_DEVICE WHERE SENSEBOT_DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(deleteDBQuery);
|
||||||
|
stmt.setString(1, iotDeviceId);
|
||||||
|
int rows = stmt.executeUpdate();
|
||||||
|
if (rows > 0) {
|
||||||
|
status = true;
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Sensebot device " + iotDeviceId + " data has deleted" +
|
||||||
|
" from the Sensebot database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while deleting Sensebot device " + iotDeviceId;
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<IotDevice> getAllIotDevices()
|
||||||
|
throws IotDeviceManagementDAOException {
|
||||||
|
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
IotDevice iotDevice;
|
||||||
|
List<IotDevice> iotDevices = new ArrayList<IotDevice>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = SensebotDAO.getConnection();
|
||||||
|
String selectDBQuery =
|
||||||
|
"SELECT SENSEBOT_DEVICE_ID, DEVICE_NAME " +
|
||||||
|
"FROM SENSEBOT_DEVICE";
|
||||||
|
stmt = conn.prepareStatement(selectDBQuery);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
iotDevice = new IotDevice();
|
||||||
|
iotDevice.setIotDeviceId(resultSet.getString(SensebotConstants.DEVICE_PLUGIN_DEVICE_ID));
|
||||||
|
iotDevice.setIotDeviceName(resultSet.getString(SensebotConstants.DEVICE_PLUGIN_DEVICE_NAME));
|
||||||
|
|
||||||
|
Map<String, String> propertyMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
iotDevice.setDeviceProperties(propertyMap);
|
||||||
|
iotDevices.add(iotDevice);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("All Sensebot device details have fetched from Sensebot database.");
|
||||||
|
}
|
||||||
|
return iotDevices;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String msg = "Error occurred while fetching all Sensebot device data'";
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new IotDeviceManagementDAOException(msg, e);
|
||||||
|
} finally {
|
||||||
|
IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
SensebotDAO.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains utility methods used by Sensebot plugin.
|
||||||
|
*/
|
||||||
|
public class SensebotUtils {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(SensebotUtils.class);
|
||||||
|
|
||||||
|
public static String getDeviceProperty(Map<String, String> deviceProperties, String property) {
|
||||||
|
|
||||||
|
String deviceProperty = deviceProperties.get(property);
|
||||||
|
|
||||||
|
if (deviceProperty == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.internal;
|
||||||
|
|
||||||
|
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.device.mgt.common.spi.DeviceManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService;
|
||||||
|
import org.wso2.carbon.device.mgt.iot.sample.sensebot.plugin.impl.SensebotManagerService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.iot.sensebot.internal.SensebotManagementServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="wso2.carbon.device.mgt.iot.common.DeviceTypeService"
|
||||||
|
* interface="org.wso2.carbon.device.mgt.iot.common.service.DeviceTypeService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDeviceTypeService"
|
||||||
|
* unbind="unsetDeviceTypeService"
|
||||||
|
*/
|
||||||
|
public class SensebotManagementServiceComponent {
|
||||||
|
|
||||||
|
private ServiceRegistration sensebotServiceRegRef;
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(SensebotManagementServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Activating Sensebot Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
BundleContext bundleContext = ctx.getBundleContext();
|
||||||
|
|
||||||
|
sensebotServiceRegRef = bundleContext
|
||||||
|
.registerService(DeviceManagementService.class.getName(),
|
||||||
|
new SensebotManagerService(), null);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Sensebot Device Management Service Component has been successfully " +
|
||||||
|
"activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(
|
||||||
|
"Error occurred while activating Sensebot Device Management Service Component",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext ctx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("De-activating Sensebot Device Management Service Component");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (sensebotServiceRegRef != null) {
|
||||||
|
sensebotServiceRegRef.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug(
|
||||||
|
"Sensebot Device Management Service Component has been successfully " +
|
||||||
|
"de-activated");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while de-activating Sensebot Device Management bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
/* This is to avoid this component getting initialized before the
|
||||||
|
common registered */
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Data source service set to mobile service component");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user