/**
* 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.util;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cbus.Group;
import com.daveoxley.cbus.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.util.Conversions;
/**
*
* @author dave
*/
@Name("cBusAction")
@AutoCreate
@Scope(ScopeType.APPLICATION)
public class CBusAction {
private boolean testMode = false;
@Create
public void init() {
Map<String, Conversions.PropertyValue> properties = (Map<String, Conversions.PropertyValue>)Contexts.getApplicationContext().get(Component.PROPERTIES);
String demo_str = properties.get("com.daveoxley.cnery.TEST_MODE").getSingleValue();
testMode = Boolean.parseBoolean(demo_str);
}
public Response on(Group group) throws CGateException {
if (testMode)
return null;
return group.on();
}
public Response off(Group group) throws CGateException {
if (testMode)
return null;
return group.off();
}
public Response ramp(Group group, int level, int seconds) throws CGateException {
if (testMode)
return null;
return group.ramp(level, seconds);
}
public Response toggle(Group group) throws CGateException {
if (group.getLevel() > 0)
return off(group);
else
return on(group);
}
public List<Response> bellPress(Group group) throws CGateException {
ArrayList<Response> responses = new ArrayList<Response>();
if (testMode)
return responses;
responses.add(group.on());
try {
Thread.sleep(250); // Sleep for 0.25s
}
catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
responses.add(group.off());
return responses;
}
public void handleResponse(Response resp) throws CGateException {
if (!testMode && resp != null) resp.handle200();
}
public void handleResponses(List<Response> responses) throws CGateException {
if (testMode)
return;
for (Response response : responses)
handleResponse(response);
}
}