From 0e47fbc48a9459bf92c1382edd7296ee47bfc58b Mon Sep 17 00:00:00 2001 From: Shabirmean Date: Wed, 20 Jan 2016 18:44:23 +0530 Subject: [PATCH] Refactored the RaspberryPi device-type components & features --- .../pom.xml | 4 +- .../impl/RaspberryPiControllerService.java} | 62 ++--- .../service/impl}/dto/DeviceJSON.java | 2 +- .../impl}/exception/RaspberrypiException.java | 2 +- .../transport/RaspberryPiMQTTConnector.java | 179 ++++++++++++++ .../impl}/util/RaspberrypiServiceUtils.java | 4 +- .../raspberrypi/service/dto/DeviceJSON.java | 36 --- .../transport/RaspberryPiMQTTSubscriber.java | 134 ---------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 15 +- .../pom.xml | 6 +- .../impl/RaspberryPiManagerService.java} | 59 +---- .../webapp/META-INF/webapp-classloading.xml | 0 .../src/main/webapp/WEB-INF/cxf-servlet.xml | 11 +- .../src/main/webapp/WEB-INF/web.xml | 0 .../exception/RaspberrypiException.java | 31 --- .../transport/RaspberryPiMQTTSubscriber.java | 134 ---------- .../service/util/RaspberrypiServiceUtils.java | 234 ------------------ components/device-mgt-iot-raspberrypi/pom.xml | 2 +- .../VirtualFireAlarmMQTTConnector.java | 1 - .../pom.xml | 4 +- pom.xml | 2 +- 21 files changed, 228 insertions(+), 694 deletions(-) rename components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/{service/RaspberryPiService.java => controller/service/impl/RaspberryPiControllerService.java} (83%) rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service => org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl}/dto/DeviceJSON.java (93%) rename components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/{service => controller/service/impl}/exception/RaspberrypiException.java (92%) create mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java rename components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/{service => controller/service/impl}/util/RaspberrypiServiceUtils.java (97%) delete mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java delete mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl => org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl}/pom.xml (94%) rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java => org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/manager/service/impl/RaspberryPiManagerService.java} (88%) rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl => org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl}/src/main/webapp/META-INF/webapp-classloading.xml (100%) rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl => org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl}/src/main/webapp/WEB-INF/cxf-servlet.xml (79%) rename components/device-mgt-iot-raspberrypi/{org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl => org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl}/src/main/webapp/WEB-INF/web.xml (100%) delete mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java delete mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java delete mode 100644 components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/pom.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/pom.xml index 3e778e17f0..2de29d034a 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/pom.xml +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/pom.xml @@ -32,8 +32,8 @@ org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl 1.9.2-SNAPSHOT war - WSO2 Carbon - IoT Server RaspberryPi API - WSO2 Carbon - RaspberryPi Service API Implementation + WSO2 Carbon - IoT Server RaspberryPi Controller API + WSO2 Carbon - RaspberryPi Controller Service API Implementation http://wso2.org diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/RaspberryPiControllerService.java similarity index 83% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/RaspberryPiControllerService.java index 0167576357..9b3c63ce96 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/RaspberryPiControllerService.java @@ -16,37 +16,24 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.raspberrypi.service; +package org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl; -import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.device.DeviceType; import org.wso2.carbon.apimgt.annotations.device.feature.Feature; -import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil; -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.DeviceManagement; import org.wso2.carbon.device.mgt.iot.DeviceValidator; -import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo; -import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient; import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppAccount; -import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; -import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppServerClient; -import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException; import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; +import org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.transport.RaspberryPiMQTTConnector; import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.dto.DeviceJSON; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.transport.RaspberryPiMQTTSubscriber; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.util.RaspberrypiServiceUtils; +import org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.dto.DeviceJSON; +import org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.util.RaspberrypiServiceUtils; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord; -import org.wso2.carbon.device.mgt.iot.util.ZipArchive; -import org.wso2.carbon.device.mgt.iot.util.ZipUtil; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -54,21 +41,14 @@ import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @API(name = "raspberrypi", version = "1.0.0", context = "/raspberrypi") @DeviceType(value = "raspberrypi") -public class RaspberryPiService { +public class RaspberryPiControllerService { - private static Log log = LogFactory.getLog(RaspberryPiService.class); + private static Log log = LogFactory.getLog(RaspberryPiControllerService.class); //TODO; replace this tenant domain private static final String SUPER_TENANT = "carbon.super"; @@ -80,35 +60,25 @@ public class RaspberryPiService { public static final String MQTT_PROTOCOL = "MQTT"; private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); - private RaspberryPiMQTTSubscriber raspberryPiMQTTSubscriber; + private RaspberryPiMQTTConnector raspberryPiMQTTConnector; /** - * @param raspberryPiMQTTSubscriber + * @param raspberryPiMQTTConnector */ - public void setRaspberryPiMQTTSubscriber(final RaspberryPiMQTTSubscriber raspberryPiMQTTSubscriber) { - this.raspberryPiMQTTSubscriber = raspberryPiMQTTSubscriber; - /*if (MqttConfig.getInstance().isEnabled()) { - Runnable xmppStarter = new Runnable() { - @Override - public void run() { - raspberryPiMQTTSubscriber.initConnector(); - raspberryPiMQTTSubscriber.connectAndSubscribe(); - } - }; - - Thread xmppStarterThread = new Thread(xmppStarter); - xmppStarterThread.setDaemon(true); - xmppStarterThread.start(); + public void setRaspberryPiMQTTConnector(final RaspberryPiMQTTConnector raspberryPiMQTTConnector) { + this.raspberryPiMQTTConnector = raspberryPiMQTTConnector; + if (MqttConfig.getInstance().isEnabled()) { + raspberryPiMQTTConnector.connect(); } else { - log.warn("MQTT disabled in 'devicemgt-config.xml"); - }*/ + log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, RaspberryPiMQTTConnector not started."); + } } /** * @return */ - public RaspberryPiMQTTSubscriber getRaspberryPiMQTTSubscriber() { - return raspberryPiMQTTSubscriber; + public RaspberryPiMQTTConnector getRaspberryPiMQTTConnector() { + return raspberryPiMQTTConnector; } diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/dto/DeviceJSON.java similarity index 93% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/dto/DeviceJSON.java index c8695d707d..4163dfee4b 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/dto/DeviceJSON.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.raspberrypi.service.dto; +package org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.dto; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/exception/RaspberrypiException.java similarity index 92% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/exception/RaspberrypiException.java index 21063ee9dc..26fa4d538f 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/exception/RaspberrypiException.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.raspberrypi.service.exception; +package org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.exception; public class RaspberrypiException extends Exception { private static final long serialVersionUID = 118512086957330189L; diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java new file mode 100644 index 0000000000..be2da09251 --- /dev/null +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/transport/RaspberryPiMQTTConnector.java @@ -0,0 +1,179 @@ +/* + * 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.raspberrypi.controller.service.impl.transport; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.iot.config.server.DeviceManagementConfigurationManager; +import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; +import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttSubscriber; +import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; +import org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.util.RaspberrypiServiceUtils; +import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; +import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException; +import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler; + +import java.io.File; +import java.util.Calendar; +import java.util.UUID; + +public class RaspberryPiMQTTConnector extends MQTTTransportHandler { + private static Log log = LogFactory.getLog(RaspberryPiMQTTConnector.class); + + private static final String serverName = + DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); + + private static final String subscribeTopic = + serverName + File.separator + "+" + File.separator + RaspberrypiConstants.DEVICE_TYPE + + File.separator + "+" + File.separator + "publisher"; + + private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); + + private RaspberryPiMQTTConnector() { + super(iotServerSubscriber, RaspberrypiConstants.DEVICE_TYPE, + MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic); + } + + @Override + public void connect() { + Runnable connector = new Runnable() { + public void run() { + while (!isConnected()) { + try { + String brokerUsername = MqttConfig.getInstance().getMqttQueueUsername(); + String brokerPassword = MqttConfig.getInstance().getMqttQueuePassword(); + setUsernameAndPassword(brokerUsername, brokerPassword); + connectToQueue(); + } catch (TransportHandlerException e) { + log.error("Connection to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e); + try { + Thread.sleep(timeoutInterval); + } catch (InterruptedException ex) { + log.error("MQTT-Connector: Thread Sleep Interrupt Exception.", ex); + } + } + + try { + subscribeToQueue(); + } catch (TransportHandlerException e) { + log.warn("Subscription to MQTT Broker at: " + mqttBrokerEndPoint + " failed", e); + } + } + } + }; + + Thread connectorThread = new Thread(connector); + connectorThread.setDaemon(true); + connectorThread.start(); + } + + @Override + public void processIncomingMessage(MqttMessage message, String... messageParams) throws TransportHandlerException { + String topic = messageParams[0]; + // owner and the deviceId are extracted from the MQTT topic to which the messgae was received. + String ownerAndId = topic.replace(serverName + File.separator, ""); + ownerAndId = ownerAndId.replace(File.separator + RaspberrypiConstants.DEVICE_TYPE + File.separator, ":"); + ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); + + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; + String receivedMessage = message.toString(); + + if (log.isDebugEnabled()) { + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); + log.debug("Message [" + receivedMessage + "] topic: [" + topic + "]"); + } + + if (receivedMessage.contains("PUBLISHER")) { + float temperature = Float.parseFloat(receivedMessage.split(":")[2]); + + if (!RaspberrypiServiceUtils.publishToDAS(owner, deviceId, temperature)) { + log.error("MQTT Subscriber: Publishing data to DAS failed."); + } + + if (log.isDebugEnabled()) { + log.debug("MQTT Subscriber: Published data to DAS successfully."); + } + + } else if (receivedMessage.contains("TEMPERATURE")) { + String temperatureValue = receivedMessage.split(":")[1]; + SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE, + temperatureValue, + Calendar.getInstance().getTimeInMillis()); + } + } + + @Override + public void disconnect() { + Runnable stopConnection = new Runnable() { + public void run() { + while (isConnected()) { + try { + closeConnection(); + } catch (MqttException e) { + if (log.isDebugEnabled()) { + log.warn("Unable to 'STOP' MQTT connection at broker at: " + mqttBrokerEndPoint + + " for device-type - " + RaspberrypiConstants.DEVICE_TYPE, e); + } + + try { + Thread.sleep(timeoutInterval); + } catch (InterruptedException e1) { + log.error("MQTT-Terminator: Thread Sleep Interrupt Exception at device-type - " + + RaspberrypiConstants.DEVICE_TYPE, e1); + } + } + } + } + }; + + Thread terminatorThread = new Thread(stopConnection); + terminatorThread.setDaemon(true); + terminatorThread.start(); + } + + @Override + public void processIncomingMessage() throws TransportHandlerException { + + } + + @Override + public void processIncomingMessage(MqttMessage message) throws TransportHandlerException { + + } + + @Override + public void publishDeviceData() throws TransportHandlerException { + + } + + @Override + public void publishDeviceData(MqttMessage publishData) throws TransportHandlerException { + + } + + @Override + public void publishDeviceData(String... publishData) throws TransportHandlerException { + + } +} + diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/util/RaspberrypiServiceUtils.java similarity index 97% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/util/RaspberrypiServiceUtils.java index 61bbfc02f3..213e871404 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/controller/service/impl/util/RaspberrypiServiceUtils.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.raspberrypi.service.util; +package org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -29,8 +29,6 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException; import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.iot.DeviceController; -import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; import javax.ws.rs.HttpMethod; diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java deleted file mode 100644 index c8695d707d..0000000000 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/dto/DeviceJSON.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.raspberrypi.service.dto; - -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 float value; -} diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java deleted file mode 100644 index 2de9b9a257..0000000000 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.raspberrypi.service.transport; - -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.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.iot.config.server.DeviceManagementConfigurationManager; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttSubscriber; -import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.util.RaspberrypiServiceUtils; -import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; - -import java.io.File; -import java.util.Calendar; -import java.util.UUID; - -public class RaspberryPiMQTTSubscriber extends MqttSubscriber { - private static Log log = LogFactory.getLog(RaspberryPiMQTTSubscriber.class); - - private static final String serverName = - DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); - private static final String subscribeTopic = - serverName + File.separator + "+" + File.separator + RaspberrypiConstants.DEVICE_TYPE + - File.separator + "+" + File.separator + "publisher"; - - private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); - private String mqttEndpoint; - - private RaspberryPiMQTTSubscriber() { - super(iotServerSubscriber, RaspberrypiConstants.DEVICE_TYPE, - MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic); - } - - public void initConnector() { - mqttEndpoint = MqttConfig.getInstance().getMqttQueueEndpoint(); - } - - public void connectAndSubscribe() { - try { - super.connectAndSubscribe(); - } catch (DeviceManagementException e) { - log.error("Subscription to MQTT Broker at: " + mqttEndpoint + " failed"); - retryMQTTSubscription(); - } - } - - @Override - protected void postMessageArrived(String topic, MqttMessage mqttMessage) { - String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + RaspberrypiConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); - - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; - String receivedMessage = mqttMessage.toString(); - - if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: {OWNER-" + owner + "} & {DEVICE.ID-" + deviceId + "}"); - log.debug("MQTT: Received Message [" + receivedMessage + "] topic: [" + topic + "]"); - } - - if (receivedMessage.contains("PUBLISHER")) { - float temperature = Float.parseFloat(receivedMessage.split(":")[2]); - - if (!RaspberrypiServiceUtils.publishToDAS(owner, deviceId, temperature)) { - log.error("MQTT Subscriber: Publishing data to DAS failed."); - } - - if (log.isDebugEnabled()) { - log.debug("MQTT Subscriber: Published data to DAS successfully."); - } - - } else if (receivedMessage.contains("TEMPERATURE")) { - String temperatureValue = receivedMessage.split(":")[1]; - SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE, - temperatureValue, - Calendar.getInstance().getTimeInMillis()); - } - } - - private void retryMQTTSubscription() { - Thread retryToSubscribe = new Thread() { - @Override - public void run() { - while (true) { - if (!isConnected()) { - if (log.isDebugEnabled()) { - log.debug("Subscriber re-trying to reach MQTT queue...."); - } - - try { - RaspberryPiMQTTSubscriber.super.connectAndSubscribe(); - } catch (DeviceManagementException e1) { - if (log.isDebugEnabled()) { - log.debug("Attempt to re-connect to MQTT-Queue failed"); - } - } - } else { - break; - } - - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - log.error("MQTT: Thread S;eep Interrupt Exception"); - } - } - } - }; - - retryToSubscribe.setDaemon(true); - retryToSubscribe.start(); - } -} - diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml index 78b5061a0b..0ccfb9745e 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -25,14 +25,11 @@ 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"> - - - - + - - + + @@ -40,8 +37,8 @@ - + diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/pom.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/pom.xml similarity index 94% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/pom.xml rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/pom.xml index 0bc06dc399..3ae27fe1a2 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/pom.xml +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/pom.xml @@ -29,11 +29,11 @@ 4.0.0 - org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl + org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl 1.9.2-SNAPSHOT war - WSO2 Carbon - IoT Server RaspberryPi API - WSO2 Carbon - RaspberryPi Service API Implementation + WSO2 Carbon - IoT Server RaspberryPi Manager API + WSO2 Carbon - RaspberryPi Manager Service API Implementation http://wso2.org diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/manager/service/impl/RaspberryPiManagerService.java similarity index 88% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/manager/service/impl/RaspberryPiManagerService.java index b2cffe1996..c58a2f3c10 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/RaspberryPiService.java +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/manager/service/impl/RaspberryPiManagerService.java @@ -16,41 +16,39 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.raspberrypi.service; +package org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.device.DeviceType; -import org.wso2.carbon.apimgt.annotations.device.feature.Feature; import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil; 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.DeviceManagement; -import org.wso2.carbon.device.mgt.iot.DeviceValidator; import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo; import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppAccount; import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig; import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppServerClient; import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException; import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.dto.DeviceJSON; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.transport.RaspberryPiMQTTSubscriber; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.util.RaspberrypiServiceUtils; -import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; -import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord; import org.wso2.carbon.device.mgt.iot.util.ZipArchive; import org.wso2.carbon.device.mgt.iot.util.ZipUtil; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.*; +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.MediaType; import javax.ws.rs.core.Response; @@ -58,16 +56,15 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @DeviceType(value = "raspberrypi") -public class RaspberryPiService { +public class RaspberryPiManagerService { - private static Log log = LogFactory.getLog(RaspberryPiService.class); + private static Log log = LogFactory.getLog(RaspberryPiManagerService.class); //TODO; replace this tenant domain private static final String SUPER_TENANT = "carbon.super"; @@ -79,38 +76,6 @@ public class RaspberryPiService { public static final String MQTT_PROTOCOL = "MQTT"; private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); - private RaspberryPiMQTTSubscriber raspberryPiMQTTSubscriber; - - /** - * @param raspberryPiMQTTSubscriber - */ - public void setRaspberryPiMQTTSubscriber( - final RaspberryPiMQTTSubscriber raspberryPiMQTTSubscriber) { - this.raspberryPiMQTTSubscriber = raspberryPiMQTTSubscriber; - - if (MqttConfig.getInstance().isEnabled()) { - Runnable xmppStarter = new Runnable() { - @Override - public void run() { - raspberryPiMQTTSubscriber.initConnector(); - raspberryPiMQTTSubscriber.connectAndSubscribe(); - } - }; - - Thread xmppStarterThread = new Thread(xmppStarter); - xmppStarterThread.setDaemon(true); - xmppStarterThread.start(); - } else { - log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started."); - } - } - - /** - * @return - */ - public RaspberryPiMQTTSubscriber getRaspberryPiMQTTSubscriber() { - return raspberryPiMQTTSubscriber; - } /* --------------------------------------------------------------------------------------- Device management specific APIs diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/META-INF/webapp-classloading.xml similarity index 100% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/META-INF/webapp-classloading.xml rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/META-INF/webapp-classloading.xml diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml similarity index 79% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml index 78b5061a0b..48216c9b82 100644 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -28,11 +28,10 @@ - + - - + @@ -40,9 +39,5 @@ - - - diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/webapp/WEB-INF/web.xml rename to components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl/src/main/webapp/WEB-INF/web.xml diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java deleted file mode 100644 index 21063ee9dc..0000000000 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/exception/RaspberrypiException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.raspberrypi.service.exception; - -public class RaspberrypiException extends Exception { - private static final long serialVersionUID = 118512086957330189L; - - public RaspberrypiException(String errorMessage) { - super(errorMessage); - } - - public RaspberrypiException(String errorMessage, Throwable throwable) { - super(errorMessage, throwable); - } -} diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java deleted file mode 100644 index 2de9b9a257..0000000000 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/transport/RaspberryPiMQTTSubscriber.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.raspberrypi.service.transport; - -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.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.iot.config.server.DeviceManagementConfigurationManager; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttSubscriber; -import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; -import org.wso2.carbon.device.mgt.iot.raspberrypi.service.util.RaspberrypiServiceUtils; -import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager; - -import java.io.File; -import java.util.Calendar; -import java.util.UUID; - -public class RaspberryPiMQTTSubscriber extends MqttSubscriber { - private static Log log = LogFactory.getLog(RaspberryPiMQTTSubscriber.class); - - private static final String serverName = - DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); - private static final String subscribeTopic = - serverName + File.separator + "+" + File.separator + RaspberrypiConstants.DEVICE_TYPE + - File.separator + "+" + File.separator + "publisher"; - - private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); - private String mqttEndpoint; - - private RaspberryPiMQTTSubscriber() { - super(iotServerSubscriber, RaspberrypiConstants.DEVICE_TYPE, - MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic); - } - - public void initConnector() { - mqttEndpoint = MqttConfig.getInstance().getMqttQueueEndpoint(); - } - - public void connectAndSubscribe() { - try { - super.connectAndSubscribe(); - } catch (DeviceManagementException e) { - log.error("Subscription to MQTT Broker at: " + mqttEndpoint + " failed"); - retryMQTTSubscription(); - } - } - - @Override - protected void postMessageArrived(String topic, MqttMessage mqttMessage) { - String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, ""); - ownerAndId = ownerAndId.replace(File.separator + RaspberrypiConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "publisher", ""); - - String owner = ownerAndId.split(":")[0]; - String deviceId = ownerAndId.split(":")[1]; - String receivedMessage = mqttMessage.toString(); - - if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: {OWNER-" + owner + "} & {DEVICE.ID-" + deviceId + "}"); - log.debug("MQTT: Received Message [" + receivedMessage + "] topic: [" + topic + "]"); - } - - if (receivedMessage.contains("PUBLISHER")) { - float temperature = Float.parseFloat(receivedMessage.split(":")[2]); - - if (!RaspberrypiServiceUtils.publishToDAS(owner, deviceId, temperature)) { - log.error("MQTT Subscriber: Publishing data to DAS failed."); - } - - if (log.isDebugEnabled()) { - log.debug("MQTT Subscriber: Published data to DAS successfully."); - } - - } else if (receivedMessage.contains("TEMPERATURE")) { - String temperatureValue = receivedMessage.split(":")[1]; - SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE, - temperatureValue, - Calendar.getInstance().getTimeInMillis()); - } - } - - private void retryMQTTSubscription() { - Thread retryToSubscribe = new Thread() { - @Override - public void run() { - while (true) { - if (!isConnected()) { - if (log.isDebugEnabled()) { - log.debug("Subscriber re-trying to reach MQTT queue...."); - } - - try { - RaspberryPiMQTTSubscriber.super.connectAndSubscribe(); - } catch (DeviceManagementException e1) { - if (log.isDebugEnabled()) { - log.debug("Attempt to re-connect to MQTT-Queue failed"); - } - } - } else { - break; - } - - try { - Thread.sleep(5000); - } catch (InterruptedException e1) { - log.error("MQTT: Thread S;eep Interrupt Exception"); - } - } - } - }; - - retryToSubscribe.setDaemon(true); - retryToSubscribe.start(); - } -} - diff --git a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java b/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java deleted file mode 100644 index 56fe46dabb..0000000000 --- a/components/device-mgt-iot-raspberrypi/org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/util/RaspberrypiServiceUtils.java +++ /dev/null @@ -1,234 +0,0 @@ -/* - * 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.raspberrypi.service.util; - -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.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException; -import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.iot.DeviceController; -import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; -import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants; - -import javax.ws.rs.HttpMethod; -import java.io.BufferedReader; -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.CountDownLatch; -import java.util.concurrent.Future; - -public class RaspberrypiServiceUtils { - private static final Log log = LogFactory.getLog(RaspberrypiServiceUtils.class); - - //TODO; replace this tenant domain - private static final String SUPER_TENANT = "carbon.super"; - private static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature"; - - public static String sendCommandViaHTTP(final String deviceHTTPEndpoint, String urlContext, - boolean fireAndForgot) throws DeviceManagementException { - - String responseMsg = ""; - String urlString = RaspberrypiConstants.URL_PREFIX + deviceHTTPEndpoint + urlContext; - - 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 future = httpclient.execute( - request, new FutureCallback() { - @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; - } - - - public static boolean sendCommandViaMQTT(String deviceOwner, String deviceId, String resource, - String state) throws DeviceManagementException { - - boolean result; - DeviceController deviceController = new DeviceController(); - - try { - result = deviceController.publishMqttControl(deviceOwner, RaspberrypiConstants.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; - } - - /* --------------------------------------------------------------------------------------- - Utility methods relevant to creating and sending http requests - --------------------------------------------------------------------------------------- */ - - /* This methods creates and returns a http connection object */ - - public static HttpURLConnection getHttpConnection(String urlString) throws - DeviceManagementException { - - URL connectionUrl = null; - HttpURLConnection httpConnection; - - 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 */ - - public static String readResponseFromGetRequest(HttpURLConnection httpConnection) - throws DeviceManagementException { - BufferedReader bufferedReader; - 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; - StringBuilder completeResponse = new StringBuilder(); - - 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(); - } - - public static boolean publishToDAS(String owner, String deviceId, float temperature) { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - ctx.setTenantDomain(SUPER_TENANT, true); - DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService( - DeviceAnalyticsService.class, null); - Object metdaData[] = {owner, RaspberrypiConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()}; - Object payloadData[] = {temperature}; - - try { - deviceAnalyticsService.publishEvent(TEMPERATURE_STREAM_DEFINITION, "1.0.0", metdaData, new Object[0], payloadData); - } catch (DataPublisherConfigurationException e) { - return false; - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - return true; - } -} diff --git a/components/device-mgt-iot-raspberrypi/pom.xml b/components/device-mgt-iot-raspberrypi/pom.xml index 668edbae63..41315ebffb 100644 --- a/components/device-mgt-iot-raspberrypi/pom.xml +++ b/components/device-mgt-iot-raspberrypi/pom.xml @@ -37,7 +37,7 @@ org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl - org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl + org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl org.wso2.carbon.device.mgt.iot.raspberrypi.controller.service.impl diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmMQTTConnector.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmMQTTConnector.java index 38575c59e6..620ca3c757 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmMQTTConnector.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/transport/VirtualFireAlarmMQTTConnector.java @@ -109,7 +109,6 @@ public class VirtualFireAlarmMQTTConnector extends MQTTTransportHandler { } }; - Thread connectorThread = new Thread(connector); connectorThread.setDaemon(true); connectorThread.start(); diff --git a/features/device-mgt-iot-raspberrypi-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml b/features/device-mgt-iot-raspberrypi-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml index a24c3bccc1..4cd281f3c4 100644 --- a/features/device-mgt-iot-raspberrypi-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml +++ b/features/device-mgt-iot-raspberrypi-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/pom.xml @@ -44,7 +44,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl + org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl war @@ -108,7 +108,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl + org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl war true diff --git a/pom.xml b/pom.xml index 3a3b614aae..fe97a1f3c7 100644 --- a/pom.xml +++ b/pom.xml @@ -470,7 +470,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.raspberrypi.mgt.service.impl + org.wso2.carbon.device.mgt.iot.raspberrypi.manager.service.impl ${carbon.iot.device.mgt.version} war