From 4b78aafef8912ce71b1741b55a782595cbe6e1a6 Mon Sep 17 00:00:00 2001 From: Shabirmean Date: Wed, 20 Jan 2016 14:39:14 +0530 Subject: [PATCH] Refactored the Arduino device-type component & feature --- .../pom.xml | 4 +- .../impl/ArduinoControllerService.java} | 65 ++---- .../service/impl}/dto/DeviceJSON.java | 2 +- .../impl}/exception/ArduinoException.java | 2 +- .../impl/transport/ArduinoMQTTConnector.java | 199 ++++++++++++++++++ .../impl}/util/ArduinoServiceUtils.java | 4 +- .../transport/ArduinoMQTTSubscriber.java | 146 ------------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 12 +- .../pom.xml | 6 +- .../service/impl/ArduinoManagerService.java} | 9 +- .../webapp/META-INF/webapp-classloading.xml | 0 .../src/main/webapp/WEB-INF/cxf-servlet.xml | 6 +- .../src/main/webapp/WEB-INF/web.xml | 0 components/device-mgt-iot-arduino/pom.xml | 2 +- .../VirtualFireAlarmMQTTConnector.java | 2 +- .../pom.xml | 4 +- pom.xml | 2 +- 17 files changed, 243 insertions(+), 222 deletions(-) rename components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/{service/ArduinoService.java => controller/service/impl/ArduinoControllerService.java} (81%) rename components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/{service => controller/service/impl}/dto/DeviceJSON.java (93%) rename components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/{service => controller/service/impl}/exception/ArduinoException.java (92%) create mode 100644 components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java rename components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/{service => controller/service/impl}/util/ArduinoServiceUtils.java (97%) delete mode 100644 components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/transport/ArduinoMQTTSubscriber.java rename components/device-mgt-iot-arduino/{org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl => org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl}/pom.xml (95%) rename components/device-mgt-iot-arduino/{org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java => org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/manager/service/impl/ArduinoManagerService.java} (98%) rename components/device-mgt-iot-arduino/{org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl => org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl}/src/main/webapp/META-INF/webapp-classloading.xml (100%) rename components/device-mgt-iot-arduino/{org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl => org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl}/src/main/webapp/WEB-INF/cxf-servlet.xml (87%) rename components/device-mgt-iot-arduino/{org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl => org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl}/src/main/webapp/WEB-INF/web.xml (100%) diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/pom.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/pom.xml index 6965fad463..cea548cb70 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/pom.xml +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/pom.xml @@ -32,8 +32,8 @@ org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl 1.9.2-SNAPSHOT war - WSO2 Carbon - IoT Server Arduino API - WSO2 Carbon - Arduino Service API Implementation + WSO2 Carbon - IoT Server Arduino Controller API + WSO2 Carbon - Arduino Service Controller API Implementation http://wso2.org diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/ArduinoControllerService.java similarity index 81% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/ArduinoControllerService.java index b29a0ffa32..53b4f69d98 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/ArduinoControllerService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -11,12 +11,12 @@ * 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 + * 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.arduino.service; +package org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.logging.Log; @@ -24,26 +24,14 @@ 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.arduino.plugin.constants.ArduinoConstants; -import org.wso2.carbon.device.mgt.iot.arduino.service.dto.DeviceJSON; -import org.wso2.carbon.device.mgt.iot.arduino.service.transport.ArduinoMQTTSubscriber; -import org.wso2.carbon.device.mgt.iot.arduino.service.util.ArduinoServiceUtils; +import org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.dto.DeviceJSON; +import org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.transport.ArduinoMQTTConnector; +import org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.util.ArduinoServiceUtils; import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig; -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.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; @@ -51,24 +39,18 @@ import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Calendar; -import java.util.Date; import java.util.HashMap; import java.util.LinkedList; -import java.util.List; import java.util.Map; import java.util.NoSuchElementException; -import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @API( name="arduino", version="1.0.0", context="/arduino") @DeviceType( value = "arduino") -public class ArduinoService { +public class ArduinoControllerService { - private static Log log = LogFactory.getLog(ArduinoService.class); + private static Log log = LogFactory.getLog(ArduinoControllerService.class); //TODO; replace this tenant domain private static final String SUPER_TENANT = "carbon.super"; @@ -79,42 +61,31 @@ public class ArduinoService { public static final String HTTP_PROTOCOL = "HTTP"; public static final String MQTT_PROTOCOL = "MQTT"; - private ArduinoMQTTSubscriber arduinoMQTTSubscriber; + private ArduinoMQTTConnector arduinoMQTTConnector; private static Map> replyMsgQueue = new HashMap<>(); private static Map> internalControlsQueue = new HashMap<>(); private ConcurrentHashMap deviceToIpMap = new ConcurrentHashMap<>(); /** - * @param arduinoMQTTSubscriber an object of type "ArduinoMQTTSubscriber" specific for this ArduinoService + * @param arduinoMQTTConnector an object of type "ArduinoMQTTConnector" specific for this ArduinoControllerService */ @SuppressWarnings("unused") - public void setArduinoMQTTSubscriber( - final ArduinoMQTTSubscriber arduinoMQTTSubscriber) { - this.arduinoMQTTSubscriber = arduinoMQTTSubscriber; - + public void setArduinoMQTTConnector( + final ArduinoMQTTConnector arduinoMQTTConnector) { + this.arduinoMQTTConnector = arduinoMQTTConnector; if (MqttConfig.getInstance().isEnabled()) { - Runnable xmppStarter = new Runnable() { - @Override - public void run() { - arduinoMQTTSubscriber.initConnector(); - arduinoMQTTSubscriber.connectAndSubscribe(); - } - }; - - Thread xmppStarterThread = new Thread(xmppStarter); - xmppStarterThread.setDaemon(true); - xmppStarterThread.start(); + arduinoMQTTConnector.connect(); } else { - log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, ArduinoMQTTSubscriber not started."); + log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, ArduinoMQTTConnector not started."); } } /** - * @return the "ArduinoMQTTSubscriber" object of this ArduinoService instance + * @return the "ArduinoMQTTConnector" object of this ArduinoControllerService instance */ @SuppressWarnings("unused") - public ArduinoMQTTSubscriber getArduinoMQTTSubscriber() { - return arduinoMQTTSubscriber; + public ArduinoMQTTConnector getArduinoMQTTConnector() { + return arduinoMQTTConnector; } /** diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/dto/DeviceJSON.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/dto/DeviceJSON.java similarity index 93% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/dto/DeviceJSON.java rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/dto/DeviceJSON.java index 1be336bbde..84b15f07de 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/dto/DeviceJSON.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/dto/DeviceJSON.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.arduino.service.dto; +package org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.dto; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/exception/ArduinoException.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/exception/ArduinoException.java similarity index 92% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/exception/ArduinoException.java rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/exception/ArduinoException.java index ee81f9798e..82766a0dbc 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/exception/ArduinoException.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/exception/ArduinoException.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.arduino.service.exception; +package org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.exception; public class ArduinoException extends Exception { private static final long serialVersionUID = 118512086957330189L; diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java new file mode 100644 index 0000000000..60676875a3 --- /dev/null +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/transport/ArduinoMQTTConnector.java @@ -0,0 +1,199 @@ +/* + * 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.arduino.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.iot.arduino.plugin.constants.ArduinoConstants; +import org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.ArduinoControllerService; +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.transport.TransportHandlerException; +import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler; + +import java.io.File; +import java.util.LinkedList; +import java.util.UUID; + +public class ArduinoMQTTConnector extends MQTTTransportHandler { + private static Log log = LogFactory.getLog(ArduinoMQTTConnector.class); + + private static final String serverName = + DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); + + private static final String subscribeTopic = + serverName + File.separator + "+" + File.separator + ArduinoConstants.DEVICE_TYPE + File.separator + "#"; + + private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); + + private static final String MESSAGE_TO_SEND = "IN"; + private static final String MESSAGE_RECEIVED = "OUT"; + + private ArduinoMQTTConnector() { + super(iotServerSubscriber, ArduinoConstants.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 + ArduinoConstants.DEVICE_TYPE + File.separator, ":"); + + String owner = ownerAndId.split(":")[0]; + String deviceId = ownerAndId.split(":")[1]; + + if (log.isDebugEnabled()) { + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); + } + + int lastIndex = message.toString().lastIndexOf(":"); + String msgContext = message.toString().substring(lastIndex + 1); + + LinkedList deviceControlList; + LinkedList replyMessageList; + + if (msgContext.equals(MESSAGE_TO_SEND) || msgContext.equals(ArduinoConstants.STATE_ON) || msgContext.equals( + ArduinoConstants.STATE_OFF)) { + + if (log.isDebugEnabled()) { + log.debug("Received a control message: "); + log.debug("Control message topic: " + topic); + log.debug("Control message: " + message.toString()); + } + + synchronized (ArduinoControllerService.getInternalControlsQueue()) { + deviceControlList = ArduinoControllerService.getInternalControlsQueue().get(deviceId); + if (deviceControlList == null) { + ArduinoControllerService.getInternalControlsQueue() + .put(deviceId, deviceControlList = new LinkedList()); + } + } + deviceControlList.add(message.toString()); + + } else if (msgContext.equals(MESSAGE_RECEIVED)) { + + if (log.isDebugEnabled()) { + log.debug("Received reply from a device: "); + log.debug("Reply message topic: " + topic); + log.debug("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<>()); + } + } + replyMessageList.add(message.toString()); + } + } + + @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 - " + ArduinoConstants.DEVICE_TYPE, e); + } + + try { + Thread.sleep(timeoutInterval); + } catch (InterruptedException e1) { + log.error("MQTT-Terminator: Thread Sleep Interrupt Exception at device-type - " + + ArduinoConstants.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-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/util/ArduinoServiceUtils.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/util/ArduinoServiceUtils.java similarity index 97% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/util/ArduinoServiceUtils.java rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/util/ArduinoServiceUtils.java index 35d86b01df..8ab6683275 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/util/ArduinoServiceUtils.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/controller/service/impl/util/ArduinoServiceUtils.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.iot.arduino.service.util; +package org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -29,9 +29,7 @@ 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.arduino.plugin.constants.ArduinoConstants; -import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException; import javax.ws.rs.HttpMethod; import java.io.BufferedReader; diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/transport/ArduinoMQTTSubscriber.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/transport/ArduinoMQTTSubscriber.java deleted file mode 100644 index 0edb99a16d..0000000000 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/transport/ArduinoMQTTSubscriber.java +++ /dev/null @@ -1,146 +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.arduino.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.arduino.plugin.constants.ArduinoConstants; -import org.wso2.carbon.device.mgt.iot.arduino.service.ArduinoService; -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 java.io.File; -import java.util.LinkedList; -import java.util.UUID; - -public class ArduinoMQTTSubscriber extends MqttSubscriber { - private static Log log = LogFactory.getLog(ArduinoMQTTSubscriber.class); - - private static final String serverName = - DeviceManagementConfigurationManager.getInstance().getDeviceManagementServerInfo().getName(); - private static final String subscribeTopic = - serverName + File.separator + "+" + File.separator + ArduinoConstants.DEVICE_TYPE + File.separator + "#"; - - - private static final String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5); - private static String mqttEndpoint; - - private ArduinoMQTTSubscriber() { - super(iotServerSubscriber, ArduinoConstants.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 message) { - int lastIndex = topic.lastIndexOf("/"); - String deviceId = topic.substring(lastIndex + 1); - - lastIndex = message.toString().lastIndexOf(":"); - String msgContext = message.toString().substring(lastIndex + 1); - - LinkedList deviceControlList = null; - LinkedList replyMessageList = null; - - if (msgContext.equals("IN") || msgContext.equals(ArduinoConstants.STATE_ON) || msgContext.equals( - ArduinoConstants.STATE_OFF)) { - - if (log.isDebugEnabled()) { - log.debug("Received a control message: "); - log.debug("Control message topic: " + topic); - log.debug("Control message: " + message.toString()); - } - - synchronized (ArduinoService.getInternalControlsQueue()) { - deviceControlList = ArduinoService.getInternalControlsQueue().get(deviceId); - if (deviceControlList == null) { - ArduinoService.getInternalControlsQueue() - .put(deviceId, deviceControlList = new LinkedList()); - } - } - deviceControlList.add(message.toString()); - - } else if (msgContext.equals("OUT")) { - - if (log.isDebugEnabled()) { - log.debug("Recieved reply from a device: "); - log.debug("Reply message topic: " + topic); - log.debug("Reply message: " + message.toString().substring(0, lastIndex)); - } - - synchronized (ArduinoService.getReplyMsgQueue()) { - replyMessageList = ArduinoService.getReplyMsgQueue().get(deviceId); - if (replyMessageList == null) { - ArduinoService.getReplyMsgQueue() - .put(deviceId, replyMessageList = new LinkedList()); - } - } - replyMessageList.add(message.toString()); - } - } - - 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 { - ArduinoMQTTSubscriber.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-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml index 1be763aa18..291443c92f 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,11 +26,11 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - + - - + + @@ -39,8 +39,8 @@ - + diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/pom.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/pom.xml similarity index 95% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/pom.xml rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/pom.xml index d7f30a06fe..947854d9b5 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/pom.xml +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/pom.xml @@ -29,11 +29,11 @@ 4.0.0 - org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl + org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl 1.9.2-SNAPSHOT war - WSO2 Carbon - IoT Server Arduino Manager API - WSO2 Carbon - Arduino Manager API Implementation + WSO2 Carbon - IoT Server Arduino ManagerService API + WSO2 Carbon - Arduino ManagerService API Implementation http://wso2.org diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/manager/service/impl/ArduinoManagerService.java similarity index 98% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/manager/service/impl/ArduinoManagerService.java index 3d20fdec40..c9ba03c399 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/manager/service/impl/ArduinoManagerService.java @@ -11,16 +11,15 @@ * 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 + * 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.arduino.service; +package org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl; 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.webapp.publisher.KeyGenerationUtil; import org.wso2.carbon.device.mgt.common.Device; @@ -57,9 +56,9 @@ import java.util.List; import java.util.UUID; @DeviceType( value = "arduino") -public class ArduinoService { +public class ArduinoManagerService { - private static Log log = LogFactory.getLog(ArduinoService.class); + private static Log log = LogFactory.getLog(ArduinoManagerService.class); //TODO; replace this tenant domain private static final String SUPER_TENANT = "carbon.super"; diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/META-INF/webapp-classloading.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/META-INF/webapp-classloading.xml similarity index 100% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/META-INF/webapp-classloading.xml rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/META-INF/webapp-classloading.xml diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml similarity index 87% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml index f13e094338..98bfb6d536 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,10 +26,10 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - + - + diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/WEB-INF/web.xml b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/webapp/WEB-INF/web.xml rename to components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl/src/main/webapp/WEB-INF/web.xml diff --git a/components/device-mgt-iot-arduino/pom.xml b/components/device-mgt-iot-arduino/pom.xml index 25706f77ad..e1081d0faa 100644 --- a/components/device-mgt-iot-arduino/pom.xml +++ b/components/device-mgt-iot-arduino/pom.xml @@ -37,7 +37,7 @@ org.wso2.carbon.device.mgt.iot.arduino.plugin.impl - org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl + org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl org.wso2.carbon.device.mgt.iot.arduino.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 bd4d9f280c..38575c59e6 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 @@ -133,7 +133,7 @@ public class VirtualFireAlarmMQTTConnector extends MQTTTransportHandler { String deviceId = ownerAndId.split(":")[1]; if (log.isDebugEnabled()) { - log.debug("Received MQTT message for: {OWNER-" + owner + "} & {DEVICE.ID-" + deviceId + "}"); + log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]"); } String actualMessage; diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml index 7a5e51aae2..b400235d83 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/pom.xml @@ -44,7 +44,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl + org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl war @@ -99,7 +99,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl + org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl war true diff --git a/pom.xml b/pom.xml index 8e95e17623..42dab55597 100644 --- a/pom.xml +++ b/pom.xml @@ -406,7 +406,7 @@ org.wso2.carbon.devicemgt-plugins - org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl + org.wso2.carbon.device.mgt.iot.arduino.manager.service.impl ${carbon.iot.device.mgt.version} war