/**
* 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;
import com.daveoxley.cnery.dao.SceneActionConditionDAO;
import com.daveoxley.cnery.dao.SceneActionDAO;
import com.daveoxley.cnery.dao.SceneActivationDAO;
import com.daveoxley.cnery.dao.SceneConditionDAO;
import com.daveoxley.cnery.dao.SceneDAO;
import com.daveoxley.cnery.entities.SceneAction;
import com.daveoxley.cnery.entities.SceneActionCondition;
import com.daveoxley.cnery.entities.SceneActivation;
import com.daveoxley.cnery.entities.SceneCondition;
import com.daveoxley.cbus.CGateException;
import com.daveoxley.cbus.CGateSession;
import com.daveoxley.cbus.Group;
import com.daveoxley.cbus.Network;
import com.daveoxley.cnery.actions.SceneActionHome;
import com.daveoxley.cnery.actions.SceneHome;
import com.daveoxley.cnery.entities.AbstractCondition.ActionType;
import com.daveoxley.cnery.entities.Scene;
import com.daveoxley.cnery.processing.ProcessQueueListener;
import com.workplacesystems.queuj.process.QueujFactory;
import java.io.Serializable;
import java.util.TreeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.Transactional;
/**
* <p>Application scope data bean for your application. Create properties
* here to represent cached data that should be made available to all users
* and pages in the application.</p>
*
* <p>An instance of this class will be created for you automatically,
* the first time your application evaluates a value binding expression
* or method binding expression that references a managed bean using
* this class.</p>
*
* @author Dave Oxley <dave@daveoxley.co.uk>
*/
@Name("applicationBean")
@Startup(depends={"queujInitialiser", "cGateSession"})
@Scope(ScopeType.APPLICATION)
public class ApplicationBean implements Serializable {
private final static Log log = LogFactory.getLog(ApplicationBean.class);
@In
private CGateSession cGateSession;
@In
private SceneDAO sceneDAO;
@In
private SceneActivationDAO sceneActivationDAO;
@In
private SceneConditionDAO sceneConditionDAO;
@In
private SceneActionDAO sceneActionDAO;
@In
private SceneActionConditionDAO sceneActionConditionDAO;
@In(create=true)
private SceneHome sceneHome;
@In(create=true)
private SceneActionHome sceneActionHome;
/**
* <p>Construct a new application data bean instance.</p>
*/
public ApplicationBean() {
}
@Create
@Transactional
public void init() {
log.info("Creating ApplicationBean");
QueujFactory.getProcessServer((String)null, null).registerListener(new ProcessQueueListener());
for (Scene scene : sceneDAO.findScenes()) {
if (scene.getActivateProcess() != null &&
((scene.getStatePersistence() == Scene.StatePersistence.TRIGGER && scene.getDeactivateProcess() == null)) ||
(scene.getStatePersistence() != Scene.StatePersistence.TRIGGER && scene.getDeactivateProcess() != null))
continue;
sceneHome.clearInstance();
sceneHome.setId(scene.getId());
scene = sceneHome.getInstance();
sceneHome.update();
}
for (SceneAction sceneAction : sceneActionDAO.findSceneActions()) {
if (sceneAction.getProcess() != null)
continue;
sceneActionHome.clearInstance();
sceneActionHome.setId(sceneAction.getId());
sceneAction = sceneActionHome.getInstance();
sceneActionHome.update();
}
final TreeSet<Network> groupNetworks = new TreeSet<Network>();
for (SceneActivation sceneActivation : sceneActivationDAO.findSceneActivations()) {
try {
Group group = (Group)cGateSession.getCGateObject(sceneActivation.getGroupAddress());
if (!groupNetworks.contains(group.getNetwork()))
groupNetworks.add(group.getNetwork());
} catch (Exception ex) {}
}
for (SceneCondition sceneCondition : sceneConditionDAO.findSceneConditions()) {
if (sceneCondition.getActionType() == ActionType.GROUP) {
try {
Group group = (Group)cGateSession.getCGateObject(sceneCondition.getDependGroup());
if (!groupNetworks.contains(group.getNetwork()))
groupNetworks.add(group.getNetwork());
} catch (Exception ex) {}
}
}
for (SceneAction sceneAction : sceneActionDAO.findSceneActions()) {
try {
Group group = (Group)cGateSession.getCGateObject(sceneAction.getAddress());
if (!groupNetworks.contains(group.getNetwork()))
groupNetworks.add(group.getNetwork());
} catch (Exception ex) {}
}
for (SceneActionCondition sceneActionCondition : sceneActionConditionDAO.findSceneActionConditions()) {
if (sceneActionCondition.getActionType() == ActionType.GROUP) {
try {
Group group = (Group)cGateSession.getCGateObject(sceneActionCondition.getDependGroup());
if (!groupNetworks.contains(group.getNetwork()))
groupNetworks.add(group.getNetwork());
} catch (Exception ex) {}
}
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
log.info("Opening " + groupNetworks.size() + " networks");
for (Network network : groupNetworks) {
boolean success = false;
int attempts = 0;
while (attempts++ < 10 && !success) {
try {
log.info("Attempt " + attempts + " to open network: " + network.getName() + " (" + network.getAddress() + ")");
network.open();
success = true;
} catch (CGateException ex) {
if (attempts < 10) {
try {
Thread.sleep(10000);
} catch (InterruptedException ex1) {}
}
}
}
}
}
});
thread.setDaemon(true);
thread.start();
}
}