/** * 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.cnery.dao.SceneDAO; import javax.jws.WebMethod; import javax.jws.WebService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jboss.seam.Component; import org.jboss.seam.async.Asynchronous; import uk.me.jstott.coordconv.LatitudeLongitude; /** * * @author Dave Oxley <dave@daveoxley.co.uk> */ @WebService(serviceName = "GPSSensor") public class GPSSensor { private final static Log log = LogFactory.getLog(GPSSensor.class); private ProcessGPSSensorImpl async = null; private synchronized ProcessGPSSensorImpl getAsync() { if (async == null) async = new ProcessGPSSensorImpl(); return async; } @WebMethod(operationName = "getGPSHomeLocation") public Number[] getGPSHomeLocation() { try{ LatitudeLongitude position = (LatitudeLongitude)Component.getInstance("position"); Integer gpsRadius = (Integer)Component.getInstance("gpsRadius"); Number[] positionArray = new Number[3]; positionArray[0] = position.getLatitude(); positionArray[1] = position.getLongitude(); positionArray[2] = gpsRadius; return positionArray; } catch (Exception e) { e.printStackTrace(); } return null; } @WebMethod(operationName = "enteringHomeLocation") public Boolean enteringHomeLocation() { try { SceneDAO sceneDAO = (SceneDAO)Component.getInstance(SceneDAO.class); for (final com.daveoxley.cnery.entities.Scene scene : sceneDAO.findScenesGPSEntering()) { Thread thread = new Thread(new Runnable() { @Override public void run() { getAsync().execute(null, scene, true); } }); thread.start(); } return true; } catch (Exception e) { e.printStackTrace(); } return false; } @WebMethod(operationName = "leavingHomeLocation") public Boolean leavingHomeLocation() { try { SceneDAO sceneDAO = (SceneDAO)Component.getInstance(SceneDAO.class); for (final com.daveoxley.cnery.entities.Scene scene : sceneDAO.findScenesGPSLeaving()) { Thread thread = new Thread(new Runnable() { @Override public void run() { getAsync().execute(null, scene, false); } }); thread.start(); } return true; } catch (Exception e) { e.printStackTrace(); } return false; } private class ProcessGPSSensorImpl extends Asynchronous { public void execute(Object timer, final com.daveoxley.cnery.entities.Scene scene, final boolean entering) { (new Asynchronous.ContextualAsynchronousRequest(timer) { @Override protected void process() { log.debug("Running thread for scene " + scene.getName()); try { com.daveoxley.cnery.entities.Scene.GPSSceneAction action = entering ? scene.getGpsEnterAreaAction() : scene.getGpsLeaveAreaAction(); if (action == com.daveoxley.cnery.entities.Scene.GPSSceneAction.ACTIVATE_SCENE) scene.getActivateProcess().startNow(); else if (action == com.daveoxley.cnery.entities.Scene.GPSSceneAction.RESET_SCENE) if (scene.getDeactivateProcess() != null) scene.getDeactivateProcess().startNow(); } catch (Exception e) { new CGateException(e); } } }).run(); } @Override public void execute(Object timer) { throw new UnsupportedOperationException("Not supported yet."); } @Override protected void handleException(Exception exception, Object timer) {} }; }