/**
* 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.factories;
import com.daveoxley.cbus.CGateConnectException;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cbus.CGateInterface;
import com.daveoxley.cbus.CGateSession;
import com.daveoxley.cnery.dao.SettingDAO;
import com.daveoxley.cnery.entities.Setting;
import com.daveoxley.cnery.scenes.GCMGroupListener;
import com.daveoxley.cnery.scenes.GroupAjaxListener;
import com.daveoxley.cnery.scenes.SceneStatusChangeCallback;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.util.Conversions;
import org.jboss.seam.util.Conversions.PropertyValue;
/**
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@Name("cGateSessionFactory")
public class CGateSessionFactory implements Serializable {
private final static Log log = LogFactory.getLog(CGateSessionFactory.class);
@In
private SettingDAO settingDAO;
@Factory(value="cGateSession",autoCreate=true,scope=ScopeType.APPLICATION)
@Transactional
public CGateSession createCGateSession() throws UnknownHostException, CGateConnectException {
CGateSession cGateSession = null;
boolean success = false;
int attempts = 0;
while (attempts++ < 3 && !success) {
try {
log.info("Attempt " + attempts + " to connect to CGate");
cGateSession = createCGateSession0();
cGateSession.connect();
success = true;
} catch (Exception e) {
log.info("Cannot connect to CGate - " + e.toString());
if (attempts < 3) {
try {
Thread.sleep(10000);
} catch (InterruptedException ex1) {}
}
}
}
return cGateSession;
}
public CGateSession createCGateSession0() throws UnknownHostException {
Map<String, Conversions.PropertyValue> properties = (Map<String, PropertyValue>)Contexts.getApplicationContext().get(Component.PROPERTIES);
String demo_str = properties.get("com.daveoxley.cnery.DEMO").getSingleValue();
boolean demo = Boolean.parseBoolean(demo_str);
log.info("Connecting to CGate as demo: " + String.valueOf(demo));
String cGateServer = null;
int cGateCommandPort = 0;
int cGateEventPort = 0;
int cGateSCPort = 0;
if (demo)
{
cGateServer = "localhost";
cGateCommandPort = 30023;
cGateEventPort = 30024;
cGateSCPort = 30025;
}
else
{
Setting cGateServerSetting = settingDAO.findSettingByName("cgate_server");
if (cGateServerSetting != null)
cGateServer = cGateServerSetting.getValue();
Setting cGateCommandPortSetting = settingDAO.findSettingByName("cgate_command_port");
if (cGateCommandPortSetting != null)
cGateCommandPort = Integer.parseInt(cGateCommandPortSetting.getValue());
Setting cGateEventPortSetting = settingDAO.findSettingByName("cgate_event_port");
if (cGateEventPortSetting != null)
cGateEventPort = Integer.parseInt(cGateEventPortSetting.getValue());
Setting cGateSCPortSetting = settingDAO.findSettingByName("cgate_status_change_port");
if (cGateSCPortSetting != null)
cGateSCPort = Integer.parseInt(cGateSCPortSetting.getValue());
}
InetAddress cGateServerAddr = InetAddress.getByName(cGateServer);
CGateSession _cGateSession = CGateInterface.connect(cGateServerAddr, cGateCommandPort, cGateEventPort, cGateSCPort);
if (!demo) {
_cGateSession.registerStatusChangeCallback(SceneStatusChangeCallback.getInstance());
_cGateSession.registerStatusChangeCallback(GroupAjaxListener.getInstance());
//_cGateSession.registerStatusChangeCallback(GCMGroupListener.getInstance());
}
return _cGateSession;
}
@Observer("invalidateCGateSession")
public void removeCGateSession() {
CGateSession cGateSession = (CGateSession)Contexts.getApplicationContext().get("cGateSession");
if (cGateSession != null && cGateSession.isConnected())
{
try {
cGateSession.close();
} catch (CGateException ex) {
}
cGateSession = null;
}
Contexts.getApplicationContext().remove("cGateSession");
}
}