Commit 548eb15e authored by Julian Zobel's avatar Julian Zobel
Browse files

Refactored the way that energy consumation is calculated for the energy...

Refactored the way that energy consumation is calculated for the energy models. Now the consumation is calculated on every energy component for itself, since the state change is of no interest to the energy model. Makes the process easier.
Monitor logging can now be added to the respective energy components, if that is wished again (was removed from the energy model anyways!).
parent 4c4a719e
......@@ -21,35 +21,26 @@
package de.tud.kom.p2psim.api.energy;
/**
* The {@link EnergyModel} registers via this listener with each
* {@link EnergyComponent}.
* This listener gives {@link EnergyComponent}s access to the {@link EnergyModel}.
*
* @author Bjoern Richerzhagen
* @version 1.0, 27.02.2012
* @author Julian Zobel
* @version 1.0, 07.09.2018
*/
public interface EnergyEventListener {
// TODO
/**
* Notify the Model that a component switched its state
* Notify the energy model that a component has consumed energy.
*
* @param component
* @param oldState
* @param newState
* @param timeSpentInOldState
* @param component The energy component that has consumed energy.
* @param consumedEnergy The amount of energy that was consumed by that component (uJ)
*/
public void switchedState(EnergyComponent component, EnergyState oldState,
EnergyState newState, long timeSpentInOldState);
public void componentConsumedEnergy(EnergyComponent component, double consumedEnergy);
/**
* Allows the energy model to check, if a component is able to be turned on.
* Return false, if the battery is empty or there is any other reason for
* the component not to be online.
* Allows a component to check, if it is able to be turned on.
*
* @param component
* @return
* @param component The energy component that is checked to be able to go online.
* @return False if there is any reason, that the component cannot be turned on; true if it can be turned on.
*/
public boolean turnOn(EnergyComponent component);
public boolean componentCanBeActivated(EnergyComponent component);
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.energy.models;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.energy.EnergyComponent;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.impl.energy.Battery;
public class ComponentBasedEnergyModel extends AbstractEnergyModel implements EnergyEventListener {
public ComponentBasedEnergyModel(SimHost host, Battery bat) {
super(host, bat);
}
@Override
public void registerComponent(EnergyComponent comp) {
energyComponents.add(comp);
comp.setEnergyEventListener(this);
}
@Override
public void componentConsumedEnergy(EnergyComponent component, double consumedEnergy) {
if (!bat.isEmpty()) {
bat.consumeEnergy(consumedEnergy);
monitorEnergyConsumation(component, consumedEnergy);
if (bat.isEmpty()) {
/*
* Battery is now empty. Go offline.
*/
for (SimNetInterface net : getHost().getNetworkComponent()
.getSimNetworkInterfaces()) {
net.goOffline();
}
}
}
}
@Override
public boolean componentCanBeActivated(EnergyComponent component) {
if(!bat.isEmpty()) {
return true;
}
return false;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.energy.models;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import org.joda.time.chrono.JulianChronology;
import de.tud.kom.p2psim.api.analyzer.EnergyAnalyzer;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyComponent;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.energy.EnergyInfo;
import de.tud.kom.p2psim.api.energy.EnergyModel;
import de.tud.kom.p2psim.api.energy.EnergyState;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.impl.energy.Battery;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent.AnalyzerNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.battery.BatterySensor;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSDataCallback;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInfoProperties;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInformationProvider.SiSProviderHandle;
import de.tudarmstadt.maki.simonstrator.api.component.sis.exception.InformationNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sis.type.SiSTypes;
/**
* The default energy model that is composed of different
* {@link EnergyComponent}s and configured via the factory. In contrast to the
* <i>old</i> EnergyModels these are Host components, ie. there is one instance
* for each host.
*
* @author Bjoern Richerzhagen, Julian Zobel
* @version 2.0, 07.09.2018
*/
public class ModularEnergyModel extends AbstractEnergyModel implements EnergyEventListener {
public ModularEnergyModel(SimHost host, Battery bat) {
super(host, bat);
}
@Override
public void initialize() {
//
try {
SiSComponent sis = host.getComponent(SiSComponent.class);
sis.provide().nodeState(SiSTypes.ENERGY_BATTERY_LEVEL,
new SiSDataCallback<Double>() {
Set<INodeID> localID = INodeID.getSingleIDSet(getHost()
.getId());
@Override
public Double getValue(INodeID nodeID,
SiSProviderHandle providerHandle)
throws InformationNotAvailableException {
if (nodeID.equals(getHost().getId())) {
return getCurrentPercentage();
} else {
throw new InformationNotAvailableException();
}
}
@Override
public Set<INodeID> getObservedNodes() {
return localID;
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
sis.provide().nodeState(SiSTypes.ENERGY_BATTERY_CAPACITY,
new SiSDataCallback<Double>() {
Set<INodeID> localID = INodeID.getSingleIDSet(getHost()
.getId());
@Override
public Double getValue(INodeID nodeID,
SiSProviderHandle providerHandle)
throws InformationNotAvailableException {
if (nodeID.equals(getHost().getId())) {
return getCurrentEnergyLevel();
} else {
throw new InformationNotAvailableException();
}
}
@Override
public Set<INodeID> getObservedNodes() {
return localID;
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
} catch (ComponentNotAvailableException e) {
// OK
}
}
@Override
public void registerComponent(EnergyComponent comp) {
comp.setEnergyEventListener(this);
energyComponents.add(comp);
}
@Override
public void switchedState(EnergyComponent component, EnergyState oldState,
EnergyState newState, long timeSpentInOldState) {
if (!bat.isEmpty()) {
double consumedEnergy = oldState.getEnergyConsumption()
* (timeSpentInOldState / (double) Time.SECOND);
bat.consumeEnergy(consumedEnergy);
/*
* TODO Refactor the Energy-Analyzer to support EnergyComponents
* directly, rather than strings.
*/
try {
Monitor.get(EnergyAnalyzer.class).consumeEnergy(getHost(),
consumedEnergy, component);
} catch (AnalyzerNotAvailableException e1) {
//
}
// log.debug(component.toString() + " consumed " + consumedEnergy
// + " uJ in State " + oldState.getName() + " after spending "
// + (timeSpentInOldState/Simulator.SECOND_UNIT) + " sec there.");
if (bat.isEmpty()) {
/*
* Battery is now empty. Go offline.
*/
try {
Monitor.get(EnergyAnalyzer.class).batteryIsEmpty(getHost());
} catch (AnalyzerNotAvailableException e) {
//
}
for (SimNetInterface net : getHost().getNetworkComponent()
.getSimNetworkInterfaces()) {
net.goOffline();
}
}
}
}
@Override
public boolean turnOn(EnergyComponent component) {
return !bat.isEmpty();
}
@Override
public void reset() {
bat.reset();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment