package schedframe.resources.devices; import java.util.ArrayList; import java.util.List; import org.joda.time.DateTimeUtils; import schedframe.Parameters; import schedframe.resources.computing.profiles.energy.EnergyEvent; import schedframe.resources.computing.profiles.energy.EnergyEventType; import schedframe.resources.computing.profiles.energy.power.PowerProfile; import schedframe.resources.computing.profiles.energy.power.PowerState; import schedframe.resources.computing.profiles.energy.power.PowerStateName; import schedframe.resources.computing.profiles.energy.power.PowerUsage; import schedframe.resources.computing.profiles.energy.power.StandardPowerStateName; import schedframe.resources.computing.profiles.energy.power.ui.PowerInterface; public class DevicePowerInterface implements PowerInterface{ protected PowerStateName currentPowerState; protected PowerProfile powerProfile; protected PhysicalResource resource; public DevicePowerInterface(PhysicalResource resource, PowerProfile powerProfile){ this.resource = resource; this.powerProfile = powerProfile; this.currentPowerState = StandardPowerStateName.ON; } public boolean setPowerState(PowerStateName state) { if(supportPowerState(state)){ currentPowerState = state; resource.handleEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, resource.getFullName())); return true; } return false; } public PowerStateName getPowerState() { return currentPowerState; } public List getSupportedPowerStates() throws NoSuchFieldException { List powerStates = new ArrayList(); for(PowerState powerState: powerProfile.getSupportedPowerStates()){ powerStates.add(powerState); } return powerStates; } public boolean supportPowerState(PowerStateName state) { for(PowerState powerState: powerProfile.getSupportedPowerStates()){ if(powerState.getName().equals(state)){ return true; } } return false; } public double getPowerConsumption(PowerStateName state) throws NoSuchFieldException { double powerConsumption = 0; if(supportPowerState(state)){ for(PowerState powerState: powerProfile.getSupportedPowerStates()){ if(powerState.getName().equals(state)){ powerConsumption = powerState.getPowerUsage(); break; } } } else { throw new NoSuchFieldException("Power state not supported"); } return powerConsumption; } public PowerUsage getRecentPowerUsage() { PowerUsage powerUsage = null; int lastIdx = getPowerUsageHistory().size() - 1; if(lastIdx >= 0) powerUsage = getPowerUsageHistory().get(lastIdx); else { try { powerUsage = new PowerUsage(DateTimeUtils.currentTimeMillis(), getPowerConsumption(currentPowerState)); } catch (NoSuchFieldException e) { } } return powerUsage; } public List getPowerUsageHistory(){ return powerProfile.getPowerUsageHistory(); } public Parameters getParameters() { return powerProfile.getParameters(); } }