/**
* C-Nery - A home automation web application for C-Bus.
* Copyright (C) 2008,2009,2012 Dave Oxley <dave@daveoxley.co.uk>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.daveoxley.cnery.webservice;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cbus.CGateSession;
import com.daveoxley.cbus.Response;
import com.daveoxley.cnery.util.CBusAction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.jboss.seam.Component;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@WebService(serviceName = "Group")
public class Group {
@WebMethod(operationName = "getName")
public String getGroupName(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
return getGroup(projectName, networkId, applicationId, groupId).getName();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@WebMethod(operationName = "toggle")
public Boolean toggle(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
com.daveoxley.cbus.Group group = getGroup(projectName, networkId, applicationId, groupId);
CBusAction cba = getCBusAction();
Response resp = cba.toggle(group);
cba.handleResponse(resp);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@WebMethod(operationName = "on")
public Boolean on(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
com.daveoxley.cbus.Group group = getGroup(projectName, networkId, applicationId, groupId);
CBusAction cba = getCBusAction();
Response resp = cba.on(group);
cba.handleResponse(resp);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@WebMethod(operationName = "off")
public Boolean off(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
com.daveoxley.cbus.Group group = getGroup(projectName, networkId, applicationId, groupId);
CBusAction cba = getCBusAction();
Response resp = cba.off(group);
cba.handleResponse(resp);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@WebMethod(operationName = "ramp")
public Boolean ramp(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId,
@WebParam(name = "level") int level,
@WebParam(name = "seconds") int seconds) {
try{
com.daveoxley.cbus.Group group = getGroup(projectName, networkId, applicationId, groupId);
CBusAction cba = getCBusAction();
Response resp = cba.ramp(group, level, seconds);
cba.handleResponse(resp);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@WebMethod(operationName = "bellPress")
public Boolean bellPress(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
com.daveoxley.cbus.Group group = getGroup(projectName, networkId, applicationId, groupId);
CBusAction cba = getCBusAction();
List<Response> resps = cba.bellPress(group);
cba.handleResponses(resps);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@WebMethod(operationName = "getLevel")
public Integer getLevel(@WebParam(name = "project") String projectName,
@WebParam(name = "network") int networkId,
@WebParam(name = "application") int applicationId,
@WebParam(name = "group") int groupId) {
try{
return getGroup(projectName, networkId, applicationId, groupId).getLevel();
}
catch (Exception e) {
e.printStackTrace();
}
return -1;
}
private CBusAction getCBusAction() {
return (CBusAction)Component.getInstance(CBusAction.class);
}
private com.daveoxley.cbus.Group getGroup(String projectName, int networkId, int applicationId, int groupId) throws CGateException {
CGateSession cGateSession = (CGateSession)Component.getInstance("cGateSession");
com.daveoxley.cbus.Project project = com.daveoxley.cbus.Project.getProject(cGateSession, projectName);
com.daveoxley.cbus.Network network = project.getNetwork(networkId);
com.daveoxley.cbus.Application application = network.getApplication(applicationId);
return application.getGroup(groupId);
}
}