فهرست منبع

Fixes temperature bugs

Andreas T. Meyer-Berg 3 سال پیش
والد
کامیت
cfbe20ff1b

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

@@ -34,7 +34,7 @@ public class FloatSensorDevice extends SmartDevice implements FloatSensor, Sched
 	/**
 	 * Timestep it will be udpated again
 	 */
-	long nextSimulationTime = 0;
+	protected long nextSimulationTime = 0;
 	
 	/**
 	 * Update interval

+ 4 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/SmartTemperatureProducer.java

@@ -23,11 +23,11 @@ public class SmartTemperatureProducer extends FloatSensorDevice implements Senso
 		 */
 		float cTemp = roomStatus.getTemperature();
 		if(tempChangePerHour > 0) {
-			roomStatus.setTemperature(Math.min(cTemp, cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
+			roomStatus.setTemperature(Math.min(getTrueTemperature(), cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
 		} else if(tempChangePerHour < 0) {
-			roomStatus.setTemperature(Math.max(cTemp, cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
+			roomStatus.setTemperature(Math.max(getTrueTemperature(), cTemp + tempChangePerHour * (float)super.updateInterval/3600000f));
 		}
-		if(roomStatus.getTemperature() == getFSval()) {
+		if(roomStatus.getTemperature() == getTrueTemperature()) {
 			tempChangePerHour = 0;
 		}
 	}
@@ -43,6 +43,7 @@ public class SmartTemperatureProducer extends FloatSensorDevice implements Senso
 		 * Change temperature in 1 hour to target temperature 
 		 */
 		tempChangePerHour = trueTemperature-roomStatus.getTemperature();
+		roomStatus.setTemperature(roomStatus.getTemperature()+tempChangePerHour);
 	}
 
 	@Override

+ 13 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/devices/SmartTemperatureSensor.java

@@ -15,18 +15,29 @@ public class SmartTemperatureSensor extends FloatSensorDevice implements SensorL
 		SimulationManager.scheduleEvent(this);
 	}
 	
+	@Override
+	public void simulateEvent(long time) {
+		updateData();
+		//Update again in 10 seconds
+		nextSimulationTime=time + updateInterval;
+		SimulationManager.scheduleEvent(this);
+	}
+	
 	@Override
 	protected void updateData() {
 		/**
 		 * Update Temperature to Room Temperature
 		 */
-		super.setFSval(roomStatus.getTemperature());
-		SimulationManager.scheduleEvent(this);
+		setFSval(roomStatus.getTemperature());
 	}
 
 	@Override
 	public String getSensorLabel() {
 		return "" + getFSval();
 	}
+	@Override
+	public float getFSval() {
+		return roomStatus.getTemperature();
+	}
 	
 }

+ 42 - 24
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/menuBar/MenuBarNetworkExamples.java

@@ -799,9 +799,27 @@ public class MenuBarNetworkExamples extends JMenu{
 		/**
 		 * Reset network, etc.
 		 */
+
+		long hour = 60 * 60 * 1000;
+		long endTime = 24*hour;
+		long noAnomEnd = 26*hour;
+		long lightEnd = 27*hour;
+		long tempEnd = 28*hour;
+		long stepLength=hour;
+		long currentSimTime=0;
+		
+		SimulationController sim = controller.getSimulationController();
 		if(run) {
 			controller.getSimulationController().resetSimulation();
 			controller.getNetworkController().deleteNetworkModel();
+			/*
+			 * Simulate 24 hours
+			 */
+			sim.setStartTime(0);
+			sim.resetSimulation();
+			sim.setStepDuration(hour);
+			sim.setPrintPackets(false);
+			sim.setEndTime(endTime);
 		}
 		/*
 		 * Main networking devices 
@@ -950,7 +968,9 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 			PacketCaptureController captureController = controller.getSimulationController().getPacketCaptureController();
 			//captureController.addLinkToCollector(collectorEM, zigbee);
-			captureController.addLinkToCollector(collectorKNN, zigbee);
+			//captureController.addLinkToCollector(collectorKNN, zigbee);
+			captureController.addDeviceToCollector(collectorKNN, smartLight);
+			captureController.addDeviceToCollector(collectorKNN, heaterDevice);
 			//captureController.addLinkToCollector(collectorHC, zigbee);
 			captureController.addPacketCollector(collectorKNN);
 			//captureController.addPacketCollector(collectorEM);
@@ -960,17 +980,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			
 			long currentTime = System.currentTimeMillis();
 			
-			long hour = 60 * 60 * 1000;
-			/*
-			 * Simulate 24 hours
-			 */
-			SimulationController sim = controller.getSimulationController();
-			sim.setStartTime(0);
-			sim.resetSimulation();
-			sim.setStepDuration(hour);
-			sim.setPrintPackets(false);
-			long endTime = 24*hour;
-			sim.setEndTime(endTime);
+
 
 			/**
 			 * Training events
@@ -981,11 +991,10 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 			NormalDistribution lightDist = new NormalDistribution(hour/2, hour/4);
 			long eventTime = 0;
-			while(eventTime < endTime + hour/2) {
+			while(eventTime < noAnomEnd) {
 				/**
 				 * At least 1 second between events
 				 */
-				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
 				SimulationManager.scheduleEvent(new AbstractEvent(eventTime) {
 					
 					@Override
@@ -994,6 +1003,7 @@ public class MenuBarNetworkExamples extends JMenu{
 						smartLight.setTrueStatus(!smartLight.isTrueStatus());
 					}
 				});
+				eventTime+=Math.max(1000, Math.round(lightDist.sample()));
 				
 			}
 			/**
@@ -1001,7 +1011,7 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 			NormalDistribution tempDist = new NormalDistribution(2*hour, hour/2);
 			eventTime = 0;
-			while(eventTime < 27 * hour) {
+			while(eventTime < lightEnd) {
 				/**
 				 * At least 1 second between events
 				 */
@@ -1019,8 +1029,10 @@ public class MenuBarNetworkExamples extends JMenu{
 			}
 			
 			System.out.println("Training:");//TODO
-			for(int i=0; i<24; i++)
-				sim.getSimulationManager().simulateTimeIntervall(i*hour, hour);
+			while(currentSimTime<endTime) {
+				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
+				currentSimTime+=stepLength;
+			}
 			
 			long new_time = System.currentTimeMillis();
 			long elapsed_time = new_time-currentTime;
@@ -1045,8 +1057,10 @@ public class MenuBarNetworkExamples extends JMenu{
 			 */
 			snifferKNN.setCurrentScenario("NoAnomalies");
 			System.out.println("Test w/0 anomalies:");//TODO
-			for(int i=24; i<26; i++)
-				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
+			while(currentSimTime<noAnomEnd) {
+				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
+				currentSimTime+=stepLength;
+			}
 			
 			new_time = System.currentTimeMillis();
 			elapsed_time = new_time-currentTime;
@@ -1061,8 +1075,10 @@ public class MenuBarNetworkExamples extends JMenu{
 			System.out.println("Light Anomaly:");
 			smartLight.setTrueStatus(!smartLight.getBSval());
 			smartLight.setLabel((short)-1);
-			for(int i=26; i<27; i++)
-				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
+			while(currentSimTime<lightEnd) {
+				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
+				currentSimTime+=stepLength;
+			}
 			smartLight.setLabel((short)0);
 			smartLight.setBSval(smartLight.isTrueStatus());
 			
@@ -1099,9 +1115,11 @@ public class MenuBarNetworkExamples extends JMenu{
 			System.out.println("Temperature Anomaly:");
 			heaterDevice.setFSval(21f);
 			heaterDevice.setTrueTemperature(34f);
-			heaterDevice.setLabel((short)-1);//-1 Value anomaly
-			for(int i=27; i<29; i++)
-				sim.getSimulationManager().simulateTimeIntervall(hour*i, hour);
+			heaterDevice.setLabel((short)-1);//-1 Value anomaly			
+			while(currentSimTime<tempEnd) {
+				sim.getSimulationManager().simulateTimeIntervall(currentSimTime, stepLength);
+				currentSimTime+=stepLength;
+			}
 			heaterDevice.setFSval(21f);
 			heaterDevice.setTrueTemperature(21f);
 			heaterDevice.setLabel((short)0);