1 | package schedframe.resources.devices; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import org.joda.time.DateTimeUtils; |
---|
7 | |
---|
8 | import schedframe.Parameters; |
---|
9 | import schedframe.resources.computing.profiles.energy.EnergyEvent; |
---|
10 | import schedframe.resources.computing.profiles.energy.EnergyEventType; |
---|
11 | import schedframe.resources.computing.profiles.energy.airthroughput.AirFlowValue; |
---|
12 | import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputProfile; |
---|
13 | import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputState; |
---|
14 | import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputStateName; |
---|
15 | import schedframe.resources.computing.profiles.energy.airthroughput.StandardAirThroughputStateName; |
---|
16 | import schedframe.resources.computing.profiles.energy.airthroughput.ui.AirThroughputInterface; |
---|
17 | |
---|
18 | public class DeviceAirThroughputInterface implements AirThroughputInterface{ |
---|
19 | |
---|
20 | protected AirThroughputStateName currentAirThroughputState; |
---|
21 | protected AirThroughputProfile airThroughputProfile; |
---|
22 | protected PhysicalResource resource; |
---|
23 | |
---|
24 | public DeviceAirThroughputInterface(PhysicalResource resource, AirThroughputProfile airThroughputProfile){ |
---|
25 | this.resource = resource; |
---|
26 | this.airThroughputProfile = airThroughputProfile; |
---|
27 | this.currentAirThroughputState = StandardAirThroughputStateName.ON; |
---|
28 | } |
---|
29 | |
---|
30 | public AirThroughputStateName getAirThroughputState() { |
---|
31 | return currentAirThroughputState; |
---|
32 | } |
---|
33 | |
---|
34 | public boolean setAirThroughputState(AirThroughputStateName state) { |
---|
35 | if(supportAirThroughputState(state)){ |
---|
36 | currentAirThroughputState = state; |
---|
37 | |
---|
38 | //TO DO - notifications should be called for all resources starting form the lowest layer |
---|
39 | resource.handleEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, resource.getFullName())); |
---|
40 | resource.handleEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, resource.getFullName())); |
---|
41 | return true; |
---|
42 | } |
---|
43 | return false; |
---|
44 | } |
---|
45 | |
---|
46 | public boolean supportAirThroughputState(AirThroughputStateName state) { |
---|
47 | for(AirThroughputState airFlowState: airThroughputProfile.getAirThroughputStates()){ |
---|
48 | if(airFlowState.getName().equals(state.getName())){ |
---|
49 | return true; |
---|
50 | } |
---|
51 | } |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | public List<AirThroughputState> getSupportedAirThroughputStates() throws NoSuchFieldException { |
---|
56 | List<AirThroughputState> airThroughputStates = new ArrayList<AirThroughputState>(); |
---|
57 | for(AirThroughputState airFlowState: airThroughputProfile.getAirThroughputStates()){ |
---|
58 | airThroughputStates.add(airFlowState); |
---|
59 | } |
---|
60 | return airThroughputStates; |
---|
61 | } |
---|
62 | |
---|
63 | public double getAirFlow(AirThroughputStateName state) throws NoSuchFieldException { |
---|
64 | double airThroughput = 0; |
---|
65 | if(supportAirThroughputState(state)){ |
---|
66 | for(AirThroughputState airFlowState: airThroughputProfile.getAirThroughputStates()){ |
---|
67 | if(airFlowState.getName().equals(state.getName())){ |
---|
68 | airThroughput = airFlowState.getValue(); |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | return airThroughput; |
---|
74 | } |
---|
75 | |
---|
76 | public double getPowerConsumption(AirThroughputStateName state) throws NoSuchFieldException { |
---|
77 | double powerConsumption = 0; |
---|
78 | if(supportAirThroughputState(state)){ |
---|
79 | for(AirThroughputState airFlowState: airThroughputProfile.getAirThroughputStates()){ |
---|
80 | if(airFlowState.getName().equals(state.getName())){ |
---|
81 | powerConsumption = airFlowState.getPowerUsage(); |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | return powerConsumption; |
---|
87 | } |
---|
88 | |
---|
89 | public AirFlowValue getRecentAirFlow() { |
---|
90 | AirFlowValue airFlow = null; |
---|
91 | int lastIdx = getAirFlowHistory().size() - 1; |
---|
92 | if(lastIdx >= 0) |
---|
93 | airFlow = getAirFlowHistory().get(lastIdx); |
---|
94 | else { |
---|
95 | try { |
---|
96 | airFlow = new AirFlowValue(DateTimeUtils.currentTimeMillis(), getAirFlow(currentAirThroughputState)); |
---|
97 | } catch (NoSuchFieldException e) { |
---|
98 | } |
---|
99 | } |
---|
100 | return airFlow; |
---|
101 | } |
---|
102 | |
---|
103 | public List<AirFlowValue> getAirFlowHistory(){ |
---|
104 | return airThroughputProfile.getAirThroughputHistory(); |
---|
105 | } |
---|
106 | |
---|
107 | public Parameters getParameters() { |
---|
108 | return airThroughputProfile.getParameters(); |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|