Permalink
Browse files

Add event websocket to RestAPI

  • Loading branch information...
mcicolella committed Jul 5, 2018
1 parent be631f6 commit 1163bafc9e7c1c23bcc0c017064a8124f30019a3
@@ -25,6 +25,7 @@
import com.freedomotic.events.ObjectHasChangedBehavior;
import com.freedomotic.events.PluginHasChanged;
import com.freedomotic.events.ZoneHasChanged;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereEventResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereMessageCalloutResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmosphereObjectChangeResource;
import com.freedomotic.plugins.devices.restapiv3.resources.atmosphere.AtmospherePluginChangeResource;
@@ -55,9 +56,11 @@
private AtmosphereZoneChangeResource atmosphereZoneChangeResource;
@Inject
private AtmospherePluginChangeResource atmospherePluginChangeResource;
@Inject
@Inject
private AtmosphereMessageCalloutResource atmosphereMessageCalloutResource;
@Inject
private AtmosphereEventResource atmosphereEventResource;
// Hold a preconfigurd static web security manager which can be used by Shiro
public static DefaultWebSecurityManager defaultWebSecurityManager;
@@ -129,9 +132,11 @@ protected void onEvent(EventTemplate event) {
atmosphereZoneChangeResource.broadcast(event);
} else if (event instanceof PluginHasChanged) {
atmospherePluginChangeResource.broadcast(event);
} else if (event instanceof MessageEvent){
} else if (event instanceof MessageEvent) {
atmosphereMessageCalloutResource.broadcast(event);
}
// broadcast all events to AtmosphereEventResource
atmosphereEventResource.broadcast(event);
}
public final void createDefaultWebSecurityManager() {
@@ -0,0 +1,147 @@
/**
*
* Copyright (c) 2009-2018 Freedomotic team http://freedomotic.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Freedomotic; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
package com.freedomotic.plugins.devices.restapiv3.resources.atmosphere;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.freedomotic.api.Client;
import com.freedomotic.api.EventTemplate;
import com.freedomotic.api.Plugin;
import com.freedomotic.events.MessageEvent;
import com.freedomotic.events.ObjectHasChangedBehavior;
import com.freedomotic.events.PluginHasChanged;
import com.freedomotic.events.ZoneHasChanged;
import com.freedomotic.plugins.devices.restapiv3.RestAPIv3;
import com.freedomotic.plugins.devices.restapiv3.representations.MessageCalloutRepresentation;
import com.freedomotic.things.EnvObjectLogic;
import com.wordnik.swagger.annotations.Api;
import javax.ws.rs.Path;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Mauro Cicolella
*/
@Path(AtmosphereEventResource.PATH)
@Api(value = "ws_event", description = "WS for receiving event notifications", position = 10)
@AtmosphereService(
dispatch = false,
interceptors = {AtmosphereResourceLifecycleInterceptor.class},
path = "/" + RestAPIv3.API_VERSION + "/ws/" + AtmosphereEventResource.PATH,
servlet = "org.glassfish.jersey.servlet.ServletContainer")
public class AtmosphereEventResource extends AbstractWSResource {
private static final Logger LOG = LoggerFactory.getLogger(AtmosphereEventResource.class.getName());
public final static String PATH = "event";
@Override
public void broadcast(EventTemplate message) {
if (api != null) {
String msg = "";
String msgType = "";
String payload = "";
if (message instanceof ObjectHasChangedBehavior) {
if (api != null) {
EnvObjectLogic obj = api.things().findOne(message.getPayload().getStatementValue("object.uuid"));
if (obj == null) {
payload = "{}";
} else {
try {
payload = om.writeValueAsString(obj.getPojo());
} catch (JsonProcessingException ex) {
LOG.error("Error processing Json data", ex);
}
}
}
msgType = "object-changed";
} else if (message instanceof ZoneHasChanged) {
msgType = "zone-changed";
} else if (message instanceof PluginHasChanged) {
switch (message.getProperty("plugin.action")) {
case "START":
msgType = "plugin-started";
if (api != null) {
for (Client client : api.getClients("plugin")) {
Plugin plugin = (Plugin) client;
if (plugin.getName().equalsIgnoreCase(message.getPayload().getStatementValue("plugin.name"))) {
payload = plugin.toString();
}
}
}
break;
case "STOP":
msgType = "plugin-stopped";
if (api != null) {
for (Client client : api.getClients("plugin")) {
Plugin plugin = (Plugin) client;
if (plugin.getName().equalsIgnoreCase(message.getPayload().getStatementValue("plugin.name"))) {
payload = plugin.toString();
}
}
}
break;
case "ENQUEUE":
msgType = "plugin-installed";
if (api != null) {
for (Client client : api.getClients("plugin")) {
Plugin plugin = (Plugin) client;
if (plugin.getName().equalsIgnoreCase(message.getPayload().getStatementValue("plugin.name"))) {
try {
payload = om.writeValueAsString(plugin);
} catch (JsonProcessingException ex) {
LOG.error("Error processing Json data", ex);
}
}
}
}
break;
case "DEQUEUE":
msgType = "plugin-uninstalled";
payload = message.getPayload().getStatementValue("plugin.name");
break;
}
} else if (message instanceof MessageEvent) {
try {
msgType = "message-callout";
payload = om.writeValueAsString(new MessageCalloutRepresentation(message.getProperty("message.text")));
} catch (JsonProcessingException ex) {
LOG.error("Error processing Json data", ex);
}
}
// broadcast message
try {
BroadcasterFactory
.getDefault()
.lookup("/" + RestAPIv3.API_VERSION + "/ws/" + AtmosphereEventResource.PATH)
.broadcast(om.writeValueAsString(msgType + "#" + payload));
} catch (JsonProcessingException ex) {
}
}
}
}

0 comments on commit 1163baf

Please sign in to comment.