/* * 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 . * */ package de.tud.kom.p2psim.impl.topology.component; import java.util.LinkedList; import java.util.Set; import de.tud.kom.p2psim.api.common.SimHost; import de.tud.kom.p2psim.api.energy.ComponentType; import de.tud.kom.p2psim.api.energy.EnergyModel; import de.tud.kom.p2psim.api.network.SimNetInterface; import de.tud.kom.p2psim.api.topology.Topology; import de.tud.kom.p2psim.api.topology.movement.MovementModel; import de.tud.kom.p2psim.api.topology.movement.SimUAVLocationActuator; import de.tud.kom.p2psim.api.topology.movement.UAVMovementModel; import de.tud.kom.p2psim.api.topology.placement.PlacementModel; import de.tud.kom.p2psim.impl.energy.RechargeableBattery; import de.tud.kom.p2psim.impl.energy.components.ActuatorEnergyComponent; import de.tud.kom.p2psim.impl.energy.models.AbstractEnergyModel; import de.tud.kom.p2psim.impl.topology.placement.UAVBasePlacement; import de.tud.kom.p2psim.impl.topology.util.PositionVector; import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException; import de.tudarmstadt.maki.simonstrator.api.component.overlay.OverlayComponent; import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint; import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location; import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.ReachedLocationCallback; /** * Topology component used for UAVs. * * @author Julian Zobel * @version 1.0, 06.09.2018 */ public class UAVTopologyComponent extends AbstractTopologyComponent implements SimUAVLocationActuator { public enum UAVstate {OFFLINE, ACTION, RETURN, CRASHED} private UAVMovementModel movement; private OverlayComponent uavOverlayComponent; private ActuatorEnergyComponent actuator; private RechargeableBattery battery; private UAVstate state = UAVstate.OFFLINE; private PositionVector baseLocation; /** * Create a TopologyComponent for the current host. * * @param host * @param topology * @param movementModel */ public UAVTopologyComponent(SimHost host, Topology topology, MovementModel movementModel, PlacementModel placementModel, boolean registerAsInformationProviderInSiS) { super(host, topology, movementModel, placementModel, registerAsInformationProviderInSiS); } @Override public void initialize() { super.initialize(); try { actuator = getHost().getComponent(EnergyModel.class) .getComponent(ComponentType.ACTUATOR, ActuatorEnergyComponent.class); } catch (ComponentNotAvailableException e) { System.err.println("No Acutator Energy Component was found!"); } try { battery = (RechargeableBattery) getHost().getComponent(AbstractEnergyModel.class).getBattery(); } catch (ComponentNotAvailableException e) { System.err.println("No Battery Component was found!"); } baseLocation = position.clone(); } public void setState(UAVstate newState) { this.state = newState; } public void setUAVComponent(OverlayComponent uavOverlayComponent) { this.uavOverlayComponent = uavOverlayComponent; } public OverlayComponent getUAVComponent() { return uavOverlayComponent; } @Override public double getMinMovementSpeed() { return movement.getMinCruiseSpeed(); } @Override public double getMaxMovementSpeed() { return movement.getMaxCruiseSpeed(); } @Override public double getMovementSpeed() { return movement.getCurrentSpeed(); } @Override public void setMovementSpeed(double speed) { movement.setPreferredCruiseSpeed(speed); } @Override public boolean isActive() { return actuator.isOn(); } @Override public boolean activate() { if(actuator.turnOn()) { state = UAVstate.ACTION; return true; } else { return false; } } @Override public boolean deactivate() { actuator.turnOff(); if(this.position.getAltitude() != 0) { state = UAVstate.CRASHED; System.err.println("UAV was destroyed due to actuator deactivation during flight"); uavOverlayComponent.shutdown(); for (SimNetInterface net : getHost().getNetworkComponent() .getSimNetworkInterfaces()) { net.goOffline(); } } else { state = UAVstate.OFFLINE; } return true; } @Override public PositionVector getCurrentLocation() { return position.clone(); } @Override public double getCurrentBatteryLevel() { return battery.getCurrentPercentage(); } @Override public UAVMovementModel getUAVMovement() { return movement; } @Override public void setUAVMovement(UAVMovementModel uavMovement) { this.movement = uavMovement; } @Override public ActuatorEnergyComponent getActuatorEnergyComponent() { return actuator; } public void updateCurrentLocation(Location location, double actuatorLoad) { super.updateCurrentLocation(location); actuator.useActuator(actuatorLoad); } @Override public Set getAllAttractionPoints() { throw new UnsupportedOperationException(); } @Override public void setTargetLocation(PositionVector targetLocation, ReachedLocationCallback cb) { movement.setTargetLocation(new PositionVector(targetLocation), cb); } @Override public void addTargetLocation(PositionVector targetLocation, ReachedLocationCallback cb) { movement.addTargetLocation(new PositionVector(targetLocation), cb); } @Override public void setTargetLocationRoute(LinkedList route, ReachedLocationCallback cb) { LinkedList positionvectorlist = new LinkedList<>(); for (Location loc : route) { positionvectorlist.add(new PositionVector(loc)); } movement.setTargetLocationRoute(positionvectorlist, cb); } @Override public void removeAllTargetLocations() { movement.removeTargetLocations(); } @Override public void setTargetAttractionPoint(AttractionPoint targetAttractionPoint) { throw new UnsupportedOperationException(); } @Override public AttractionPoint getCurrentTargetAttractionPoint() { throw new UnsupportedOperationException(); } @Override public LinkedList getTargetLocations() { return movement.getTargetLocations(); } public UAVstate getUAVState() { return state; } @Override public void returnToBase(ReachedLocationCallback cb) { this.state = UAVstate.RETURN; ReachedLocationCallback returnCallback = new ReachedLocationCallback() { @Override public void reachedLocation() { cb.reachedLocation(); deactivate(); connectToBaseCharger(); } }; movement.setTargetLocation(baseLocation, returnCallback); } private void connectToBaseCharger() { BaseTopologyComponent base = UAVBasePlacement.base; } }