Browse Source

Updates FloatSensor to update itself every 10 seconds

Andreas T. Meyer-Berg 5 years ago
parent
commit
335b16cd8a

+ 25 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/FloatSensorDevice.java

@@ -2,7 +2,9 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.devices;
 
 import java.util.Random;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Schedulable;
 
 
 /**
@@ -10,7 +12,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  *
  * @author Andreas T. Meyer-Berg
  */
-public class FloatSensorDevice extends SmartDevice implements FloatSensor {
+public class FloatSensorDevice extends SmartDevice implements FloatSensor, Schedulable {
 
 	/**
 	 * Minimum value
@@ -29,6 +31,11 @@ public class FloatSensorDevice extends SmartDevice implements FloatSensor {
 	 */
 	private float oldVal;
 	
+	/**
+	 * Timestep it will be udpated again
+	 */
+	long nextSimulationTime = 0;
+	
 	/**
 	 * Name of the sensor reading
 	 */
@@ -56,6 +63,7 @@ public class FloatSensorDevice extends SmartDevice implements FloatSensor {
 		val = 22;
 		oldVal = 22;
 		infoName = "temperature";
+		SimulationManager.scheduleEvent(this);
 	}
 	
 	@Override
@@ -101,6 +109,22 @@ public class FloatSensorDevice extends SmartDevice implements FloatSensor {
 	
 	@Override
 	public void simulateTimeStep(long startTime, long duration) {
+		
+	}
+	
+	@Override
+	public long getEventTime() {
+		return nextSimulationTime;
+	}
+	@Override
+	public void simulateEvent(long time) {
+		updateData();
+		//Update again in 10 seconds
+		nextSimulationTime=time *10000;
+		SimulationManager.scheduleEvent(this);
+	}
+	
+	private void updateData() {
 		float newVal = val + (val-oldVal)+new Random().nextFloat()/2.0f-0.25f;
 		newVal = Math.max(Math.min(newVal, max),min);
 		this.oldVal = val;