1 | package dk.deepthought.sidious.services; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | |
6 | import org.apache.commons.logging.Log; |
7 | import org.apache.commons.logging.LogFactory; |
8 | |
9 | import dk.deepthought.sidious.greenhouse.ClimaticState; |
10 | import dk.deepthought.sidious.greenhouse.SensorInput; |
11 | import dk.deepthought.sidious.supportsystem.State; |
12 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
13 | |
14 | /** |
15 | * This class handles interaction with environment. It implements convenience |
16 | * methods for handling of sensors. |
17 | * |
18 | * @author Deepthought |
19 | * |
20 | */ |
21 | public class ServiceEngine { |
22 | |
23 | /** |
24 | * All the sensors in the environment. |
25 | */ |
26 | private static Collection<SensorInput> sensorList = new ArrayList<SensorInput>(); |
27 | |
28 | /** |
29 | * Logger for this class |
30 | */ |
31 | private static final Log logger = LogFactory.getLog(ServiceEngine.class); |
32 | |
33 | |
34 | /** |
35 | * Returns a snapshot of the sensors and their current setting in the |
36 | * system. |
37 | * |
38 | * @return the sensors of the system |
39 | */ |
40 | private synchronized static Collection<SensorInput> getEnvironmentSensors() { |
41 | return sensorList; |
42 | } |
43 | |
44 | /** |
45 | * Method returns the current state of the system. |
46 | * |
47 | * @return the current state |
48 | */ |
49 | public static State getCurrentState() { |
50 | return new ClimaticState(getEnvironmentSensors()); |
51 | } |
52 | |
53 | /** |
54 | * Method returns the value of the sensor associated with the given |
55 | * <code>id</code>. |
56 | * |
57 | * @param id |
58 | * the id of the sensor |
59 | * @return the value of the sensor |
60 | */ |
61 | public static double getSensorValue(SuperLinkID id) { |
62 | if (id == null) { |
63 | throw new IllegalArgumentException("null not valid id"); |
64 | } |
65 | if (logger.isDebugEnabled()) { |
66 | logger.debug("getSensorValue(SuperLinkID id=" + id + ") - start"); |
67 | } |
68 | Collection<SensorInput> sensors = getEnvironmentSensors(); |
69 | for (SensorInput input : sensors) { |
70 | if (input.getID().equals(id)) { |
71 | double returndouble = input.getValue(); |
72 | if (logger.isDebugEnabled()) { |
73 | logger.debug("getSensorValue(SuperLinkID id=" + id |
74 | + ") - end - return value=" + returndouble); |
75 | } |
76 | return returndouble; |
77 | } |
78 | } |
79 | logger.error("getSensorValue(SuperLinkID id=" + id |
80 | + ") - end - return value=" + Double.NaN); |
81 | return Double.NaN; |
82 | } |
83 | |
84 | /** |
85 | * @param sensorList |
86 | * the sensorList to set |
87 | */ |
88 | public synchronized static void setSensorList( |
89 | Collection<SensorInput> sensorList) { |
90 | ServiceEngine.sensorList = sensorList; |
91 | } |
92 | |
93 | /** |
94 | * Resets the service engine. |
95 | */ |
96 | public static void reset() { |
97 | sensorList = new ArrayList<SensorInput>(); |
98 | } |
99 | |
100 | } |