001 package dk.deepthought.sidious.services;
002
003 import java.util.ArrayList;
004 import java.util.Collection;
005
006 import org.apache.commons.logging.Log;
007 import org.apache.commons.logging.LogFactory;
008
009 import dk.deepthought.sidious.greenhouse.ClimaticState;
010 import dk.deepthought.sidious.greenhouse.SensorInput;
011 import dk.deepthought.sidious.supportsystem.State;
012 import dk.deepthought.sidious.supportsystem.SuperLinkID;
013
014 /**
015 * This class handles interaction with environment. It implements convenience
016 * methods for handling of sensors.
017 *
018 * @author Deepthought
019 *
020 */
021 public class ServiceEngine {
022
023 /**
024 * All the sensors in the environment.
025 */
026 private static Collection<SensorInput> sensorList = new ArrayList<SensorInput>();
027
028 /**
029 * Logger for this class
030 */
031 private static final Log logger = LogFactory.getLog(ServiceEngine.class);
032
033
034 /**
035 * Returns a snapshot of the sensors and their current setting in the
036 * system.
037 *
038 * @return the sensors of the system
039 */
040 private synchronized static Collection<SensorInput> getEnvironmentSensors() {
041 return sensorList;
042 }
043
044 /**
045 * Method returns the current state of the system.
046 *
047 * @return the current state
048 */
049 public static State getCurrentState() {
050 return new ClimaticState(getEnvironmentSensors());
051 }
052
053 /**
054 * Method returns the value of the sensor associated with the given
055 * <code>id</code>.
056 *
057 * @param id
058 * the id of the sensor
059 * @return the value of the sensor
060 */
061 public static double getSensorValue(SuperLinkID id) {
062 if (id == null) {
063 throw new IllegalArgumentException("null not valid id");
064 }
065 if (logger.isDebugEnabled()) {
066 logger.debug("getSensorValue(SuperLinkID id=" + id + ") - start");
067 }
068 Collection<SensorInput> sensors = getEnvironmentSensors();
069 for (SensorInput input : sensors) {
070 if (input.getID().equals(id)) {
071 double returndouble = input.getValue();
072 if (logger.isDebugEnabled()) {
073 logger.debug("getSensorValue(SuperLinkID id=" + id
074 + ") - end - return value=" + returndouble);
075 }
076 return returndouble;
077 }
078 }
079 logger.error("getSensorValue(SuperLinkID id=" + id
080 + ") - end - return value=" + Double.NaN);
081 return Double.NaN;
082 }
083
084 /**
085 * @param sensorList
086 * the sensorList to set
087 */
088 public synchronized static void setSensorList(
089 Collection<SensorInput> sensorList) {
090 ServiceEngine.sensorList = sensorList;
091 }
092
093 /**
094 * Resets the service engine.
095 */
096 public static void reset() {
097 sensorList = new ArrayList<SensorInput>();
098 }
099
100 }
|