Skip to content

Commit

Permalink
Merge pull request #173 from NuwanSameera/IoTS-1.0.0-M2
Browse files Browse the repository at this point in the history
Digital display new operations added and modified display agent added.
  • Loading branch information
ruwany committed Jan 27, 2016
2 parents 55d97dd + 41de719 commit af4a30d
Show file tree
Hide file tree
Showing 26 changed files with 407 additions and 231 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api.model;

public class ScreenShotModel {

private String[] scrrenShotData;
private int length;

public ScreenShotModel(){

}

public ScreenShotModel(String[] scrrenShotData , int length){
this.scrrenShotData = scrrenShotData;
this.length = length;
}

public void setScrrenShotData(String[] scrrenShotData){
this.scrrenShotData = scrrenShotData;
}

public void setLength(int length){
this.length = length;
}

public String[] getScrrenShotData(){
return this.scrrenShotData;
}

public int getLength(){
return this.length;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import org.apache.commons.logging.LogFactory;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.json.JSONObject;
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
import org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api.model.ScreenShotModel;
import org.wso2.carbon.device.mgt.iot.digitaldisplay.controller.api.websocket.DigitalDisplayWebSocketServerEndPoint;
import org.wso2.carbon.device.mgt.iot.digitaldisplay.plugin.constants.DigitalDisplayConstants;
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.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ScheduledFuture;

@SuppressWarnings("no JAX-WS annotation")
public class DigitalDisplayMQTTConnector extends MQTTTransportHandler {
Expand All @@ -25,9 +30,13 @@ public class DigitalDisplayMQTTConnector extends MQTTTransportHandler {

private static String iotServerSubscriber = UUID.randomUUID().toString().substring(0, 5);

private ScheduledFuture<?> dataPushServiceHandler;

private Map<String, ScreenShotModel> screenshots = new HashMap<>();

private DigitalDisplayMQTTConnector() {
super(iotServerSubscriber, DigitalDisplayConstants.DEVICE_TYPE,
MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic);
MqttConfig.getInstance().getMqttQueueEndpoint(), subscribeTopic);
}

@Override
Expand Down Expand Up @@ -65,36 +74,62 @@ public void run() {

@Override
public void processIncomingMessage(MqttMessage message, String... messageParams) {
if(messageParams.length != 0) {
String topic = messageParams[0];
String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, "");
ownerAndId = ownerAndId.replace(File.separator + DigitalDisplayConstants.DEVICE_TYPE + File.separator, ":");
ownerAndId = ownerAndId.replace(File.separator + "digital_display_publisher", "");

String owner = ownerAndId.split(":")[0];
String deviceId = ownerAndId.split(":")[1];
String[] messageData = message.toString().split(":");

if (log.isDebugEnabled()) {
log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]");
}
String topic = messageParams[0];
String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, "");
ownerAndId = ownerAndId.replace(File.separator + DigitalDisplayConstants.DEVICE_TYPE + File.separator, ":");
ownerAndId = ownerAndId.replace(File.separator + "digital_display_publisher", "");

if (messageData.length == 3) {
String randomId = messageData[0];
String requestMessage = messageData[1];
String result = messageData[2];
String owner = ownerAndId.split(":")[0];
String deviceId = ownerAndId.split(":")[1];

if (log.isDebugEnabled()) {
log.debug("Return result " + result + " for Request " + requestMessage);
String[] messageData = message.toString().split("::");
if (log.isDebugEnabled()) {
log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]");
}

String token = messageData[0];
if (messageData.length == 2) {
String responseMessage = messageData[1];
DigitalDisplayWebSocketServerEndPoint.sendMessage(token, new StringBuilder(responseMessage));
} else if (messageData.length == 3) {
String response = messageData[2];
JSONObject schreenshot = new JSONObject(response);
String pic_id = schreenshot.getString("pic_id");
String data = schreenshot.getString("data");
int pos = schreenshot.getInt("pos");
int length = schreenshot.getInt("size");
createScreenShot(token, pic_id, pos, length, data);
}
}

private void createScreenShot(String token, String pic_id, int pos, int length, String data) {

ScreenShotModel screenShotModel = screenshots.get(pic_id);

if (screenShotModel == null) {
screenShotModel = new ScreenShotModel();
screenShotModel.setScrrenShotData(new String[length + 1]);
screenShotModel.setLength(0);
screenshots.put(pic_id, screenShotModel);
}
if (screenShotModel.getLength() <= length) {
screenShotModel.getScrrenShotData()[pos] = data;
System.out.println(screenShotModel.getLength());
screenShotModel.setLength(screenShotModel.getLength() + 1);
if (screenShotModel.getLength() == (length + 1)) {
StringBuilder displayScreenShot = new StringBuilder("Screenshot||");
for (String screenshot : screenShotModel.getScrrenShotData()) {
displayScreenShot.append(screenshot);
}
DigitalDisplayWebSocketServerEndPoint.sendMessage(randomId, result);
screenshots.remove(pic_id);
DigitalDisplayWebSocketServerEndPoint.sendMessage(token, displayScreenShot);
}
}
}

public void publishToDigitalDisplay(String topic, String payLoad, int qos, boolean retained)
throws TransportHandlerException {
if(log.isDebugEnabled()){
if (log.isDebugEnabled()) {
log.debug("Publishing message [" + payLoad + "to topic [" + topic + "].");
}
publishToQueue(topic, payLoad, qos, retained);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@
import org.apache.commons.logging.LogFactory;

import javax.inject.Singleton;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@ServerEndpoint(value = "/digital-display-server-end-point")
@ServerEndpoint(value = "/{token}")
@Singleton
public class DigitalDisplayWebSocketServerEndPoint {

private static Log log = LogFactory.getLog(DigitalDisplayWebSocketServerEndPoint.class);

private static Map<String,Session> clientSessions = new HashMap<>();
private static Map<String, Session> clientSessions = new HashMap<>();

/**
* This method will be invoked when a client requests for a
Expand All @@ -29,12 +24,8 @@ public class DigitalDisplayWebSocketServerEndPoint {
* @param userSession the userSession which is opened.
*/
@OnOpen
public void onOpen(Session userSession){
UUID uuid = UUID.randomUUID();
log.info("Generated Random Id " + uuid.toString());
log.info(" Connected with Session Id : " + userSession.getId());
clientSessions.put(uuid.toString() , userSession);
userSession.getAsyncRemote().sendText("RandomID:" + uuid.toString());
public void onOpen(Session userSession, @PathParam("token") String token) {
clientSessions.put(token, userSession);
}

/**
Expand All @@ -44,29 +35,28 @@ public void onOpen(Session userSession){
* @param userSession the userSession which is opened.
*/
@OnClose
public void onClose(Session userSession){
log.info("Client disconnected - Session Id : " + userSession.getId());
public void onClose(Session userSession) {
clientSessions.values().remove(userSession);

}

@OnError
public void onError(Throwable t){
log.error("Error occurred " + t );
public void onError(Throwable t) {
log.error("Error occurred " + t);
}

/**
* This method will be invoked when a message received from device
* to send client.
*
* @param randomId the client of message to be sent.
* @param token the client of message to be sent.
* @param message the message sent by device to client
*/
public static void sendMessage(String randomId , String message){
if(clientSessions.keySet().contains(randomId)){
clientSessions.get(randomId).getAsyncRemote().sendText(message);
log.info("Message : " + message + " send to Session Id : " + randomId);
}else {
public static void sendMessage(String token, StringBuilder message) {
Session session = clientSessions.get(token);
if (session != null) {
session.getAsyncRemote().sendText(message.toString());
} else {
log.error("Client already disconnected.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public class DigitalDisplayConstants {
public final static String DEVICE_TYPE = "digital_display";
public final static String DEVICE_PLUGIN_DEVICE_NAME = "DEVICE_NAME";
public final static String DEVICE_PLUGIN_DEVICE_ID = "DIGITAL_DISPLAY_DEVICE_ID";
public final static String SHUTDOWN_DISPLAY_CONSTANT = "shutdown_display";
public final static String RESTART_SERVER_CONSTANT = "restart_server";
public final static String RESTART_DISPLAY_CONSTANT = "restart_display";
public final static String REMOVE_DIRECTORY_CONSTANT = "remove_dir_and_content";
public final static String REMOVE_CONTENT_CONSTANT = "remove_content";
public final static String CLOSE_BROWSER_CONSTANT = "close_browser";
public final static String RESTART_BROWSER_CONSTANT = "restart_browser";
public final static String TERMINATE_DISPLAY_CONSTANT = "terminate_display";
public final static String EDIT_SEQUENCE_CONSTANT = "edit_content_config";
public final static String EDIT_SEQUENCE_CONSTANT = "edit_sequence";
public final static String UPLOAD_CONTENT_CONSTANT = "upload_content";
public final static String ADD_NEW_RESOURCE_CONSTANT = "add_new_resource";
public final static String REMOVE_RESOURCE_CONSTANT = "remove_resources";
public final static String GET_STATUS_CONSTANT = "get_status";
public final static String SCREENSHOT_CONSTANT = "get_screenshot";
public final static String GET_CONTENTLIST_CONSTANT = "get_content_list";
public final static String GET_DEVICE_STATUS_CONSTANT = "get_device_status";
public final static String PUBLISH_TOPIC = "wso2/iot/%s/digital_display/%s/digital_display_subscriber";

}
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ for P in paho-mqtt lxml; do
}
done

unzip DigitalDisplay
cp ./deviceConfig.properties ./DigitalDisplay



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ ps aux|grep httpserver.py|awk '{print $2}'|xargs kill -9
#xdotool mousemove 0 0
#xdotool search -name LXTerminal windowunmap
#cd ~/
unzip DigitalDisplay
cp ./deviceConfig.properties ./DigitalDisplay
cd ./DigitalDisplay/
mkdir -p tmp/dd-kernel-test
python displayagent.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<img src="{{@unit.publicUri}}/images/display-icon.png"/>
{{/zone}}

{{#zone "operation-status"}}
<div id="div-operation-status" class="hidden" align="center" style="padding: 10px; margin-bottom: 5px">
</div>
{{/zone}}

{{#zone "device-opetations"}}
<div style="background: #11375B; color: #fff; padding: 10px; margin-bottom: 5px">
Operations
Expand All @@ -20,6 +25,7 @@
{{/zone}}

{{#zone "device-detail-properties"}}

<div class="media">
<div class="media-left col-xs-12 col-sm-2 col-md-2 col-lg-2">
<ul class="list-group" role="tablist">
Expand All @@ -31,26 +37,56 @@
</ul>
</div>
<div class="media-body add-padding-left-5x remove-padding-xs tab-content">
<div class="panel-group tab-content">

<div class="panel panel-default tab-pane" id="policies" role="tabpanel"
aria-labelledby="policies">
<div class="panel-heading">Policies</div>
<div class="panel-body">
<div id="policy-spinner" class="wr-advance-operations-init hidden">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<i class="fw fw-settings fw-spin fw-2x"></i>
&nbsp;&nbsp;&nbsp;
Loading Policies . . .
<br>
<br>
</div>
<div id="policy-list-container">
<div class="panel-body">
No policies found
<div class="panel panel-default tab-pane active"
id="device_statistics" role="tabpanel" aria-labelledby="device_statistics">
<div style="background: #11375B; color: #fff; padding: 10px; margin-bottom: 5px">
Display Status
</div>
<div class="col-md-12">
<div id="screeshot" class="col-md-4">
<div>Display View</div>
<a id="zoom-image" href="{{@unit.publicUri}}/images/default-screen.png" target="_blank">
<img id="img" src="{{@unit.publicUri}}/images/default-screen.png"
style="width: 40% ; height: 40% ; float: left" class="img-responsive"/>
</a>
</div>
<div id="content-list-pane" class="col-md-4">
<div>Content List</div>
<ul id="content-list"></ul>
</div>
<div id="device-statics-pane" class="col-md-4">
<div>Device Statics</div>
<ul id="device-statics"></ul>
</div>
</div>
</div>

<div class="panel-group tab-content">
<div class="panel panel-default tab-pane" id="policy_compliance" role="tabpanel"
aria-labelledby="policy_compliance">
<div class="panel-heading">Policy Compliance <span><a href="#"
id="refresh-policy"><i
class="fw fw-refresh"></i></a></span></div>
<div class="panel panel-default tab-pane" id="policies" role="tabpanel"
aria-labelledby="policies">
<div class="panel-heading">Policies</div>
<div class="panel-body">
<div id="policy-spinner" class="wr-advance-operations-init hidden">
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<i class="fw fw-settings fw-spin fw-2x"></i>
&nbsp;&nbsp;&nbsp;
Loading Policies . . .
<br>
<br>
</div>
<div id="policy-list-container">
<div class="panel-body">
No policies found
</div>
<br class="c-both"/>
</div>
<br class="c-both"/>
</div>
</div>
<a class="padding-left"
Expand Down Expand Up @@ -86,4 +122,13 @@
</div>
</div>
</div>
{{/zone}}
{{/zone}}

{{#zone "bottomJs"}}
<script type="text/javascript">
var token = "{{token}}";
var port = "{{port}}";
var host = "{{host}}";
</script>
{{js "js/websocket.js"}}
{{/zone}}
Loading

0 comments on commit af4a30d

Please sign in to comment.